-
Notifications
You must be signed in to change notification settings - Fork 1
/
loadmnistdialog.cpp
124 lines (110 loc) · 3.34 KB
/
loadmnistdialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "loadmnistdialog.h"
#include "ui_loadmnistdialog.h"
loadMNISTDialog::loadMNISTDialog(QWidget *parent, QMLP *mlp) :
QMainWindow(parent),
ui(new Ui::loadMNISTDialog),
_mlp(mlp)
{
ui->setupUi(this);
show();
thread = new MNISTThread(this, _mlp);
connect(thread, &MNISTThread::finished, this, &QMainWindow::hide);
connect(thread, &MNISTThread::state, ui->progressBar, &QProgressBar::setValue);
string filename1 = QFileDialog::getOpenFileName(this, "Open trainImages", QDir::homePath(), "trainImages").toStdString();
string filename2 = QFileDialog::getOpenFileName(this, "Open trainLabels", QDir::homePath(), "trainLabels").toStdString();
thread->start(filename1, filename2);
}
loadMNISTDialog::~loadMNISTDialog()
{
delete ui;
}
void MNISTThread::start(string filename1, string filename2)
{
_filename1 = filename1;
_filename2 = filename2;
QThread::start();
}
void MNISTThread::run()
{
_mlp->setInput(readMNISTPics());
_mlp->setOutput(readMNISTLabels());
_mlp->setArchitecture(MLP::INIT);
emit finished();
}
int MNISTThread::reverseInt(int i)
{
unsigned char ch1, ch2, ch3, ch4;
ch1=i&255;
ch2=(i>>8)&255;
ch3=(i>>16)&255;
ch4=(i>>24)&255;
return((int)ch1<<24)+((int)ch2<<16)+((int)ch3<<8)+ch4;
}
EigenMatrix MNISTThread::readMNISTPics()
{
EigenMatrix dataSet;
ifstream file (_filename1,ios::binary);
if (file.is_open())
{
int magicNumber=0, numberOfImages=0, nbRows=0, nbCols=0;
file.read((char*)&magicNumber,sizeof(magicNumber));
magicNumber= reverseInt(magicNumber);
file.read((char*)&numberOfImages,sizeof(numberOfImages));
numberOfImages= reverseInt(numberOfImages);
file.read((char*)&nbRows,sizeof(nbRows));
nbRows= reverseInt(nbRows);
file.read((char*)&nbCols,sizeof(nbCols));
nbCols= reverseInt(nbCols);
numberOfImages = 2000;
dataSet.resize(nbRows * nbCols, numberOfImages);
int j=1;
for(int i=0;i<numberOfImages;++i)
{
for(int r=0;r<nbRows;++r)
{
for(int c=0;c<nbCols;++c)
{
unsigned char temp=0;
file.read((char*)&temp,sizeof(temp));
dataSet(nbRows * r + c, i) = temp;
}
}
if (j*numberOfImages/100 <= i)
{
emit state(j);
j++;
}
}
}
file.close();
return dataSet;
}
EigenMatrix MNISTThread::readMNISTLabels()
{
EigenMatrix dataSet;
ifstream file (_filename2,ios::binary);
if (file.is_open())
{
int magicNumber=0, numberOfImages=0;
file.read((char*)&magicNumber,sizeof(magicNumber));
magicNumber= reverseInt(magicNumber);
file.read((char*)&numberOfImages,sizeof(numberOfImages));
numberOfImages= reverseInt(numberOfImages);
numberOfImages = 2000;
dataSet = -EigenMatrix::Ones(10, numberOfImages);
int j=1;
for(int i=0;i<numberOfImages;++i)
{
unsigned char temp=0;
file.read((char*)&temp,sizeof(temp));
dataSet(temp, i) = 1;
if (j*numberOfImages/100 <= i)
{
emit state(j);
j++;
}
}
}
file.close();
return dataSet;
}