Visualze the dataset images

hesham_gabr: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/d/9d9a869caf28f373d8659361990b6e399a8ddbbd.png" data-download-href="https://discuss.pytorch.org/uploads/default/9d9a869caf28f373d8659361990b6e399a8ddbbd" title="d"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/d/9d9a869caf28f373d8659361990b6e399a8ddbbd_2_690x256.png" alt="d" data-base62-sha1="mue22xWvfX1avykC4TxJQHbz1Nz" width="690" height="256" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/d/9d9a869caf28f373d8659361990b6e399a8ddbbd_2_690x256.png, https://discuss.pytorch.org/uploads/default/optimized/3X/9/d/9d9a869caf28f373d8659361990b6e399a8ddbbd_2_1035x384.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/9/d/9d9a869caf28f373d8659361990b6e399a8ddbbd.png 2x" data-dominant-color="4E4E4D"><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">d</span><span class="informations">1284×478 17 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>when I try to visualize the images of the dataset, always give me a white image<br>
and when I feed another data set the correct image is was visualized</p>
<p>This is a code<br>
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 = 64</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/drive/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>train_images, train_labels = next(iter(train_loader))</p>
<p>for img in train_images:</p>
<p>print(‘\nShape of the image is: {0}’.format(train_images.shape))</p>
<p>grid = torchvision.utils.make_grid(img, nrow = 5)</p>
<p>img = train_images[2]</p>
<p>label = train_labels[2]</p>
<p>plt.imshow(np.transpose(grid,(1,2,0)), cmap = ‘gray’)</p>
<p>plt.show()</p>
<p>print(f"Label: {label}")</p>
<p>break</p>

hesham_gabr: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/e/ee1efa462936a858fc9ce8377fa6a93142c4f822.png" data-download-href="https://discuss.pytorch.org/uploads/default/ee1efa462936a858fc9ce8377fa6a93142c4f822" title="d"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/e/ee1efa462936a858fc9ce8377fa6a93142c4f822_2_690x308.png" alt="d" data-base62-sha1="xYw0rPpkZ3ZeOrZSIsYffE7NsY2" width="690" height="308" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/e/ee1efa462936a858fc9ce8377fa6a93142c4f822_2_690x308.png, https://discuss.pytorch.org/uploads/default/optimized/3X/e/e/ee1efa462936a858fc9ce8377fa6a93142c4f822_2_1035x462.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/e/e/ee1efa462936a858fc9ce8377fa6a93142c4f822.png 2x" data-dominant-color="373736"><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">d</span><span class="informations">1248×558 42.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>This when i upload another directory of the same data and the same arrangment</p>

hesham_gabr: <p>the extenstions of white output image is png<br>
and another images file is Jpeg</p>

ptrblck: <p>Could you check the value range of the white image and make sure <code>matplotlib</code> is able to visualize it?<br>
Based on the data type and range I guess <code>matplotlib</code> might automatically rescale the numpy array and could yield an “empty” image.</p>

hesham_gabr: <p>images are 16 bit and I normalize them and resize to 224 pixels, but the problem is still shown.</p>