无法预测ImageNet类的输入[Keras + Tensorflow]

时间:2016-12-02 06:48:10

标签: python tensorflow deep-learning keras imagenet

我跟着这个回购(https://github.com/iamgroot42/keras-finetuning),我已完成了培训。

现在,我想预测我自己的数据集(包含2个类,Avocado和芒果)和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)

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

也许您只需要在class indeximagenet 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])

大小)