项目作者: takafumihoriuchi

项目描述 :
MNIST dataset loader for C
高级语言: C
项目地址: git://github.com/takafumihoriuchi/MNIST_for_C.git
创建时间: 2018-03-13T04:43:56Z
项目社区:https://github.com/takafumihoriuchi/MNIST_for_C

开源协议:MIT License

下载


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

  1. #include "mnist.h"
  2. int main(void)
  3. {
  4. load_mnist();
  5. int i;
  6. for (i=0; i<784; i++) {
  7. printf("%1.1f ", test_image[0][i]);
  8. if ((i+1) % 28 == 0) putchar('\n');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 0.0 0.0 0.0 0.0 0.0 0.5 1.0 0.8 0.1 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
  37. // 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
  38. printf("label: %d\n", test_label[0]);
  39. // label: 7
  40. return 0;
  41. }

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