TensorFlow learning – CIFAR-10 (python for data visualization)

The data downloaded by CIFAR-10 are all [binary] files

(1) Introduction to the CIFAR-10 dataset

The dataset is divided into 5 training blocks and 1 testing block, each with 10,000 images. The test block contains 1000 images randomly selected from each class. The training blocks contain these images in random order, but some training blocks may contain more images than other classes. The training block contains 5000 images per class. 
②data – a 100003072 size uint8s array. Each row of the array stores a 3232 image. The 1st 1024 contains the red channel value, the next 1 contains the green, and the last 1024 contains the blue. The image is stored in row order, so the first 32 columns of the array are the red channel values ​​for row 1 of the image. 
labels – a list of 10000 numbers in the range 0-9. The value of index i represents the label of the ith image in the array data. 
label_names – a 10-element list giving meaningful names to the numeric labels in labels. For example, label_names[0] == “airplane”, label_names[1] == “automobile”, etc.

First give a cifar data download link: http://www.cs.toronto.edu/~kriz/cifar.html 
The link mentions three data versions, namely python, matlab, binary version, suitable for python, matlab, The C program 
uses python to convert cifar data into images. After
downloading the Python version, you can use the following code

(3) Code

# -*- coding:utf-8 -*-
import pickle as p
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as plimg
from PIL import Image
def load_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, 32)
        Y = np.array(Y)
        return X, Y

def load_CIFAR_Labels(filename):
    with open(filename, 'rb') as f:
        lines = [x for x in 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.shape
     print  "Saving image:" 
    for i in xrange(imgX.shape[ 0 ]):
        imgs = imgX[i - 1 ]
         if i < 100 : #Only loop 100 pictures, commenting out this sentence can facilitate all pictures, there are many pictures, it 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" ) #The folder is the RGB fused image 
            for j in xrange(imgs.shape[ 0 ]):
                img = imgs[j - 1]
                name = "img" + str(i) + str(j) + ".png" 
                print  "Saving image" + name
                plimg.imsave( "/data/image/" + name, img) #The folder is the RGB separated image

    print  "Save completed."

Leave a Comment

Your email address will not be published. Required fields are marked *