Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
188 lines (150 loc) 路 6.27 KB
/
Copy pathmain.cpp
File metadata and controls
188 lines (150 loc) 路 6.27 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <Eigen/Core>
#include <fstream>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <string>
#include <vector>
// Neural Networks
#include "src/modules/nn/loss/loss.hpp"
#include "src/modules/nn/model/model.hpp"
#include "src/modules/nn/optimizer/optimizer.hpp"
#include "src/modules/nn/prepare/prepare.hpp"
#include "src/nn/include/layer.hpp"
// Core
#include "src/core/utilities/multivariate_normal/multivariate_normal.hpp"
using namespace std;
using namespace cv;
using namespace Eigen;
using namespace nn;
const string root = "/Users/aurorastudyvn/Workspace/ML/VisionCpp";
const string image_path = root + "/image.jpg";
const string video_path = root + "/video.mp4";
// Function to read IDX3-UBYTE files
std::vector<std::vector<unsigned char>> readIDX3UByteFile(const std::string &filename) {
std::ifstream file(filename, std::ios::binary);
if (!file) {
std::cerr << "Failed to open the IDX3-UBYTE file." << std::endl;
return {};
}
// Read the IDX3-UBYTE file header
char magicNumber[4];
char numImagesBytes[4];
char numRowsBytes[4];
char numColsBytes[4];
file.read(magicNumber, 4);
file.read(numImagesBytes, 4);
file.read(numRowsBytes, 4);
file.read(numColsBytes, 4);
std::cout << static_cast<int>(numImagesBytes[0]) << " " << static_cast<int>(numImagesBytes[1])
<< " " << (int)static_cast<unsigned char>(numImagesBytes[2]) << " "
<< static_cast<int>(numImagesBytes[3]) << " " << std::endl;
// Convert the header information from big-endian to native endianness
int numImages = (static_cast<unsigned char>(numImagesBytes[0]) << 24) |
(static_cast<unsigned char>(numImagesBytes[1]) << 16) |
(static_cast<unsigned char>(numImagesBytes[2]) << 8) |
static_cast<unsigned char>(numImagesBytes[3]);
int numRows = (static_cast<unsigned char>(numRowsBytes[0]) << 24) |
(static_cast<unsigned char>(numRowsBytes[1]) << 16) |
(static_cast<unsigned char>(numRowsBytes[2]) << 8) |
static_cast<unsigned char>(numRowsBytes[3]);
int numCols = (static_cast<unsigned char>(numColsBytes[0]) << 24) |
(static_cast<unsigned char>(numColsBytes[1]) << 16) |
(static_cast<unsigned char>(numColsBytes[2]) << 8) |
static_cast<unsigned char>(numColsBytes[3]);
// Initialize a vector to store the images
std::vector<std::vector<unsigned char>> images;
for (int i = 0; i < numImages; i++) {
// Read each image as a vector of bytes
std::vector<unsigned char> image(numRows * numCols);
file.read((char *)(image.data()), numRows * numCols);
images.push_back(image);
}
file.close();
return images;
}
// Function to read IDX3-UBYTE files
std::vector<std::vector<unsigned char>> readLabelFile(const std::string &filename) {
std::ifstream file(filename, std::ios::binary);
if (!file) {
std::cerr << "Failed to open the IDX3-UBYTE file." << std::endl;
return {};
}
// Read the IDX3-UBYTE file header
char magicNumber[4];
char numImagesBytes[4];
file.read(magicNumber, 4);
file.read(numImagesBytes, 4);
// Convert the header information from big-endian to native endianness
int numImages = (static_cast<unsigned char>(numImagesBytes[0]) << 24) |
(static_cast<unsigned char>(numImagesBytes[1]) << 16) |
(static_cast<unsigned char>(numImagesBytes[2]) << 8) |
static_cast<unsigned char>(numImagesBytes[3]);
// Initialize a vector to store the images
std::vector<std::vector<unsigned char>> images;
for (int i = 0; i < numImages; i++) {
// Read each image as a vector of bytes
std::vector<unsigned char> image(1);
file.read((char *)(image.data()), 1);
images.push_back(image);
}
file.close();
return images;
}
void read_mnist() {
string X_train_path =
"/Users/aurorastudyvn/Workspace/ML/VisionCpp/"
"train-images-idx3-ubyte:train-images.idx3-ubyte";
string y_train_path =
"/Users/aurorastudyvn/Workspace/ML/VisionCpp/"
"train-labels-idx1-ubyte:train-labels.idx1-ubyte";
std::vector<std::vector<unsigned char>> imagesFile = readIDX3UByteFile(X_train_path);
std::vector<std::vector<unsigned char>> labelsFile = readLabelFile(y_train_path);
std::vector<Mat> imagesData; // Store your images
std::vector<int> labelsData; // Corresponding labels
for (int imgCnt = 0; imgCnt < (int)imagesFile.size(); imgCnt++) {
int rowCounter = 0;
int colCounter = 0;
Mat tempImg = Mat::zeros(Size(28, 28), CV_8UC1);
for (int i = 0; i < (int)imagesFile[imgCnt].size(); i++) {
tempImg.at<uchar>(Point(colCounter++, rowCounter)) = (int)imagesFile[imgCnt][i];
if ((i) % 28 == 0) {
rowCounter++;
colCounter = 0;
if (i == 756) break;
}
}
std::cout << (int)labelsFile[imgCnt][0] << std::endl;
imagesData.push_back(tempImg);
labelsData.push_back((int)labelsFile[imgCnt][0]);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
constexpr int N = 10000;
int d = 2;
int d1 = 5;
int classes = 4;
MatrixXd X(N, d);
MatrixXd Y(N, classes);
prepare(N, classes, d, X, Y);
constexpr int T = N * 0.8;
MatrixXd XTrain = X.block(0, 0, T, d);
MatrixXd YTrain = Y.block(0, 0, T, classes);
MatrixXd XTest = X.block(T, 0, N - T, d);
MatrixXd YTest = Y.block(T, 0, N - T, classes);
vector<unique_ptr<Layer>> layers;
layers.push_back(make_unique<ReLU>(d, d1));
layers.push_back(make_unique<Softmax>(d1, classes));
unique_ptr<Loss> loss = make_unique<CrossEntropyLoss>();
unique_ptr<Optimizer> optimizer = make_unique<SGD>(1e-1, 0.9, true, 0.0);
nn::Sequential sequential(layers, loss, optimizer);
constexpr int epochs = 30;
constexpr int batchSize = 256;
sequential.fit(XTrain, YTrain, epochs, batchSize);
MatrixXd O = sequential.predict(XTest);
const double accuracy = sequential.evaluate(YTest, O);
cout << "Test Accuracy: " << accuracy * 100.0 << "%" << endl;
return EXIT_SUCCESS;
}