【问题标题】:Issue retrieiving value error `decode_predictions` expects a batch of predictions问题检索值错误`decode_predictions`需要一批预测
【发布时间】:2021-03-16 11:50:40
【问题描述】:

我有以下代码试图对部分 resnet 模型执行预测。但是,我正在检索错误。

def layer_input_shape(Model, layer_index):
  input_shape = np.array(Model.layers[layer_index - 1].output_shape)
  input_shape = np.ndarray.tolist(np.delete(input_shape, 0))
  return input_shape

def resnet50_Model(Model, trainable=True):
  input_shape = layer_input_shape(Model, 1)
  input = tf.keras.layers.Input(shape=input_shape)
  first_layer = Model.layers[0]
  first_layer.trainable = trainable
  out = first_layer(input)
  for i in range(1, 12):
    layer_i = Model.layers[i]
    layer_i.trainable = trainable
    out = layer_i(out)
  out = Conv2D(filters=2, kernel_size=2, strides=(2,2), activation='relu')(out)
  out = Flatten()(out)
  out = Dense(units=2,activation='softmax')(out)
  result_model = tf.keras.models.Model(inputs=[input], outputs=out)
  return result_model

  from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
  img='/content/elephant.jpg'
  img = image.load_img(img, target_size=(224, 224))
  x = image.img_to_array(img)
  x = np.expand_dims(x, axis=0)
  x = preprocess_input(x)
  preds = resnet_skip_model.predict(x)
  print('Predicted:', decode_predictions(preds, top=3)[0])

检索以下错误:

ValueError: `decode_predictions` expects a batch of predictions (i.e. a 2D array of shape (samples, 
1000)). Found array with shape: (1, 3)

【问题讨论】:

    标签: python machine-learning keras conv-neural-network pre-trained-model


    【解决方案1】:

    我添加了两个输出密集层,所以我只能预测两个类,当我调用 decode 时,它​​预计最后一个密集层有 1000 个输出,因此将单位从 2 更改为 1000

     out = Dense(units=1000,activation='softmax')(out)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-18
      • 2017-06-14
      • 1970-01-01
      • 2021-03-25
      • 1970-01-01
      • 2018-11-16
      • 2018-08-21
      • 1970-01-01
      相关资源
      最近更新 更多