在Keras中获得预测 [英] Obtaining a prediction in Keras

查看:62
本文介绍了在Keras中获得预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地在Keras中训练了一个简单的模型来对图像进行分类:

model = Sequential()

model.add(Convolution2D(32, 3, 3, border_mode='valid', input_shape=(img_channels, img_rows, img_cols),
                        activation='relu', name='conv1_1'))
model.add(Convolution2D(32, 3, 3, activation='relu', name='conv1_2'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Convolution2D(64, 3, 3, border_mode='valid', activation='relu', name='conv2_1'))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv2_2'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))

model.add(Dense(nb_classes, activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])

我也可以使用来预测图像类别

y_pred = model.predict_classes(img, 1, verbose=0)

但是,y_pred的输出始终是二进制的.使用predict_probapredict时似乎也是如此.我的输出是这种形式

[[ 1.  0.  0.  0.]]
[[ 0.  1.  0.  0.]]

这行得通,但是例如,我希望每个分类都有一个概率百分比

[[ 0.8  0.1  0.1  0.4]]

我如何在Keras中获得它?

解决方案

Softmax可能会产生类似单热"的输出.考虑以下示例:

# Input; Exponent; Softmax value 
20    485165195  0.99994
 9         8103  0.00002
 5          148  0.00000
10        22026  0.00005
------------------------
# Sum 485195473  1

由于指数函数增长非常快,因此softmax开始产生从数量级1开始的热输出.在Keras中,稳压器.

I have successfully trained a simple model in Keras to classify images:

model = Sequential()

model.add(Convolution2D(32, 3, 3, border_mode='valid', input_shape=(img_channels, img_rows, img_cols),
                        activation='relu', name='conv1_1'))
model.add(Convolution2D(32, 3, 3, activation='relu', name='conv1_2'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Convolution2D(64, 3, 3, border_mode='valid', activation='relu', name='conv2_1'))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv2_2'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))

model.add(Dense(nb_classes, activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])

I can also predict the image classes using

y_pred = model.predict_classes(img, 1, verbose=0)

However the output of y_pred is always binary. This also seems to be the case when using predict_proba and predict. My outputs are in this form

[[ 1.  0.  0.  0.]]
[[ 0.  1.  0.  0.]]

This works OK, but I'd like to have a probability percent for each classification, for example

[[ 0.8  0.1  0.1  0.4]]

How do I get this in Keras?

解决方案

Softmax might yield "one-hot" like output. Consider the following example:

# Input; Exponent; Softmax value 
20    485165195  0.99994
 9         8103  0.00002
 5          148  0.00000
10        22026  0.00005
------------------------
# Sum 485195473  1

Since the exponential function grows very fast softmax starts yielding one-hot like output starting from order of magnitude 1. In Keras implementation of the softmax function the maximum value is subtracted from the input, but in the stated above case it won't make any difference.

Possible ways to fix this:

  1. Make sure that input images are rescaled, so that pixels values are between 0 and 1.

  2. Add some regularizers to your model.

这篇关于在Keras中获得预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆