这主要是因为您没有为初始模型准备输入(其维度)。这是一种可能的解决方案。
型号
from tensorflow.keras.applications import *
IMAGE_SIZE = [244,720]
inception = InceptionV3(input_shape=IMAGE_SIZE + [3],
weights='imagenet', include_top=False)
# check it's input shape
inception.input_shape
(None, 244, 720, 3)
推理
让我们通过将样本传递给模型来测试它。
from PIL import Image
a = Image.open('/content/1.png').convert('RGB')
display(a)
检查它的基本属性。
a.mode, a.size, a.format
('RGB', (297, 308), None)
所以,它的形状已经在 (297 x 308 x 3) 中了。但是为了能够将它传递给模型,我们需要一个额外的轴,即批处理轴。为此,我们可以这样做
import tensorflow as tf
import numpy as np
a = tf.expand_dims(np.array(a), axis=0)
a.shape
TensorShape([1, 308, 297, 3])
好多了。现在,我们可能想要标准化我们的数据并根据模型输入形状调整它的大小。为此,我们可以这样做:
a = tf.divide(a, 255)
a = tf.image.resize(a, [244,720])
a.shape
TensorShape([1, 244, 720, 3])
最后,将它传递给模型。
inception(a).shape
TensorShape([1, 6, 21, 2048])
# or, preserve the prediction to later analysis
y_pred = inception(a)
更新
如果你使用的是[tf.keras]图像处理函数,将图像加载为PIL格式,那么我们可以简单地做:
image = tf.keras.preprocessing.image.load_img('/content/1.png',
target_size=(244,720))
input_arr = tf.keras.preprocessing.image.img_to_array(image)
input_arr = np.array([input_arr]) # Convert single image to a batch.
inception(input_arr).shape
TensorShape([1, 6, 21, 2048])