From 81d1f54c981980d268ffd38f3400f222098f5cbe Mon Sep 17 00:00:00 2001 From: mandlm Date: Mon, 26 Oct 2015 22:05:50 +0100 Subject: [PATCH] Started loader-class for 8x8 pixel digit training data --- Layer.cpp | 2 +- gui/NeuroUI/NeuroUI.pro | 6 ++++-- gui/NeuroUI/netlearner.cpp | 6 +++++- gui/NeuroUI/trainingdataloader.cpp | 29 +++++++++++++++++++++++++++++ gui/NeuroUI/trainingdataloader.h | 26 ++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 gui/NeuroUI/trainingdataloader.cpp create mode 100644 gui/NeuroUI/trainingdataloader.h diff --git a/Layer.cpp b/Layer.cpp index 96010c5..8ddcad8 100644 --- a/Layer.cpp +++ b/Layer.cpp @@ -54,7 +54,7 @@ void Layer::connectTo(const Layer & nextLayer) void Layer::updateInputWeights(Layer & prevLayer) { - static const double trainingRate = 0.3; + static const double trainingRate = 0.2; for (size_t targetLayerIndex = 0; targetLayerIndex < sizeWithoutBiasNeuron(); ++targetLayerIndex) { diff --git a/gui/NeuroUI/NeuroUI.pro b/gui/NeuroUI/NeuroUI.pro index 66639e1..cd77734 100644 --- a/gui/NeuroUI/NeuroUI.pro +++ b/gui/NeuroUI/NeuroUI.pro @@ -18,14 +18,16 @@ SOURCES += main.cpp\ ../../Net.cpp \ ../../Neuron.cpp \ netlearner.cpp \ - errorplotter.cpp + errorplotter.cpp \ + trainingdataloader.cpp HEADERS += neuroui.h \ ../../Layer.h \ ../../Net.h \ ../../Neuron.h \ netlearner.h \ - errorplotter.h + errorplotter.h \ + trainingdataloader.h FORMS += neuroui.ui diff --git a/gui/NeuroUI/netlearner.cpp b/gui/NeuroUI/netlearner.cpp index af849db..8005ef3 100644 --- a/gui/NeuroUI/netlearner.cpp +++ b/gui/NeuroUI/netlearner.cpp @@ -1,5 +1,6 @@ #include "netlearner.h" #include "../../Net.h" +#include "trainingdataloader.h" #include @@ -9,6 +10,9 @@ void NetLearner::run() { QElapsedTimer timer; + TrainingDataLoader dataLoader; + dataLoader.addSamples("../NeuroUI/training data/mnist_train0.jpg", 0); + Net myNet; try { @@ -26,7 +30,7 @@ void NetLearner::run() timer.start(); - size_t numIterations = 1000000; + size_t numIterations = 2000000; for (size_t iteration = 0; iteration < numIterations; ++iteration) { std::vector inputValues = diff --git a/gui/NeuroUI/trainingdataloader.cpp b/gui/NeuroUI/trainingdataloader.cpp new file mode 100644 index 0000000..8c99d41 --- /dev/null +++ b/gui/NeuroUI/trainingdataloader.cpp @@ -0,0 +1,29 @@ +#include "trainingdataloader.h" + +#include +#include + +TrainingDataLoader::TrainingDataLoader() +{ + +} + +void TrainingDataLoader::addSamples(const QString &sourceFile, TrainingDataLoader::SampleId sampleId) +{ + QImage sourceImage; + sourceImage.load(sourceFile); + + Sample sample; + sample.first = sampleId; + + for (unsigned int y = 0; y < 8; ++y) + { + for (unsigned int x = 0; x < 8; ++x) + { + sample.second[x + y * 8] = qGray(sourceImage.pixel(x, y)) / 255.0; + } + } + + m_samples.push_back(sample); +} + diff --git a/gui/NeuroUI/trainingdataloader.h b/gui/NeuroUI/trainingdataloader.h new file mode 100644 index 0000000..912b426 --- /dev/null +++ b/gui/NeuroUI/trainingdataloader.h @@ -0,0 +1,26 @@ +#ifndef TRAININGDATALOADER_H +#define TRAININGDATALOADER_H + +#include +#include +#include + +#include + +class TrainingDataLoader +{ +public: + using SampleData = double[64]; + using SampleId = unsigned int; + using Sample = std::pair; + +private: + std::list m_samples; + +public: + TrainingDataLoader(); + + void addSamples(const QString &sourceFile, SampleId sampleId); +}; + +#endif // TRAININGDATALOADER_H