-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdata.cpp
More file actions
593 lines (537 loc) · 19.7 KB
/
Copy pathdata.cpp
File metadata and controls
593 lines (537 loc) · 19.7 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// ---------------------------------------------------------------------------
// File: data.cpp
// Load images and labels. Functions to load batched input images and labels
// for MNIST, Cifar-10 and ImageNet in raw or batched format.
// ---------------------------------------------------------------------------
// See our arXiv paper for detail: https://arxiv.org/abs/2006.16578
// Ang Li, Scientist, Pacific Northwest National Laboratory(PNNL), U.S.
// Homepage: http://www.angliphd.com
// GitHub repo: http://www.github.com/pnnl/TCBNN
// PNNL-IPID: 31925-E, ECCN: EAR99, IR: PNNL-SA-152850
// BSD Lincese.
// Richland, 99352, WA, USA. June-30-2020.
// ---------------------------------------------------------------------------
#include "data.h"
//========================== MNIST ================================
inline int 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;
}
void read_MNIST_raw(string filename, string labelname, uchar* images, unsigned* labels, const unsigned batch)
{
const int image_height = 28;
const int image_width = 28;
const int image_channel = 1;
if (images == NULL)
{
fprintf(stderr, "NULL images pointer in reading MNIST in data.cpp.\n");
exit(1);
}
if (labels == NULL)
{
fprintf(stderr, "NULL labels pointer in reading MNIST in data.cpp.\n");
exit(1);
}
memset(images,0,batch*image_height*image_width*image_channel*sizeof(uchar));
memset(labels,0,batch*sizeof(unsigned));
//Read images
ifstream file(filename, ios::binary);
if (file.is_open())
{
int magic_number = 0;
int number_of_images = 0;
int n_rows = 0;
int n_cols = 0;
file.read((char*) &magic_number, sizeof(magic_number));
magic_number = ReverseInt(magic_number);
file.read((char*) &number_of_images,sizeof(number_of_images));
number_of_images = ReverseInt(number_of_images);
file.read((char*) &n_rows, sizeof(n_rows));
n_rows = ReverseInt(n_rows);
file.read((char*) &n_cols, sizeof(n_cols));
n_cols = ReverseInt(n_cols);
#ifdef VERBOSE
printf("Reading raw MNIST images in a batch of %u in data.cpp.\n", batch);
#endif
for (int i = 0; i < batch; ++i)
for (int h = 0; h < image_height; ++h)
for (int w = 0; w < image_width; ++w)
{
uchar temp = 0;
file.read((char*) &temp, sizeof(temp));
images[i*image_height*image_width+h*image_width+w] = temp;
}
}
else
{
fprintf(stderr, "Error in reading MNIST image file '%s' in data.cpp.\n", filename.c_str());
exit(1);
}
//Read labels
ifstream labelfile(labelname, ios::binary);
if (labelfile.is_open())
{
int magic_number = 0;
int number_of_images = 0;
int n_rows = 0;
int n_cols = 0;
labelfile.read((char*) &magic_number, sizeof(magic_number));
magic_number = ReverseInt(magic_number);
labelfile.read((char*) &number_of_images,sizeof(number_of_images));
number_of_images = ReverseInt(number_of_images);
#ifdef VERBOSE
printf("Reading raw MNIST labels in a batch of %u in data.cpp.\n", batch);
#endif
for(int i = 0; i < batch; ++i)
{
uchar temp = 0;
labelfile.read((char*) &temp, sizeof(temp));
labels[i]= (int)temp;
}
}
else
{
fprintf(stderr, "Error in reading MNIST label file '%s' in data.cpp.\n", labelname.c_str());
exit(1);
}
file.close();
labelfile.close();
}
void read_MNIST_normalized(string filename, string labelname, float* images, unsigned* labels, const unsigned batch)
{
const int image_height = 28;
const int image_width = 28;
const int image_channel = 1;
if (images == NULL)
{
fprintf(stderr, "NULL images pointer in reading MNIST in data.cpp.\n");
exit(1);
}
if (labels == NULL)
{
fprintf(stderr, "NULL labels pointer in reading MNIST in data.cpp.\n");
exit(1);
}
memset(images,0,batch*image_height*image_width*image_channel*sizeof(float));
memset(labels,0,batch*sizeof(unsigned));
//Read images
ifstream file(filename, ios::binary);
if (file.is_open())
{
int magic_number = 0;
int number_of_images = 0;
int n_rows = 0;
int n_cols = 0;
file.read((char*) &magic_number, sizeof(magic_number));
magic_number = ReverseInt(magic_number);
file.read((char*) &number_of_images,sizeof(number_of_images));
number_of_images = ReverseInt(number_of_images);
file.read((char*) &n_rows, sizeof(n_rows));
n_rows = ReverseInt(n_rows);
file.read((char*) &n_cols, sizeof(n_cols));
n_cols = ReverseInt(n_cols);
#ifdef VERBOSE
printf("Reading normalized MNIST images in a batch of %u in data.cpp.\n", batch);
#endif
for (int i = 0; i < batch; ++i)
for (int h = 0; h < image_height; ++h)
for (int w = 0; w < image_width; ++w)
{
uchar temp = 0;
file.read((char*) &temp, sizeof(temp));
images[i*image_height*image_width+h*image_width+w] = (((float)temp/255.f)-0.1307f)/0.3081f; //Normalize
}
}
else
{
fprintf(stderr, "Error in reading MNIST image file '%s' in data.cpp.\n", filename.c_str());
exit(1);
}
//Read labels
ifstream labelfile (labelname, ios::binary);
if (labelfile.is_open())
{
int magic_number = 0;
int number_of_images = 0;
int n_rows = 0;
int n_cols = 0;
labelfile.read((char*) &magic_number, sizeof(magic_number));
magic_number = ReverseInt(magic_number);
labelfile.read((char*) &number_of_images,sizeof(number_of_images));
number_of_images = ReverseInt(number_of_images);
#ifdef VERBOSE
printf("Reading normalized MNIST labels in a batch of %u in data.cpp.\n", batch);
#endif
for(int i = 0; i < batch; ++i)
{
uchar temp = 0;
labelfile.read((char*) &temp, sizeof(temp));
labels[i]= (int)temp;
}
}
else
{
fprintf(stderr, "Error in reading MNIST label file '%s' in data.cpp.\n", labelname.c_str());
exit(1);
}
file.close();
labelfile.close();
}
//========================== CIFAR-10 ================================
void read_CIFAR10_raw(string filename, unsigned* images, unsigned* labels, const unsigned batch)
{
const int image_height = 32;
const int image_width = 32;
const int image_channel = 3;
if (images == NULL)
{
fprintf(stderr, "NULL images pointer in reading Cifar-10 in data.cpp.\n");
exit(1);
}
if (labels == NULL)
{
fprintf(stderr, "NULL labels pointer in reading Cifar-10 in data.cpp.\n");
exit(1);
}
memset(images,0,batch*image_height*image_width*sizeof(unsigned));
memset(labels,0,batch*sizeof(unsigned));
uchar* ui = (uchar*)&images[0];
ifstream file(filename, ios::binary);
if (file.is_open())
{
#ifdef VERBOSE
printf("Reading raw CIFAR-10 in a batch of %u in data.cpp.\n", batch);
#endif
for (int l = 0; l < batch; ++l)
{
//Cifar10 data stored in <1xlabel><r:1024><g:1024><b:1024>
unsigned char temp = 0;
file.read((char*) &temp, sizeof(temp));
labels[l] = (unsigned)temp;
for (int c = 0; c<image_channel; c++) //channel
for (int h=0; h<image_height; h++)
for (int w=0; w<image_width; w++)
{
file.read((char*) &temp, sizeof(temp));
ui[(l*image_height*image_width+h*image_width+w)*4+c] = temp;
}
}
}
else
{
fprintf(stderr, "Error in reading Cifar-10 bin file '%s' in data.cpp.\n", filename.c_str());
exit(1);
}
file.close();
}
void read_CIFAR10_normalized(string filename, float* images, unsigned* labels, const unsigned batch)
{
const int image_height = 32;
const int image_width = 32;
const int image_channel = 3;
if (images == NULL)
{
fprintf(stderr, "NULL images pointer in reading Cifar-10 in data.cpp.\n");
exit(1);
}
if (labels == NULL)
{
fprintf(stderr, "NULL labels pointer in reading Cifar-10 in data.cpp.\n");
exit(1);
}
memset(images,0,batch*image_height*image_width*image_channel*sizeof(float));
memset(labels,0,batch*sizeof(unsigned));
ifstream file(filename, ios::binary);
if (file.is_open())
{
#ifdef VERBOSE
printf("Reading normalized CIFAR-10 in a batch of %u in data.cpp.\n", batch);
#endif
for (int l = 0; l < batch; ++l)
{
//Cifar10 data stored in <1xlabel><r:1024><g:1024><b:1024>
unsigned char temp = 0;
file.read((char*) &temp, sizeof(temp));
labels[l] = (unsigned)temp;
//Processing Red channel
for (int h=0; h<image_height; h++)
for (int w=0; w<image_width; w++)
{
file.read((char*) &temp, sizeof(temp));
images[l*image_height*image_width*image_channel
+ 0*image_height*image_width + h*image_width + w] =
((float)temp/255.0f-0.4914f)/0.2470f;
}
//Processing Green channel
for (int h=0; h<image_height; h++)
for (int w=0; w<image_width; w++)
{
file.read((char*) &temp, sizeof(temp));
images[l*image_height*image_width*image_channel
+ 1*image_height*image_width + h*image_width + w] =
((float)temp/255.0f-0.4822f)/0.2435f;
}
//Processing Blue channel
for (int h=0; h<image_height; h++)
for (int w=0; w<image_width; w++)
{
file.read((char*) &temp, sizeof(temp));
images[l*image_height*image_width*image_channel
+ 2*image_height*image_width + h*image_width + w] =
((float)temp/255.0f-0.4465f)/0.2616f;
}
}
}
else
{
fprintf(stderr, "Error in reading Cifar-10 bin file '%s' in data.cpp.\n", filename.c_str());
exit(1);
}
file.close();
}
//========================== ImageNet =================================
void read_raw_jpeg(const char* filename, uchar* fdata, int image_height, int image_width)
{
unsigned long x, y;
unsigned long data_size; // length of the file
int channels; // 3 =>RGB 4 =>RGBA
unsigned int type;
unsigned char * rowptr[1]; // pointer to an array
unsigned char * jdata; // data for the image
struct jpeg_decompress_struct info; //for our jpeg info
struct jpeg_error_mgr err; //the error handler
FILE* file = fopen(filename, "rb"); //open the file
info.err = jpeg_std_error(& err);
jpeg_create_decompress(& info); //fills info structure
if(!file)
{
fprintf(stderr, "Error reading JPEG file %s in data.cpp.\n", filename);
exit(1);
}
jpeg_stdio_src(&info, file);
jpeg_read_header(&info, TRUE); // read jpeg file header
jpeg_start_decompress(&info); // decompress the file
//set width and height
x = info.output_width;
y = info.output_height;
channels = info.num_components;
data_size = x * y * 4;
jdata = (uchar*)malloc(data_size);
memset(jdata,0,data_size);
while (info.output_scanline < info.output_height)
{
// Enable jpeg_read_scanlines() to fill our jdata array
rowptr[0] = (uchar*)jdata + 3* info.output_width * info.output_scanline;
jpeg_read_scanlines(&info, rowptr, 1);
}
jpeg_finish_decompress(&info);
double scale_w = (double)x/(double)image_width;
double scale_h = (double)y/(double)image_height;
for (int c=0; c<3; c++) // channel
{
for (int h=0; h<image_height; h++)
{
for (int w=0; w<image_width; w++)
{
int map_h = floor(scale_h*(double)h);
int map_w = floor(scale_w*(double)w);
fdata[(h*image_width+w)*4+c] = jdata[c*x*y+ map_h*x+map_w];
}
}
}
free(jdata);
}
void read_ImageNet_raw(const char* descfilename, unsigned* images, unsigned* labels, int batch)
{
const int image_height = 224;
const int image_width = 224;
const int image_channel = 3;
if (images == NULL)
{
fprintf(stderr, "NULL images pointer in reading ImageNet in data.cpp.\n");
exit(1);
}
if (labels == NULL)
{
fprintf(stderr, "NULL labels pointer in reading ImageNet in data.cpp.\n");
exit(1);
}
memset(images,0,batch*image_height*image_width*sizeof(unsigned));
memset(labels,0,batch*sizeof(unsigned));
FILE* descfile = fopen("imagenet_files.txt", "r");
if(descfile == NULL)
{
fprintf(stderr, "Error reading Imagenet Description File '%s'.\n", descfilename);
exit(1);
}
#ifdef VERBOSE
printf("Reading raw ImageNet in a batch of %u in data.cpp.\n", batch);
#endif
char filename[128];
for (int i=0; i<batch; i++)
{
unsigned tag;
fscanf(descfile, " %[^,\n],", filename);
fscanf(descfile, "%u", &tag);
labels[i] = tag;
read_raw_jpeg(filename, (uchar*)&images[i*image_height*image_width], image_height, image_width);
}
fclose(descfile);
}
inline float lerp(float s, float e, float t)
{
return s+(e-s)*t;
}
inline float blerp(float c00, float c10, float c01, float c11, float tx, float ty)
{
return lerp(lerp(c00, c10, tx), lerp(c01, c11, tx), ty);
}
void scale_transform(const uchar *src, uchar *dst, int dst_h, int dst_w, int src_h, int src_w)
{
int x, y;
for(x=0, y=0; y<dst_h; x++)
{
if(x > dst_w)
{
x=0; y++;
}
float gx = x / (float)(dst_w) * (src_w-1);
float gy = y / (float)(dst_h) * (src_h-1);
int gxi = (int)gx;
int gyi = (int)gy;
for (int c=0; c<3; c++)
{
float c00 = (float)src[c*src_w*src_h + (gyi*src_w)+gxi];
float c10 = (float)src[c*src_w*src_h + (gyi*src_w)+gxi+1];
float c01 = (float)src[c*src_w*src_h + ((gyi+1)*src_w)+gxi];
float c11 = (float)src[c*src_w*src_h + ((gyi+1)*src_w)+gxi+1];
float result = blerp(c00, c10, c01, c11, gx-gxi, gy-gyi);
dst[c*dst_h*dst_w + y*dst_w + x] = (unsigned char)result;
}
}
}
void read_normalized_jpeg(const char* filename, float* fdata, int image_height, int image_width)
{
unsigned long org_width, org_height;
unsigned long data_size; // length of the file
int channels; // 3 =>RGB 4 =>RGBA
unsigned int type;
unsigned char * rowptr[1]; // pointer to an array
unsigned char * hwc_data; // HWC data for the image
unsigned char * chw_data; // CHW data for the image
struct jpeg_decompress_struct info; //for our jpeg info
struct jpeg_error_mgr err; //the error handler
FILE* file = fopen(filename, "rb"); //open the file
info.err = jpeg_std_error(& err);
jpeg_create_decompress(& info); //fills info structure
if(!file)
{
fprintf(stderr, "Error reading JPEG file %s in data.cpp.\n", filename);
exit(1);
}
jpeg_stdio_src(&info, file);
jpeg_read_header(&info, TRUE); // read jpeg file header
jpeg_start_decompress(&info); // decompress the file
//set width and height
org_width = info.output_width;
org_height = info.output_height;
channels = info.num_components;
data_size = org_width * org_height * 4;
hwc_data = (uchar*)malloc(data_size);
memset(hwc_data,0,data_size);
while (info.output_scanline < info.output_height)
{
// Enable jpeg_read_scanlines() to fill our hwc_data array
rowptr[0] = (uchar*)hwc_data + 3* info.output_width * info.output_scanline;
jpeg_read_scanlines(&info, rowptr, 1);
}
jpeg_finish_decompress(&info);
chw_data = (uchar*)malloc(data_size);
memset(chw_data,0,data_size);
//Convert from HWC to CHW
for (int h=0; h<org_height; h++)
for (int w=0; w<org_width; w++)
for (int c=0; c<3; c++)
chw_data[c*org_height*org_width+h*org_width+w]
= hwc_data[h*org_width*3 + w*3 + c];
//Scale and Bilinear Interpolate
float scale = 256.0f/min(org_width, org_height);
int scaled_height = 0;
int scaled_width = 0;
if (org_width < org_height)
{
scaled_width = 256; scaled_height = int(scale*org_height);
}
else
{
scaled_height = 256; scaled_width = int(scale*org_width);
}
//printf("=========%d,%d=========\n",scaled_height, scaled_width);
uchar* scale_data = (uchar*)malloc(3*scaled_height*scaled_width*sizeof(uchar));
scale_transform(chw_data, scale_data, scaled_height, scaled_width, org_height, org_width);
//CenterCrop and Normalize
for (int h=0; h<image_height; h++)
{
for (int w=0; w<image_width; w++)
{
int map_h = h + (scaled_height - image_height)/2;
int map_w = w + (scaled_width - image_width)/2;
//printf("h:%d, w:%d, map_h:%d, map_w:%d, scale:%f, scaled_height:%d, scaled_width:%d\n", h,w,map_h,map_w,scale, scaled_height, scaled_width);
fdata[0*image_height*image_width + h*image_width + w]
= ((float)scale_data[0*scaled_width*scaled_height
+ map_h*scaled_width+map_w]/255.0f)*4.3668f-2.1179f;
fdata[1*image_height*image_width + h*image_width + w]
= ((float)scale_data[1*scaled_width*scaled_height
+ map_h*scaled_width+map_w]/255.0f)*4.4643f-2.0357f;
fdata[2*image_height*image_width + h*image_width + w]
= ((float)scale_data[2*scaled_width*scaled_height
+ map_h*scaled_width+map_w]/255.0f)*4.4444f-1.8044f;
}
}
free(chw_data);
free(hwc_data);
free(scale_data);
}
void read_ImageNet_normalized(const char* descfilename, float* images, unsigned* labels, int batch)
{
const int image_height = 224;
const int image_width = 224;
const int image_channel = 3;
if (images == NULL)
{
fprintf(stderr, "NULL images pointer in reading ImageNet in data.cpp.\n");
exit(1);
}
if (labels == NULL)
{
fprintf(stderr, "NULL labels pointer in reading ImageNet in data.cpp.\n");
exit(1);
}
memset(images,0,batch*image_height*image_width*image_channel*sizeof(float));
memset(labels,0,batch*sizeof(unsigned));
FILE* descfile = fopen(descfilename, "r");
if(descfile == NULL)
{
fprintf(stderr, "Error reading Imagenet Description File '%s'.\n", descfilename);
exit(1);
}
#ifdef VERBOSE
printf("Reading normalized ImageNet in a batch of %u in data.cpp.\n", batch);
#endif
char filename[128];
for (int i=0; i<batch; i++)
{
unsigned tag;
fscanf(descfile, " %[^,\n],", filename);
fscanf(descfile, "%u", &tag);
labels[i] = tag;
//printf("Load file: %s with tag: %d\n",filename, tag);
read_normalized_jpeg(filename, &images[i*image_height*image_width*image_channel], image_height, image_width);
}
fclose(descfile);
}