python - Keras 返回二进制结果

标签 python tensorflow machine-learning keras classification

我想预测 2 种疾病的种类,但我得到的结果是二进制的(如 1.0 和 0.0)。我怎样才能获得这些的准确性(如 0.7213)?

<小时/>

训练代码:

from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense

# Intialising the CNN
classifier = Sequential()

# Step 1 - Convolution
classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))

# Step 2 - Pooling
classifier.add(MaxPooling2D(pool_size = (2, 2)))

# Adding a second convolutional layer
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))

# Step 3 - Flattening
classifier.add(Flatten())

# Step 4 - Full connection
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))

# Compiling the CNN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

# Part 2 - Fitting the CNN to the images
import h5py

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('training_set',
                                                 target_size = (64, 64),
                                                 batch_size = 32,
                                                 class_mode = 'binary')

test_set = test_datagen.flow_from_directory('test_set',
                                            target_size = (64, 64),
                                            batch_size = 32,
                                            class_mode = 'binary')


classifier.fit_generator(training_set,
                         steps_per_epoch = 100,
                         epochs = 1,
                         validation_data = test_set,
                         validation_steps = 100)
<小时/>

单个预测代码:

import numpy as np
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img,image

test_image = image.load_img('path_to_image', target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)

print(result[0][0]) # Prints 1.0 or 0.0
# I want accuracy rate for this prediction like 0.7213
<小时/>

文件结构如下:

  • 测试集

    • 良性
      • benigne_images
    • 温柔
      • melignant_images
  • 训练集

训练集结构也与测试集相同。

最佳答案

更新:正如您在评论中澄清的那样,您正在寻找给定一个测试样本的每个类别的概率。因此你可以使用predict方法。但是,请注意,您必须首先以与训练阶段相同的方式对图像进行预处理:

test_image /= 255.0
result = classifier.predict(test_image)

结果将是给定图像属于第一类(即正类)的概率。

<小时/>

如果您有测试数据生成器,则可以使用 evaluate_generator()以获得模型在测试数据上的损失以及准确性(或您设置的任何其他指标)。

例如,在拟合模型后,即使用 fit_generator,您可以在测试数据生成器上使用 evaluate_generator,即 test_set:

loss, acc = evaluate_generator(test_set)

关于python - Keras 返回二进制结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53562493/

相关文章:

python - 为什么我的 gunicorn Python/Flask worker 退出信号项?

python - 使用 NLTK 进行文本预处理

python - Django 表单在提交之前正在验证

python - 如何在Python中对许多彩色图像进行直方图均衡?

python - 如何使用 TensorFlow reader 和 queue 同时读取两个文件?

python - 使用 tensorflow 运行线性回归时出现类型错误。 ' Op has type float64 that does not match type float32 of argument'

machine-learning - 如何添加我的神经网络可以预测的每个分类的具体数字?

language-agnostic - 是否有任何自学的声明式/归纳式编程语言可以输入预期的结果,而不是遵循的过程?

machine-learning - PyTorch 中的双向 LSTM 输出问题

python - Scrapy:蜘蛛中间件中的异步数据库请求?