blob: d15f263609a043fb708da07bf158379eb722acdc [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
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}},
FinnWilliamsArmaf8b72d2019-05-22 14:50:55 +010095 const std::array<float, 3>& stddev = {{1.0, 1.0, 1.0}},
96 const float scale = 255.0f);
telsoa01c577f2c2018-08-31 09:22:23 +010097
telsoa014fcda012018-03-09 14:13:49 +000098 void Write(WriteFormat format, const char* filePath) const;
99
100private:
101 static unsigned int GetSingleElementSizeInBytes()
102 {
103 return sizeof(decltype(std::declval<InferenceTestImage>().m_Data[0]));
104 }
105
106 std::vector<uint8_t> m_Data;
107 unsigned int m_Width;
108 unsigned int m_Height;
109 unsigned int m_NumChannels;
110};
111
telsoa01c577f2c2018-08-31 09:22:23 +0100112// Common names used to identify a channel in a pixel.
telsoa014fcda012018-03-09 14:13:49 +0000113enum class ImageChannel
114{
115 R,
116 G,
117 B
118};
119
telsoa01c577f2c2018-08-31 09:22:23 +0100120// Channel layouts handled by the test framework.
telsoa014fcda012018-03-09 14:13:49 +0000121enum class ImageChannelLayout
122{
123 Rgb,
124 Bgr
125};
126
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.
130std::vector<float> GetImageDataInArmNnLayoutAsNormalizedFloats(ImageChannelLayout layout,
Matteo Martincigh9a5f9f22019-10-31 11:02:47 +0000131 const InferenceTestImage& image);
telsoa014fcda012018-03-09 14:13:49 +0000132
telsoa01c577f2c2018-08-31 09:22:23 +0100133// 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 +0000134// 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.
137std::vector<float> GetImageDataInArmNnLayoutAsFloatsSubtractingMean(ImageChannelLayout layout,
Matteo Martincigh9a5f9f22019-10-31 11:02:47 +0000138 const InferenceTestImage& image,
139 const std::array<float, 3>& mean);
surmeh01bceff2f2018-03-29 16:29:27 +0100140
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.
144std::vector<float> GetImageDataAsNormalizedFloats(ImageChannelLayout layout,
145 const InferenceTestImage& image);