如何获取概率/置信作为使用keras在python CNN输出?

问题描述 投票:-1回答:3

所以,我是新来深学习,我已经开始与猫,狗数据集使用Keras CNN的模型。

在我的代码,我无法得到概率为两classifier.predictclassifier.predict_proba输出。我刚开始输出作为[[0,1]][[1,0]]。我试过几个图像。

但是我正在寻找类似的东西,[[0.4,0.6]][[0.89,0.11]]

我试着改变从binary_crossentropy损失函数categorical_crossentropy

我试着改变从sigmoid输出层softmax的激活功能。

我也试着从class_mode改变flow_from_directorybinarycategorical

我想我可能会错误的数据类型,输出数组的类型是FLOAT32。但即使是错误的,我不知道该怎么虽然改变它。

我无法找到我要去哪里错了。请澄清/帮助。谢谢。

为什么我需要的概率?

在我的另一个项目,我会分裂的图像成更小的碎片“N”数字。然后我会分开使用的块“N”数字的分类,并找到一件与概率最大。对于这一点,我不会用猫,狗的数据集,虽然。这对滨采摘以及该数据集也将作为“YES”或“NO”二进制输出。这个任何建议也欢迎。谢谢。

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)
python tensorflow keras neural-network conv-neural-network
3个回答
0
投票

我想我找到了错误。您与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。


0
投票

我正在寻找类似的东西,[0.4,0.6],[0.89,0.11]

classifier.predict是你应该用它来获得概率方法。你能再次检查,考虑下面的提示?

有两种方法来建立一个二元分类:

  1. NN与乙状结肠激活一个输出神经元。输出a被解释为概率为1类,从而为2类的概率是1-a
  2. NN使用SOFTMAX激活两个输出神经元。然后,每个神经元被解释为一个类的概率。

两者都是有效的选项,但因为你正在做2.你应该使用SOFTMAX激活。

我试图从binary_crossentropy到categorical_crossentropy变化的损失函数。

这不应该有所作为,这是基本相同的公式。

我想我可能会错误的数据类型,输出数组的类型是FLOAT32。但即使是错误的,我不知道该怎么虽然改变它。

这也是不错误的原因,由于类型float32是正确的概率的输出。


0
投票

无论哪种预测()或predict_generator()会工作。

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(test_image2)

print(luna)

如果你想要在“N”的图像预测概率(或“N”的形象,你的情况的子集),你可以尝试predict_generator():

from keras.preprocessing.image import ImageDataGenerator

test_datagen=ImageDataGenerator(rescale=1./255)
test_set = test_datagen.flow_from_directory('dataset/test_set',
                                             target_size=(64,64),
                                             batch_size=32,
                                             class_mode='categorical')

predicted_probabilities = classifier.predict_generator(test_set)
print(predicted_probabilities)

使用以下的四舍五入至小数点后2位比例进行打印:

print(np.round(luna*100,2))
print(np.round(predicted_probabilities*100,2))

让我知道这对你的作品!

© www.soinside.com 2019 - 2024. All rights reserved.