我试图从keras模型中获得每个类的概率 . 请在下面找到样本keras模型:
width = 80
height = 80
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=( width, height, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten()) # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64))
model.add(Activation('relu'))
#model.add(Dropout(0.5))
model.add(Dense(2))
model.add(Activation('softmax'))
model.compile(loss='sparse_categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
但是,在训练模型后,我通过以下方式加载要预测的图像:
img = image.load_img('Test2.jpg', target_size=(80, 80))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
classes = model.predict_proba(images, batch_size=1)
print(classes)
[[ 0. 1.]]
我仍然得到类标签,而不是概率 . 有什么提示我做错了什么?
EDIT 这就是模型的训练方式:
train_datagen = ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
train_generator = train_datagen.flow_from_directory(
'.\\train', # this is the target directory
target_size=(width, height), # all images will be resized to 150x150
batch_size=batch_size,
class_mode='binary',
shuffle=True) # since we use binary_crossentropy loss, we need binary labels
# this is a similar generator, for validation data
validation_generator = test_datagen.flow_from_directory(
'.\\validate',
target_size=(width, height),
batch_size=batch_size,
class_mode='binary',
shuffle=True)
model.fit_generator(
train_generator,
steps_per_epoch=4000,
epochs=2,
validation_data=validation_generator,
validation_steps=1600)
1 回答
问题是你在
ImageDataGenerator
中使用了'sparse_categorical_crossentropy'
和class_mode='binary'
.你有两种可能性:
将损失更改为
'categorical_crossentropy'
并设置class_mode='categorical'
.保持原样,但设置
class_mode='sparse'
.要么工作 .
有关两个损失之间的差异,请参阅this answer(在Tensorflow中,但它也适用于Keras) . 简短版本是稀疏损失期望标签是整数类(例如,1,2,3 ......),而普通的标签需要单热编码矢量(例如
[0, 1, 0, 0]
) .干杯
EDIT :正如@Simeon Kredatus指出的,这是一个正常化问题 . 通过在训练集和测试集的
ImageDataGenerator
构造函数中设置适当的标志,即samplewise_center=True
和samplewise_std_normalization=True
,可以很容易地解决这个问题 .更新答案,以便人们可以看到解决方案 . 一般来说,请记住垃圾桶中的垃圾原则 .