telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 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> |
| 8 | |
| 9 | #include <array> |
| 10 | #include <cstdint> |
| 11 | #include <vector> |
| 12 | #include <utility> |
| 13 | |
| 14 | class InferenceTestImageException : public armnn::Exception |
| 15 | { |
| 16 | public: |
| 17 | using Exception::Exception; |
| 18 | }; |
| 19 | |
| 20 | class InferenceTestImageLoadFailed : public InferenceTestImageException |
| 21 | { |
| 22 | public: |
| 23 | using InferenceTestImageException::InferenceTestImageException; |
| 24 | }; |
| 25 | |
| 26 | class InferenceTestImageOutOfBoundsAccess : public InferenceTestImageException |
| 27 | { |
| 28 | public: |
| 29 | using InferenceTestImageException::InferenceTestImageException; |
| 30 | }; |
| 31 | |
| 32 | class InferenceTestImageResizeFailed : public InferenceTestImageException |
| 33 | { |
| 34 | public: |
| 35 | using InferenceTestImageException::InferenceTestImageException; |
| 36 | }; |
| 37 | |
| 38 | class InferenceTestImageWriteFailed : public InferenceTestImageException |
| 39 | { |
| 40 | public: |
| 41 | using InferenceTestImageException::InferenceTestImageException; |
| 42 | }; |
| 43 | |
| 44 | class UnknownImageChannelLayout : public InferenceTestImageException |
| 45 | { |
| 46 | public: |
| 47 | using InferenceTestImageException::InferenceTestImageException; |
| 48 | }; |
| 49 | |
| 50 | class InferenceTestImage |
| 51 | { |
| 52 | public: |
| 53 | enum class WriteFormat |
| 54 | { |
| 55 | Png, |
| 56 | Bmp, |
| 57 | Tga |
| 58 | }; |
| 59 | |
| 60 | explicit InferenceTestImage(const char* filePath); |
| 61 | |
| 62 | InferenceTestImage(InferenceTestImage&&) = delete; |
| 63 | InferenceTestImage(const InferenceTestImage&) = delete; |
| 64 | InferenceTestImage& operator=(const InferenceTestImage&) = delete; |
| 65 | InferenceTestImage& operator=(InferenceTestImage&&) = delete; |
| 66 | |
| 67 | unsigned int GetWidth() const { return m_Width; } |
| 68 | unsigned int GetHeight() const { return m_Height; } |
| 69 | unsigned int GetNumChannels() const { return m_NumChannels; } |
| 70 | unsigned int GetNumElements() const { return GetWidth() * GetHeight() * GetNumChannels(); } |
| 71 | unsigned int GetSizeInBytes() const { return GetNumElements() * GetSingleElementSizeInBytes(); } |
| 72 | |
| 73 | // Returns the pixel identified by the given coordinates as a 3-channel value. |
| 74 | // Channels beyond the third are dropped. If the image provides less than 3 channels, the non-existent |
| 75 | // channels of the pixel will be filled with 0. Channels are returned in RGB order (that is, the first element |
| 76 | // of the tuple corresponds to the Red channel, whereas the last element is the Blue channel). |
| 77 | std::tuple<uint8_t, uint8_t, uint8_t> GetPixelAs3Channels(unsigned int x, unsigned int y) const; |
| 78 | |
| 79 | void Resize(unsigned int newWidth, unsigned int newHeight); |
| 80 | void Write(WriteFormat format, const char* filePath) const; |
| 81 | |
| 82 | private: |
| 83 | static unsigned int GetSingleElementSizeInBytes() |
| 84 | { |
| 85 | return sizeof(decltype(std::declval<InferenceTestImage>().m_Data[0])); |
| 86 | } |
| 87 | |
| 88 | std::vector<uint8_t> m_Data; |
| 89 | unsigned int m_Width; |
| 90 | unsigned int m_Height; |
| 91 | unsigned int m_NumChannels; |
| 92 | }; |
| 93 | |
| 94 | // Common names used to identify a channel in a pixel |
| 95 | enum class ImageChannel |
| 96 | { |
| 97 | R, |
| 98 | G, |
| 99 | B |
| 100 | }; |
| 101 | |
| 102 | // Channel layouts handled by the test framework |
| 103 | enum class ImageChannelLayout |
| 104 | { |
| 105 | Rgb, |
| 106 | Bgr |
| 107 | }; |
| 108 | |
| 109 | // Reads the contents of an inference test image as 3-channel pixels whose channel values have been normalized (scaled) |
| 110 | // and now lie in the range [0,1]. Channel data is stored according to the ArmNN layout (CHW). The order in which |
| 111 | // channels appear in the resulting vector is defined by the provided layout. |
| 112 | std::vector<float> GetImageDataInArmNnLayoutAsNormalizedFloats(ImageChannelLayout layout, |
| 113 | const InferenceTestImage& image); |
| 114 | |
| 115 | // Reads the contents of an inference test image as 3-channel pixels whose value is the result of subtracting the mean |
| 116 | // from the values in the original image. Channel data is stored according to the ArmNN layout (CHW). The order in |
| 117 | // which channels appear in the resulting vector is defined by the provided layout. The order of the channels of the |
| 118 | // provided mean should also match the given layout. |
| 119 | std::vector<float> GetImageDataInArmNnLayoutAsFloatsSubtractingMean(ImageChannelLayout layout, |
| 120 | const InferenceTestImage& image, |
| 121 | const std::array<float, 3>& mean); |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 122 | |
| 123 | // Reads the contents of an inference test image as 3-channel pixels and returns the image data as normalized float |
| 124 | // values. The returned image stay in the original order (HWC) order. The C order may be changed according to the |
| 125 | // supplied layout value. |
| 126 | std::vector<float> GetImageDataAsNormalizedFloats(ImageChannelLayout layout, |
| 127 | const InferenceTestImage& image); |