Mxnet Import Image data

Source: Internet
Author: User
Tags shuffle mxnet

Mxnet framework is used to do image-related projects, There are two main ways of reading images: the first is read. rec format files, similar to the Caffe framework of Lmdb, the advantage is that. rec files are stable and can be reproduced on other computers, with the disadvantage that space (. rec file size is basically the size of the image storage), and the addition and deletion of data is not flexible. The second is. LST and the way images are combined, first generated in front. The. lst file is also generated during the rec file, and this. lst file is the corresponding list of image paths and tags, which means that by maintaining the list to control the changes in your training set and test set, the advantage is that it is flexible and does not occupy space, but the disadvantage is that if the image format It's easy to make mistakes and if the images in the image folder for some of the image paths in the list are deleted, you can't find them, and if you're not reading images from a solid-state drive, it's going to be slow.

Mxnet image data Import module mainly has Mxnet.io.ImageRecordIter and mxnet.image.ImageIter two classes, the former is mainly used to read. Rec format data, the latter can read. rec format files, but also can read the original image data. Script image.py can be found in ~/mxnet/python/mxnet/image.py. 1. Rec

There are two main steps: Build. LST and build. Rec 1.1, build. LST

It's your image that needs to be prepared. Suppose your image data is placed in the/home/image folder, a total of 10 categories, then there should be 10 subfolders under the/home/image folder, each subfolder belongs to this class image file, you can use English name these subfolders to express categories, this does not matter, Even if you use the 10 numbers from 1 to 10 to name the 10 subfolders, it's OK to use the English name to remember what category the images in this folder belong to. Alternatively, if you want to place the generated. lst file in the/home/lst folder, the path to your mxnet project is ~/incubator-mxnet, then run the following command to generate the. lst file:
Python ~/incubator-mxnet/tools/im2rec.py--list true--recursive true--train-ratio 0.9/home/lst/data/home/image
Or
Os.system (' python%s/tools/im2rec.py--list=1--recursive=1--shuffle=1--test-ratio=0.2 Data/caltech data/101_ Objectcategories '%os.environ[' mxnet_home '])
The –list parameter must be true to indicate that you are generating the. lst file, the –recursive argument must be true to write all the image paths into a. lst file, and the –train-ratio parameter indicates how much of the train and Val are divided by the default of 1, The representation is train data.

This will generate Data_train.lst and data_val.lst two files under the/home/lst folder.
. lst File Sample: The first column is index, the second column is label, and the third column is the image path

Of course, sometimes your data images are not placed in a folder in a single category, so consider modifying the script to generate the. lst file in the same format for subsequent generation. rec files. 1.2, generate. Rec

What you need to prepare is the first step to generate the. lst file and your image.
If you want to place the generated. rec file under the same/home/lst folder as the. lst file (which is typically the case), then run the following command to generate the. rec File:
Python ~/incubator-mxnet/tools/im2rec.py--num-thread 4/home/lst/home/image
Or
Os.system ("Python%s/tools/im2rec.py--num-thread=4--pass-through=1 data/caltech data/101_objectcategories"%o s.environ[' Mxnet_home '])
Here's the penultimate argument:/home/lst is the path to your. lst file, and you don't have to specify the. lst file name, because the code automatically searches for all files under/home/lst folder that end with. lst. Last parameter:/home/image is the path of your image. –num-thread 4 This parameter is for 4 threads to execute, when you have a large amount of data, the process of generating. Rec will be slower, so it can be accelerated.

After a successful run, the Data_train.rec and Data_val.rec files are generated under the/home/rec folder, and then the Mxnet.io.ImageRecordIter class can be used to import the. rec file.
A further two. idx files are generated, which can be ignored.

Mx.io.ImageRecordIter (Path_imgrec = args.data_train, Path_imgidx = Args.data_train_idx,
        Label_width = 1, Mean_r = rgb_mean[0], Mean_g = rgb_mean[1], Mean_b = rgb_mean[2], data_name = ' data ', Label_name = ' Softmax_label ' , Data_shape = image_shape, batch_size = args.batch_size, Rand_crop =
        Args.random_crop, Max_random_scale = args.max_random_scale, pad = args.pad_size, Fill_value = 127, Min_random_scale = Args.min_random_scale, Max_aspect_ratio = Args.max_r
        Andom_aspect_ratio, Random_h = args.max_random_h, random_s = args.max_random_s, random_l = args.max_random_l, Max_rotate_angle = Args.max_random_rotate_angle, max_shear _ratio = Args.max_raNdom_shear_ratio, Rand_mirror = args.random_mirror, preprocess_threads = Args.data_nthreads, Shuffle = True, Num_parts = nworker, Part_index = rank)
2. LST + image

here. lst file generation is the same as the previous 1.1 parts. Then use the Mxnet.image.ImageIter class to import. LST and image data.

Mxnet.image.ImageIter is a very important class. In Mxnet, when you want to read the image data, You can use im2rec.py to generate LST and rec files, and then use the Mxnet.io.ImageRecordIter class to read the rec file or use the Mxnet.image.ImageIter class to read the rec file, but this function can also read the image file directly compared to the former, so You can not generate the memory of the rec file, only the original image files and LST files can be. In addition, in Mxnet.io.ImageRecordIter for data preprocessing operations are fixed, not easy to modify, but mxnet.image.ImageIter can be very flexible to add a variety of preprocessing operations. Next look at this class. Mxnet.image.ImageIter

An example of the use of an Imageiter class in official documents:

Data_iter = Mx.image.ImageIter (batch_size=4, data_shape= (3, MB, MB),
                              path_imgrec= "./data/caltech.rec",
                              path_imgidx= "./data/caltech.idx")

# Data_iter type is the Mxnet.image.ImageIter
#reset () function is: resents the Iterator
to the beginning of the data Data_iter.reset ()

#batch的类型是mxnet. Io.databatch because Next () The return value of the method is the Databatch
batch = Data_iter.next ()

#data是一个NDArray, which represents the data in the first batch, because the Batch_size size here is 4, So the size of data is 4*3*227*227
data = batch.data[0]

#这个for循环就是读取这个batch中的每张图像并显示 for I in
Range (4):
    Plt.subplot (1,4,i+1)
    plt.imshow (Data[i].asnumpy (). Astype (Np.uint8). Transpose ((1,2,0))
plt.show ()

The flexibility to add preprocessing in Mxnet.image.ImageIter can be done through the Mxnet.image.CreateAugmenter () function, which is fully defined by this function:
Mxnet.image.CreateAugmenter (Data_shape, resize=0, Rand_crop=false, Rand_resize=false, Rand_mirror=false, Mean=none, Std=none, Brightness=0, contrast=0, saturation=0, pca_noise=0, inter_method=2)

The specific contents of this function can be read as follows: ~/mxnet/python/mxnet/image.py
modifies the data.py to alter the data-reading entry. The path is: ~/mxnet/example/image-classification/common/data.py.
Find the Get_rec_iter () function in the data.py script, and you can see this part of the Get_rec.iter ():

Train = Mx.io.ImageRecordIter (Path_imgrec = args.data_train, label_width = 1, mea
        N_r = Rgb_mean[0], Mean_g = rgb_mean[1], Mean_b = rgb_mean[2], Data_name = ' data ', Label_name = ' Softmax_label ', Data_shape = Image_shape    , batch_size = args.batch_size, Rand_crop = Args.random_crop, Max_random_scale = Args.max_random_scale, pad = args.pad_size, Fill_value = 127, Min_ra            Ndom_scale = Args.min_random_scale, Max_aspect_ratio = Args.max_random_aspect_ratio, Random_h = Args.max_random_h, random_s = args.max_random_s, random_l = args.max_random_l
        , Max_rotate_angle = args.max_random_rotate_angle, Max_shear_ratio = Args.max_random_shear_ratio,      Rand_mirror   = Args.random_mirror, preprocess_threads = args.data_nthreads, shuffle = True, num _parts = nworker, Part_index = rank)

This code is the process of reading data from the Rec file. Instead of Mx.io.ImageRecordIter (), we instead use Mx.image.ImageIter () and modify it as follows:

Train = Mx.image.ImageIter (
        batch_size            = args.batch_size,
        data_shape          = (3,224,224),
        label_ Width           = 1,
        path_imglist          = args.data_train,
        path_root              = args.image_train,
        part_index            = rank,
        shuffle                  = True,
        data_name           = ' data ',
        label_name           = ' Softmax_label ',
        aug_list                 = Mx.image.CreateAugmenter (3,224,224), resize=224,rand_crop=true,rand_mirror=true,mean=true ))

Here the Path_imglist parameter and the Path_root parameter are unique to this class, Represents the path to. lst files and images, and this. lst file is what you are generating. rec file, so this class just doesn't need the. rec file, but the. lst file is still needed, just a list file, saving a lot of storage space and making it easier to change the data later, as long as it is regenerated. LST , without having to take the time to generate space-occupying. rec files. In addition, because there is no args.image_train in the original data.py script, you need to add it yourself and add it in the same way as other args, so you can import this parameter.

The Val section can be modified like this, and the most important thing here is the last parameter aug_list, which represents all the preprocessed lists, but there's generally no such thing as Crop,mirror in Val. Why would you use the Aug_list parameter? These lines of code from the Init () function of the Imageiter class in the image.py script:

If Aug_list is None:
     self.auglist = Createaugmenter (Data_shape, **kwargs)
else:
     self.auglist = aug_list

is if aug_list This parameter is not assigned (the default is None), the image is not preprocessed; If this parameter has a value, then the Createaugmenter () function is invoked to generate the preprocessing list.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.