[PYTHON] Caffe Model Zoo for beginners [Age and gender classification]

I wanted to run caffe in anaconda environment, but it didn't work even if I changed Makefile.config. Make a note of the build procedure in an environment that does not use version control. If you make a mistake, I would appreciate it if you could contact me.

Installation on ubuntu

General dependencies

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev

14.04

sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

Click here for other versions http://caffe.berkeleyvision.org/install_apt.html

Click here for other OS http://caffe.berkeleyvision.org/installation.html

Get the environment

git clone https://github.com/BVLC/caffe.git

Makefile.config settings

cd caffe
cp Makefile.config.example Makefile.config

Makefile.config



# USE_CUDNN := 1
↓
USE_CUDNN := 1

#cuda modified to suit your environment
CUDA_DIR := /usr/local/cuda-7.0

#If I wanted to do it with anaconda, I should be able to change this, but it didn't work in my environment.
ANACONDA_HOME := $(HOME)/anaconda
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
                $(ANACONDA_HOME)/include/python2.7 \
                $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include

PYTHON_LIB := $(ANACONDA_HOME)/lib


compile

cmake .
make -j4 all
make install

If you want to recompile

make clean and make -j4 all make clean: Deletes the intermediate file created when creating an application and the application itself as a result of creation.

python preferences

python compilation etc.

sudo apt-get install python-dev python-pip python-numpy python-skimage gfortran
sudo pip install -r ~/caffe/python/requirements.txt
make pycaffe

Pass through

export PYTHONPATH=/home/ubuntu/caffe/build/caffe/python/:$PYTHONPATH

OK if you can import

python
>>>import caffe

Set the path

.badhrc


export CAFFE_HOME=Where you installed caffe
export PATH=${CAFFE_HOME}/bin:${PATH}
export LD_LIBRARY_PATH=${CAFFE_HOME}/lib:${LD_LIBRARY_PATH}
export PYTHONPATH=${CAFFE_HOME}/python:${PYTHONPATH}

※Caution Even with this, classify.py may not work. http://qiita.com/Bonnnou_108/items/41e6dadeff1310b4eb5d

If you follow the yahoo article, it should work, but you may get an error. https://techblog.yahoo.co.jp/programming/caffe-intro/

python classify.py --raw_scale 255 ../101_ObjectCategories/airplanes/image_0001.jpg ../result.npy

Details of the error

ValueError: Mean shape incompatible with input shape

Edit here caffe/python/caffe/io.py

if ms != self.inputs[in_][1:]:
    raise ValueError('Mean shape incompatible with input shape.')

if ms != self.inputs[in_][1:]:
    print(self.inputs[in_])
    in_shape = self.inputs[in_][1:]
    m_min, m_max = mean.min(), mean.max()
    normal_mean = (mean - m_min) / (m_max - m_min)
    mean = resize_image(normal_mean.transpose((1,2,0)),in_shape[1:]).transpose((2,0,1)) * (m_max - m_min) + m_min
    #raise ValueError('Mean shape incompatible with input shape.')

Caffe Model Zoo

Age and gender classification model

emotion dataset

http://www.ics.uci.edu/~xzhu/face/ http://www.openu.ac.il/home/hassner/Adience/data.html

age_net.caffemodel ... age classification model deploy_age.prototxt ... Age classification numbers and labels mean.binaryproto ... for average images gender_net.caffemodel ... Gender classification model deploy_gender.prototxt ... Gender classification numbers and labels

download model

wget http://www.openu.ac.il/home/hassner/projects/cnn_agegender/cnn_age_gender_models_and_data.0.0.1.zip
unzip cnn_age_gender_models_and_data.0.0.1.zip

import

import os
import numpy as np
import matplotlib.pyplot as plt

caffe_root = './caffe-master/' 
import sys
sys.path.insert(0, caffe_root + 'python')
import caffe

plt.rcParams['figure.figsize'] = (10, 10)
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

Loading mean image

mean_filename='./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]

ダウンロード.png

Loading age network

age_net_pretrained='./age_net.caffemodel'
age_net_model_file='./deploy_age.prototxt'
age_net = caffe.Classifier(age_net_model_file, age_net_pretrained,
                       mean=mean,
                       channel_swap=(2,1,0),
                       raw_scale=255,
                       image_dims=(256, 256))

Loading gender network

gender_net_pretrained='./gender_net.caffemodel'
gender_net_model_file='./deploy_gender.prototxt'
gender_net = caffe.Classifier(gender_net_model_file, gender_net_pretrained,
                       mean=mean,
                       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)']
gender_list=['Male','Female']

Loading and displaying images

example_image = './example_image.jpg'
input_image = caffe.io.load_image(example_image)
_ = plt.imshow(input_image)

Age forecast

prediction = age_net.predict([input_image]) 

print 'predicted age:', age_list[prediction[0].argmax()]

Output result: predicted age: (0, 2)

Gender prediction

prediction = gender_net.predict([input_image]) 

print 'predicted gender:', gender_list[prediction[0].argmax()]

Output result: predicted gender: Female

Filter visualization

In CNN, weights are called filters.

def showimage(im):
    if im.ndim == 3:
        im = im[:, :, ::-1]
    plt.set_cmap('jet')
    plt.imshow(im)
    

def vis_square(data, padsize=1, padval=0):
    data -= data.min()
    data /= data.max()
    
    #Force the number of filters to be square
    n = int(np.ceil(np.sqrt(data.shape[0])))
    padding = ((0, n ** 2 - data.shape[0]), (0, padsize), (0, padsize)) + ((0, 0),) * (data.ndim - 3)
    data = np.pad(data, padding, mode='constant', constant_values=(padval, padval))
    
    #Tile the filter to the image
    data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1)))
    data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:])
    
    showimage(data)

Load gender network without average image for better visualization

age_net = caffe.Classifier(age_net_model_file, age_net_pretrained,
                       channel_swap=(2,1,0),
                       raw_scale=255,
                       image_dims=(256, 256))

prediction = age_net.predict([input_image]) 

Enter image

Functions that start with input or _ may appear. In python, _ may be used to mean that it can be referenced from an external class but not referenced.

_ = plt.imshow(input_image)

First layer filter conv1

filters = age_net.params['conv1'][0].data[:49]
vis_square(filters.transpose(0, 2, 3, 1))

ダウンロード (1).png

First layer output conv1

feat = age_net.blobs['conv1'].data[4, :49]
vis_square(feat, padval=1)

ダウンロード (2).png

Fine-tuning with Flickr images

Move to Forta with caffe

cd ~/caffe

Download trained model

scripts/download_model_binary.py models/bvlc_reference_caffenet

Download images from Flickr

python examples/finetune_flickr_style/assemble_data.py --workers=1 --images=2000 --seed 831486

Fine tuning with Flickr data with bvlc_reference_caffenet.caffemodel as the initial value.

./build/tools/caffe train -solver models/finetune_flickr_style/solver.prototxt -weights models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel

Regression Methods for Localization

Mountains, roads, runners スクリーンショット 2017-06-08 19.54.04.png To understand the image ● Object localization ● Object segmentation ● Human pose estimation スクリーンショット 2017-06-08 19.55.19.png Formulated as a DNN-based regression ● Deep Neural Net-based Regression ● Object Mask Regression ● Object Bounding Box Regression ● Human Pose Estimation DNN-based Regression

reference

fine-tuning http://hirotaka-hachiya.hatenablog.com/entry/2015/02/21/072255

https://bfeba431-a-62cb3a1a-s-sites.googlegroups.com/site/deeplearningcvpr2014/RegressionMethodsforLocalization.pdf?attachauth=ANoY7cpwt94hVCZXGTsfVWXtfKcakWqTiHT9TEYM6tLGdUk2jmlmBHiyEaL3qByRJEeBn-2EtPhanI3uoT58LSiRDl_A6JP51_8jm8LqcbyLZYo2bSMJpvbmCXlP4fMiRtJLT7nXmUu0QERcZEnYd_Ly-kka7TKKUEyk4-ez1iXr5ROM-G_2jjLa21y3y1y6s9sQK3Q0KJVvIHAAmzKn7uonJ4N2Q3f3dLVs7QQyw4MgIDd_ZiYcati9Ktkjq51cvzjLOPMfa7d6&attredirects=0

Recommended Posts

Caffe Model Zoo for beginners [Age and gender classification]
CNN (1) for image classification (for beginners)
Run the Caffe model on Google Colaboratory to predict the age and gender of the world's supermodels
Data set IMDB-WIKI for estimating age and gender from facial images
Implementation and explanation using XGBoost for beginners
[Explanation for beginners] TensorFlow basic syntax and concept
Load caffe model with Chainer and classify images
Causal reasoning and causal search with Python (for beginners)