2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
7 #include <armnn/Exceptions.hpp>
8 #include <VerificationHelpers.hpp>
15 class InferenceTestImageException : public armnn::Exception
18 using Exception::Exception;
21 class InferenceTestImageLoadFailed : public InferenceTestImageException
24 using InferenceTestImageException::InferenceTestImageException;
27 class InferenceTestImageOutOfBoundsAccess : public InferenceTestImageException
30 using InferenceTestImageException::InferenceTestImageException;
33 class InferenceTestImageResizeFailed : public InferenceTestImageException
36 using InferenceTestImageException::InferenceTestImageException;
39 class InferenceTestImageWriteFailed : public InferenceTestImageException
42 using InferenceTestImageException::InferenceTestImageException;
45 class UnknownImageChannelLayout : public InferenceTestImageException
48 using InferenceTestImageException::InferenceTestImageException;
51 class InferenceTestImage
54 enum class WriteFormat
61 // Common names used to identify a channel in a pixel.
62 enum class ResizingMethods
65 BilinearAndNormalized,
68 explicit InferenceTestImage(const char* filePath);
70 InferenceTestImage(InferenceTestImage&&) = delete;
71 InferenceTestImage(const InferenceTestImage&) = delete;
72 InferenceTestImage& operator=(const InferenceTestImage&) = delete;
73 InferenceTestImage& operator=(InferenceTestImage&&) = delete;
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(); }
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;
87 void StbResize(InferenceTestImage& im, const unsigned int newWidth, const unsigned int newHeight);
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}});
97 void Write(WriteFormat format, const char* filePath) const;
100 static unsigned int GetSingleElementSizeInBytes()
102 return sizeof(decltype(std::declval<InferenceTestImage>().m_Data[0]));
105 std::vector<uint8_t> m_Data;
106 unsigned int m_Width;
107 unsigned int m_Height;
108 unsigned int m_NumChannels;
111 // Common names used to identify a channel in a pixel.
112 enum class ImageChannel
119 // Channel layouts handled by the test framework.
120 enum class ImageChannelLayout
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.
129 std::vector<float> GetImageDataInArmNnLayoutAsNormalizedFloats(ImageChannelLayout layout,
130 const InferenceTestImage& image);
132 // Reads the contents of an inference test image as 3-channel pixels, whose value is the result of subtracting the mean
133 // 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.
136 std::vector<float> GetImageDataInArmNnLayoutAsFloatsSubtractingMean(ImageChannelLayout layout,
137 const InferenceTestImage& image,
138 const std::array<float, 3>& mean);
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.
143 std::vector<float> GetImageDataAsNormalizedFloats(ImageChannelLayout layout,
144 const InferenceTestImage& image);