简体   繁体   English

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

[英]Gender detection and age classification using deep learning

I am working on a project where the goal is to detect gender and classify image.我正在做一个项目,其目标是检测性别和分类图像。 I did some research and found out a research paper: AgeandGenderClassificationusingConvolutionalNeuralNetworks by Gil Levi and Tal Hassnar.我做了一些研究,发现了一篇研究论文:Gil Levi 和 Tal Hassnar 的 AgeandGenderClassificationusingConvolutionalNeuralNetworks。 I tried to replicate the deep network created by them, originally in Caffe, in Keras.我试图复制他们创建的深层网络,最初是在 Caffe 中,在 Keras 中。 But the problem is the model is stuck at 50% accuracy(basically random coin toss).但问题是模型的准确率停留在 50%(基本上是随机抛硬币)。 What is it that i am doing wrong.我做错了什么。 Any help is much appreciated.任何帮助深表感谢。 BTW i am using the adience data set as the original paper.顺便说一句,我使用 adience 数据集作为原始论文。 PS: I have completely removed the LRN Layers as they are not available in Keras. PS:我已经完全删除了 LRN 层,因为它们在 Keras 中不可用。 ( I think their absence should not hurt the accuracy of the model ) Here is the code. (我认为他们的缺席不应该影响模型的准确性)这是代码。

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

deepface package for python offers out-of-the-box function for age and gender prediction. 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"])

It can also analyze emotion and ethnicity as well.它还可以分析情绪和种族。 You can drop unnecessary actions.您可以删除不必要的操作。

There was a problem in my approach.我的方法有问题。 I was not cropping the faces.我没有裁剪脸。 And hence the model could not make sense of the random background in every image.因此该模型无法理解每张图像中的随机背景。

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

相关问题 如何使用深度学习基于全身识别性别? - How to recognition gender based on the whole body using Deep learning? 使用深度学习的小物体检测 - Small object detection using deep learning 1 通道/灰度图像,用于使用深度学习/cnn 检测 object - 1 Channel / grey images for object detection using deep learning/cnn 如何使用tflearn深度学习进行文档分类 - How to use tflearn deep learning for document classification 将自定义数据集添加到快速文本分类深度学习中 - add custom dataset into fasttext classification deep learning 使用深度学习防止在多类分类中过度拟合特定类 - Prevent overfitting to specific class in multi-class classification using deep learning 适用于多级分类的适当深度学习结构 - Appropriate Deep Learning Structure for multi-class classification 我试图将图像预测到我的预训练深度学习分类模型 - I tried to predict an image to my pretrained deep learning classification model Keras 深度学习 IMDB 评论分类——如何添加随机评论? - Keras Deep Learning IMDB review classification - how to add random review? 是否应使用深度学习分类对细节进行分类,例如瓶中的液位 - Should deep learning classification be used to classify details such as liquid level in bottle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM