首页 文章

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

提问于
浏览
0

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

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

我的model-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)

有人能帮帮我吗?

1 回答

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

    尺寸)

相关问题