A modern header-only C++ library for loading idx files like MNIST dataset
No need for additional building, this is a header only library.
#include <idxcpp.hpp>That's it! Just make sure you build your project with C++17 or higher.
It's quite simple to use!
Data is stored internally as a single dimensional vector. You can access the each element with [] operator and cast the pointer to a suitable type.
From test/mnist_test/main.cpp
using namespace Idxcpp;
// Make sure your current working directory is correct
std::cout << std::filesystem::current_path() << std::endl;
Idx trainData(".\\data\\train-images.idx3-ubyte");
for (int i = 0; i < trainData.getDimensions()[1]; i++) {
for (int j = 0; j < trainData.getDimensions()[2]; j++) {
// Returns the pointer where the element is
char* ptr = trainData[0][i][j];
// Prints the data as 2 digit hex
printf("%02x", *ptr & 0xff);
}
std::cout << std::endl;
}Result:
00000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000
000000000000000000000000031212127e88af1aa6fff77f00000000
00000000000000001e245e9aaafdfdfdfdfde1acfdf2c34000000000
0000000000000031eefdfdfdfdfdfdfdfdfb5d525238270000000000
0000000000000012dbfdfdfdfdfdc6b6f7f100000000000000000000
0000000000000000509c6bfdfdcd0b002b9a00000000000000000000
0000000000000000000e019afd5a0000000000000000000000000000
00000000000000000000008bfdbe0200000000000000000000000000
00000000000000000000000bbefd4600000000000000000000000000
00000000000000000000000023f1e1a06c0100000000000000000000
0000000000000000000000000051f0fdfd7719000000000000000000
00000000000000000000000000002dbafdfd961b0000000000000000
000000000000000000000000000000105dfcfdbb0000000000000000
0000000000000000000000000000000000f9fdf94000000000000000
00000000000000000000000000002e82b7fdfdcf0200000000000000
0000000000000000000000002794e5fdfdfdfab60000000000000000
000000000000000000001872ddfdfdfdfdc94e000000000000000000
00000000000000001742d5fdfdfdfdc6510200000000000000000000
00000000000012abdbfdfdfdfdc35009000000000000000000000000
0000000037ace2fdfdfdfdf4850b0000000000000000000000000000
0000000088fdfdfdd487841000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000
If you do not want to use C++17 or higher in your project, that's fine!
Just make sure to define the macro BEFORE including the idxcpp.hpp
#define IDXCPP_CPP_BACKWARDS_COMPABILITY
#include <idxcpp.hpp>