Last active
December 29, 2019 08:45
-
-
Save victoriastuart/d82c94219d398d2c4252a174b1b3f9e9 to your computer and use it in GitHub Desktop.
Caffe - age, gender CNN with image crop ...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
"""AGE AND GENDER CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORKS - DEMO | |
This code is originates with the paper: | |
Gil Levi and Tal Hassner, "Age and Gender Classification Using Convolutional Neural Networks," | |
IEEE Workshop on Analysis and Modeling of Faces and Gestures (AMFG), at the IEEE Conf. on | |
Computer Vision and Pattern Recognition (CVPR), Boston, June 2015 | |
http://www.cv-foundation.org/openaccess/content_cvpr_workshops_2015/W08/html/Levi_Age_and_Gender_2015_CVPR_paper.html | |
http://www.cv-foundation.org/openaccess/content_cvpr_workshops_2015/W08/papers/Levi_Age_and_Gender_2015_CVPR_paper.pdf | |
Age and Gender Classification using Deep Convolutional Neural Networks | Gil's CV blog | |
https://gilscvblog.com/2015/11/19/age-and-gender-classification-using-deep-convolutional-neural-networks/ | |
------------------------------------------------------------------------------ | |
PDF: | |
http://www.openu.ac.il/home/hassner/projects/cnn_agegender/CNN_AgeGenderEstimation.pdf | |
GitHub: | |
GilLevi/AgeGenderDeepLearning | |
https://github.com/GilLevi/AgeGenderDeepLearning | |
Age and Gender Classification using Convolutional Neural Networks | |
{ Age Classification CNN | Gender Classification CNN } | |
https://gist.github.com/GilLevi/c9e99062283c719c03de | |
GitXiv: | |
http://www.gitxiv.com/posts/sKTDSvwzoiRaojLG6/age-and-gender-classification-using-convolutional-neural | |
Project page: | |
Age and Gender Classification using Convolutional Neural Networks | |
http://www.openu.ac.il/home/hassner/projects/cnn_agegender/ | |
""" | |
# ---------------------------------------------------------------------------- | |
# CROP IMAGES VIA DLIB: | |
# --------------------- | |
# Source: vigneshmahalingam via Gil Levi (pers. comm., indicated); posted at | |
# https://github.com/GilLevi/AgeGenderDeepLearning/issues/4 | |
# Edited by Victoria Stuart | |
# Environment: Python 2.7 venv: {Caffe '1.0.0-rc3' | Dlib '19.2.99'} installed ... | |
# ---------------------------------------------------------------------------- | |
# PYTHON 2 FUTURE PRINT FROM PYTHON 3: | |
# ------------------------------------ | |
# must appear at top of script: | |
from __future__ import print_function | |
# ---------------------------------------------------------------------------- | |
# CAFFE VERBOSITY (WARNINGS): | |
# --------------------------- | |
# Place near top of script, BEFORE "import caffe" statement: | |
import os | |
os.environ['GLOG_minloglevel'] = '2' | |
# 0: debug; 1:info | 2:warnings; 3: errors | |
#os.environ['GLOG_minloglevel'] = '2' | |
# ---------------------------------------------------------------------------- | |
# (REMAINING) IMPORT STATEMENTS: | |
# ------------------------------ | |
import caffe, dlib, io | |
import matplotlib.pyplot as plt | |
from skimage import io | |
# ---------------------------------------------------------------------------- | |
# LOAD THE MEAN IMAGE: | |
# -------------------- | |
mean_filename='/mnt/Vancouver/apps/AgeGenderDeepLearning/cnn_age_gender_models_and_data.0.0.2/mean.binaryproto' | |
proto_data = open(mean_filename, "rb").read() | |
a = caffe.io.caffe_pb2.BlobProto.FromString(proto_data) | |
mean = caffe.io.blobproto_to_array(a)[0] | |
# ---------------------------------------------------------------------------- | |
# LOAD THE AGE, GENDER NETWORKS: | |
# ------------------------------ | |
# NOTE: be sure to make the "/mnt/Vancouver/apps" path section per your local, | |
# cloned "AgeGenderDeepLearning" GitHub repo | |
age_net_pretrained='/mnt/Vancouver/apps/AgeGenderDeepLearning/cnn_age_gender_models_and_data.0.0.2/age_net.caffemodel' | |
age_net_model_file='/mnt/Vancouver/apps/AgeGenderDeepLearning/cnn_age_gender_models_and_data.0.0.2/deploy_age.prototxt' | |
age_net = caffe.Classifier(age_net_model_file, age_net_pretrained, | |
mean=mean.mean(1).mean(1), | |
channel_swap=(2,1,0), | |
raw_scale=255, | |
image_dims=(256, 256)) | |
gender_net_pretrained='/mnt/Vancouver/apps/AgeGenderDeepLearning/cnn_age_gender_models_and_data.0.0.2/gender_net.caffemodel' | |
gender_net_model_file='/mnt/Vancouver/apps/AgeGenderDeepLearning/cnn_age_gender_models_and_data.0.0.2/deploy_gender.prototxt' | |
gender_net = caffe.Classifier(gender_net_model_file, gender_net_pretrained, | |
mean=mean.mean(1).mean(1), | |
channel_swap=(2,1,0), | |
raw_scale=255, | |
image_dims=(256, 256)) | |
# ---------------------------------------------------------------------------- | |
# LABELS: | |
# ------- | |
#age_list=['(0, 2)','(4, 6)','(8, 12)','(15, 20)','(25, 32)','(38, 43)','(48, 53)','(60, 100)'] | |
age_list=['00-02','04-06','08-12','15-20','25-32','38-43','48-53','60-100'] | |
gender_list=['Male', 'Female'] | |
# ---------------------------------------------------------------------------- | |
# CROP FACE VIA DLIB: | |
# ------------------- | |
detector = dlib.get_frontal_face_detector() | |
# Victoria: "img21" is a file (me!) on my system ;-) | |
img21 = '/home/victoria/projects/computer_vision/cnn_age_gender_demo/images_uncropped/victoria-50.jpg' | |
im_name = img21 | |
img = io.imread(os.path.join('./',im_name)) | |
faces = detector(img) | |
input_image_cropped = caffe.io.load_image(os.path.join('./', im_name)) | |
cropped_face = input_image_cropped[faces[0].top():faces[0].bottom(), faces[0].left():faces[0].right(), :] | |
h = faces[0].bottom() - faces[0].top() | |
w = faces[0].right() - faces[0].left() | |
age_prediction_cropped = age_net.predict([cropped_face]) | |
print('\n\t predicted age (Dlib-cropped image):', age_list[age_prediction_cropped[0].argmax()]) | |
gender_prediction_cropped = gender_net.predict([cropped_face]) | |
print('\tpredicted gender (Dlib-cropped image):', gender_list[gender_prediction_cropped[0].argmax()]) | |
plt.figure("cropped_face") | |
plt.imshow(cropped_face) | |
plt.imsave('cropped_face.png', cropped_face) | |
# ---------------------------------------------------------------------------- | |
# Plot figures [via "plt.figure()" statements, above]: | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Age, gender classification is understandably very challenging! Just Google [ microsoft age gender accuracy] ...
While the script works, (unless I'm doing something horribly wrong) the age / gender classifications are truly terrible! :-(
With a different version of the code, above, I've tried / compared:
... all these images (source; two different crops) give different results -- really, no apparent correlation with one another or ground truth! :-(
Ground truth: