我的模型一直在预测同一张脸,我该如何解决?

问题描述 投票:4回答:1

我用CNN训练了一个模型,并用opencv对网络摄像头进行实时人脸识别。我的模型出现了一些问题,比如我在训练的时候得到了100%的准确率,我知道一定是出了什么问题,另外一个问题是,当它在网络摄像头图像上预测时,无论我显示谁的脸,它只给我一个标签:无论我显示我的脸还是其他人的脸,它都会给出一个 "我 "的标签。

注意,我的数据集里只有250张图片。200张图片用于训练,50张图片用于测试。对于每一个集子,我把它们分成2个类,"me "和 "not_me",其中 "me "文件夹包含我的脸的图像,"not_me "文件夹包含不同人的脸的图像。

是模型的数据不够用还是有其他问题?

请大家帮忙,让我能从我的错误中吸取教训。先谢谢你。

myface_model.py

# Importing the Keras libraries and packages
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
from keras.preprocessing.image import ImageDataGenerator

# Initialising 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'))

# output layer
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
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 = 'binary')

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

classifier.fit_generator(training_set,
                         steps_per_epoch = 200,
                         epochs = 25,
                         validation_data = test_set,
                         validation_steps = 50)

#save model for later use
fer_json = classifier.to_json()
with open("fer.json", "w") as json_file:
    json_file.write(fer_json)
classifier.save_weights("fer.h5")

myface_detector.py

import os
import cv2
import numpy as np
from keras.models import model_from_json
from keras.preprocessing import image

#load model
classifier = model_from_json(open("fer.json", "r").read())
#load weights
classifier.load_weights('fer.h5')

face_haar_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

cap=cv2.VideoCapture(0)

while True:
    # captures frame and returns boolean value and captured image
    ret,test_img=cap.read()

    if not ret:
        continue

    convt_img= cv2.cvtColor(test_img, cv2.COLOR_BGR2RGB)

    faces_detected = face_haar_cascade.detectMultiScale(convt_img, 1.3, 5)

    for (x,y,w,h) in faces_detected:
        cv2.rectangle(test_img, (x,y), (x+w,y+h), (255,0,0), thickness=4)
        roi = convt_img[y:y+h,x:x+w]
        roi = cv2.resize(roi,(64,64))
        roi = roi.astype("float") / 255.0
        img_pixels = image.img_to_array(roi)
        img_pixels = np.expand_dims(img_pixels, axis = 0)

        predictions = classifier.predict(img_pixels)

        #find max indexed array
        max_index = np.argmax(predictions[0])

        myFace = ('me', 'not_me')

        predicted_myFace = myFace[max_index]

        cv2.putText(test_img, predicted_myFace, (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)

    cv2.imshow('my face predictions',test_img)

    if cv2.waitKey(10) == ord('q'):#wait until 'q' key is pressed
        break

cap.release()
cv2.destroyAllWindows()
python tensorflow machine-learning keras deep-learning
1个回答
1
投票

二进制类

所以,问题的关键来自于我们选择如何表述我们的模型。具体来说在这几行。

classifier.add(Dense(units = 1, activation = 'sigmoid'))

和...

training_set = train_datagen.flow_from_directory('dataset/training_set',
                                                 target_size = (64, 64),
                                                 batch_size = 32,
                                                 class_mode = 'binary') #<--------

test_set = test_datagen.flow_from_directory('dataset/test_set',
                                            target_size = (64, 64),
                                            batch_size = 32,
                                            class_mode = 'binary') #<-------------

这里我们选择给我们的CNN一个输出节点,并且有二进制的类标签。这意味着 predict 方法的内容是这样的 [[0.12956393]]. 这个值代表了测试图像具有类标签1的预测概率(在本例中,标签为'not_me')。要提取类标签,我们可以简单地做。

predictions = classifier.predict(img_pixels)
predicted_label = predictions[0][0] > 0.5
myFace = ('me', 'not_me')
predicted_myFace = myFace[predicted_label]

然而,为了遵循你的原始代码的意图,根据最高概率选择类标签,并允许我们扩展这个应用程序,以便在需要时轻松预测更多的类,我们只需要对模型进行小的修改,以使用分类类标签。

分类类

我们可以使用分类标签来代替二进制标签,这将允许我们使用0...n的类标签。这将允许我们使用0...n的类标签。在这种情况下,我们只需进行以下编辑

classifier.add(Dense(units = 2, activation = 'sigmoid')) # note that units = 2 now

training_set = train_datagen.flow_from_directory(
    'dataset/training_set',
    target_size = (64, 64),
    batch_size = 32,
    class_mode = 'categorical') # note the new class mode

test_set = test_datagen.flow_from_directory(
    'dataset/test_set',
    target_size = (64, 64),
    batch_size = 32,
    class_mode = 'categorical') # note the new class mode

通过给我们的CNN提供两个输出节点,并将类模式从二进制改为分类,我们的CNN现在能够输出每个类标签的预测概率,即返回值的 predict 方法会看起来像:

[[0.012211  0.9917401]]

这意味着你在myface_detector.py中的代码将按原样工作。

热门问题
推荐问题
最新问题