-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcifar10_loader.cpp
More file actions
38 lines (29 loc) · 1.15 KB
/
cifar10_loader.cpp
File metadata and controls
38 lines (29 loc) · 1.15 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
#include "cifar10_loader.h"
#include <stdio.h>
#include <stdlib.h>
std::vector<CIFAR10Image> load_cifar10_bin(const char* data_path) {
std::vector<CIFAR10Image> dataset(NUM_IMAGES);
FILE* file = fopen(data_path, "rb");
if (!file) {
fprintf(stderr, "Error opening file: %s\n", data_path);
exit(1);
}
for (int i = 0; i < NUM_IMAGES; ++i) {
unsigned char label;
fread(&label, LABEL_BYTES, 1, file);
unsigned char buffer[IMAGE_BYTES];
fread(buffer, IMAGE_BYTES, 1, file);
cv::Mat img(IMAGE_SIZE, IMAGE_SIZE, CV_8UC3);
for (int row = 0; row < IMAGE_SIZE; ++row) {
for (int col = 0; col < IMAGE_SIZE; ++col) {
img.at<cv::Vec3b>(row, col)[0] = buffer[row * IMAGE_SIZE + col]; // Blue
img.at<cv::Vec3b>(row, col)[1] = buffer[IMAGE_SIZE * IMAGE_SIZE + row * IMAGE_SIZE + col]; // Green
img.at<cv::Vec3b>(row, col)[2] = buffer[2 * IMAGE_SIZE * IMAGE_SIZE + row * IMAGE_SIZE + col]; // Red
}
}
dataset[i].image = img;
dataset[i].label = (int)label;
}
fclose(file);
return dataset;
}