Data loader showing weird images when plotting

I wanted to plot images from data loader and it shows little different images.

## Specify appropriate transforms, and batch_sizes

normalizer = transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))

transformed = transforms.Compose([

    transforms.Resize((224,224)),

    transforms.ColorJitter(),

    transforms.RandomHorizontalFlip(),

    transforms.RandomAffine(30),

    transforms.ToTensor(),

    normalizer,

])

transformed_test = transforms.Compose([

    transforms.Resize((299,299)),

    transforms.ToTensor(),

    normalizer,    

])

train_data = datasets.ImageFolder('/content/dogImages/train', transform=transformed)

test_data = datasets.ImageFolder('/content/dogImages/test', transform=transformed_test)

valid_data = datasets.ImageFolder('/content/dogImages/valid', transform=transformed)

train_loader = torch.utils.data.DataLoader(train_data,batch_size=batch_size,  shuffle=True, num_workers=0)

test_loader = torch.utils.data.DataLoader(test_data, batch_size=batch_size, num_workers=0)

valid_loader = torch.utils.data.DataLoader(valid_data, batch_size=batch_size, num_workers=0, shuffle=True)

## Visualizing

dataiter = iter(test_loader)

images, labels = dataiter.next()

images = images.numpy() 

#plot

fig = plt.figure(figsize=(25,4))

for idx in np.arange(10):

  ax = fig.add_subplot(2, 10/2, idx+1, xticks=[], yticks=[])

  plt.imshow(np.transpose(images[idx], (1,2,0)).astype('uint8'))

  ax.set_title(train_data.classes[idx])

when plotting when i dont use .astype('uint8') it show some message like this

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

showing image like this

I hadnt used any color jitters to test images. Even multiplying by 255 didnt worked. Help!!

Color changes could be due to normalization. Could you try plotting them after you turn off normalization ?

Is there any way to turn off normalization?
I tried to multiply by 255, then plotted showing same results

Remove normalizer from test transforms.
This might reduce your test accuracy though. Ideally, you’d want to unnormalize images at the time you plot them.

1 Like

Help me, please
I try this code with many directory data set and work fine in visualization
but with this data directory, it produced a weird visualization

import os

import torch

from torchvision import datasets, transforms

from torch.utils.data import Dataset, DataLoader

import torch

import torchvision

import torchvision.transforms as transforms

import matplotlib.pyplot as plt

import cv2

import numpy as np

Write data loaders for training, validation, and test sets

Specify appropriate transforms, and batch_sizes

from PIL import ImageFile

ImageFile.LOAD_TRUNCATED_IMAGES = True

number of subprocesses to use for data loading

num_workers = 0

how many samples per batch to load

batch_size = 16

data_transform_train = transforms.Compose([

transforms.Resize(256),

transforms.CenterCrop(224),

transforms.RandomHorizontalFlip(),

transforms.ToTensor(),

# transforms.Normalize((.5), (.5))

])

data_transform_test = transforms.Compose([

transforms.Resize(256),

transforms.CenterCrop(224),

transforms.ToTensor(),

# transforms.Normalize((0.5), (0.5))

])

data_dir = ‘/content/gdrive/MyDrive/graduation_project_dataset/Mammogram’

train_dir = os.path.join(data_dir, ‘train’)

valid_dir = os.path.join(data_dir, ‘valid’)

test_dir = os.path.join(data_dir, ‘test’)

train_data = datasets.ImageFolder(train_dir, transform=data_transform_train)

valid_data = datasets.ImageFolder(valid_dir, transform=data_transform_test)

test_data = datasets.ImageFolder(test_dir, transform=data_transform_test)

train_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, num_workers=num_workers, shuffle=True)

valid_loader = torch.utils.data.DataLoader(valid_data, batch_size=batch_size, num_workers=num_workers, shuffle=True)

test_loader = torch.utils.data.DataLoader(test_data, batch_size=batch_size, num_workers=num_workers, shuffle=True)

loaders_scratch = {

'train' : train_loader,

'valid' : valid_loader,

'test'  : test_loader

}

dataiter = iter(train_loader)

images, labels = dataiter.next()

images = images.numpy()

#plot

fig = plt.figure(figsize=(25,4))

for idx in np.arange(3):

ax = fig.add_subplot(1, 10/2, idx+2, xticks=[], yticks=[])

plt.imshow(np.transpose(images[idx] * 255, (1,2,0)))

ax.set_title(train_data.classes[idx])
print(images.shape)

Thanks