Convert cifar-10 binary DataSet to picture format in Python "Rookie tutorial" __python

Source: Internet
Author: User
Tags keras
Search all over the internet there is no direct use of Python to convert cifar-10 into picture format, so special to open a tutorial for everyone to Exchange learning. I am committed to creating a rookie tutorial. 0. The picture format that has been turned good for everyone to download.
[Baidu Disk] (https://pan.baidu.com/s/1skN4jW5)
Password Z6i3

From top to bottom for the picture of 0-9 classes, a total of 10 categories, including training set 5W picture, each class 5 K, validation set 1W picture, each Class 1 K Zhang. Total 6W picture.

1. Prepare tool CIFAR10 data set, need to download and extract it into the same folder as the transformation script. Keras official download path for Keras's cifar-10 official download path python dependency library: NumPy, and so on. (If the error occurred during the run, please install the dependent library according to the wrong prompts) 2. Code

#encoding: Utf-8 #author: Guohuifeng #email: 309884616@qq.com from Scipy.misc import imsave import numpy as NP # Decompress, return the extracted word Code def unpickle (file): import cpickle fo = open (file, ' RB ') Dict = Cpickle.load (FO) fo.close () return D
ICT # Generate training set pictures, if PNG format is required, just change the name of the image suffix. For j in range (1, 6): Dataname = "Data_batch_" + str (j) # reads the data_batch12345 file in the current directory, Dataname is also the path to the Data_batch file, this article and the script
    The file is in the same directory. XTR = Unpickle (dataname) print Dataname + "is loading ..." For I in range (0, 10000): img = Np.reshape (Xtr [' Data '] [i], (3, a)) # xtr[' data ' for picture binary data img = img.transpose (1, 2, 0) # Read image picname = ' train/' + str (
        xtr[' labels '][i]) + ' _ ' + str (i + (j-1) *10000) + '. jpg ' # xtr[' labels '] for the label of the picture, value range 0-9, in this article, the train folder needs to exist and is in the same directory as the script file.

Imsave (Picname, img) print Dataname + "loaded." Print "Test_batch is loading ..." # Generate test Set Picture testxtr = Unpickle ("Test_batch") for I in range (0, 10000): IMG = Np.reshap E (testxtr[' data '][i], (3, 32) img = Img.transpose (1, 2, 0) picname = ' test/' + str (testxtr[' labels '][i]) + ' _ ' + str (i) + '. jpg ' IMS Ave (Picname, img) print "Test_batch loaded."
3. Inspection
According to this article's picture naming way, uses "the label _ the current batch picture ordinal number" the way, may solve the picture name repetition to bring the question. The following test is done according to the big class display in the No. 0 chapter of the previous article:
Class No. 0, airplane, right

Class 6th, Frog, right

4. Prospects for future work

cifar-100 picture conversion is similar, please convert it if you are interested. 5.GitHub Address

Of course, you must give a git address.
Remember to give me star when it's easy to use.

Related Article

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.