This repository contains a header file for loading MNIST dataset in C language.
-
download the following data files
- train image :
train-images.idx3-ubyte
- train label :
train-labels.idx1-ubyte
- test image :
t10k-images.idx3-ubyte
- test label :
t10k-labels.idx1-ubyte
you can find these files inside
/data
directory in this repository
they are also available at http://yann.lecun.com/exdb/mnist/ - train image :
-
download
mnist.h
-
in
mnist.h
, set the appropriate data path to the following macros#define TRAIN_IMAGE (ex: "./data/train-images.idx3-ubyte")
#define TRAIN_LABEL (ex: "./data/train-labels.idx1-ubyte")
#define TEST_IMAGE (ex: "./data/t10k-images.idx3-ubyte")
#define TEST_LABEL (ex: "./data/t10k-labels.idx1-ubyte")
-
add
#include "mnist.h"
in your C code
after calling the following void function,
load_mnist();
you will be able to access the corresponding data through the following array:
- train image : train_image[60000][784] (type: double, normalized, flattened)
- train label : train_label[60000] (type: int)
- test image : test_image[10000][784] (type: double, normalized, flattened)
- test label : test_label[10000] (type: int)
-
void load_mnist ( void )
load mnist data to respective array (as mentioned above) -
void print_mnist_pixel ( double data_image[][], int num_data )
print pixel values of all mnist images to stdout
data_image[][]:train_image
ortest_image
num_data:NUM_TRAIN
orNUM_TEST
-
void print_mnist_label ( double data_label[][], int num_data )
print label of all mnist data to stdout
data_label[][]:train_image
ortest_image
num_data:NUM_TRAIN
orNUM_TEST
-
void save_mnist_pgm ( double data_image[][SIZE], int index )
save mnist image as pgm file
data_image[][]:train_image
ortest_image
index: index of data to save (0~59999
for train image,0~9999
for test image)
#include "mnist.h"
int main(void)
{
load_mnist();
int i;
for (i=0; i<784; i++) {
printf("%1.1f ", test_image[0][i]);
if ((i+1) % 28 == 0) putchar('\n');
}
// 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
// 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 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