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

时间:2017-08-11 11:36:17

标签: python tensorflow keras keras-2

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

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

训练过程看起来很好但是当我尝试用它来预测图像时会导致这个错误。

  

ValueError:检查时出错:预期input_1具有形状   (无,299,299,3)但是有形状的数组(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)表示299 X 299中有3个通道的1个图像。 在这种情况下,我训练模型的预期输入是什么(无,299,299,3)?如何从(299,299,3)创建(无,299,299,3)?

1 个答案:

答案 0 :(得分:2)

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

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