如何从 (299,299,3) 创建 (None,299,299,3)?

How can I create a (None,299,299,3) from (299,299,3)?

我正在尝试使用基于 Keras 2 incepctionV3 的训练模型来预测图像以用于测试目的。我的原始模型运行良好,然后我尝试创建一个具有指定 input_shape (299,299,3)

的模型
base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(299,299,3)) 

训练过程看起来不错,但是当我尝试使用它来预测图像时,它导致了这个错误。

ValueError: Error when checking : expected input_1 to have shape (None, 299, 299, 3) but got array with shape (1, 229, 229, 3)

    import sys
    import argparse
    import numpy as np
    from PIL import Image
    from io import BytesIO

    from keras.preprocessing import image
    from keras.models import load_model
    from keras.applications.inception_v3 import preprocess_input


    target_size = (229, 229) #fixed size for InceptionV3 architecture


    def predict(model, img, target_size):
      """Run model prediction on image
      Args:
        model: keras model
        img: PIL format image
        target_size: (w,h) tuple
      Returns:
        list of predicted labels and their probabilities
      """
      if img.size != target_size:
        img = img.resize(target_size)

      x = image.img_to_array(img)
      print(x.shape)
      print("model input",model.inputs)
      print("model output",model.outputs)
      x = np.expand_dims(x, axis=0)
      #x = x[None,:,:,:]
      print(x.shape)
      x = preprocess_input(x)

      print(x.shape)
      preds = model.predict(x)
      print('Predicted:',preds)
      return preds[0]

这是打印出来的

(229, 229, 3)  
('model input', [<tf.Tensor 'input_1:0' shape=(?, 299, 299, 3) dtype=float32>])  
('model output', [<tf.Tensor 'dense_2/Softmax:0' shape=(?, 5) dtype=float32>]) 
(1, 229, 229, 3) 
(1, 229, 229, 3)

(1,299,299,3) 表示 1 张 299 X 299 图像,3 通道。 在这种情况下,我训练的模型 (None,299,299,3) 的预期输入是什么意思?如何从 (299,299,3) 创建一个 (None,299,299,3)?

这里的问题是图片大小,将需要的大小设置为299, 299

target_size = (299, 299) #fixed size for InceptionV3 architecture

你应该使用

preds = model.predict(x, batch_size=1)
默认情况下

batch_size=32。而且你只有一张图像可以预测。 (None,299,299,3) 在这种情况下意味着 model.predict 期望形状为 (n,299,299,3) 且 n > batch_size 的数组逐批处理,return (n, outputs_dim) 维预测数组。