所以,我是深度学习的新手,我从使用 Keras 的 CNN 模型的猫和狗数据集开始。
在我的代码中,我无法获取 classifier.predict
或 classifier.predict_proba
的输出概率。我只是得到 [[0,1]]
或 [[1,0]]
的输出。我尝试过使用几张图像。
但我正在寻找类似 [[0.4,0.6]]
、[[0.89,0.11]]
我尝试将损失函数从 binary_crossentropy
更改为 categorical_crossentropy
。
我尝试将输出层的激活函数从 sigmoid
更改为 softmax
。
我还尝试将 flow_from_directory
中的 class_mode
从 binary
更改为 categorical
。
我认为数据类型可能出错,因为输出数组的类型是 float32。但即使这是错误,我也不知道如何更改它。
我找不到哪里出错了。请澄清/帮助。谢谢。
Why do I need probabilities?
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.
Link代码见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
重新调整训练和测试数据。但在测试单个图像时您并没有这样做。
试试这个:
# 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。
关于python - 如何在 python 中使用 keras 获得概率/置信度作为 CNN 的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54513742/