简体   繁体   English

Tensorflow 如何为模型预测输入正确重塑图像数组

[英]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.但是我一直在尝试使图像形状和变暗对model.predict输入有效时遇到错误,所以此时我陷入困境,不知道如何正确地重塑我的图像。 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 (1) 关于维度的错误 - 我添加了第四个维度,现在一切正常,然后
  2. (2) Error about invalid input image shape (2) 关于无效输入图像形状的错误

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. Reshape 不是您要寻找的,reshape 只会重新排列维度并更改维度之间的值,但不会生成新值以适应所需的大小。 What you want to resize the images.您想要调整图像大小的内容。 TensorFlow has a convenient function to resize a batch of images: TensorFlow 有一个方便的功能来调整一批图像的大小:

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. np.reshape无法调整图像大小。 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.例如,您可以将 (20, 50) 数组重新整形为 (20, 5, 10) 数组,因为 20x50=20x5x10,但是您不能将 (375, 500, 3) 图像重新整形为 (192, 192, 3)图像。

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/ )相反,您应该使用PIL.Image ( https://www.google.com/amp/s/www.geeksforgeeks.org/python-pil-image-resize-method/amp/ ) 中的resize方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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