blob: bbeb8caf55f8c9644ca466baa8a7318c0fb016d0 [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
telsoa014fcda012018-03-09 14:13:49 +00002// 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#include "MnistDatabase.hpp"
6
Derek Lamberti08446972019-11-26 16:38:31 +00007#include <armnn/Logging.hpp>
8
telsoa014fcda012018-03-09 14:13:49 +00009#include <fstream>
10#include <vector>
11
12constexpr int g_kMnistImageByteSize = 28 * 28;
13
14void EndianSwap(unsigned int &x)
15{
16 x = (x >> 24) | ((x << 8) & 0x00FF0000) | ((x >> 8) & 0x0000FF00) | (x << 24);
17}
18
19MnistDatabase::MnistDatabase(const std::string& binaryFileDirectory, bool scaleValues)
20 : m_BinaryDirectory(binaryFileDirectory)
21 , m_ScaleValues(scaleValues)
22{
23}
24
25std::unique_ptr<MnistDatabase::TTestCaseData> MnistDatabase::GetTestCaseData(unsigned int testCaseId)
26{
27 std::vector<unsigned char> I(g_kMnistImageByteSize);
28 unsigned int label = 0;
29
30 std::string imagePath = m_BinaryDirectory + std::string("t10k-images.idx3-ubyte");
31 std::string labelPath = m_BinaryDirectory + std::string("t10k-labels.idx1-ubyte");
32
33 std::ifstream imageStream(imagePath, std::ios::binary);
34 std::ifstream labelStream(labelPath, std::ios::binary);
35
36 if (!imageStream.is_open())
37 {
Derek Lamberti08446972019-11-26 16:38:31 +000038 ARMNN_LOG(fatal) << "Failed to load " << imagePath;
telsoa014fcda012018-03-09 14:13:49 +000039 return nullptr;
40 }
41 if (!labelStream.is_open())
42 {
Derek Lamberti08446972019-11-26 16:38:31 +000043 ARMNN_LOG(fatal) << "Failed to load " << imagePath;
telsoa014fcda012018-03-09 14:13:49 +000044 return nullptr;
45 }
46
47 unsigned int magic, num, row, col;
48
telsoa01c577f2c2018-08-31 09:22:23 +010049 // Checks the files have the correct header.
telsoa014fcda012018-03-09 14:13:49 +000050 imageStream.read(reinterpret_cast<char*>(&magic), sizeof(magic));
51 if (magic != 0x03080000)
52 {
Derek Lamberti08446972019-11-26 16:38:31 +000053 ARMNN_LOG(fatal) << "Failed to read " << imagePath;
telsoa014fcda012018-03-09 14:13:49 +000054 return nullptr;
55 }
56 labelStream.read(reinterpret_cast<char*>(&magic), sizeof(magic));
57 if (magic != 0x01080000)
58 {
Derek Lamberti08446972019-11-26 16:38:31 +000059 ARMNN_LOG(fatal) << "Failed to read " << labelPath;
telsoa014fcda012018-03-09 14:13:49 +000060 return nullptr;
61 }
62
telsoa01c577f2c2018-08-31 09:22:23 +010063 // Endian swaps the image and label file - all the integers in the files are stored in MSB first(high endian)
64 // format, hence it needs to flip the bytes of the header if using it on Intel processors or low-endian machines
telsoa014fcda012018-03-09 14:13:49 +000065 labelStream.read(reinterpret_cast<char*>(&num), sizeof(num));
66 imageStream.read(reinterpret_cast<char*>(&num), sizeof(num));
67 EndianSwap(num);
68 imageStream.read(reinterpret_cast<char*>(&row), sizeof(row));
69 EndianSwap(row);
70 imageStream.read(reinterpret_cast<char*>(&col), sizeof(col));
71 EndianSwap(col);
72
telsoa01c577f2c2018-08-31 09:22:23 +010073 // Reads image and label into memory.
telsoa014fcda012018-03-09 14:13:49 +000074 imageStream.seekg(testCaseId * g_kMnistImageByteSize, std::ios_base::cur);
75 imageStream.read(reinterpret_cast<char*>(&I[0]), g_kMnistImageByteSize);
76 labelStream.seekg(testCaseId, std::ios_base::cur);
77 labelStream.read(reinterpret_cast<char*>(&label), 1);
78
79 if (!imageStream.good())
80 {
Derek Lamberti08446972019-11-26 16:38:31 +000081 ARMNN_LOG(fatal) << "Failed to read " << imagePath;
telsoa014fcda012018-03-09 14:13:49 +000082 return nullptr;
83 }
84 if (!labelStream.good())
85 {
Derek Lamberti08446972019-11-26 16:38:31 +000086 ARMNN_LOG(fatal) << "Failed to read " << labelPath;
telsoa014fcda012018-03-09 14:13:49 +000087 return nullptr;
88 }
89
90 std::vector<float> inputImageData;
91 inputImageData.resize(g_kMnistImageByteSize);
92
93 for (unsigned int i = 0; i < col * row; ++i)
94 {
Matthew Sloyan80c6b142020-09-08 12:00:32 +010095 // Static_cast of unsigned char is safe with float
96 inputImageData[i] = static_cast<float>(I[i]);
telsoa014fcda012018-03-09 14:13:49 +000097
98 if(m_ScaleValues)
99 {
100 inputImageData[i] /= 255.0f;
101 }
102 }
103
104 return std::make_unique<TTestCaseData>(label, std::move(inputImageData));
105}