我正在使用Keras构建多类别的多标签对象识别CNN,它将在一定程度上确定图像属于哪个类别。不幸的是,我似乎无法用它来给出百分比,但是当我运行model.predict()
方法时,每个类只能得到0或1的值。我的代码有什么问题吗,或者只是尚未对足够的图像进行训练? (是的,我正在使用该模型从未见过的新照片进行预测。)
#building the CNN
classifier = Sequential()
classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation =
'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Dropout(0.25))
classifier.add(Flatten())
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 64, activation = 'relu'))
classifier.add(Dense(units = 4, activation = 'sigmoid'))
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy',
metrics = ['accuracy'])
datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
validation_split = 0.1)
training_set = datagen.flow_from_directory('images',
target_size = (64, 64),
batch_size = 32,
class_mode = 'categorical',
subset = 'training')
test_set = datagen.flow_from_directory('images',
target_size = (64, 64),
batch_size = 32,
class_mode = 'categorical',
subset = 'validation')
classifier.fit_generator(training_set,
steps_per_epoch = training_set.samples,
epochs = 10,
validation_data = test_set,
validation_steps = test_set.samples)
#predicting new image class
test_image = image.load_img(r'XXXX', target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict_proba(test_image)
print (result)
训练输出:
# Epoch 10/10
# 237/237 [==============================] - 14s 58ms/step - loss: 1.3194e-04 - acc: 1.0000 - val_loss: 0.0312 - val_acc: 0.9860
这些是我的损失/准确性。
[[1. 1. 0. 0.]]
这是预测新图像的示例结果。我什至给了我的模型高度无关的图像,这些图像仍然返回相似的结果。
我意识到237张照片是一个很小的训练池。在接下来的几个月中,我将增加图像数据库。
答案 0 :(得分:2)
要获取百分比输出,请在输出层中使用softmax激活函数以获取概率。由于梯度消失的问题,您无论如何都不应该使用S型激活函数。
classifier = Sequential()
classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation =
'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Dropout(0.25))
classifier.add(Flatten())
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 64, activation = 'relu'))
classifier.add(Dense(units = 4, activation = 'softmax'))
另一个可能的原因可能是数据缩放。有关解决方案,请参阅Returning probabilities in a classification prediction in Keras?。