0
$\begingroup$

I have a Tensorflow model weight file that I am using to make the prediction on test images. These test images are in NumPy array format and the shapes of the images are (720, 1280, 3). I am getting the following error while making the prediction-

ValueError: Input 0 is incompatible with layer model: expected shape=(None, 416, 416, 3), found shape=(1, 720, 1280, 3)

When I tried to change the shape like below-

image_np.shape=(416,416,3)

It is giving me the following error-

ValueError: cannot reshape array of size 2764800 into shape (416,416,3)

I am using Tensorflow 2.x with Python 3.7.

Please help me to resolve this issue.

$\endgroup$

2 Answers 2

0
$\begingroup$

The issue is that the model expects images of 416 by 416 pixels, whereas you are using larger images. Simply using reshape doesn't work since the overall number of pixels is still to high for a 416x416 image (720 * 1280 > 416 * 416). Therefore you have to resize your image first to 416x416 before passing it to your model. You can either directly resize to 416x416 (which would give an image with a different aspect ratio) or resize first but retain the aspect ratio and then pad the image to get to 416x416. You could use the resize function from the cv2 library to resize an image.

$\endgroup$
1
  • $\begingroup$ I understand your point. Since input images are in NumPy format; I tried np.resize(image_np,(416,416,3)) and the error is resolved now. $\endgroup$
    – Hitman
    Dec 23, 2021 at 11:57
1
$\begingroup$

you can use use "change mode" to change the channel into channel first or channel last format.

from tensorflow.keras.preprocessing.image import  img_to_array
y = np.zeros((len(ids), IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.float32)
for n, id_ in tqdm(enumerate(ids), total=len(ids)):
    # Load images
    img = load_img(path + '/images/' + id_, color_mode="rgb")
    img = img_to_array(img,data_format='channels_first')
$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.