Git Product home page Git Product logo

mnist_for_c's Introduction

MNIST for C

This repository contains a header file for loading MNIST dataset in C language.

preparation

  1. 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/

  2. download mnist.h

  3. 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")
  4. add #include "mnist.h" in your C code

usage

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)

functions

  • 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 or test_image
    num_data: NUM_TRAIN or NUM_TEST

  • void print_mnist_label ( double data_label[][], int num_data )
    print label of all mnist data to stdout
    data_label[][]: train_image or test_image
    num_data: NUM_TRAIN or NUM_TEST

  • void save_mnist_pgm ( double data_image[][SIZE], int index )
    save mnist image as pgm file
    data_image[][]: train_image or test_image
    index: index of data to save (0~59999 for train image, 0~9999 for test image)

example

#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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 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

	printf("label: %d\n", test_label[0]);
	// label: 7

	return 0;
}

to see an actual runnig code, compile and run example.c

mnist_for_c's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

mnist_for_c's Issues

System return void image from index 6

Hello,

Sorry, but I have a problem using your files.
I can run the example but when index exceeds 6 on test_image arrays, I got all data at 0.
So I get all images from index 6 white.

Thanks in advance for the help.

Loads only first image

In training dataset, the program only reads the first image, the rest of array is blank.
In testing dataset reads 5 images, rest is also blank.
Both label files work fine (load all the values).
Caused me a lot of headaches.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.