如何在python中使用keras获取CNN的概率/置信度输出? [英] How do I get probability/confidence as output for a CNN using keras in python?
问题描述
因此,我是深度学习的新手,我从使用Keras的CNN模型的猫和狗数据集开始。
So, I'm new to deep learning and I've started with cats and dogs dataset for a CNN Model using Keras.
在我的代码中,我无法获得概率作为 classifier.predict
或 classifier.predict_proba
的输出。我只是将输出显示为 [[0,1]]
或 [[1,0]]
。我尝试了几种图像。
In my code, I'm unable to get probabilities as output for both classifier.predict
or classifier.predict_proba
. I'm just getting the output as [[0,1]]
or [[1,0]]
. I've tried with several images.
但是我正在寻找类似的东西, [[0.4,0.6]]
, [[0.89,0.11]]
But I'm looking for something like, [[0.4,0.6]]
, [[0.89,0.11]]
我尝试从 binary_crossentropy $ c $更改损失函数c>更改为
categorical_crossentropy
。
我尝试将输出层的激活功能从 sigmoid
更改为 softmax
。
I've tried changing the activation function of the output layer from sigmoid
to softmax
.
我也尝试过更改 class_mode $
flow_from_directory
中的c $ c>从 binary
到 categorical
。
I've also tried changing the class_mode
in flow_from_directory
from binary
to categorical
.
我认为数据类型可能会出错,因为输出数组的类型是float32。但是即使那是错误,我也不知道如何更改。
我无法找到我要去哪里。请澄清/帮助。谢谢。
I'm unable to find where I'm going wrong. Please clarify/help. Thanks.
为什么我需要概率?
Why do I need probabilities?
在另一个项目中,我将图像拆分为小件的数量为n。然后,我将在n个零件上分别使用分类器,并找到概率最大的零件。为此,我不会使用猫和狗的数据集。这是用于bin-picking,该数据集也将以 YES或 NO的形式输出为二进制。也欢迎对此提出任何建议。谢谢。
In my another project, I'll be splitting an image into 'n' number of smaller pieces. I'll then use the classifier on 'n' number of pieces separately and find the one piece with the largest probability. For this, I won't use the dataset of cats and dogs though. It's for bin-picking and that dataset will also be binary output as 'YES' or 'NO'. Any suggestions for this is also welcome. Thanks.
链接来访问Github中的代码。
Link for the code in Github.
#Building the CNN
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
#Initialising the CNN
classifier = Sequential()
#Step 1 - Convolution
classifier.add(Convolution2D(filters=32,kernel_size=[3,3],input_shape=(64,64,3),activation='relu'))
#Step 2 - Pooling
classifier.add(MaxPooling2D(pool_size=(2,2),strides=2))
#Adding another Convolutional Layer for better accuracy
#classifier.add(Convolution2D(filters=32,kernel_size=[3,3],activation='relu'))
#classifier.add(MaxPooling2D(pool_size=(2,2),strides=2))
#Step 3 - Flattening
classifier.add(Flatten())
#Step 4 - Fully Connected Layers
classifier.add(Dense(units= 64, activation='relu'))
classifier.add(Dense(units= 2, activation='softmax'))
#Compiling the CNN
classifier.compile(optimizer='adam',loss = 'categorical_crossentropy', metrics=['accuracy'])
#Part 2 - Fitting the CNN to the images
from keras.preprocessing.image import ImageDataGenerator
train_datagen=ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen=ImageDataGenerator(rescale=1./255)
training_set = train_datagen.flow_from_directory('dataset/training_set',
target_size=(64,64),
batch_size=32,
class_mode='categorical')
test_set = test_datagen.flow_from_directory('dataset/test_set',
target_size=(64,64),
batch_size=32,
class_mode='categorical')
classifier.fit_generator(training_set,
steps_per_epoch=250,
epochs=3, #Just for time being I've kept very few epochs.
validation_data=test_set,
validation_steps=62)
#Making new Predictions
import numpy as np
from keras.preprocessing import image
test_image_luna=image.load_img('dataset/single/SkilletLuna.JPG',target_size=(64,64))
test_image2=image.img_to_array(test_image_luna)
test_image2=np.expand_dims(test_image2,axis=0)
luna=classifier.predict_proba(test_image2)
In [11]: luna
...:
Out[11]: array([[0., 1.]], dtype=float32)
推荐答案
我认为我发现了错误。您正在使用 ImageDataGenerator
重新缩放训练和测试数据。但是在测试单个图像时,您没有这样做。
尝试以下操作:
I think I found the mistake. You are rescaling your train and test data with the ImageDataGenerator
. But you are not doing that when testing a single image.
Try this:
# Making new Predictions
import numpy as np
from keras.preprocessing import image
test_image_luna = image.load_img('D:\\NR\\data\\live2013\\caps.bmp', target_size=(64,64))
test_image2 = image.img_to_array(test_image_luna)/255.
test_image2 = np.expand_dims(test_image2, axis=0)
luna = classifier.predict_proba(test_image2)
高输入值导致非常高的输出值。由于您使用的是softmax激活,因此这些值会导致预测非常接近0和1。
The high input values lead to very high output values. Since you are using softmax activation these values are leading to predictions very close to 0 and 1.
这篇关于如何在python中使用keras获取CNN的概率/置信度输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!