Why CIFAR-10 images are not displayed properly using matplotlib?

前端 未结 9 854
独厮守ぢ
独厮守ぢ 2021-02-06 12:28

From the training set I took a image(\'img\') of size (3,32,32). I have used plt.imshow(img.T). The image is not clear. Now changes I have to make to image(\'img\') to make it m

9条回答
  •  执念已碎
    2021-02-06 12:59

    I made a function to plot the RGB image from a row in the CIFAR10 dataset.The image will be blurry at best since the original size of the image is very small (32px X 32px).

    sample image

    def unpickle(file):
        with open(file, 'rb') as fo:
            dict1 = pickle.load(fo, encoding='bytes')
        return dict1
    
    pd_tr = pd.DataFrame()
    tr_y = pd.DataFrame()
    
    for i in range(1,6):
        data = unpickle('data/data_batch_' + str(i))
        pd_tr = pd_tr.append(pd.DataFrame(data[b'data']))
        tr_y = tr_y.append(pd.DataFrame(data[b'labels']))
        pd_tr['labels'] = tr_y
    
    tr_x = np.asarray(pd_tr.iloc[:, :3072])
    tr_y = np.asarray(pd_tr['labels'])
    ts_x = np.asarray(unpickle('data/test_batch')[b'data'])
    ts_y = np.asarray(unpickle('data/test_batch')[b'labels'])    
    labels = unpickle('data/batches.meta')[b'label_names']
    
    def plot_CIFAR(ind):
        arr = tr_x[ind]
        sc_dpi = 157.35
        R = arr[0:1024].reshape(32,32)/255.0
        G = arr[1024:2048].reshape(32,32)/255.0
        B = arr[2048:].reshape(32,32)/255.0
    
        img = np.dstack((R,G,B))
        title = re.sub('[!@#$b]', '', str(labels[tr_y[ind]]))
        fig = plt.figure(figsize=(3,3))
        ax = fig.add_subplot(111)
        ax.imshow(img,interpolation='bicubic')
        ax.set_title('Category = '+ title,fontsize =15)
    
    plot_CIFAR(4)
    

提交回复
热议问题