Classifying Grayscale Images using Pycaffe

If you have trained a model with 1-dimensional gray image, and want to classify another gray image, the following is the hack worked for me:

  1. copy the offical classify.py in $CAFFE_ROOT/python/classify.py
  2. specify input_dim as 1, 1, x, x in deploy.prototxt
  3. change all call to caffe.io.load_image(fname) in classify.py to caffe.io.load_image(fname, False) because if you do not specify the second parameter as False, True will be used by default, the meaning of the second parameter is to tell load_image whether the image is color or gray, if it’s in color, then the returned image will have shape (width, height, 3) or (width, height, 4) depending on whether the alpha channel exists. If you specify False, the shape will be (width, height, 1) as you want.
  4. specify –channel_swap ‘0’ in python classify.py because this value is to reorder RGB to BGR, let’s say we have an image im, im is in numpy array format, and im.shape = (10, 10, 3), then caffe will do im = im[:, :, channel_wap] to swap channels, if you do not specify --channel_swap, it will be "2,1,0" by default, then in caffe, im = im[:, :, [2, 1, 0]], but the gray image’s shape is really (10, 10, 1) (if you follow the 2 step), so an index out of bounds exception will be raised. So just specify ‘0’ to --channel_swap, then caffe will run im = im[:, :, [0]], that’s fine.

then just use the official classify.py.

Here is the gist of classify.py and test.sh worked for me.
classify.py: https://gist.github.com/uronce-cc/869afe1bd85e79dda111
test.sh: https://gist.github.com/uronce-cc/e834e9cd2a0a62ceb5d5

Hope it will work for you too.

Installing frontalization 0.1.3: Face Frontalization in Unconstrained Images using MATLAB R2015b on Ubuntu 16.04

Library source: http://www.openu.ac.il/home/hassner/projects/frontalize/

Dependencies:
The code uses the following dependencies. You MUST have these installed and available on the MATLAB path:

1. calib-1.0.1 function available from: http://www.openu.ac.il/home/hassner/projects/poses/calib.1.0.1.zip
Installation: unzip calib.1.0.1, rename it to calib under frontaliztion home directory. Then rename calib_cv2.4.mexa64 to calib.mexa64

2. Facial feature detection functions. The code provides examples
of frontalization using different facial landmark detection methods. Currently supported are:
– SDM (default, used in paper; We don’t use this at the moment ),
– The facial feature detector of Zhu and Ramanan (We don’t use this at the moment)
DLIB detector (Our chosen method) .
– Any sparse (five-point) facial landmark detector. (We don’t use this at the moment)

3. OpenCV required by calib for calibration routines and some of the

detectors for cascase classifiers (We have already discussed about OpenCV installation in other blog posts. Check those.)

Frontalization set up:

1. Setup Dlib: Download from http://dlib.net/files/dlib-19.1.tar.bz2

tar jxvf dlib-19.1.tar.bz2
 cd dlib-19.1/
 cd examples/
cd build/
 cmake ..
 cmake --build . --config Release

2. Install dlib dependency (if required):

sudo apt-get install libboost-python1.58.0

3. Open demo.m

change line 86 from :
 detector = 'SDM'; to detector = 'dlib';

4. Open facial_feature_detection.m

Go to case ‘dlib’

change line 106 to following:
 Model3D = load('model3Ddlib'); % reference 3D points corresponding to dlib detections
 Model3D = Model3D.model_dlib;
and change line 111 to following:
 fidu_XY = load('dlib_xy.mat'); % load detections performed by Python script on current image
 fidu_XY = reshape(fidu_XY.lmarks,68,2);

5. Now open dlib_detect_script.py

Comment out line 7:
 #from Utils import HOME
Add the following two lines at the end: (Change image list as you like)
 lmarks, bboxes = get_landmarks(['test.jpg'])
 savemat('dlib_xy.mat', {'lmarks':lmarks})

6. Run the python file, this will create the dlib_xy.mat file

8. Now run demo.m , to see the frontalization demo result.