How to resize image data

username: oasjd7
cooked: <p>I have image dataset : (55000,32,32,3)<br>
I want to make this image to (55000,28,28,3)</p>
<p>This data is numpy.ndarray type.<br>
I tried data.resize(55000,28,28,3) but there is error.</p>
<p><code>ValueError: resize only works on single-segment arrays</code></p>
<p>How can I resize this image?</p>

username: pbloem
cooked: <p>What kind of resize do you want to do? Do you want to actually rescale the images (downsampling), or do you want to select the center 28x28 pixels, dis arding the rest (cropping)?</p>
<p>Is this a preprocessing operation (before the data goes into your model), or do you want to do this as part of your model?</p>
<p>Also, are you using a dataloader to read the images, or are you getting the data array from somewhere else?</p>

username: oasjd7
cooked: <p>I just want to re-scaling not cropping.<br>
MNIST size is 28x28, and SVHN is 32x32.<br>
so I want to make this images to have same size.<br>
and then I input those images to my network.</p>

username: pbloem
cooked: <p>In that case, the standard approach is to transform the images during data loading. Note the <code>transforms</code> variable in <a href="https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html#sphx-glr-beginner-blitz-cifar10-tutorial-py" rel="nofollow noopener">this example</a>: you can add a <a href="https://pytorch.org/docs/stable/torchvision/transforms.html#torchvision.transforms.Resize" rel="nofollow noopener">Resize</a> operation to that pipeline (before the ToTensor) to scale the images.</p>
<p>If you’re not using a dataloader, it’s a bit trickier. I think the best option is to transform your data to numpy, <a href="http://scikit-image.org/docs/dev/api/skimage.transform.html#skimage.transform.rescale" rel="nofollow noopener">use scikit-image to resize the images</a> and then transform it back to pytorch.</p>
<p>Cropping would actually be easier. For that you could just do:<br>
<code>data = data[:, :, 2:31, 2:31]</code></p>
<p>Note that pytorch image arrays are dimensioned as (batch, channels, height, width).</p>

username: oasjd7
cooked: <p><a class="mention" href="/u/pbloem">@pbloem</a><br>
I want to calculate pixel-mean to normalization.<br>
To do this,  Should I use scikit-image ?</p>
<p>Original size : [55000, 32, 32, 3]<br>
I want to make this to [55000,28, 28, 3]</p>

username: pbloem
cooked: <p>Sorry, I don’t understand what you mean by “pixel-mean to normalization.”</p>
<p>If you want downsample the images, and you’re not using torchvision to load the data, then yes, scikit-image is a good option.</p>

username: oasjd7
cooked: <p>Original images have  [55000, 32, 32, 3] shape as numpy.ndarray.</p>
<p>I applied <code>rescale(svhn_train, 0.875).shape</code><br>
and It becomes [48125, 28, 28, 3].<br>
What`s wrong in my rescaling?</p>

username: pbloem
cooked: <p>Two things have gone wrong. First, scikit assumes that the <em>first</em> dimensions are to be rescaled. Second, it assumes that the data is a single 3D image  (because it has 4 dimensions), instead of many 2D images, so it rescales the first 3 dimensions.</p>
<p>I also linked to the wrong function… <a href="http://scikit-image.org/docs/dev/api/skimage.transform.html#resize" rel="nofollow noopener">resize</a> is easier to use.</p>
<p>Try something like this:</p>
<pre><code class="lang-auto">data = np.transpose(data, (1, 2, 3, 0)) # put height and width in front
data = skimage.transform.resize(data, (28, 28)) 
data = np.transpose(data, (3, 0, 1, 2) ) # move back 
</code></pre>

username: oasjd7
cooked: <pre><code class="lang-auto">svhn_train = svhn_train.transpose((2, 3, 1, 0)) # [32, 32, 3, 55000]

svhn_train = resize(svhn_train, (28, 28))
-&gt; ValueError: len(output_shape) cannot be smaller than the image dimensions


</code></pre>

username: pbloem
cooked: <p>Sorry, looks like <code>resize()</code> doesn’t handle extra dimensions the way I thought. Bit of a hack, but you can fold the batch dimension into the channel dimension:</p>
<pre><code class="lang-auto">data = data.transpose((1, 2, 3, 0)) 
data = resize(data.reshape(32, 32, -1), (28, 28)) 
data = data.reshape(28, 28, 3, -1)
data = data.transpose((3, 0, 1, 2))
</code></pre>
<p>This doesn’t give me any errors when I try it with a random array (but you may want to plot of few images to make sure nothing gets messed up).</p>

username: oasjd7
cooked: <p>I check that size is changed.<br>
I plot the images but image sizes are equal (resolutions are different.)<br>
I use this code.</p>
<pre><code class="lang-auto">def imshow_grid

(images, shape=[2, 8]):
    """Plot images in a grid of a given shape."""
    fig = plt.figure(1)
    grid = ImageGrid(fig, 111, nrows_ncols=shape, axes_pad=0.05)

    size = shape[0] * shape[1]
    for i in range(size):
        grid[i].axis('off')
        grid[i].imshow(images[i])  # The AxesGrid object work as a list of axes.

    plt.show()
</code></pre>
<p>32x32<br>
<img src="https://discuss.pytorch.org/uploads/default/original/2X/8/8af4e7540ea9597c46f56e24968d7ba959345c87.png" alt="32x32" data-base62-sha1="jPgARl854YjTB3Mj12o5Aj1EA4f" width="640" height="480"> !<br>
28x28<br>
<img src="https://discuss.pytorch.org/uploads/default/original/2X/b/b12d743ece68dbb62afdba7cf0a5b6bb5808e459.png" alt="28x28" data-base62-sha1="phnTCOqPu05IU51d4IgseEmEIt3" width="640" height="480"></p>

username: sugar2019
cooked: <p>My data.size is (321,321,3),and I want to resize to (48,48,3).<br>
I meet the same problem that the size change to (48,48,3),but the image is very burry.<br>
Can you give me a solution? Thank you!</p>

username: bolt25
cooked: <p>Try using transforms. It has a resize option too</p>