In [6]:
%matplotlib inline
%pylab inline


Populating the interactive namespace from numpy and matplotlib
WARNING: pylab import has clobbered these variables: ['repeat']
`%matplotlib` prevents importing * from pylab and numpy

In [7]:
repeat = 1

Load test data


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

path = '/data/'
group = 'ibug_face_66'
db = 'afw'

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


- Loading 337 assets: [====================] 100%

In [9]:
from menpo.visualize import visualize_images

visualize_images(test_images)


Active Appearance Models


In [10]:
from menpofast.feature import no_op, fast_dsift
from alabortijcv2015.utils import pickle_load

aam_type = 'GlobalAAM_PWA'
features_type = no_op.__name__

aam = pickle_load(path + 'PhD/Models/ijcv2015/exp1_' + aam_type + '_' + features_type)

Fitting Parameters


In [11]:
sampling_step = 2

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

n_shape = [3, 12]
n_appearance = [25, 50]

noise_std = [0.04]
alphas = np.arange(0, 1.1, 0.1)
max_iters = 40
prior = False

imshow(sampling_mask)


Out[11]:
<matplotlib.image.AxesImage at 0x7f6504684690>

Simultaneous Algorithms

Simultaneous Symmetric Compositional (SSC)


In [ ]:
from alabortijcv2015.aam import GlobalAAM, PatchAAM, LinearGlobalAAM, LinearPatchAAM, PartsAAM
from alabortijcv2015.aam import StandardAAMFitter, LinearAAMFitter, PartsAAMFitter
from alabortijcv2015.aam.algorithm import SSC

algorithm_cls = SSC

if isinstance(aam, PartsAAM):
    fitter = PartsAAMFitter(aam, algorithm_cls=algorithm_cls, n_shape=n_shape,
                            n_appearance=n_appearance, sampling_mask=sampling_mask)
elif isinstance(aam, GlobalAAM) or isinstance(aam, PatchAAM): 
    fitter = StandardAAMFitter(aam, algorithm_cls=algorithm_cls, n_shape=n_shape,
                               n_appearance=n_appearance, sampling_step=sampling_step)
else:
    fitter = LinearAAMFitter(aam, algorithm_cls=algorithm_cls, n_shape=n_shape,
                             n_appearance=n_appearance, sampling_step=sampling_step)

In [ ]:
from alabortijcv2015.utils import pickle_dump
from alabortijcv2015.aam import SerializableAAMFitterResult

for n in noise_std:
    
    print 'noise:', n
    
    for a in alphas:
        
        fitter_results = []
    
        for seed in xrange(repeat):

            np.random.seed(seed=seed)

            for j, i in enumerate(test_images):

                gt_s = i.landmarks[group].lms

                if n is not None:
                    s = fitter.perturb_shape(gt_s, noise_std=n)
                else:
                    s = gt_s

                fr = fitter.fit(i, s, gt_shape=gt_s, max_iters=max_iters, prior=prior, a=a)

                fitter_results.append(fr)
                fr.downscale = 0.5

                #print 'Image: ', j
                #print fr

            if n is (None or 0):
                break

        print 'alpha:', a,
        print 'rmse:', np.mean([fr.final_error() for fr in fitter_results])
        
        alg_type = fitter._algorithms[0].__class__.__name__
        results = [SerializableAAMFitterResult('none', fr.shapes(), fr.costs(), fr.n_iters, alg_type, fr.gt_shape) 
                   for fr in fitter_results]

        pickle_dump(results, path + 'PhD/Results/ijcv2015/exp4_' + aam_type + '_' + features_type + '_' + alg_type + 
                    '_' + db + '_' + str(n) + '_' + str(a))

from menpofit.visualize import visualize_fitting_results

visualize_fitting_results(fitter_results)

Alternating Algorithms

Alternating Symmetric Compositional (ASC)


In [ ]:
from alabortijcv2015.aam import GlobalAAM, PatchAAM, LinearGlobalAAM, LinearPatchAAM, PartsAAM
from alabortijcv2015.aam import StandardAAMFitter, LinearAAMFitter, PartsAAMFitter
from alabortijcv2015.aam.algorithm import ASC

algorithm_cls = ASC

if isinstance(aam, PartsAAM):
    fitter = PartsAAMFitter(aam, algorithm_cls=algorithm_cls, n_shape=n_shape,
                            n_appearance=n_appearance, sampling_mask=sampling_mask)
elif isinstance(aam, GlobalAAM) or isinstance(aam, PatchAAM): 
    fitter = StandardAAMFitter(aam, algorithm_cls=algorithm_cls, n_shape=n_shape,
                               n_appearance=n_appearance, sampling_step=sampling_step)
else:
    fitter = LinearAAMFitter(aam, algorithm_cls=algorithm_cls, n_shape=n_shape,
                             n_appearance=n_appearance, sampling_step=sampling_step)

In [ ]:
from alabortijcv2015.utils import pickle_dump
from alabortijcv2015.aam import SerializableAAMFitterResult
    
for n in noise_std:
    
    print 'noise:', n,
    
    for a in alphas:
        
        fitter_results = []
    
        for seed in xrange(repeat):

            np.random.seed(seed=seed)

            for j, i in enumerate(test_images):

                gt_s = i.landmarks[group].lms

                if n is not None:
                    s = fitter.perturb_shape(gt_s, noise_std=n)
                else:
                    s = gt_s

                fr = fitter.fit(i, s, gt_shape=gt_s, max_iters=max_iters, prior=prior, a=a)

                fitter_results.append(fr)
                fr.downscale = 0.5

                #print 'Image: ', j
                #print fr

            if n is (None or 0):
                break

        print 'alpha:', a,
        print 'rmse:', np.mean([fr.final_error() for fr in fitter_results])
        
        alg_type = fitter._algorithms[0].__class__.__name__
        results = [SerializableAAMFitterResult('none', fr.shapes(), fr.costs(), fr.n_iters, alg_type, fr.gt_shape) 
                   for fr in fitter_results]
        

        pickle_dump(results, path + 'PhD/Results/ijcv2015/exp4_' + aam_type + '_' + features_type + '_' + alg_type + 
                    '_' + db + '_' + str(n) + '_' + str(a))

from menpofit.visualize import visualize_fitting_results

visualize_fitting_results(fitter_results)

Bayesian Algorithms

Bayesian Symmetric Compositional (BSC)


In [ ]:
from alabortijcv2015.aam import GlobalAAM, PatchAAM, LinearGlobalAAM, LinearPatchAAM, PartsAAM
from alabortijcv2015.aam import StandardAAMFitter, LinearAAMFitter, PartsAAMFitter
from alabortijcv2015.aam.algorithm import BSC

algorithm_cls = BSC

if isinstance(aam, PartsAAM):
    fitter = PartsAAMFitter(aam, algorithm_cls=algorithm_cls, n_shape=n_shape,
                            n_appearance=n_appearance, sampling_mask=sampling_mask)
elif isinstance(aam, GlobalAAM) or isinstance(aam, PatchAAM): 
    fitter = StandardAAMFitter(aam, algorithm_cls=algorithm_cls, n_shape=n_shape,
                               n_appearance=n_appearance, sampling_step=sampling_step)
else:
    fitter = LinearAAMFitter(aam, algorithm_cls=algorithm_cls, n_shape=n_shape,
                             n_appearance=n_appearance, sampling_step=sampling_step)

In [ ]:
from alabortijcv2015.utils import pickle_dump
from alabortijcv2015.aam import SerializableAAMFitterResult
    
for n in noise_std:
    
    print 'noise:', n
    
    for a in alphas:
        
        fitter_results = []
    
        for seed in xrange(repeat):

            np.random.seed(seed=seed)

            for j, i in enumerate(test_images):

                gt_s = i.landmarks[group].lms

                if n is not None:
                    s = fitter.perturb_shape(gt_s, noise_std=n)
                else:
                    s = gt_s

                fr = fitter.fit(i, s, gt_shape=gt_s, max_iters=max_iters, prior=prior, a=a)

                fitter_results.append(fr)
                fr.downscale = 0.5

                #print 'Image: ', j
                #print fr

            if n is (None or 0):
                break
            
        print 'alpha:', a,
        print 'rmse:', np.mean([fr.final_error() for fr in fitter_results])
        
        alg_type = fitter._algorithms[0].__class__.__name__
        results = [SerializableAAMFitterResult('none', fr.shapes(), fr.costs(), fr.n_iters, alg_type, fr.gt_shape) 
                   for fr in fitter_results]

        pickle_dump(results, path + 'PhD/Results/ijcv2015/exp4_' + aam_type + '_' + features_type + '_' + alg_type + 
                    '_' + db + '_' + str(n) + '_' + str(a))

from menpofit.visualize import visualize_fitting_results

visualize_fitting_results(fitter_results)