我在Keras中训练了一个模型(二元图像分类,重训练的Xception模型)。现在如果我输入
model.predict(np.random.rand(1, 300, 300, 3))
我得到的输出是
array([[0.68225867, 0.3177413 ]], dtype=float32)
这就是我希望用真实图像得到的结果。然而,当我这样输入真实图像时:
from scipy.misc import imread,imresizex=imread('processed_dataset/test/EM/bull_212.jpg',mode='RGB')x=imresize(x,(300,300))x=np.invert(x)x=x.reshape(-1,300,300,3)model.predict(x)
我总是得到相同的输出:
array([[1., 0.]], dtype=float32)
无论输入什么图像,模型输出的都是[1., 0]。如果我这样输入图像,结果也是一样的:
img = image.load_img('processed_dataset/test/EM/bull_212.jpg', target_size=(img_width, img_height))x = image.img_to_array(img)x = np.expand_dims(x, axis=0)images = np.vstack([x])model.predict(images, batch_size=1)
我的想法是:如果输入一个np.random生成的“图像”能得到期望的结果,那么问题就在于我如何输入真实图像。如何正确地做这件事以得到我想要的结果?
回答:
看起来你没有应用训练模型时使用的归一化处理,如果不这样做,那么输入将完全不同,这会使神经元饱和,产生不一致的输出。