-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnist_file.c
213 lines (169 loc) · 5.66 KB
/
mnist_file.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "include/mnist_file.h"
/**
* Convert from the big endian format in the dataset if we're on a little endian
* machine.
*/
uint32_t map_uint32(uint32_t in)
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
return (
((in & 0xFF000000) >> 24) |
((in & 0x00FF0000) >> 8) |
((in & 0x0000FF00) << 8) |
((in & 0x000000FF) << 24)
);
#else
return in;
#endif
}
/**
* Read labels from file.
*
* File format: http://yann.lecun.com/exdb/mnist/
*/
uint8_t * get_labels(const char * path, uint32_t * number_of_labels)
{
FILE * stream;
mnist_label_file_header_t header;
uint8_t * labels;
stream = fopen(path, "rb");
if (NULL == stream) {
fprintf(stderr, "Could not open file: %s\n", path);
return NULL;
}
if (1 != fread(&header, sizeof(mnist_label_file_header_t), 1, stream)) {
fprintf(stderr, "Could not read label file header from: %s\n", path);
fclose(stream);
return NULL;
}
header.magic_number = map_uint32(header.magic_number);
header.number_of_labels = map_uint32(header.number_of_labels);
if (MNIST_LABEL_MAGIC != header.magic_number) {
fprintf(stderr, "Invalid header read from label file: %s (%08X not %08X)\n", path, header.magic_number, MNIST_LABEL_MAGIC);
fclose(stream);
return NULL;
}
*number_of_labels = header.number_of_labels;
labels = malloc(*number_of_labels * sizeof(uint8_t));
if (labels == NULL) {
fprintf(stderr, "Could not allocated memory for %d labels\n", *number_of_labels);
fclose(stream);
return NULL;
}
if (*number_of_labels != fread(labels, 1, *number_of_labels, stream)) {
fprintf(stderr, "Could not read %d labels from: %s\n", *number_of_labels, path);
free(labels);
fclose(stream);
return NULL;
}
fclose(stream);
return labels;
}
/**
* Read images from file.
*
* File format: http://yann.lecun.com/exdb/mnist/
*/
mnist_image_t * get_images(const char * path, uint32_t * number_of_images)
{
FILE * stream;
mnist_image_file_header_t header;
mnist_image_t * images;
stream = fopen(path, "rb");
if (NULL == stream) {
fprintf(stderr, "Could not open file: %s\n", path);
return NULL;
}
if (1 != fread(&header, sizeof(mnist_image_file_header_t), 1, stream)) {
fprintf(stderr, "Could not read image file header from: %s\n", path);
fclose(stream);
return NULL;
}
header.magic_number = map_uint32(header.magic_number);
header.number_of_images = map_uint32(header.number_of_images);
header.number_of_rows = map_uint32(header.number_of_rows);
header.number_of_columns = map_uint32(header.number_of_columns);
if (MNIST_IMAGE_MAGIC != header.magic_number) {
fprintf(stderr, "Invalid header read from image file: %s (%08X not %08X)\n", path, header.magic_number, MNIST_IMAGE_MAGIC);
fclose(stream);
return NULL;
}
if (MNIST_IMAGE_WIDTH != header.number_of_rows) {
fprintf(stderr, "Invalid number of image rows in image file %s (%d not %d)\n", path, header.number_of_rows, MNIST_IMAGE_WIDTH);
}
if (MNIST_IMAGE_HEIGHT != header.number_of_columns) {
fprintf(stderr, "Invalid number of image columns in image file %s (%d not %d)\n", path, header.number_of_columns, MNIST_IMAGE_HEIGHT);
}
*number_of_images = header.number_of_images;
images = malloc(*number_of_images * sizeof(mnist_image_t));
if (images == NULL) {
fprintf(stderr, "Could not allocated memory for %d images\n", *number_of_images);
fclose(stream);
return NULL;
}
if (*number_of_images != fread(images, sizeof(mnist_image_t), *number_of_images, stream)) {
fprintf(stderr, "Could not read %d images from: %s\n", *number_of_images, path);
free(images);
fclose(stream);
return NULL;
}
fclose(stream);
return images;
}
mnist_dataset_t * mnist_get_dataset(const char * image_path, const char * label_path)
{
mnist_dataset_t * dataset;
uint32_t number_of_images, number_of_labels;
dataset = calloc(1, sizeof(mnist_dataset_t));
if (NULL == dataset) {
return NULL;
}
dataset->images = get_images(image_path, &number_of_images);
if (NULL == dataset->images) {
mnist_free_dataset(dataset);
return NULL;
}
dataset->labels = get_labels(label_path, &number_of_labels);
if (NULL == dataset->labels) {
mnist_free_dataset(dataset);
return NULL;
}
if (number_of_images != number_of_labels) {
fprintf(stderr, "Number of images does not match number of labels (%d != %d)\n", number_of_images, number_of_labels);
mnist_free_dataset(dataset);
return NULL;
}
dataset->size = number_of_images;
return dataset;
}
/**
* Free all the memory allocated in a dataset. This should not be used on a
* batched dataset as the memory is allocated to the parent.
*/
void mnist_free_dataset(mnist_dataset_t * dataset)
{
free(dataset->images);
free(dataset->labels);
free(dataset);
}
/**
* Fills the batch dataset with a subset of the parent dataset.
*/
int mnist_batch(mnist_dataset_t * dataset, mnist_dataset_t * batch, int size, int number)
{
int start_offset;
start_offset = size * number;
if (start_offset >= dataset->size) {
return 0;
}
batch->images = &dataset->images[start_offset];
batch->labels = &dataset->labels[start_offset];
batch->size = size;
if (start_offset + batch->size > dataset->size) {
batch->size = dataset->size - start_offset;
}
return 1;
}