Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 more clearly visible. Thanks.

This is the image I got

like image 696
Siddharth Avatar asked Mar 14 '16 19:03

Siddharth


People also ask

What is the pixel size of CIFAR-10 image dataset?

CIFAR-10 Photo Classification Dataset The dataset is comprised of 60,000 32×32 pixel color photographs of objects from 10 classes, such as frogs, birds, cats, ships, etc. The class labels and their standard associated integer values are listed below.

Is CIFAR-10 a subset of Imagenet?

CIFAR-10 is a labeled subset of the 80 million tiny images dataset.


1 Answers

This file reads the cifar10 dataset and plots individual images using matplotlib.

import _pickle as pickle
import argparse
import numpy as np
import os
import matplotlib.pyplot as plt

cifar10 = "./cifar-10-batches-py/"

parser = argparse.ArgumentParser("Plot training images in cifar10 dataset")
parser.add_argument("-i", "--image", type=int, default=0, 
                    help="Index of the image in cifar10. In range [0, 49999]")
args = parser.parse_args()


def unpickle(file):
    with open(file, 'rb') as fo:
        data = pickle.load(fo, encoding='bytes')
    return data

def cifar10_plot(data, meta, im_idx=0):
    im = data[b'data'][im_idx, :]
    
    im_r = im[0:1024].reshape(32, 32)
    im_g = im[1024:2048].reshape(32, 32)
    im_b = im[2048:].reshape(32, 32)

    img = np.dstack((im_r, im_g, im_b))

    print("shape: ", img.shape)
    print("label: ", data[b'labels'][im_idx])
    print("category:", meta[b'label_names'][data[b'labels'][im_idx]])         
    
    plt.imshow(img) 
    plt.show()


def main():
    batch = (args.image // 10000) + 1
    idx = args.image - (batch-1)*10000

    data = unpickle(os.path.join(cifar10, "data_batch_" + str(batch)))
    meta = unpickle(os.path.join(cifar10, "batches.meta"))

    cifar10_plot(data, meta, im_idx=idx)

    
if __name__ == "__main__":
    main()
like image 190
Sounak Avatar answered Oct 15 '22 22:10

Sounak