2016-12-02 134 views
0

我跟着这个回购(https://github.com/iamgroot42/keras-finetuning),我已经完成了培训。无法预测来自ImageNet类的输入[Keras + Tensorflow]

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

我的模型labels.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) 

有没有人可以帮助我?

+0

你可以发布你的模型定义是什么? –

+0

@ avijit-dasgupta以下是完整的脚本:github.com/iamgroot42/keras-finetuning/blob/master/net.py –

回答

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]) 

大小)