In [ ]:
%matplotlib inline
%pylab inline

Load training data


In [ ]:
import menpo.io as mio
from menpo.landmark import labeller, ibug_face_66
from menpofast.utils import convert_from_menpo

training_images = []
for i in mio.import_images('/data/PhD/DataBases/faces/lfpw/trainset/', verbose=True, 
                           max_images=None):
    
    # convert the image from menpo Image to menpofast Image (channels at front)
    i = convert_from_menpo(i)
    
    i.rescale_landmarks_to_diagonal_range(200)
    i.crop_to_landmarks_proportion_inplace(0.5)
    labeller(i, 'PTS', ibug_face_66)
    if i.n_channels == 3:
        i = i.as_greyscale(mode='average')
    training_images.append(i)

In [ ]:
from menpo.visualize import visualize_images

visualize_images(training_images)

Load test data


In [ ]:
import menpo.io as mio
from menpo.landmark import labeller, ibug_face_66
from menpofast.utils import convert_from_menpo

test_images = []
for i in mio.import_images('/data/PhD/DataBases/faces/afw/', verbose=True, 
                           max_images=None):
    
    # convert the image from menpo Image to menpofast Image (channels at front)
    i = convert_from_menpo(i)
    
    i.rescale_landmarks_to_diagonal_range(200)
    i.crop_to_landmarks_proportion_inplace(0.5)
    labeller(i, 'PTS', ibug_face_66)
    if i.n_channels == 3:
        i = i.as_greyscale(mode='average')
    test_images.append(i)

In [ ]:
from menpo.visualize import visualize_images

visualize_images(test_images)

Active Pictorial Structure

Build


In [ ]:
from menpofast.feature import no_op, aam_dsift
from antonakoscvpr2015.menpofast.builder import APSBuilder

aps = APSBuilder(adjacency_array=None, 
                 root_vertex=0, 
                 patch_shape=(17, 17),
                 features=aam_dsift, 
                 normalization_diagonal=100,
                 n_levels=2, 
                 downscale=2, 
                 scaled_shape_models=False,
                 max_shape_components=25,
                 n_appearance_parameters=500,
                 use_procrustes=True,
                 gaussian_per_patch=True).build(training_images, 
                                                group='ibug_face_66', 
                                                verbose=True)

In [ ]:
from menpofast.image import Image

Image(aps.appearance_models[0][0][37, 0, : , ...]).view()

In [ ]:
aps.appearance_models[0][0].shape

Test


In [ ]:
sampling_mask = np.require(np.zeros((17, 17)), dtype=np.bool)
sampling_mask[2::4, 2::4] = True
sampling_mask[:] = True

imshow(sampling_mask)

In [ ]:
from antonakoscvpr2015.menpofast.fitter import LucasKanadeAPSFitter
from antonakoscvpr2015.menpofast.algorithm import Forward, Inverse
                  
fitter = LucasKanadeAPSFitter(aps, algorithm=Inverse, n_shape=[3, 12], use_deformation=False,
                              sampling_mask=sampling_mask)

In [ ]:
np.random.seed(seed=1)

fitting_results = []

for j, i in enumerate(test_images):
    
    gt_s = i.landmarks['ibug_face_66'].lms
    s = fitter.perturb_shape(gt_s, noise_std=0.04)
    
    fr = fitter.fit(i, s, gt_shape=gt_s, max_iters=20)
    
    fitting_results.append(fr)
    
    print 'Image: ', j
    #print fr

In [ ]:
from menpofit.visualize import visualize_fitting_results

visualize_fitting_results(fitting_results)

Timings

Total


In [ ]:
%timeit fitter.fit(i, s, gt_shape=gt_s, max_iters=20)

Per line


In [ ]:
import line_profiler
import IPython

ip = IPython.get_ipython()
ip.define_magic('lprun', line_profiler.magic_lprun)

In [ ]:
from antonakoscvpr2015.menpofast.algorithm import Forward, Inverse

%lprun -f Forward.run fitter.fit(i, s, gt_shape=gt_s, max_iters=2)