简体   繁体   English

cnn model 用于二进制分类,86% val_accuracy 总是返回 1

[英]cnn model for binary classification with 86% val_accuracy always returning 1

I created a CNN model for binary classification.我为二进制分类创建了一个 CNN model。 I used a balanced database of 300 images.我使用了一个包含 300 张图像的平衡数据库。 I know it's a small database but I used data augmentation.我知道这是一个小型数据库,但我使用了数据增强。 After fitting the model I got 86% val_accuracy on the validation set, but when I wanted to print the probability for each picture, I got probability 1 for most pictures from the first class and even all probabilities are >0.5, and probability 1 for all images from the second class.在拟合 model 后,我在验证集上得到了 86% 的 val_accuracy,但是当我想打印每张图片的概率时,我从第一张 class 中得到大多数图片的概率 1,甚至所有概率都 > 0.5,概率为 1来自第二个 class 的图像。

This is my model这是我的 model

model = keras.Sequential([
layers.InputLayer(input_shape=[128, 128, 3]),

preprocessing.Rescaling(scale=1/255),
preprocessing.RandomContrast(factor=0.10),
preprocessing.RandomFlip(mode='horizontal'),
preprocessing.RandomRotation(factor=0.10),

layers.BatchNormalization(renorm=True),
layers.Conv2D(filters=64, kernel_size=3, activation='relu', padding='same'),
layers.MaxPool2D(),

layers.BatchNormalization(renorm=True),
layers.Conv2D(filters=128, kernel_size=3, activation='relu', padding='same'),
layers.MaxPool2D(),

layers.BatchNormalization(renorm=True),
layers.Conv2D(filters=256, kernel_size=3, activation='relu', padding='same'),
layers.Conv2D(filters=256, kernel_size=3, activation='relu', padding='same'),
layers.MaxPool2D(),

layers.BatchNormalization(renorm=True),
layers.Flatten(),
layers.Dense(8, activation='relu'),
layers.Dense(1, activation='sigmoid'),])

This is the accuracy plot这是精度 plot

准确度图

Thank you guys感谢你们

I think the preprocessor function scales the pixel values between -1 and +1.我认为预处理器 function 在 -1 和 +1 之间缩放像素值。 However you rescaled your images for training between 0 and 1. try replacing但是,您将图像重新缩放以在 0 和 1 之间进行训练。尝试替换

image = preprocess_input(image)

with

image=image/255

see if that works看看这是否有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM