简体   繁体   中英

Tensorflow how do I reshape array of images properly for model prediction input

I have trained model and I am trying to test it

import tensorflow as tf
import os
import cv2
import matplotlib.pyplot as plt
import numpy as np


# Some paths

paths = {
    "cats": "data\\images\\cats",
    "dogs": "data\\images\\dogs",
    "img": "data\\images"
}

label_to_index = {
    "cat": 0,
    "dog": 1
}

index_to_label = {
    0: "cat",
    1: "dog"
}

animals = {
    "cats_labels": [label_to_index["cat"] for _ in range(len(paths["cats"]))],
    "dogs_labels": [label_to_index["dog"] for _ in range(len(paths["dogs"]))],
    "cats": [os.path.join(paths["cats"], img) for img in os.listdir(paths["cats"])],
    "dogs": [os.path.join(paths["dogs"], img) for img in os.listdir(paths["dogs"])],
}

# Load model
model = tf.keras.models.load_model('models/cats_dogs_model_1.h5')

# Load one image for test
img = cv2.imread(animals['cats'][0])

# Predictions input needs to be an array
test_img = [img]

# (1) mobilenetv2_1.00_192_input expected to have 4 dimensions, image now has 3 dims ->
# -> add new dim
test_img = [np.expand_dims(img, axis=0) for img in test_img]
print("Shape is ", test_img[0].shape)               # New Shape is  (1, 375, 500, 3) , was (375, 500, 3) 
print("Number if dimentions is ", test_img[0].ndim) # New Number if dimentions is  4 , was 3

# (2) ValueError: Error when checking input: expected mobilenetv2_1.00_192_input
# to have shape (192, 192, 3) but got array with shape (375, 500, 3)
# in the next line I am trying to reshape image for necessary sizes:
# test_img = [np.reshape(img, (192, 192)) for img in test_img]
# but if I uncomment it new error will be raised:
# ValueError: cannot reshape array of size 562500 into shape (192,192)

predictions = model.predict(test_img) # !!! error raises here
plt.imshow(test_img[0])
label = np.argmax(predictions)
plt.title(label)
plt.show()

But I encounter errors all the time I am trying to make image shape and dims valid for model.predict input, so I am stuck at this moment without understanding how to reshape my image properly. I hope anybody could explain me what is wrong with my image transormation, because this part is a black box for me now.

Errors I encounter:

  1. (1) Error about dimentions - I added fourth dim, and everything is ok for now, then
  2. (2) Error about invalid input image shape

Reshape is not you are looking for, reshape only rearranges the dimensions and changes values across dimensions, but never generates new values to fit a required size. What you want to resize the images. TensorFlow has a convenient function to resize a batch of images:

import tensorflow as tf

# ... 

model = tf.keras.models.load_model('models/cats_dogs_model_1.h5')

img = cv2.imread(animals['cats'][0])

test_img = [img]
test_img = [np.expand_dims(img, axis=0) for img in test_img]

# It is better to have a single tensor instead of a list of tensors, therefore,
# before resizing the images concatenate all them in a tensor
test_img = tf.concat(test_img, axis=0)
test_img = tf.image.resize(test_img, [192, 192])

np.reshape cannot resize an image. It's used to, you guessed it, reshape an array to another shape without changing the number of elements it contains. For instance, you can reshape a (20, 50) array into a (20, 5, 10) array because 20x50=20x5x10, but you can't reshape a (375, 500, 3) image into a (192, 192, 3) image.

Instead, you should use the resize method from PIL.Image ( https://www.google.com/amp/s/www.geeksforgeeks.org/python-pil-image-resize-method/amp/ )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM