Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Caffe - age, gender CNN with image crop ...
#!/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()
Owner

victoriastuart commented Nov 26, 2016

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:

* raw (unscaled) images

* [96x96] aligned / cropped faces from those images, using the Dlib script
  (.../openface/util/align-dlib.py) provided in my OpenFace install; and

* the Dlib code snippet above (larger images: cropped faces)

... all these images (source; two different crops) give different results -- really, no apparent correlation with one another or ground truth! :-(

Ground truth:

* Victoria: female (transsexual), age 55
* Carmine: female, age 42
Age, gender predictions: uncropped vs cropped [96x96] images
------------------------------------------------------------
IMAGE  |  PREDICTION:   AGE     GENDER
------------------------------------------------------------
example_image           00-02   female
carmine-01              08-12   female
carmine-01 (cropped)    38-43   female
carmine-02              05-20   female
carmine-02 (cropped)    38-43   female
carmine-03              45-53   female
carmine-03 (cropped)    00-02   female
carmine-04              08-12   female
carmine-04 (cropped)    08-12   female
carmine-05              00-02   female
carmine-05 (cropped)    25-32   female
carmine-06              38-43   female
carmine-06 (cropped)    38-43   female
carmine-07              08-12   female
carmine-07 (cropped)    25-32     male [misgendered]
carmine-08.             25-32   female
carmine-08 (cropped)    08-12   female
carmine-09              60-100  female
carmine-09 (cropped)    08-12     male [misgendered]
carmine-11              25-32   female
carmine-11 (cropped)    25-32   female
carmine-36              00-02   female
carmine-36 (cropped)    38-43   female
---------------------------------------
victoria-01             08-12   female
victoria-01 (cropped)   08-12   female
victoria-03             00-02   female
victoria-03 (cropped)   08-12   female
victoria-04             25-32   female
victoria-04 (cropped)   38-43   female
victoria-05             25-32   female
victoria-05 (cropped)   08-12   female
victoria-06             60-100  female
victoria-06 (cropped)   45-53   female
victoria-07             25-32   female
victoria-07 (cropped)   00-02   female
victoria-08             08-12   female
victoria-08 (cropped)   08-12   female
victoria-09             60-100  female
victoria-09 (cropped)   38-43   female
victoria-10             25-32   female
victoria-10 (cropped)   25-32   female
victoria-50             38-43     male [misgendered]
victoria-50 (cropped)   08-12   female
victoria-24             08-43     male [misgendered]
victoria-24 (cropped)   38-43   female
victoria-39             00-02   female
victoria-39 (cropped)   00-02   female
victoria-40             38-43     male [misgendered]
victoria-40 (cropped)   60-100  female
------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment