简体   繁体   English

Keras CNN只能预测一个班级

[英]Keras CNN predicts only a single class

I've been trying to do some basic facial recognition using Keras, but I always get the same label for some reason. 我一直在尝试使用Keras进行一些基本的面部识别,但是由于某些原因,我总是得到相同的标签。 I am training it on 2 classes, Person A and Person B. 我正在A类和B类两个班上对其进行培训。

After its own validation testing, I input images that were neither in the training nor the validation set but it always gives me the same label. 经过自己的验证测试后,我输入的图像既不在训练中也不在验证集中,但始终为我提供相同的标签。 I have reduced the number of validation samples in this case for the sake of time, but even with more validation samples it doesn't appear to work. 为了节省时间,在这种情况下,我减少了验证样本的数量,但是即使有更多的验证样本,它似乎也不起作用。 I have tried it with binary and sigmoid, but that doesn't help me either. 我已经用二进制和sigmoid进行了尝试,但这也无济于事。

My accuracy often jumps up to 90% and even 100% at times, and my loss is around 0.05 to 0.1. 我的准确度经常有时会跳升到90%甚至100%,而我的损失大约是0.05到0.1。 Person A has 809 images while Person B has 777 images for training. 人员A具有809张图像,而人员B具有777张图像进行训练。

The end goal I need is to output probabilities of the image belonging to a certain class, and I would greatly appreciate any help since I am new to this field. 我需要的最终目标是输出属于某个类别的图像的概率,由于我是该领域的新手,所以我将不胜感激。 Thanks! 谢谢!

from keras.preprocessing.image import ImageDataGenerator
from keras import backend as K
import keras
from keras.models import Sequential
from keras.layers import Convolution2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
K.set_image_dim_ordering('th')
import tensorflow as tf
config = tf.ConfigProto( device_count = {'GPU': 1 , 'CPU': 56} )
sess = tf.Session(config=config)
keras.backend.set_session(sess)
K.tensorflow_backend._get_available_gpus()

# Setting the image size
img_width, img_height = 250, 250

train_data_dir = 'data/train2'
validation_data_dir = 'data/validation2'
datagen = ImageDataGenerator(rescale=1./255)

# Train and Test Generators
train_generator = datagen.flow_from_directory(
        train_data_dir,
        target_size=(img_width, img_height),
        batch_size=32,
        class_mode = 'categorical')

validation_generator = datagen.flow_from_directory(
        validation_data_dir,
        target_size=(img_width, img_height),
        batch_size=32,
        class_mode = 'categorical')


#The actual CNN
model = Sequential()
model.add(Convolution2D(32, 3, 3, input_shape=(3, img_width, img_height)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Convolution2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), data_format="channels_first", strides=(2, 2)))


model.add(Convolution2D(64, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), data_format="channels_first", strides=(2, 2)))

model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(2))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['categorical_accuracy'])

#Setting train conditions
nb_epoch = 50
nb_train_samples = 1586
nb_validation_samples = 122
print(validation_generator.class_indices)

model.fit_generator(
        train_generator,
        samples_per_epoch=nb_train_samples,
        nb_epoch=nb_epoch,
        validation_data=validation_generator,
        nb_val_samples=nb_validation_samples)

#Saving of the model
print(model.evaluate_generator(validation_generator, nb_validation_samples))
model.save_weights('my_ML_model3_weights.h5')
model.save('my_ML_model3.h5')
print("Done")

This is how I feed extra images to it for testing. 这就是我向其提供额外图像以进行测试的方式。

import cv2
from keras.models import load_model
import numpy as np

img_width, img_height = 250, 250
x = 1

while x < 6:
        img_width, img_height = img_width, img_height
        img = cv2.imread('D:\FaceRecog\Pictures\Person1 (%s).jpg' %(x))
        img = cv2.resize(img, (img_width, img_height))
        img = img.reshape(3, img_width, img_height)
        model = load_model("my_ML_model3.h5")
        model = model.predict(img[None, :, :, :])
        print(model)
        print('-----------')
        x += 1

You use scaling in your training and validation data: 您可以在培训和验证数据中使用缩放:

datagen = ImageDataGenerator(rescale=1./255) 数据生成= ImageDataGenerator(重新缩放= 1./255)

But I cannot find any scaling of your test data. 但是我找不到您的测试数据的任何缩放比例。 That might be an issue, because the network would be trained on completely different input ranges. 这可能是一个问题,因为将在完全不同的输入范围上训练网络。 In general I would advice you to construct a similar image generator for your test images as for your training and validation data and see whether the problem persists. 通常,我建议您为测试图像和训练数据以及验证数据构建类似的图像生成器,并查看问题是否仍然存在。

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

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