Python enables visualization of cifar10 datasets

Source: Internet
Author: User

In learning TensorFlow mnist and Cifar instances, the official document gives the explanation is a picture, intuitive and clear, when we see the program downloaded data, the baby is stunned, are binary files, these binary files are not small, with a text editor open to see also do not understand , if the data is reproduced as an image, how good!

(1) CIFAR-10 Data Set Introduction

The ①cifar-10 dataset contains 60,000 32*32 color images, a total of 10 categories. There are 50,000 training images and 10,000 test images.
The dataset is divided into 5 training blocks and a test block with 10,000 images per block. The test block contains 1000 images randomly selected from each class. The training blocks contain these images in a random order, but some training blocks may contain more images than other classes. Training blocks Each class contains 5,000 images.
②data--1 a uint8s array of 10000*3072 size. Each row of the array stores 1 32*32 images. The 1th 1024 contains the red channel value, the next 1 contains the green, and the last 1024 contains the blue. Image storage is dominated by row order, so the first 32 of the array is listed as the red channel value on line 1th of the image.
Labels--1 a list of 10000 numbers in the range of 0~9. The value of index I represents the label of the I image in the array data.
The ③ data set contains 1 additional files called Batches.meta. It also contains 1 Python Dictionary objects. Like the following elements:
Label_names--1 a list of 10 elements, giving the value label in labels a meaningful name. For example, label_names[0] = = "Airplane", label_names[1] = = "Automobile" and so on.

(2) Download python version of CIFAR data

Give a Cifar data download link: http://www.cs.toronto.edu/~kriz/cifar.html
The link mentions three versions of the data, respectively, the python,matlab,binary version, respectively, suitable for the PYTHON,MATLAB,C program
We use Python to transform cifar data into images, and of course we use the Python version.
Download OK, we can use the following code

(3) Code

#-*-Coding:utf-8-*-Import PickleAs PImport NumPyAs NPImport Matplotlib.pyplotAs PltImport Matplotlib.imageAs PlimgFrom PILImport ImageDefLoad_cifar_batch(filename):"" "Load single batch of Cifar" "with open (filename,' RB ')As F:datadict = P.load (f) X = datadict[' Data '] Y = datadict[' Labels '] X = X.reshape (10000,3,32,y = Np.array (y)Return X, YDefLoad_cifar_labels(filename):with open (filename,' RB ')As F:lines = [xFor XIn F.readlines ()] Print (lines)if __name__ = ="__main__": Load_cifar_labels ("/data/cifar-10-batches-py/batches.meta") imgx, imgy = Load_cifar_batch ("/data/cifar-10-batches-py/data_batch_1")Print Imgx.shapePrint"Saving Picture:"For IIn Xrange (imgx.shape[0]): IMGs = imgx[i-1]If I <100:#只循环100张图片, this comment off can facilitate all the pictures, pictures more, may take a certain time img0 = imgs[0] Img1 = imgs[1] Img2 = imgs[2] I0 = Image.fromarray (img0) I1 = Image.fromarray (img1) i2 = Image.fromarray (img2) img = Image. Merge ( "RGB", (i0,i1,i2)) name =  "img" + str (i) Img.save ( "/data/images/" +name, "PNG") # Under the folder is the RGB fused image for J in xrange (Imgs.shape[0]): img = imgs[j-1] name = " img "+ str (i) + str (j) + Span class= "hljs-string" ". png" print  "saving picture" + Name Plimg.imsave ( "/data/image/" + Name, img)  #文件夹下是RGB分离的图像 print  "save complete."         

Python implements visualization of Cifar10 datasets

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.