python: MNIST image display

 1 import sys, os
 2 sys.path.append(os.pardir)
 3 import numpy as np
 4 from dataset.mnist import load_mnist
 5 from PIL import Image
 6 
 7 def img_show(img):
 8     pil_img = Image.fromarray(np.uint8(img))
 9     pil_img.show()
10 
11 (x_train, t_train), (x_test, t_test) = load_mnist(flatten=True,
12 normalize=False)
13 img = x_train[0]
14 label =t_train [0]
 15  Print (label) # . 5 
16  
. 17  Print (img.shape)           # (784,) 
18 is IMG = img.reshape (28, 28) # the shape of the image into the original size 
. 19  Print (IMG. Shape)           # (28, 28) 
20 is  
21 is img_show (IMG)

Mnist displayed image, the code is executed, the training image will be displayed first. sys.path.append (os.pardir) to import the parent directory, the first call load_mnist function, because you want to download MNIST data set, we need to be networked. The second and subsequent calls need only read files stored locally (pickle file) can, therefore processing time is very short.

 

load_mnist function to "(training image, the training labels), (test image, the test label)" is returned as read data MNST. You can also like

load_mnist(normalize=True, flatten=True, one_hot_label=False)

Thus, provided with three parameters. The first parameter normalize whether to set the input image is normalized to a value of 0.0 to 1.0. If this parameter is set to False , then the pixels of the input image will remain the original 0 to 255. The second parameter flatten setting whether to initiate an input image (into one-dimensional data). If this parameter is set to False , then the input image is a 1 × 28 × 28 three-dimensional array; if set to True , then the input image will be stored by an array of 784 elements thereof. The third parameter one_hot_label setting whether to save the label one-hot indicates ( one-hot representation ). one-hot representation is only the correct solution is a label, the rest of the array are all 0, as [0,0,1,0,0,0,0,0,0,0] so. When one_hot_label is False , the knowledge would like to save the correct solution 7,2 so simple label; when one_hot_label to True , the label is saved as a one-hot representation.

Python has pickle this convenient function. This feature allows you to save the program running in the object file. If you load saved pickle file, object before running the program can be restored immediately. MNIST for reading data sets  internal functions also use the pickle function (in the second and later times when read). Use pickle function, you can complete the preparatory work MNIST data efficiently. load_mnist()

 It should be noted that flatten=True when the read image is stored in the form of (one-dimensional) NumPy array of one. Thus, an image is displayed, it needs to 28 into the original shape pixels × 28 pixels. You can  specify a desired shape parameters of the method, the shape change NumPy array. Further, also need to save the converted data object into image data using PIL NumPy array, this conversion is handled by  accomplished. reshape() Image.fromarray()

 

 

= x_train IMG [ 0 ]            #x_train shape is (6000,784), i.e. 6000 728 rows of the matrix, so x_train [0] indicates the first column of data 784

= t_train label [ 0 ]        #t_train shape is (6000,), i.e., a row or a column of data 6000, so t_train [0] is the first data, where its value is 5

 

Guess you like

Origin www.cnblogs.com/taoyuxin/p/11440162.html