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

前端 未结 9 859
独厮守ぢ
独厮守ぢ 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条回答
  •  旧时难觅i
    2021-02-06 12:53

    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:
            dict = pickle.load(fo, encoding='bytes')
        return dict
    
    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()
    

提交回复
热议问题