diff options
author | telsoa01 <telmo.soares@arm.com> | 2018-03-09 14:13:49 +0000 |
---|---|---|
committer | telsoa01 <telmo.soares@arm.com> | 2018-03-09 14:13:49 +0000 |
commit | 4fcda0101ec3d110c1d6d7bee5c83416b645528a (patch) | |
tree | c9a70aeb2887006160c1b3d265c27efadb7bdbae /tests/MnistDatabase.cpp | |
download | armnn-4fcda0101ec3d110c1d6d7bee5c83416b645528a.tar.gz |
Release 18.02
Change-Id: Id3c11dc5ee94ef664374a988fcc6901e9a232fa6
Diffstat (limited to 'tests/MnistDatabase.cpp')
-rw-r--r-- | tests/MnistDatabase.cpp | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/tests/MnistDatabase.cpp b/tests/MnistDatabase.cpp new file mode 100644 index 0000000000..5c10b0c2b4 --- /dev/null +++ b/tests/MnistDatabase.cpp @@ -0,0 +1,105 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// See LICENSE file in the project root for full license information. +// +#include "MnistDatabase.hpp" + +#include <boost/numeric/conversion/cast.hpp> +#include <boost/log/trivial.hpp> +#include <boost/assert.hpp> +#include <fstream> +#include <vector> + +constexpr int g_kMnistImageByteSize = 28 * 28; + +void EndianSwap(unsigned int &x) +{ + x = (x >> 24) | ((x << 8) & 0x00FF0000) | ((x >> 8) & 0x0000FF00) | (x << 24); +} + +MnistDatabase::MnistDatabase(const std::string& binaryFileDirectory, bool scaleValues) + : m_BinaryDirectory(binaryFileDirectory) + , m_ScaleValues(scaleValues) +{ +} + +std::unique_ptr<MnistDatabase::TTestCaseData> MnistDatabase::GetTestCaseData(unsigned int testCaseId) +{ + std::vector<unsigned char> I(g_kMnistImageByteSize); + unsigned int label = 0; + + std::string imagePath = m_BinaryDirectory + std::string("t10k-images.idx3-ubyte"); + std::string labelPath = m_BinaryDirectory + std::string("t10k-labels.idx1-ubyte"); + + std::ifstream imageStream(imagePath, std::ios::binary); + std::ifstream labelStream(labelPath, std::ios::binary); + + if (!imageStream.is_open()) + { + BOOST_LOG_TRIVIAL(fatal) << "Failed to load " << imagePath; + return nullptr; + } + if (!labelStream.is_open()) + { + BOOST_LOG_TRIVIAL(fatal) << "Failed to load " << imagePath; + return nullptr; + } + + unsigned int magic, num, row, col; + + // check the files have the correct header + imageStream.read(reinterpret_cast<char*>(&magic), sizeof(magic)); + if (magic != 0x03080000) + { + BOOST_LOG_TRIVIAL(fatal) << "Failed to read " << imagePath; + return nullptr; + } + labelStream.read(reinterpret_cast<char*>(&magic), sizeof(magic)); + if (magic != 0x01080000) + { + BOOST_LOG_TRIVIAL(fatal) << "Failed to read " << labelPath; + return nullptr; + } + + // Endian swap image and label file - All the integers in the files are stored in MSB first(high endian) format, + // hence need to flip the bytes of the header if using it on Intel processors or low-endian machines + labelStream.read(reinterpret_cast<char*>(&num), sizeof(num)); + imageStream.read(reinterpret_cast<char*>(&num), sizeof(num)); + EndianSwap(num); + imageStream.read(reinterpret_cast<char*>(&row), sizeof(row)); + EndianSwap(row); + imageStream.read(reinterpret_cast<char*>(&col), sizeof(col)); + EndianSwap(col); + + // read image and label into memory + imageStream.seekg(testCaseId * g_kMnistImageByteSize, std::ios_base::cur); + imageStream.read(reinterpret_cast<char*>(&I[0]), g_kMnistImageByteSize); + labelStream.seekg(testCaseId, std::ios_base::cur); + labelStream.read(reinterpret_cast<char*>(&label), 1); + + if (!imageStream.good()) + { + BOOST_LOG_TRIVIAL(fatal) << "Failed to read " << imagePath; + return nullptr; + } + if (!labelStream.good()) + { + BOOST_LOG_TRIVIAL(fatal) << "Failed to read " << labelPath; + return nullptr; + } + + std::vector<float> inputImageData; + inputImageData.resize(g_kMnistImageByteSize); + + for (unsigned int i = 0; i < col * row; ++i) + { + inputImageData[i] = boost::numeric_cast<float>(I[i]); + + if(m_ScaleValues) + { + inputImageData[i] /= 255.0f; + } + } + + return std::make_unique<TTestCaseData>(label, std::move(inputImageData)); +} |