输入神经网络的图片大小异常
The size of the image input to the neural network is abnormal
我需要向网络输入一张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)。
main.py
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)
load_image.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()
这是这张图片:
enter image description here
怎样才能让图片顺利进入模型并得到预测结果?
您需要像这样将 numpy 数组传递给 model.predict
:
predict = model.predict(np.array([image]))[0]
print(predict)
(注意最后的[0]取输出的第一个值,因为我们传递的数组只有一个值)
我需要向网络输入一张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)。
main.py
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)
load_image.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()
这是这张图片: enter image description here
怎样才能让图片顺利进入模型并得到预测结果?
您需要像这样将 numpy 数组传递给 model.predict
:
predict = model.predict(np.array([image]))[0]
print(predict)
(注意最后的[0]取输出的第一个值,因为我们传递的数组只有一个值)