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

前端 未结 9 843
独厮守ぢ
独厮守ぢ 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:56

    Make sure you don't normalize your dataset when you want to display the image.

    Example :

    The loader...

    import torch
    from torchvision import datasets, transforms
    import matplotlib.pyplot as plt
    
    
    train_loader = torch.utils.data.DataLoader(
        datasets.CIFAR10('../data', train=True, download=True,
                         transform=transforms.Compose([
                             transforms.RandomHorizontalFlip(),
                             transforms.ToTensor(),
                            #  transforms.Normalize(
                            #      (0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))
                         ])),
        batch_size=64, shuffle=True)
    

    The code that shows the image...

    img = next(iter(train_loader))[0][0]
    plt.imshow(transforms.ToPILImage()(img))
    

    Normalized

    Normalized

    Wihtout normalization

    Not normalized

提交回复
热议问题