Display data loaded by pytorch dataloader after augmentation?

username: amy2
cooked: <p>I am new to pytorch and I am trying to work on project of human activit recognition.from my understanding the transforms operations are applied to the original data at every batch generation and upon every epoch you get different version of the dataset but the original is left unchanged and unused.<br>
But wen I get data with the shape for exemple  (112,112,16,3)<br>
where 112 are height and width , 16 are the number of rgb frames<br>
know after applying a series of transformation using pytorch,if I want to visualize the data after augmentation using the matplotlib librairy for exemple<br>
what should I take into consideration ?<br>
I ve tried something like this</p>
<blockquote>
<p>for batch in train_data_loader:<br>
inputs, targets = batch<br>
for img in inputs:<br>
image  = img.cpu().numpy()<br>
# transpose image to fit plt input<br>
image = image.T<br>
# normalise image<br>
data_min = np.min(image, axis=(1,2), keepdims=True)<br>
data_max = np.max(image, axis=(1,2), keepdims=True)<br>
scaled_data = (image - data_min) / (data_max - data_min)<br>
# show image<br>
plt.imshow(scaled_data)<br>
plt.show()</p>
</blockquote>
<p>But got an error TypeError : invalid shape (112.112.16.3) for image data</p>

username: ptrblck
cooked: <p><code>plt.imshow</code> expects a valid image format while you are trying to visualize 16 images at once.<br>
You would thus need to either visualize a single slice only or create a <code>subplot</code> and iterate the tensor in <code>dim2</code> to visualize each slice separately.<br>
Here is a minimal code snippet:</p>
<pre><code class="lang-python">x = torch.randint(0, 256, (112, 112, 16, 3)).byte()

# visualize first slice only
plt.imshow(x[:, :, 0])

# visualize all
fig, axarr = plt.subplots(4, 4)
axarr = axarr.reshape(-1)
for idx in range(x.size(2)):
    x_ = x[:, :, idx]
    ax = axarr[idx]
    ax.imshow(x_)
</code></pre>

username: amy2
cooked: <p>thank you for your answer <a class="mention" href="/u/ptrblck">@ptrblck</a><br>
juste another question if I want to read the frames from the dataloader<br>
this is the exemple I am working with</p>
<blockquote>
<pre><code>train_loader = torch.utils.data.DataLoader(train_data,batch_size=opt.batch_size, shuffle=(train_sampler is None), num_workers=opt.n_threads,pin_memory=True, sampler=train_sampler, worker_init_fn=worker_init_fn)
</code></pre>
</blockquote>
<p>I should read the image from train data ?<br>
and if I am executing the file from the anaconda prompt terminal is there  any instructions I should be adding to visaulize the images ?</p>

username: ptrblck
cooked: <p>You can read the data from any dataset or <code>DataLoader</code> and could then iterate the samples using my code.</p>
<p>To visualize images from your terminal you would need to add <code>plt.show()</code>:</p>
<pre><code class="lang-python">import numpy as np
import matplotlib.pyplot as plt

x = np.random.randint(0, 256, (224, 224, 3)).astype(np.uint8)
plt.imshow(x)
plt.show()
</code></pre>

username: amy2
cooked: <p>I used the code</p>
<blockquote>
<pre><code> for x, l in enumerate (train_loader):
</code></pre>
</blockquote>
<pre><code>    x = torch.randint(0, 256, (112, 112, 16, 3)).byte()
     fig, axarr = plt.subplots(4, 4)
     axarr = axarr.reshape(-1)
     for idx in range(x.size(2)):
      x_ = x[:, :, idx]
      ax = axarr[idx]
      ax.imshow(x_)
      plt.show()
</code></pre>
<p>But I got nothing<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/6/565e0b1f17bf79a3cc53514c8296b95bedf09a9d.png" data-download-href="https://discuss.pytorch.org/uploads/default/565e0b1f17bf79a3cc53514c8296b95bedf09a9d" title="Capture1"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/6/565e0b1f17bf79a3cc53514c8296b95bedf09a9d_2_587x500.png" alt="Capture1" data-base62-sha1="ck2y4GAPGYSrwP8DTP8XTKghaz3" width="587" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/6/565e0b1f17bf79a3cc53514c8296b95bedf09a9d_2_587x500.png, https://discuss.pytorch.org/uploads/default/original/3X/5/6/565e0b1f17bf79a3cc53514c8296b95bedf09a9d.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/6/565e0b1f17bf79a3cc53514c8296b95bedf09a9d.png 2x" data-dominant-color="F1F1F1"><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">Capture1</span><span class="informations">640×545 33.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><br>
what could it the problem?</p>

username: ptrblck
cooked: <p>The figure is properly showing one subplot. If you want to show all, move the <code>plt.show()</code> command out of the loop.</p>