2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
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}},
96 const float scale = 255.0f);
98 void Write(WriteFormat format, const char* filePath) const;
101 static unsigned int GetSingleElementSizeInBytes()
103 return sizeof(decltype(std::declval<InferenceTestImage>().m_Data[0]));
106 std::vector<uint8_t> m_Data;
107 unsigned int m_Width;
108 unsigned int m_Height;
109 unsigned int m_NumChannels;
112 // Common names used to identify a channel in a pixel.
113 enum class ImageChannel
120 // Channel layouts handled by the test framework.
121 enum class ImageChannelLayout
127 // Reads the contents of an inference test image as 3-channel pixels whose channel values have been normalized (scaled)
128 // and now lie in the range [0,1]. Channel data is stored according to the ArmNN layout (CHW). The order in which
129 // channels appear in the resulting vector is defined by the provided layout.
130 std::vector<float> GetImageDataInArmNnLayoutAsNormalizedFloats(ImageChannelLayout layout,
131 const InferenceTestImage& image);
133 // Reads the contents of an inference test image as 3-channel pixels, whose value is the result of subtracting the mean
134 // from the values in the original image. Channel data is stored according to the ArmNN layout (CHW). The order in
135 // which channels appear in the resulting vector is defined by the provided layout. The order of the channels of the
136 // provided mean should also match the given layout.
137 std::vector<float> GetImageDataInArmNnLayoutAsFloatsSubtractingMean(ImageChannelLayout layout,
138 const InferenceTestImage& image,
139 const std::array<float, 3>& mean);
141 // Reads the contents of an inference test image as 3-channel pixels and returns the image data as normalized float
142 // values. The returned image stay in the original order (HWC) order. The C order may be changed according to the
143 // supplied layout value.
144 std::vector<float> GetImageDataAsNormalizedFloats(ImageChannelLayout layout,
145 const InferenceTestImage& image);