predict('../input/cat_dog/cat_dog/zebra.jpeg')
ValueError: `decode_predictions` expects a batch of predictions (i.e. a 2D array of shape (samples, 1000)). Found array with shape: (1, 2)
这是我遇到的错误,我对InceptionResNetV2进行了微调。 所以没有上,我的分类器也只有2个类,而不是1000个。 我知道这是问题所在,因为错误提到输入形状需要1000个但得到两个。
但是,我坚持如何解决解码问题,以使输入形状期望2类而不是1000类。
我的预测功能
def predict(image_path):
# Load and resize the image using PIL.
# Plot the image.
plt.imshow(image)
plt.show()
image = Image.open(image_path).resize((299,299))
image = np.array(image)
image = image.reshape(-1,299,299,3)
image = 2 * (image/255.0)-1.0
# This outputs an array with 1000 numbers corresponding to
# the classes of the ImageNet-dataset.
pred = model.predict(image)
# Decode the output of the model.
pred_decoded = decode_predictions(pred)[0]
# Print the predictions.
for code, name, score in pred_decoded:
print("{0:>6.2%} : {1}".format(score, name))
我的微调图层
base_model = InceptionResNetV2(input_shape = (299,299,3),
include_top= False,
weights='imagenet')
for layer in base_model.layers:
layer.trainable = False
top_layers = base_model.output
top_layers = GlobalAveragePooling2D()(top_layers)
top_layers = Dense(1024, activation='relu')(top_layers)
predictions = Dense(2, activation='softmax')(top_layers)
model= Model(inputs =base_model.input, outputs = predictions)