Data loader showing weird images when plotting

username: mathematics
cooked: <p>I wanted to plot images from data loader and it shows little different images.</p>
<pre><code class="lang-auto">## 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])
</code></pre>
<p>when plotting when i dont use <code>.astype('uint8')</code> it show some message like this</p>
<pre><code class="lang-auto">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).
</code></pre>
<p>showing image like this<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/1/51944fadf80dc64e702fdf17b2e22c095919f237.png" data-download-href="https://discuss.pytorch.org/uploads/default/51944fadf80dc64e702fdf17b2e22c095919f237" title="wird"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/1/51944fadf80dc64e702fdf17b2e22c095919f237_2_690x129.png" alt="wird" data-base62-sha1="bDGqGNUJHmFrodApcE0I334ynlR" width="690" height="129" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/1/51944fadf80dc64e702fdf17b2e22c095919f237_2_690x129.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/1/51944fadf80dc64e702fdf17b2e22c095919f237_2_1035x193.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/1/51944fadf80dc64e702fdf17b2e22c095919f237.png 2x" data-dominant-color="45493B"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">wird</span><span class="informations">1336×251 148 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I hadnt used any color jitters to test images. Even multiplying by 255 didnt worked. Help!!</p>

username: TinfoilHat0
cooked: <p>Color changes could be due to normalization. Could you try plotting them after you turn off normalization ?</p>

username: mathematics
cooked: <p>Is there any way to turn off normalization?<br>
I tried to multiply by 255, then plotted showing same results</p>

username: TinfoilHat0
cooked: <p>Remove normalizer from test transforms.<br>
This might reduce your test accuracy though. Ideally, you’d want to unnormalize images at the time you plot them.</p>

username: hesham_gabr
cooked: <p>Help me, please<br>
I try this code with many directory data set and work fine in visualization<br>
but with this data directory, it produced a weird visualization</p>
<p>import os</p>
<p>import torch</p>
<p>from torchvision import datasets, transforms</p>
<p>from torch.utils.data import Dataset, DataLoader</p>
<p>import torch</p>
<p>import torchvision</p>
<p>import torchvision.transforms as transforms</p>
<p>import matplotlib.pyplot as plt</p>
<p>import cv2</p>
<p>import numpy as np</p>
<h1><a name="write-data-loaders-for-training-validation-and-test-sets-1" class="anchor" href="#write-data-loaders-for-training-validation-and-test-sets-1"></a>Write data loaders for training, validation, and test sets</h1>
<h1><a name="specify-appropriate-transforms-and-batch_sizes-2" class="anchor" href="#specify-appropriate-transforms-and-batch_sizes-2"></a>Specify appropriate transforms, and batch_sizes</h1>
<p>from PIL import ImageFile</p>
<p>ImageFile.LOAD_TRUNCATED_IMAGES = True</p>
<h1><a name="number-of-subprocesses-to-use-for-data-loading-3" class="anchor" href="#number-of-subprocesses-to-use-for-data-loading-3"></a>number of subprocesses to use for data loading</h1>
<p>num_workers = 0</p>
<h1><a name="how-many-samples-per-batch-to-load-4" class="anchor" href="#how-many-samples-per-batch-to-load-4"></a>how many samples per batch to load</h1>
<p>batch_size = 16</p>
<p>data_transform_train = transforms.Compose([</p>
<pre><code>transforms.Resize(256),

transforms.CenterCrop(224),

transforms.RandomHorizontalFlip(),

transforms.ToTensor(),

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

])
</code></pre>
<p>data_transform_test = transforms.Compose([</p>
<pre><code>transforms.Resize(256),

transforms.CenterCrop(224),

transforms.ToTensor(),

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

])
</code></pre>
<p>data_dir = ‘/content/gdrive/MyDrive/graduation_project_dataset/Mammogram’</p>
<p>train_dir = os.path.join(data_dir, ‘train’)</p>
<p>valid_dir = os.path.join(data_dir, ‘valid’)</p>
<p>test_dir = os.path.join(data_dir, ‘test’)</p>
<p>train_data = datasets.ImageFolder(train_dir, transform=data_transform_train)</p>
<p>valid_data = datasets.ImageFolder(valid_dir, transform=data_transform_test)</p>
<p>test_data = datasets.ImageFolder(test_dir, transform=data_transform_test)</p>
<p>train_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, num_workers=num_workers, shuffle=True)</p>
<p>valid_loader = torch.utils.data.DataLoader(valid_data, batch_size=batch_size, num_workers=num_workers, shuffle=True)</p>
<p>test_loader = torch.utils.data.DataLoader(test_data, batch_size=batch_size, num_workers=num_workers, shuffle=True)</p>
<p>loaders_scratch = {</p>
<pre><code>'train' : train_loader,

'valid' : valid_loader,

'test'  : test_loader
</code></pre>
<p>}</p>
<p>dataiter = iter(train_loader)</p>
<p>images, labels = dataiter.next()</p>
<p>images = images.numpy()</p>
<p><span class="hashtag-raw">#plot</span></p>
<p>fig = plt.figure(figsize=(25,4))</p>
<p>for idx in np.arange(3):</p>
<p>ax = fig.add_subplot(1, 10/2, idx+2, xticks=<span class="chcklst-box fa fa-square-o fa-fw"></span>, yticks=<span class="chcklst-box fa fa-square-o fa-fw"></span>)</p>
<p>plt.imshow(np.transpose(images[idx] * 255, (1,2,0)))</p>
<p>ax.set_title(train_data.classes[idx])<br>
print(images.shape)</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/3/c3fc7e85f7c45ad4bb5c4bb15815d8cbea0d3ced.png" data-download-href="https://discuss.pytorch.org/uploads/default/c3fc7e85f7c45ad4bb5c4bb15815d8cbea0d3ced" title="dd"><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/3/c3fc7e85f7c45ad4bb5c4bb15815d8cbea0d3ced.png" alt="dd" data-base62-sha1="rXM4x7TdhIT0B9GKEHySCYbH2eF" width="690" height="242" data-dominant-color="8C8C8B"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">dd</span><span class="informations">1219×429 17.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Thanks</p>