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?

4
  • check img.size for image before img.resize then grab error Commented Jan 16, 2021 at 18:37
  • @SurajS I checked it, it is printing the same shape (256, 256, 3) for the image causing error also. Commented Jan 16, 2021 at 18:38
  • is the resize method used here the np.resize? if yes, the doc says it is not suitable for images Commented Jan 16, 2021 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,:] Commented Jan 16, 2021 at 22:54

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.