我的模型一直在预测同一张脸,我该如何修复它?

2024-05-29 10:58:47 发布

您现在位置:Python中文网/ 问答频道 /正文

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

注意,我在数据集中只有250个图像:200个用于训练的图像和50个用于测试的图像。对于每一组,我将它们分为两类,“我”和“不我”,其中“我”文件夹包含我的面部图像,“不我”文件夹包含不同人群的面部图像

数据是否不足以满足模型或其他要求

请帮助我,以便我能从错误中吸取教训。谢谢

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_检测器.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()

Tags: fromtest图像importaddjsonimgsize
1条回答
网友
1楼 · 发布于 2024-05-29 10:58:47

二进制类

因此,问题的关键在于我们如何选择表示我们的模型。具体而言:

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的类标签。在这种情况下,我们只需进行以下编辑

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现在能够输出每个类标签的预测概率。i、 e.predict方法的返回值如下所示:

[[0.012211  0.9917401]]

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

相关问题 更多 >

    热门问题