0

I am running a neural network, whose input should be of size (128, 128, 3). I have a dataset of images of size (256, 256, 3)

So, I am resizing every image img before inputting to neural network.

img.resize(128, 128, 3)

It is working well for some batches or some epochs.

But suddenly the program returns error due to resizing image as follows

ValueError: resize only works on single-segment arrays

I thought that there may be some issue with shape of images in my dataset, but the shapes of image are same through out my dataset i.e., (256, 256, 3). And I have no clue about this error.

If there is any issue with the resize function, then how does it work for some images and pops-up error for some other? Am I wrong anywhere?

martineau
  • 119,623
  • 25
  • 170
  • 301
hanugm
  • 1,127
  • 3
  • 13
  • 44
  • check `img.size` for image before `img.resize` then grab error – Suraj Shejal Jan 16 '21 at 18:37
  • @SurajS I checked it, it is printing the same shape `(256, 256, 3)` for the image causing error also. – hanugm Jan 16 '21 at 18:38
  • is the resize method used here the `np.resize`? if yes, the doc says it is not suitable for images – sai Jan 16 '21 at 20:07
  • 1
    I think at some point in your computation some of your arrays get segmented (i.e. the array is not a contiguous area of memory anymore), **maybe** due to low available memory. But anyway `np.resize` doesn't do what you think it does here: it just chops off the array to the new size. If you want to resize the image by half it's easy: just take every second pixel `img = img[::2,::2,:]` – Stef Jan 16 '21 at 22:54

1 Answers1

1

For images, resizing using PIL is one possible method.

import numpy as np
from PIL import Image
np.array(Image.fromarray(img.astype(np.uint8)).resize((256, 256))).astype('float32')

as explained at TypeError: Cannot handle this data type: (1, 1, 3), <f4

sbkm
  • 107
  • 5