-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
346 lines (299 loc) · 14.5 KB
/
main.cpp
File metadata and controls
346 lines (299 loc) · 14.5 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include <iostream>
#include <fstream>
#include <cstdint>
#include <vector>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <cassert>
#include<chrono>
#include <omp.h>
#include <openacc.h>
#include "consts.h"
#include "hidden_layer.h"
#include "execute.h"
#include "fcl_layer.h"
#include "backprop_fcl.h"
#include "backprop_conv.h"
// g++ main.cpp execute.cpp hidden_layer.cpp consts.cpp fcl_layer.cpp backprop_conv.cpp backprop_fcl.cpp -o run.exe
// dont use recursive include, only include whats needed
using namespace std;
float get_scheduled_learning_rate(int epoch, float learning_r) {
if ((epoch + 1) % 30 == 0) {
return learning_r * 0.5f;
std::cout << "[Scheduler] Updated learning rate: " << learning_r * 0.5f << std::endl;
}
else return learning_r;
}
int readData(ifstream
&readFile,vector<Image>
&images){
for(int i=0;i<10000;i++){
uint8_t byte_value;
struct Image img = Image();
for(int i=0;i<10;i++){
img.label[i] = 0;
}
// label is now an array that has all 0s and only the label index as 1.
readFile.read(reinterpret_cast<char*>(&byte_value), 1);
// cout<< byte_value << endl;
img.label[byte_value] = 1;
// cout << "Label: ";
// for (int i = 0; i < 10; ++i) cout << img.label[i] << " ";
// cout << endl;
img.rgb.resize(3, vector<vector<float>>(32, vector<float>(32)));
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 32; j++) {
readFile.read(reinterpret_cast<char*>(&byte_value), 1);
float float_value = static_cast<float>(byte_value) / 255.0f;
img.rgb[0][i][j] = float_value;
}
}
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 32; j++) {
readFile.read(reinterpret_cast<char*>(&byte_value), 1);
float float_value = static_cast<float>(byte_value) / 255.0f;
img.rgb[1][i][j] = float_value;
}
}
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 32; j++) {
readFile.read(reinterpret_cast<char*>(&byte_value), 1);
float float_value = static_cast<float>(byte_value) / 255.0f;
img.rgb[2][i][j] = float_value;
}
}
images.push_back(img);
}
return 0;
}
int readBatch(std::ifstream &file, vector<Image> &images, int num_images_to_read = 10000) {
images.clear();
uint8_t byte_value;
for (int i = 0; i < num_images_to_read && file.peek() != EOF; ++i) {
Image img;
std::fill(img.label, img.label + 10, 0);
file.read(reinterpret_cast<char*>(&byte_value), 1);
img.label[byte_value] = 1;
img.rgb.resize(3, vector<vector<float>>(32, vector<float>(32)));
for (int c = 0; c < 3; c++) {
for (int row = 0; row < 32; row++) {
for (int col = 0; col < 32; col++) {
file.read(reinterpret_cast<char*>(&byte_value), 1);
img.rgb[c][row][col] = static_cast<float>(byte_value) / 255.0f;
}
}
}
images.push_back(img);
}
return images.size();
}
int main() {
// Read from the text file
ifstream MyTrainFile1("data\\cifar-10-binary\\data_batch_1.bin", ios::binary);
if (!MyTrainFile1.is_open()) {
cerr << "Error opening data_batch_1.bin" << endl;
return 1; // Or handle the error appropriately
}
ifstream MyTrainFile2("data\\cifar-10-binary\\data_batch_2.bin", ios::binary);
if (!MyTrainFile1.is_open()) {
cerr << "Error opening data_batch_2.bin" << endl;
return 1; // Or handle the error appropriately
}
// ifstream MyTrainFile3("data\\cifar-10-binary\\data_batch_3.bin", ios::binary);
// if (!MyTrainFile1.is_open()) {
// cerr << "Error opening data_batch_3.bin" << endl;
// return 1; // Or handle the error appropriately
// }
// ifstream MyTrainFile4("data\\cifar-10-binary\\data_batch_4.bin", ios::binary);
// if (!MyTrainFile1.is_open()) {
// cerr << "Error opening data_batch_4.bin" << endl;
// return 1; // Or handle the error appropriately
// }
ifstream MyTrainFile5("data\\cifar-10-binary\\data_batch_5.bin", ios::binary);
if (!MyTrainFile5.is_open()) {
cerr << "Error opening data_batch_5.bin" << endl;
return 1; // Or handle the error appropriately
}
ifstream MyTestFile("data\\cifar-10-binary\\test_batch.bin", ios::binary);
if (!MyTestFile.is_open()) {
cerr << "Error opening test_batch.bin" << endl;
return 1; // Or handle the error appropriately
}
vector<Image> train_images;
vector<Image> test_images;
cout << "Starting to read training data..." << endl;
readData(MyTrainFile1,train_images);
// cout << "Read " << train_images.size() << " images from data_batch_1.bin" << endl;
// cout << "First pixel value (R) of image " << train_images.size()-1 << ": "
// << train_images.back().rgb[0][0][0] << endl;
// for (int l = 0; l < 10; l++) {
// if (train_images.back().label[l] == 1) {
// cout << "Last loaded image label: " << l << endl;
// break;
// }
// }
// assert(train_images.back().rgb[0][0].size() == 3); // RGB check
// assert(train_images.back().rgb.size() == 32); // Height
// assert(train_images.back().rgb[0].size() == 32); // Width
readData(MyTrainFile2,train_images);
// cout << "Read " << train_images.size() << " images from data_batch_2.bin" << endl;
// // cout << "First pixel value (R) of image " << train_images.size()-1 << ": "
// // << train_images.back().rgb[0][0][0] << endl;
// // for (int l = 0; l < 10; l++) {
// // if (train_images.back().label[l] == 1) {
// // cout << "Last loaded image label: " << l << endl;
// // break;
// // }
// // }
// // assert(train_images.back().rgb[0][0].size() == 3); // RGB check
// // assert(train_images.back().rgb.size() == 32); // Height
// // assert(train_images.back().rgb[0].size() == 32); // Width
// readData(MyTrainFile3,train_images);
// cout << "Read " << train_images.size() << " images from data_batch_3.bin" << endl;
// // cout << "First pixel value (R) of image " << train_images.size()-1 << ": "
// // << train_images.back().rgb[0][0][0] << endl;
// // for (int l = 0; l < 10; l++) {
// // if (train_images.back().label[l] == 1) {
// // cout << "Last loaded image label: " << l << endl;
// // break;
// // }
// // }
// // assert(train_images.back().rgb[0][0].size() == 3); // RGB check
// // assert(train_images.back().rgb.size() == 32); // Height
// // assert(train_images.back().rgb[0].size() == 32); // Width
// readData(MyTrainFile4,train_images);
// cout << "Read " << train_images.size() << " images from data_batch_4.bin" << endl;
// // cout << "First pixel value (R) of image " << train_images.size()-1 << ": "
// // << train_images.back().rgb[0][0][0] << endl;
// // for (int l = 0; l < 10; l++) {
// // if (train_images.back().label[l] == 1) {
// // cout << "Last loaded image label: " << l << endl;
// // break;
// // }
// // }
// // assert(train_images.back().rgb[0][0].size() == 3); // RGB check
// // assert(train_images.back().rgb.size() == 32); // Height
// // assert(train_images.back().rgb[0].size() == 32); // Width
readData(MyTrainFile5,train_images);
cout << "Read " << train_images.size() << " images from data_batch_5.bin" << endl;
// cout << "First pixel value (R) of image " << train_images.size()-1 << ": "
// << train_images.back().rgb[0][0][0] << endl;
// cout<<train_images.back().rgb[0][1][1]<<endl;
// cout<<train_images.back().rgb[1][2][1]<<endl;
// cout<<train_images.back().rgb[0][1][4]<<endl;
// cout<<train_images.back().rgb[2][1][7]<<endl;
// cout<<train_images.back().rgb[0][1][3]<<endl;
// for (int l = 0; l < 10; l++) {
// if (train_images.back().label[l] == 1) {
// cout << "Last loaded image label: " << l << endl;
// break;
// }
// }
// // assert(train_images.back().rgb[0][0].size() == 3); // RGB check
// // assert(train_images.back().rgb.size() == 32); // Height
// // assert(train_images.back().rgb[0].size() == 32); // Width
readData(MyTestFile,test_images);
cout << "Read " << test_images.size() << " images from test_batch.bin" << endl;
// // cout << "First pixel value (R) of image " << test_images.size()-1 << ": "
// // << test_images.back().rgb[0][0][0] << endl;
// // for (int l = 0; l < 10; l++) {
// // if (test_images.back().label[l] == 1) {
// // cout << "Last loaded image label: " << l << endl;
// // break;
// // }
// // }
// // assert(test_images.back().rgb[0][0].size() == 3); // RGB check
// // assert(test_images.back().rgb.size() == 32); // Height
// // assert(test_images.back().rgb[0].size() == 32); // Width
// Close the file
MyTrainFile1.close();
MyTrainFile2.close();
// MyTrainFile3.close();
// MyTrainFile4.close();
MyTrainFile5.close();
MyTestFile.close();
// for(int i=0;i<100;i++){
// cout<< train_images[i].label << endl;
// }
vector<vector<vector<vector<vector<float>>>>> kernel_list;
vector<vector<float>> bias_list;
vector<vector<vector<float>>> weights;
vector<vector<float>> weights_bias;
vector<vector<vector<vector<vector<float>>>>> rotated_kernel_list;
vector<vector<vector<float>>> velocity_fcl; // store momentum for FCL
vector<vector<float>> velocity_fcl_bias; // shape [layers][neurons]
vector<vector<vector<vector<vector<float>>>>> velocity_kernels;
vector<vector<float>> velocity_kernels_bias;
// vector<Flat> output;
random_device rd;
mt19937 g(rd());
initialise(kernel_list, rotated_kernel_list, bias_list, weights, weights_bias, velocity_kernels, velocity_kernels_bias, velocity_fcl, velocity_fcl_bias);
cout << "Initialized kernels: " << kernel_list.size() << " conv layers" << endl;
float initial_weight = kernel_list[1][1][1][1][1];
cout << "[Before Training] Initial weight: " << initial_weight << endl;
ofstream logFile("training_log_gpu.txt"); // creates or overwrites
for (int epoch = 0; epoch < epochs; ++epoch) {
learning_rate = get_scheduled_learning_rate(epoch, learning_rate);
using namespace std::chrono;
auto epoch_start = high_resolution_clock::now();
std::vector<std::string> file_paths = {
"data\\cifar-10-binary\\data_batch_1.bin",
"data\\cifar-10-binary\\data_batch_2.bin",
"data\\cifar-10-binary\\data_batch_3.bin",
"data\\cifar-10-binary\\data_batch_4.bin",
"data\\cifar-10-binary\\data_batch_5.bin"
};
std::shuffle(file_paths.begin(), file_paths.end(), std::mt19937{std::random_device{}()});
float epoch_loss = 0.0f;
bool first_itr = (epoch == 0);
for (const auto& path : file_paths) {
ifstream data_file(path, ios::binary);
if (!data_file.is_open()) {
cerr << "Could not open " << path << endl;
continue;
}
// while (!data_file.eof()) {
vector<Image> image_buffer;
int loaded = readBatch(data_file, image_buffer, 10000);
if (loaded == 0) break;
shuffle(image_buffer.begin(), image_buffer.end(), std::mt19937{std::random_device{}()});
for (int i = 0; i < image_buffer.size(); i += batch_size) {
vector<Image> batch(image_buffer.begin() + i, image_buffer.begin() + min(i + batch_size, (int)image_buffer.size()));
float batch_loss = 0.0f;
auto t1 = chrono::high_resolution_clock::now();
process_batch(batch, kernel_list, bias_list, weights, weights_bias, batch_loss, first_itr, rotated_kernel_list, velocity_kernels, velocity_kernels_bias, velocity_fcl, velocity_fcl_bias);
auto t2 = chrono::high_resolution_clock::now();
cout << "Batch time: " << chrono::duration_cast<chrono::milliseconds>(t2 - t1).count() << " ms\n";
first_itr = false; // Only true for the first batch of the first epoch
cout << "Batch " << i/batch_size + 1<< ": Avg Loss = " << batch_loss/batch_size << endl;
epoch_loss += batch_loss;
}
// }
data_file.close();
}
auto epoch_end = high_resolution_clock::now();
auto epoch_duration = duration_cast<minutes>(epoch_end - epoch_start).count();
cout << "[Epoch " << epoch + 1 << "] Duration: " << epoch_duration << " minutes\n";
logFile << "[Epoch " << epoch + 1 << "] Duration: " << epoch_duration << " minutes\n";
cout << "Epoch " << epoch + 1 << ": Average Loss = " << epoch_loss / (50000 / batch_size) << endl;
logFile << "Epoch " << epoch + 1 << ": Average Loss = " << epoch_loss / (50000 / batch_size) << endl;
float updated_weight = kernel_list[1][1][1][1][1];
cout << "[Epoch " << epoch + 1 << "] Tracked weight: " << updated_weight << endl;
logFile << "[Epoch " << epoch + 1 << "] Tracked weight: " << updated_weight << endl;
float updated_fcl_weight = weights[1][1][1];
cout << "[Epoch " << epoch + 1 << "] FCL weight: " << updated_fcl_weight << endl;
logFile << "[Epoch " << epoch + 1 << "] FCL weight: " << updated_fcl_weight << endl;
float batch_loss = 0.0f;
float train_accuracy = process_test(train_images, kernel_list, bias_list, weights, weights_bias, batch_loss);
cout << "[Epoch " << epoch + 1 << "] Training accuracy: " << train_accuracy << endl;
logFile << "[Epoch " << epoch + 1 << "] Training accuracy: " << train_accuracy << endl;
batch_loss = 0.0f;
float test_accuracy = process_test(test_images, kernel_list, bias_list, weights, weights_bias, batch_loss);
cout << "[Epoch " << epoch + 1 << "] Test accuracy: " << test_accuracy << endl;
logFile << "[Epoch " << epoch + 1 << "] Test accuracy: " << test_accuracy << endl;
}
return 0;
}