In [ ]:
%matplotlib inline
%pylab inline

Load test data


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

path = '/data/'
group = 'streetscene_car_view_1'

test_images = []
for i in mio.import_images(path + 'PhD/DataBases/cars/cmu_car_data1/view1/',
                           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(1.5, group=group)
    i = i.rescale_landmarks_to_diagonal_range(200, group=group)
    
    if i.n_channels == 3:
        i = i.as_greyscale(mode='average')
    test_images.append(i)

In [ ]:
test_images = test_images[1::2]

In [ ]:
from menpo.visualize import visualize_images

visualize_images(test_images)

Load models


In [ ]:
from alabortcvpr2015.utils import pickle_load

parts_aam = pickle_load(path + 'PhD/Models/parts_aam_view2_fast_dsift')
global_aam = pickle_load(path + 'PhD/Models/global_aam_view2_fast_dsift')
clm = pickle_load(path + 'PhD/Models/clm_view2_fast_dsift')
parts_unified = pickle_load(path + 'PhD/Models/parts_unified_view2_fast_dsift')
global_unified = pickle_load(path + 'PhD/Models/global_unified_view2_fast_dsift')

Construct fitters


In [ ]:
sampling_step = 4

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

imshow(sampling_mask)

In [ ]:
from alabortcvpr2015.aam import PartsAAMFitter, GlobalAAMFitter, AIC, PIC
from alabortcvpr2015.clm import CLMFitter
from alabortcvpr2015.unified import PartsUnifiedFitter, GlobalUnifiedFitter, AICRLMS, PICRLMS

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

parts_aam_fitter_pic = PartsAAMFitter(parts_aam, algorithm_cls=PIC, 
                                      n_shape=n_shape, n_appearance=n_appearance, 
                                      sampling_mask=sampling_mask)
parts_aam_fitter_aic = PartsAAMFitter(parts_aam, algorithm_cls=AIC, 
                                      n_shape=n_shape, n_appearance=n_appearance, 
                                      sampling_mask=sampling_mask)

global_aam_fitter_pic = GlobalAAMFitter(global_aam, algorithm_cls=PIC, 
                                        n_shape=n_shape, n_appearance=n_appearance, 
                                        sampling_step=sampling_step)
global_aam_fitter_aic = GlobalAAMFitter(global_aam, algorithm_cls=AIC, 
                                        n_shape=n_shape, n_appearance=n_appearance, 
                                        sampling_step=sampling_step)

clm_fitter = CLMFitter(clm, n_shape=n_shape)

parts_unified_fitter_pic = PartsUnifiedFitter(parts_unified, algorithm_cls=PICRLMS, 
                                              n_shape=n_shape, n_appearance=n_appearance, 
                                              sampling_mask=sampling_mask)
parts_unified_fitter_aic = PartsUnifiedFitter(parts_unified, algorithm_cls=AICRLMS, 
                                              n_shape=n_shape, n_appearance=n_appearance, 
                                              sampling_mask=sampling_mask)

global_unified_fitter_pic = GlobalUnifiedFitter(global_unified, algorithm_cls=PICRLMS, 
                                                n_shape=n_shape, n_appearance=n_appearance, 
                                                sampling_step=sampling_step)
global_unified_fitter_aic = GlobalUnifiedFitter(global_unified, algorithm_cls=AICRLMS, 
                                                n_shape=n_shape, n_appearance=n_appearance, 
                                                sampling_step=sampling_step)

In [ ]:
fitters = [#parts_aam_fitter_pic, parts_aam_fitter_aic, 
           global_aam_fitter_pic, global_aam_fitter_aic,
           clm_fitter,
           #parts_unified_fitter_pic, parts_unified_fitter_aic,
           global_unified_fitter_pic, global_unified_fitter_aic]

In [ ]:
repeat = 1

results = [[], [], [], [], []]

for seed in xrange(repeat):
    
    np.random.seed(seed=seed)

    for j, i in enumerate(test_images):

        gt_s = i.landmarks[group].lms
        s = fitters[0].perturb_shape(gt_s, noise_std=0.05)
        
        for k, fitter in enumerate(fitters[-2:]):

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

            results[k].append(fr)
            fr.downscale = 0.5

            print 'Image: ', j
            print fr

Explore results


In [ ]:
initial_errors = [fr.initial_error() for fr in results[0]]

final_errors = []
for fitter_results in results:
    final_errors.append([fr.final_error() for fr in fitter_results])
    
errors = [initial_errors] + final_errors

In [ ]:
from menpofit.visualize import plot_ced

legend_entries = ['Initial',
                  #'Parts-AAM-PIC',
                  #'Parts-AAM-AIC',
                  'Global-AAM-PIC',
                  'Global-AAM-AIC',
                  'CLM-RLMS',
                  #'Parts-UNI-PIC-RLMS',
                  #'Parts-UNI-AIC-RLMS',
                  'Global-UNI-PIC-RLMS',
                  'Global-UNI-AIC-RLMS']

plot_ced(errors, legend_entries=legend_entries)

In [ ]:
from menpofit.visualize import visualize_fitting_results

visualize_fitting_results(results[0], color='r', linewidth=2)

Run time


In [ ]:
for (fitter, entry) in zip(fitters, legend_entries[1:]):
    
    print entry, ': '
    
    %time fitter.fit(i, s, gt_shape=gt_s, max_iters=20, prior=False)
    
    print

In [ ]: