MNIST-1LNN  1.0
A simple 1-layer neural network to recognize handwritten single digit numbers from the MNIST image files.
mnist-utils.c
Go to the documentation of this file.
1 /**
2  * @file mnist-utils.c
3  * @brief Utitlies for handling the MNIST files
4  * @see http://yann.lecun.com/exdb/mnist/
5  * @author Matt Lind
6  * @date July 2015
7  */
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 
12 #include "mnist-utils.h"
13 
14 
15 
16 
17 /**
18  * @details Reverse byte order in 32bit numbers
19  * MNIST files contain all numbers in reversed byte order,
20  * and hence must be reversed before using
21  */
22 
23 uint32_t flipBytes(uint32_t n){
24 
25  uint32_t b0,b1,b2,b3;
26 
27  b0 = (n & 0x000000ff) << 24u;
28  b1 = (n & 0x0000ff00) << 8u;
29  b2 = (n & 0x00ff0000) >> 8u;
30  b3 = (n & 0xff000000) >> 24u;
31 
32  return (b0 | b1 | b2 | b3);
33 
34 }
35 
36 
37 
38 
39 /**
40  * @details Read MNIST image file header
41  * @see http://yann.lecun.com/exdb/mnist/ for definition details
42  */
43 
44 void readImageFileHeader(FILE *imageFile, MNIST_ImageFileHeader *ifh){
45 
46  ifh->magicNumber =0;
47  ifh->maxImages =0;
48  ifh->imgWidth =0;
49  ifh->imgHeight =0;
50 
51  fread(&ifh->magicNumber, 4, 1, imageFile);
52  ifh->magicNumber = flipBytes(ifh->magicNumber);
53 
54  fread(&ifh->maxImages, 4, 1, imageFile);
55  ifh->maxImages = flipBytes(ifh->maxImages);
56 
57  fread(&ifh->imgWidth, 4, 1, imageFile);
58  ifh->imgWidth = flipBytes(ifh->imgWidth);
59 
60  fread(&ifh->imgHeight, 4, 1, imageFile);
61  ifh->imgHeight = flipBytes(ifh->imgHeight);
62 }
63 
64 
65 
66 
67 /**
68  * @details Read MNIST label file header
69  * @see http://yann.lecun.com/exdb/mnist/ for definition details
70  */
71 
72 void readLabelFileHeader(FILE *imageFile, MNIST_LabelFileHeader *lfh){
73 
74  lfh->magicNumber =0;
75  lfh->maxImages =0;
76 
77  fread(&lfh->magicNumber, 4, 1, imageFile);
78  lfh->magicNumber = flipBytes(lfh->magicNumber);
79 
80  fread(&lfh->maxImages, 4, 1, imageFile);
81  lfh->maxImages = flipBytes(lfh->maxImages);
82 
83 }
84 
85 
86 
87 
88 /**
89  * @details Open MNIST image file and read header info
90  * by reading the header info, the read pointer
91  * is moved to the position of the 1st IMAGE
92  */
93 
94 FILE *openMNISTImageFile(char *fileName){
95 
96  FILE *imageFile;
97  imageFile = fopen (fileName, "rb");
98  if (imageFile == NULL) {
99  printf("Abort! Could not fine MNIST IMAGE file: %s\n",fileName);
100  exit(0);
101  }
102 
103  MNIST_ImageFileHeader imageFileHeader;
104  readImageFileHeader(imageFile, &imageFileHeader);
105 
106  return imageFile;
107 }
108 
109 
110 
111 
112 /**
113  * @details Open MNIST label file and read header info
114  * by reading the header info, the read pointer
115  * is moved to the position of the 1st LABEL
116  */
117 
118 FILE *openMNISTLabelFile(char *fileName){
119 
120  FILE *labelFile;
121  labelFile = fopen (fileName, "rb");
122  if (labelFile == NULL) {
123  printf("Abort! Could not find MNIST LABEL file: %s\n",fileName);
124  exit(0);
125  }
126 
127  MNIST_LabelFileHeader labelFileHeader;
128  readLabelFileHeader(labelFile, &labelFileHeader);
129 
130  return labelFile;
131 }
132 
133 
134 
135 
136 /**
137  * @details Returns the next image in the given MNIST image file
138  */
139 
140 MNIST_Image getImage(FILE *imageFile){
141 
142  MNIST_Image img;
143  size_t result;
144  result = fread(&img, sizeof(img), 1, imageFile);
145  if (result!=1) {
146  printf("\nError when reading IMAGE file! Abort!\n");
147  exit(1);
148  }
149 
150  return img;
151 }
152 
153 
154 
155 
156 /**
157  * @details Returns the next label in the given MNIST label file
158  */
159 
160 MNIST_Label getLabel(FILE *labelFile){
161 
162  MNIST_Label lbl;
163  size_t result;
164  result = fread(&lbl, sizeof(lbl), 1, labelFile);
165  if (result!=1) {
166  printf("\nError when reading LABEL file! Abort!\n");
167  exit(1);
168  }
169 
170  return lbl;
171 }
172 
173 
FILE * openMNISTImageFile(char *fileName)
Read MNIST IMAGE file header.
Definition: mnist-utils.c:94
FILE * openMNISTLabelFile(char *fileName)
Read MNIST label file header.
Definition: mnist-utils.c:118
MNIST_Label getLabel(FILE *labelFile)
Returns the next label in given MNIST label file.
Definition: mnist-utils.c:160
uint32_t flipBytes(uint32_t n)
Definition: mnist-utils.c:23
Data block defining a MNIST image file header.
Definition: mnist-utils.h:55
Data block defining a MNIST label file header.
Definition: mnist-utils.h:73
void readImageFileHeader(FILE *imageFile, MNIST_ImageFileHeader *ifh)
Definition: mnist-utils.c:44
Utitlies for handling the MNIST files.
MNIST_Image getImage(FILE *imageFile)
Returns the next image in given MNIST image file.
Definition: mnist-utils.c:140
Data block defining a MNIST image.
Definition: mnist-utils.h:40
void readLabelFileHeader(FILE *imageFile, MNIST_LabelFileHeader *lfh)
Definition: mnist-utils.c:72
uint8_t MNIST_Label
Definition: mnist-utils.h:32