-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBuild Similarity model from FRGC Spring 2003.py
201 lines (141 loc) · 5.75 KB
/
Build Similarity model from FRGC Spring 2003.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
# <codecell>
import os.path
import numpy as np
import matplotlib.pyplot as plt
import cPickle
from pybug.image import DepthImage, ShapeImage, RGBImage, IntensityImage
from pybug.landmark.labels import labeller, ibug_68_closed_mouth
from pybug.shape import PointCloud
from pybug.io import auto_import
from pybug.image import MaskedNDImage
from pybug.transform import Translation, SimilarityTransform
from pybug.transform.tps import TPS
from pybug.transform.piecewiseaffine import PiecewiseAffineTransform
from pybug.groupalign import GeneralizedProcrustesAnalysis
from scipy.io import loadmat
from scipy.ndimage.filters import gaussian_filter, median_filter
from inpaint import replace_nans
import numpy as np
# <codecell>
def print_replace_line(string):
import sys
# Cheeky carriage return so we print on the same line
sys.stdout.write('\r' + string)
sys.stdout.flush()
# <markdowncell>
# ## Load all the aligned shapes from the dataset
# <codecell>
RECREATE_MESHES = False
# <codecell>
from sfs_io import load_frgc
images = load_frgc('spring2003', RECREATE_MESHES)
# <codecell>
def extract_normals(images):
vector_shape = images[0].mesh.vertex_normals.shape
N = len(images)
normals = np.zeros([N, vector_shape[0], vector_shape[1]])
for i, im in enumerate(images):
normals[i, ...] = im.mesh.vertex_normals
return normals
# <codecell>
def create_feature_space(feature_matrix, example_image, feature_space_name):
feature_space_images = []
N = feature_matrix.shape[0]
for i, n in enumerate(feature_matrix):
new_im = MaskedNDImage.blank(example_image.shape, mask=example_image.mask, n_channels=n.shape[1])
new_im.from_vector_inplace(n.flatten())
new_im.landmarks = example_image.landmarks
feature_space_images.append(new_im)
print_replace_line('Image {0} of {1}'.format(i + 1, N))
cPickle.dump(images, open('/vol/atlas/pts08/cvpr/frgc_spring2003_4_{0}.pkl'.format(feature_space_name), 'wb'), protocol=2)
return feature_space_images
# <markdowncell>
# ## Generate the frame of reference via a Similarity Transform
# <codecell>
from landmarks import ibug_68_edge
# Pull out the landmarks
labeller(images, 'PTS', ibug_68_edge)
shapes = [img.landmarks['ibug_68_edge'].lms for img in images]
# <codecell>
ref_frame = MaskedNDImage.blank([480, 360])
# <codecell>
from warp import build_similarity_transform
# Warp each of the images to the reference image
sim_transforms = [build_similarity_transform(shape) for shape in shapes]
warped_images = [img.warp_to(ref_frame.mask, t) for img, t in zip(images, sim_transforms)]
# <markdowncell>
# ## Calculate the normal matrix for all the images
# <codecell>
normal_matrix = extract_normals(warped_images)
# <markdowncell>
# ## Calculate the normals (for LS and Cosine)
# <codecell>
normal_images = create_feature_space(normal_matrix, warped_images[0], 'normals')
# <markdowncell>
# ## Calculate the Spherical feature space
# <codecell>
from cosine_normals import Spherical
spherical_matrix = Spherical().logmap(normal_matrix)
spherical_images = create_feature_space(spherical_matrix, warped_images[0], 'spherical')
# <markdowncell>
# ## Calculate the AEP feature space
# <codecell>
from vector_utils import normalise_vector
mean_normals = normalise_vector(np.mean(normal_matrix, 0))
# <codecell>
from logmap_utils import partial_logmap
from aep import AEP
aep_matrix = AEP(mean_normals).logmap(normal_matrix)
aep_images = create_feature_space(aep_matrix, warped_images[0], 'aep')
# <markdowncell>
# ## Calculate the PGA feature space
# <codecell>
from pga import PGA, intrinsic_mean
mu = intrinsic_mean(normal_matrix, PGA, max_iters=50)
# <codecell>
pga_matrix = PGA(mu).logmap(normal_matrix)
pga_images = create_feature_space(pga_matrix, warped_images[0], 'pga')
# <markdowncell>
# ## Calculate the PCA for LS, Spherical, Cosine and AEP
# <codecell>
# Create the template image
template = ref_frame
# <codecell>
from pybug.model.linear import PCAModel
normal_model = PCAModel(normal_images, center=True, n_components=200)
cosine_model = PCAModel(normal_images, center=False, n_components=200)
spherical_model = PCAModel(spherical_images, center=False, n_components=200)
aep_model = PCAModel(aep_images, center=False, n_components=200)
pga_model = PCAModel(pga_images, center=False, n_components=200)
# <codecell>
mean_normals_image = normal_model.mean
mu_image = mean_normals_image.from_vector(mu)
with open('/vol/atlas/pts08/cvpr/frgc_spring2003_sfs_sim_normal', 'wb') as f:
cPickle.dump({'appearance_model': normal_model,
'template': template,
'mean_normals': mean_normals_image},
f, protocol=2)
with open('/vol/atlas/pts08/cvpr/frgc_spring2003_sfs_sim_cosine', 'wb') as f:
cPickle.dump({'appearance_model': cosine_model,
'template': template,
'mean_normals': mean_normals_image},
f, protocol=2)
with open('/vol/atlas/pts08/cvpr/frgc_spring2003_sfs_sim_spherical', 'wb') as f:
cPickle.dump({'appearance_model': spherical_model,
'template': template,
'mean_normals': mean_normals_image},
f, protocol=2)
with open('/vol/atlas/pts08/cvpr/frgc_spring2003_sfs_sim_aep', 'wb') as f:
cPickle.dump({'appearance_model': aep_model,
'template': template,
'mean_normals': mean_normals_image},
f, protocol=2)
with open('/vol/atlas/pts08/cvpr/frgc_spring2003_sfs_sim_pga', 'wb') as f:
cPickle.dump({'appearance_model': pga_model,
'template': template,
'mean_normals': mean_normals_image,
'intrinsic_mean_normals': mu_image},
f, protocol=2)
# <codecell>