0

我关注了这个 repo ( https://github.com/iamgroot42/keras-finetuning ),我已经完成了培训。

现在,我想预测我自己的数据集(包含 2 个类,鳄梨和芒果)和 ImageNet 集的输入图像。但预测结果总是返回索引 0 或 1(我猜是鳄梨或芒果),从不返回 ImageNet 中的类。例如,我想预测来自 ImageNet 原始类的 iPod 图像,但 model.predict(...) 总是返回 0 和 1。

我的模型标签.json:

["avocados", "mangos"]

我的预测代码:

img = imresize(imread('ipod.png', mode='RGB'), (224, 224)).astype(np.float32)
img[:, :, 0] -= 123.68
img[:, :, 1] -= 116.779
img[:, :, 2] -= 103.939
img[:,:,[0,1,2]] = img[:,:,[2,1,0]]
img = img.transpose((2, 0, 1))
img = np.expand_dims(img, axis=0)
img = img.reshape(img.shape[0], n, n, n_chan)

out = model.predict(img, batch_size=batch_size)
pred = np.argmax(out, axis=1)

print(pred)

有人可以帮助我吗?

4

1 回答 1

1

也许你只需要在 to 之间class index翻译imagenet labels

尝试:

from imagenet_utils import decode_predictions

[...]

img = imresize(imread('ipod.png', mode='RGB'), (224, 224)).astype(np.float32)
img[:, :, 0] -= 123.68
img[:, :, 1] -= 116.779
img[:, :, 2] -= 103.939
img[:,:,[0,1,2]] = img[:,:,[2,1,0]]
img = img.transpose((2, 0, 1))
img = np.expand_dims(img, axis=0)
img = img.reshape(img.shape[0], n, n, n_chan)

out = model.predict(img, batch_size=batch_size)
#add decoding line here to get the top 3
print('Predicted:', decode_predictions(out, top=3)[0])

尺寸)

于 2017-01-08T12:44:31.213 回答