[英]The size of the image input to the neural network is abnormal
我需要向.network輸入一張96 * 96大小的圖片,但是我得到了這個異常:
Model: "model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_2 (InputLayer) [(None, 96, 96, 3)] 0
sequential_1 (Sequential) (None, 96, 96, 3) 0
rescaling (Rescaling) (None, 96, 96, 3) 0
mobilenetv2_0.35_96 (Functi (None, 3, 3, 1280) 410208
onal)
flatten (Flatten) (None, 11520) 0
dense (Dense) (None, 15) 172815
=================================================================
Total params: 583,023
Trainable params: 172,815
Non-trainable params: 410,208
_________________________________________________________________
(96, 96, 3)
ValueError: Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 96, 96, 3), found shape=(32, 96, 3)
這很奇怪,當我調用image.shape()
時,我得到了 (96, 96),但是這個異常顯示這個圖像大小是 (32, 96)。
主程序
import image_load
from pathlib import Path
from tensorflow import keras
base_path = Path()
model = keras.models.load_model(base_path.cwd().joinpath("my_model"))
model.summary()
image = image_load.load_and_preprocess_image(str(base_path.joinpath("cat4.jpg")))
print(image.shape)
predict = model.predict(image)
print(predict)
加載圖像.py
import tensorflow as tf
import matplotlib.pyplot as plt
def preprocess_image(image):
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.resize(image, [96, 96])
image /= 255.0 # normalize to [0,1] range
return image
def load_and_preprocess_image(path):
image = tf.io.read_file(path)
return preprocess_image(image)
def show_image(path):
plt.imshow(path)
plt.show()
這是這張圖片:在此處輸入圖片描述
怎樣才能讓圖片順利進入model並得到預測結果呢?
您需要像這樣將 numpy 數組傳遞到model.predict
:
predict = model.predict(np.array([image]))[0]
print(predict)
(注意最后的 [0] 取 output 的第一個值,因為我們只傳遞了一個值的數組)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.