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)?
Is this a preprocessing operation (before the data goes into your model), or do you want to do this as part of your model?
Also, are you using a dataloader to read the images, or are you getting the data array from somewhere else?
I just want to re-scaling not cropping.
MNIST size is 28x28, and SVHN is 32x32.
so I want to make this images to have same size.
and then I input those images to my network.
In that case, the standard approach is to transform the images during data loading. Note the transforms variable in this example: you can add a Resize operation to that pipeline (before the ToTensor) to scale the images.
If you’re not using a dataloader, it’s a bit trickier. I think the best option is to transform your data to numpy, use scikit-image to resize the images and then transform it back to pytorch.
Cropping would actually be easier. For that you could just do: data = data[:, :, 2:31, 2:31]
Note that pytorch image arrays are dimensioned as (batch, channels, height, width).
Two things have gone wrong. First, scikit assumes that the first 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.
I also linked to the wrong function… resize is easier to use.
Try something like this:
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
Sorry, looks like resize() doesn’t handle extra dimensions the way I thought. Bit of a hack, but you can fold the batch dimension into the channel dimension:
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))
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).
I check that size is changed.
I plot the images but image sizes are equal (resolutions are different.)
I use this code.
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()
My data.size is (321,321,3),and I want to resize to (48,48,3).
I meet the same problem that the size change to (48,48,3),but the image is very burry.
Can you give me a solution? Thank you!