blob: 657ea04c7bc17dc2194423ccd2fe11c21382c0b1 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// See LICENSE file in the project root for full license information.
4//
5#pragma once
6
7#include <armnn/Exceptions.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +01008#include <VerificationHelpers.hpp>
telsoa014fcda012018-03-09 14:13:49 +00009
10#include <array>
11#include <cstdint>
12#include <vector>
13#include <utility>
14
15class InferenceTestImageException : public armnn::Exception
16{
17public:
18 using Exception::Exception;
19};
20
21class InferenceTestImageLoadFailed : public InferenceTestImageException
22{
23public:
24 using InferenceTestImageException::InferenceTestImageException;
25};
26
27class InferenceTestImageOutOfBoundsAccess : public InferenceTestImageException
28{
29public:
30 using InferenceTestImageException::InferenceTestImageException;
31};
32
33class InferenceTestImageResizeFailed : public InferenceTestImageException
34{
35public:
36 using InferenceTestImageException::InferenceTestImageException;
37};
38
39class InferenceTestImageWriteFailed : public InferenceTestImageException
40{
41public:
42 using InferenceTestImageException::InferenceTestImageException;
43};
44
45class UnknownImageChannelLayout : public InferenceTestImageException
46{
47public:
48 using InferenceTestImageException::InferenceTestImageException;
49};
50
51class InferenceTestImage
52{
53public:
54 enum class WriteFormat
55 {
56 Png,
57 Bmp,
58 Tga
59 };
60
telsoa01c577f2c2018-08-31 09:22:23 +010061 // Common names used to identify a channel in a pixel.
62 enum class ResizingMethods
63 {
64 STB,
65 BilinearAndNormalized,
66 };
67
telsoa014fcda012018-03-09 14:13:49 +000068 explicit InferenceTestImage(const char* filePath);
69
70 InferenceTestImage(InferenceTestImage&&) = delete;
71 InferenceTestImage(const InferenceTestImage&) = delete;
72 InferenceTestImage& operator=(const InferenceTestImage&) = delete;
73 InferenceTestImage& operator=(InferenceTestImage&&) = delete;
74
75 unsigned int GetWidth() const { return m_Width; }
76 unsigned int GetHeight() const { return m_Height; }
77 unsigned int GetNumChannels() const { return m_NumChannels; }
78 unsigned int GetNumElements() const { return GetWidth() * GetHeight() * GetNumChannels(); }
79 unsigned int GetSizeInBytes() const { return GetNumElements() * GetSingleElementSizeInBytes(); }
80
81 // Returns the pixel identified by the given coordinates as a 3-channel value.
82 // Channels beyond the third are dropped. If the image provides less than 3 channels, the non-existent
83 // channels of the pixel will be filled with 0. Channels are returned in RGB order (that is, the first element
84 // of the tuple corresponds to the Red channel, whereas the last element is the Blue channel).
85 std::tuple<uint8_t, uint8_t, uint8_t> GetPixelAs3Channels(unsigned int x, unsigned int y) const;
86
telsoa01c577f2c2018-08-31 09:22:23 +010087 void StbResize(InferenceTestImage& im, const unsigned int newWidth, const unsigned int newHeight);
88
89
90 std::vector<float> Resize(unsigned int newWidth,
91 unsigned int newHeight,
92 const armnn::CheckLocation& location,
93 const ResizingMethods meth = ResizingMethods::STB,
94 const std::array<float, 3>& mean = {{0.0, 0.0, 0.0}},
95 const std::array<float, 3>& stddev = {{1.0, 1.0, 1.0}});
96
telsoa014fcda012018-03-09 14:13:49 +000097 void Write(WriteFormat format, const char* filePath) const;
98
99private:
100 static unsigned int GetSingleElementSizeInBytes()
101 {
102 return sizeof(decltype(std::declval<InferenceTestImage>().m_Data[0]));
103 }
104
105 std::vector<uint8_t> m_Data;
106 unsigned int m_Width;
107 unsigned int m_Height;
108 unsigned int m_NumChannels;
109};
110
telsoa01c577f2c2018-08-31 09:22:23 +0100111// Common names used to identify a channel in a pixel.
telsoa014fcda012018-03-09 14:13:49 +0000112enum class ImageChannel
113{
114 R,
115 G,
116 B
117};
118
telsoa01c577f2c2018-08-31 09:22:23 +0100119// Channel layouts handled by the test framework.
telsoa014fcda012018-03-09 14:13:49 +0000120enum class ImageChannelLayout
121{
122 Rgb,
123 Bgr
124};
125
126// Reads the contents of an inference test image as 3-channel pixels whose channel values have been normalized (scaled)
127// and now lie in the range [0,1]. Channel data is stored according to the ArmNN layout (CHW). The order in which
128// channels appear in the resulting vector is defined by the provided layout.
129std::vector<float> GetImageDataInArmNnLayoutAsNormalizedFloats(ImageChannelLayout layout,
130 const InferenceTestImage& image);
131
telsoa01c577f2c2018-08-31 09:22:23 +0100132// Reads the contents of an inference test image as 3-channel pixels, whose value is the result of subtracting the mean
telsoa014fcda012018-03-09 14:13:49 +0000133// from the values in the original image. Channel data is stored according to the ArmNN layout (CHW). The order in
134// which channels appear in the resulting vector is defined by the provided layout. The order of the channels of the
135// provided mean should also match the given layout.
136std::vector<float> GetImageDataInArmNnLayoutAsFloatsSubtractingMean(ImageChannelLayout layout,
137 const InferenceTestImage& image,
138 const std::array<float, 3>& mean);
surmeh01bceff2f2018-03-29 16:29:27 +0100139
140// Reads the contents of an inference test image as 3-channel pixels and returns the image data as normalized float
141// values. The returned image stay in the original order (HWC) order. The C order may be changed according to the
142// supplied layout value.
143std::vector<float> GetImageDataAsNormalizedFloats(ImageChannelLayout layout,
144 const InferenceTestImage& image);