telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #include "Cifar10Database.hpp" |
| 6 | |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 7 | #include <armnn/Logging.hpp> |
| 8 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 9 | #include <boost/numeric/conversion/cast.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 10 | #include <fstream> |
| 11 | #include <vector> |
| 12 | |
| 13 | constexpr unsigned int g_kCifar10ImageByteSize = 1 + 3 * 32 * 32; |
| 14 | |
| 15 | Cifar10Database::Cifar10Database(const std::string& binaryFileDirectory, bool rgbPack) |
| 16 | : m_BinaryDirectory(binaryFileDirectory), m_RgbPack(rgbPack) |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | std::unique_ptr<Cifar10Database::TTestCaseData> Cifar10Database::GetTestCaseData(unsigned int testCaseId) |
| 21 | { |
| 22 | std::vector<unsigned char> I(g_kCifar10ImageByteSize); |
| 23 | |
| 24 | std::string fullpath = m_BinaryDirectory + std::string("test_batch.bin"); |
| 25 | |
| 26 | std::ifstream fileStream(fullpath, std::ios::binary); |
| 27 | if (!fileStream.is_open()) |
| 28 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 29 | ARMNN_LOG(fatal) << "Failed to load " << fullpath; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 30 | return nullptr; |
| 31 | } |
| 32 | |
| 33 | fileStream.seekg(testCaseId * g_kCifar10ImageByteSize, std::ios_base::beg); |
| 34 | fileStream.read(reinterpret_cast<char*>(&I[0]), g_kCifar10ImageByteSize); |
| 35 | |
| 36 | if (!fileStream.good()) |
| 37 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 38 | ARMNN_LOG(fatal) << "Failed to read " << fullpath; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 39 | return nullptr; |
| 40 | } |
| 41 | |
| 42 | |
| 43 | std::vector<float> inputImageData; |
| 44 | inputImageData.resize(g_kCifar10ImageByteSize - 1); |
| 45 | |
| 46 | unsigned int step; |
| 47 | unsigned int countR_o; |
| 48 | unsigned int countG_o; |
| 49 | unsigned int countB_o; |
| 50 | unsigned int countR = 1; |
| 51 | unsigned int countG = 1 + 32 * 32; |
| 52 | unsigned int countB = 1 + 2 * 32 * 32; |
| 53 | |
| 54 | if (m_RgbPack) |
| 55 | { |
| 56 | countR_o = 0; |
| 57 | countG_o = 1; |
| 58 | countB_o = 2; |
| 59 | step = 3; |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | countR_o = 0; |
| 64 | countG_o = 32 * 32; |
| 65 | countB_o = 2 * 32 * 32; |
| 66 | step = 1; |
| 67 | } |
| 68 | |
| 69 | for (unsigned int h = 0; h < 32; h++) |
| 70 | { |
| 71 | for (unsigned int w = 0; w < 32; w++) |
| 72 | { |
| 73 | inputImageData[countR_o] = boost::numeric_cast<float>(I[countR++]); |
| 74 | inputImageData[countG_o] = boost::numeric_cast<float>(I[countG++]); |
| 75 | inputImageData[countB_o] = boost::numeric_cast<float>(I[countB++]); |
| 76 | |
| 77 | countR_o += step; |
| 78 | countG_o += step; |
| 79 | countB_o += step; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | const unsigned int label = boost::numeric_cast<unsigned int>(I[0]); |
| 84 | return std::make_unique<TTestCaseData>(label, std::move(inputImageData)); |
| 85 | } |