This topic introduces the data input method for MXNet. MXNet uses an iterator to provide data to the neural network. Iterators do some preprocessing and generate batches for the neural network.
MXNet provides basic iterators for MNIST and Recordio images. To hide the cost of I/O, MXNet uses a prefetch strategy that enables parallelism for the learning process and data fetching. Data is automatically fetched by an independent thread.
Topics:
To create a data iterator, you typically need to provide five parameters:
You must provide the Dataset Param and Batch Param, otherwise MXNet can't create the data batch. Provide other parameters as required by your algorithm and performance needs. We provide a detailed explanation and examples of the options later.
The IO API provides a simple way to create a data iterator in Scala. The following example code shows how to create a CIFAR data iterator.
scala> val dataiter = IO.ImageRecordIter(Map( scala> // Utility Parameter scala> // Optional scala> // Name of the data, should match the name of the data input of the network scala> // data_name='data', scala> // Utility Parameter scala> // Optional scala> // Name of the label, should match the name of the label parameter of the network. scala> // Usually, if the loss layer is named 'foo', then the label input has the name scala> // 'foo_label', unless overwritten scala> // label_name='softmax_label', scala> // Dataset Parameter scala> // Impulsary scala> // indicating the data file, please check the data is already there scala> "path_imgrec" -> "data/cifar/train.rec", scala> // Dataset Parameter scala> // Impulsary scala> // indicating the image size after preprocessing scala> "data_shape" -> "(3,28,28)", scala> // Batch Parameter scala> // Impulsary scala> // tells how many images in a batch scala> "batch_size" -> "100", scala> // Augmentation Parameter scala> // Optional scala> // when offers mean_img, each image will subtract the mean value at each pixel scala> "mean_img" -> "data/cifar/cifar10_mean.bin", scala> // Augmentation Parameter scala> // Optional scala> // randomly crop a patch of the data_shape from the original image scala> "rand_crop" -> "True", scala> // Augmentation Parameter scala> // Optional scala> // randomly mirror the image horizontally scala> "rand_mirror" -> "True", scala> // Augmentation Parameter scala> // Optional scala> // randomly shuffle the data scala> "shuffle" -> "False", scala> // Backend Parameter scala> // Optional scala> // Preprocessing thread number scala> "preprocess_threads" -> "4", scala> // Backend Parameter scala> // Optional scala> // Prefetch buffer size scala> "prefetch_buffer" = "1"))
First, explicitly specify the kind of data (MNIST, ImageRecord, etc.) to fetch. Then, provide the options for the dataset, batching, image augmentation, multi-tread processing, and prefetching operations. The code automatically validates the parameters. If a required parameter is missing, MXNet returns an error.
We provide a scripts to download MNIST data and CIFAR10 ImageRecord data. If you want to create your own dataset, we recommend using the Image Recordio data format.
Recordio implements a file format for a sequence of records. We recommend storing images as records and packing them together. The benefits include:
We provide the im2rec tool so you can create an Image Recordio dataset by yourself. The following walkthrough shows you how.
Download the data. You don't need to resize the images manually. You can use im2rec to resize them automatically. For details, see the “Extension” topic.
After you download the data, you need to make an image list file. The format is:
integer_image_index \t label_index \t path_to_image
Typically, the program takes the list of names of all of the images, shuffles them, then separates them into two lists: a training files name list and a testing file name list. Write the list in the right format.
This is example file:
95099 464 n04467665_17283.JPEG 10025081 412 ILSVRC2010_val_00025082.JPEG 74181 789 n01915811_2739.JPEG 10035553 859 ILSVRC2010_val_00035554.JPEG 10048727 929 ILSVRC2010_val_00048728.JPEG 94028 924 n01980166_4956.JPEG 1080682 650 n11807979_571.JPEG 972457 633 n07723039_1627.JPEG 7534 11 n01630670_4486.JPEG 1191261 249 n12407079_5106.JPEG
To generate a binary image, use im2rec in the tool folder. im2rec takes the path of the image list file you generated, the root path of the images, and the output file path as input. This process usually takes several hours, so be patient.
A sample command:
./bin/im2rec image.lst image_root_dir output.bin resize=256
For more details, run ./bin/im2rec.
The im2rec tool and IO.ImageRecordIter have multi-label support for a single image. For example, if you have four labels for a single image, you can use the following procedure to use the Recordio tools.
Write the the image list files as follows:
integer_image_index \t label_1 \t label_2 \t label_3 \t label_4 \t path_to_image
Run im2rec, adding a ‘label_width=4’ to the command argument, for example:
./bin/im2rec image.lst image_root_dir output.bin resize=256 label_width=4
In the iterator generation code, set label_width=4 and path_imglist=<<The PATH TO YOUR image.lst>>, for example:
val dataiter = IO.ImageRecordIter(Map( "path_imgrec" -> "data/cifar/train.rec", "data_shape" -> "(3,28,28)", "path_imglist" -> "data/cifar/image.lst", "label_width" -> "4" ))