2015-10-29 12:06:30 +00:00
|
|
|
#ifndef MNISTLOADER_H
|
|
|
|
#define MNISTLOADER_H
|
|
|
|
|
|
|
|
#include <string>
|
2015-10-31 13:58:49 +00:00
|
|
|
#include <vector>
|
2015-10-31 11:28:35 +00:00
|
|
|
#include <memory>
|
2015-10-29 15:00:58 +00:00
|
|
|
#include <inttypes.h>
|
2015-10-29 12:06:30 +00:00
|
|
|
|
2015-11-01 14:00:32 +00:00
|
|
|
#include <QImage>
|
|
|
|
|
2015-10-29 12:06:30 +00:00
|
|
|
class MnistLoader
|
|
|
|
{
|
2015-10-31 11:28:35 +00:00
|
|
|
private:
|
|
|
|
static const uint32_t DatabaseFileMagicNumber = 2051;
|
|
|
|
static const uint32_t LabelFileMagicNumber = 2049;
|
|
|
|
static const size_t SampleWidth = 28;
|
|
|
|
static const size_t SampleHeight = 28;
|
|
|
|
|
|
|
|
public:
|
|
|
|
template<size_t SAMPLE_WIDTH, size_t SAMPLE_HEIGHT>
|
|
|
|
class Sample
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
uint8_t label;
|
|
|
|
uint8_t data[SAMPLE_WIDTH * SAMPLE_HEIGHT];
|
2015-11-01 14:00:32 +00:00
|
|
|
|
|
|
|
QImage toQImage() const
|
|
|
|
{
|
|
|
|
return QImage(data, SAMPLE_WIDTH, SAMPLE_HEIGHT, QImage::Format_Grayscale8);
|
|
|
|
}
|
2015-10-31 11:28:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
using MnistSample = Sample<SampleWidth, SampleHeight>;
|
|
|
|
|
|
|
|
private:
|
2015-10-31 13:58:49 +00:00
|
|
|
std::vector<std::unique_ptr<MnistSample>> samples;
|
2015-10-31 11:28:35 +00:00
|
|
|
|
2015-10-29 12:06:30 +00:00
|
|
|
public:
|
|
|
|
void load(const std::string &databaseFileName, const std::string &labelsFileName);
|
2015-10-29 15:00:58 +00:00
|
|
|
|
2015-11-01 13:49:02 +00:00
|
|
|
size_t getSamleCount() const;
|
|
|
|
const MnistSample &getSample(size_t index) const;
|
2015-10-31 13:58:49 +00:00
|
|
|
const MnistSample &getRandomSample() const;
|
|
|
|
|
2015-10-29 15:00:58 +00:00
|
|
|
private:
|
|
|
|
void loadDatabase(const std::string &fileName);
|
|
|
|
void loadLabels(const std::string &fileName);
|
|
|
|
|
|
|
|
static int8_t readInt8(std::ifstream &file);
|
|
|
|
static int32_t readInt32(std::ifstream &file);
|
2015-10-29 12:06:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MNISTLOADER_H
|