使用深度学习进行性别检测和年龄分类

时间:2019-08-14 23:37:05

标签: tensorflow keras deep-learning classification conv-neural-network

我正在一个项目中,目标是检测性别并对图像进行分类。我做了一些研究,发现了一篇研究论文:Gil Levi和Tal Hassnar所著的使用卷积神经网络进行年龄和性别分类。我试图复制他们创建的深层网络,最初是在Keras的Caffe中。但是问题是模型卡住了50%的精度(基本上是随机抛硬币)。我做错了什么。任何帮助深表感谢。 顺便说一句,我正在使用aistence数据集作为原始纸张。 PS:我已经完全删除了LRN层,因为Keras中不提供它们。 (我认为他们的缺席不应该损害模型的准确性) 这是代码。

#imports
import os
import numpy as np
from PIL import Image
import pickle
from keras.models import Sequential 
from keras.callbacks import  ModelCheckpoint
from keras.layers import Dense , Conv2D , Flatten , MaxPooling2D , Dropout , AveragePooling2D
from keras import initializers
from keras import optimizers

# creating the model object
gender_model = Sequential()

# adding layers to the model
# first convolutional layer
gender_model.add( Conv2D(96 , kernel_size=(7,7) , activation='relu' , strides=4 , input_shape=(227,227,3),
                            kernel_initializer= initializers.random_normal(stddev=0.01), use_bias = 1,
                            bias_initializer = 'Zeros' , data_format='channels_last'))

gender_model.add( MaxPooling2D(pool_size=3 , strides=2) )

gender_model.add( Conv2D(256, kernel_size=(5,5) , activation='relu', strides=1 , padding='same' , input_shape=(27,27,96), 
                            kernel_initializer= initializers.random_normal(stddev=0.01) , use_bias=1,
                            bias_initializer='Ones' , data_format='channels_last') )
gender_model.add( MaxPooling2D(pool_size=3 , strides=2) )
# third convolutional layer

gender_model.add( Conv2D(384,kernel_size=(3,3) , activation='relu', strides=1 ,padding='same', input_shape=(13,13,256),
                    kernel_initializer= initializers.random_normal(stddev=0.01), use_bias=1,
                    bias_initializer = 'Zeros' , data_format='channels_last') )
gender_model.add( MaxPooling2D(pool_size=3 , strides=2) )

# Now we flatten the output of last convolutional layer
gender_model.add( Flatten() )

# Now we connect the fully connected layers
gender_model.add( Dense(512, activation='relu' , use_bias=1, kernel_initializer=initializers.random_normal(stddev=0.005),
                    bias_initializer='Ones') )
gender_model.add( Dropout(0.5))

# connecting another fully connected layer
gender_model.add( Dense(512 , activation='relu' , use_bias=1, kernel_initializer=initializers.random_normal(stddev=0.005),
                    bias_initializer='Ones'))
gender_model.add( Dropout(0.5))

# connecting the final layer
gender_model.add( Dense(2, activation='softmax' , use_bias=1, kernel_initializer=initializers.random_normal(stddev=0.01),
                    bias_initializer='Zeros'))

# compiling the model
sgd_optimizer = optimizers.SGD(lr= 0.0001 , decay=1e-7 , momentum=0.0, nesterov=False)
gender_model.compile(optimizer=sgd_optimizer , loss= 'categorical_crossentropy' , metrics=['accuracy'])
gender_model.summary()


# partioning the loaded data
X = np.load('/content/drive/My Drive/X.npy')
y = np.load('/content/drive/My Drive/y_m.npy')

X_train = X[:15000]
y_train = y[:15000]

X_val = X[15000:] 
y_val = y[15000:]

## creating chkpt path
chkpt_path = 'weights-improvement-{epoch:02d}--{val_acc:.2f}.hdf5'

checkpoint = ModelCheckpoint(chkpt_path , monitor='val_acc' , verbose=1 , save_best_only=True , mode='max')
callback_list = [checkpoint]

#finally training the model
gender_model.fit(X_train, y_train,
          batch_size=50,
          epochs=100,
          validation_data=(X_val , y_val),
          shuffle=1,
          callbacks = callback_list
          )

2 个答案:

答案 0 :(得分:1)

适用于python的deepface软件包提供了开箱即用的年龄和性别预测功能。

!pip install deepface
from deepface import DeepFace
obj = DeepFace.analyze("img1.jpg", actions = ["age", "gender", "emotion", "race"])
print(obj["age"], " years old ", obj["gender"])

它也可以分析情感和种族。您可以删除不必要的操作。

答案 1 :(得分:0)

我的方法出现问题。我没有修剪脸。因此,该模型无法理解每张图像中的随机背景。