blob: d77113c6d9c42424b8b53f6dfff1a68265fbb768 [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
5#pragma once
6
7#include "ClassifierTestCaseData.hpp"
8
9#include <array>
10#include <string>
11#include <vector>
12#include <memory>
13
14///Tf requires RGB images, normalized in range [0, 1] and resized using Bilinear algorithm
15
16
17using ImageSet = std::pair<const std::string, unsigned int>;
18
19template <typename TDataType>
20class ImagePreprocessor
21{
22public:
23 using DataType = TDataType;
24 using TTestCaseData = ClassifierTestCaseData<DataType>;
25
26 enum DataFormat
27 {
28 NHWC,
29 NCHW
30 };
31
32 explicit ImagePreprocessor(const std::string& binaryFileDirectory,
33 unsigned int width,
34 unsigned int height,
35 const std::vector<ImageSet>& imageSet,
36 float scale=1.0,
37 int32_t offset=0,
38 const std::array<float, 3> mean={{0, 0, 0}},
39 const std::array<float, 3> stddev={{1, 1, 1}},
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +010040 DataFormat dataFormat=DataFormat::NHWC,
41 unsigned int batchSize=1)
telsoa01c577f2c2018-08-31 09:22:23 +010042 : m_BinaryDirectory(binaryFileDirectory)
43 , m_Height(height)
44 , m_Width(width)
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +010045 , m_BatchSize(batchSize)
telsoa01c577f2c2018-08-31 09:22:23 +010046 , m_Scale(scale)
47 , m_Offset(offset)
48 , m_ImageSet(imageSet)
49 , m_Mean(mean)
50 , m_Stddev(stddev)
51 , m_DataFormat(dataFormat)
52 {
53 }
54
55 std::unique_ptr<TTestCaseData> GetTestCaseData(unsigned int testCaseId);
56
57private:
58 unsigned int GetNumImageElements() const { return 3 * m_Width * m_Height; }
59 unsigned int GetNumImageBytes() const { return sizeof(DataType) * GetNumImageElements(); }
60 unsigned int GetLabelAndResizedImageAsFloat(unsigned int testCaseId,
61 std::vector<float> & result);
62
63 std::string m_BinaryDirectory;
64 unsigned int m_Height;
65 unsigned int m_Width;
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +010066 unsigned int m_BatchSize;
telsoa01c577f2c2018-08-31 09:22:23 +010067 // Quantization parameters
68 float m_Scale;
69 int32_t m_Offset;
70 const std::vector<ImageSet> m_ImageSet;
71
72 const std::array<float, 3> m_Mean;
73 const std::array<float, 3> m_Stddev;
74
75 DataFormat m_DataFormat;
76};