-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcvpr_results_photoface.py
203 lines (171 loc) · 9.38 KB
/
cvpr_results_photoface.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
200
201
202
203
from pybug.image import MaskedNDImage, DepthImage
from pybug.io import auto_import
from pybug.landmark import labeller, ibug_68_closed_mouth
from copy import deepcopy
import cPickle
import numpy as np
from surface_reconstruction import frankotchellappa
import mayavi.mlab as mlab
from photometric_stereo import photometric_stereo as ps
from geometric_sfs import geometric_sfs as sfs
from mapping import AEP, PGA, Spherical, ImageMapper, IdentityMapper
def save_result_images(subject_id, feature_space, reco_type, fig_size=(512, 512)):
output_path = '/vol/atlas/pts08/cvpr/results/photoface/{0}_{1}_{2}_{3}.png'
# Left profile
mlab.view(azimuth=-80, elevation=85, roll=-145, distance=840,
focalpoint=np.array([288, 360, 64]))
mlab.savefig(output_path.format(subject_id, feature_space,
reco_type, 'left_profile'),
size=fig_size)
# Right profile
mlab.view(azimuth=94, elevation=85, roll=-145, distance=840,
focalpoint=np.array([288, 360, 64]))
mlab.savefig(output_path.format(subject_id, feature_space,
reco_type, 'right_profile'),
size=fig_size)
# Frontal
mlab.view(azimuth=180, elevation=20, roll=-90, distance=810,
focalpoint=np.array([300, 350, 67]))
mlab.savefig(output_path.format(subject_id, feature_space,
reco_type, 'frontal'),
size=fig_size)
mlab.close(all=True)
def build_mapping_object(feature_space, mean_normals, intrinsic_normals):
if feature_space == 'aep':
mapping_object = ImageMapper(AEP(
mean_normals.as_vector(keep_channels=True)))
elif feature_space == 'pga':
mapping_object = ImageMapper(PGA(
intrinsic_normals.as_vector(keep_channels=True)))
elif feature_space == 'normal':
mapping_object = ImageMapper(IdentityMapper())
elif feature_space == 'cosine':
mapping_object = ImageMapper(IdentityMapper())
elif feature_space == 'spherical':
mapping_object = ImageMapper(Spherical())
else:
raise ValueError('Unrecognised feature space!')
return mapping_object
# Set to offscreen rendering!
mlab.options.offscreen = True
photoface_path = '/mnt/atlas/databases/alex_images'
photoface_subjects = ['bej', 'bln', 'fav', 'mut', 'pet', 'rob', 'srb']
feature_spaces = ['aep', 'cosine', 'normal', 'pga', 'spherical']
# The index of the 4 images to use for SFS
sfs_index = 2
lights = np.array([[ 0.5, 0.4, 2],
[-0.5, 0.4, 2],
[-0.5, -0.4, 2],
[ 0.5, -0.4, 2]])
lights[:, 0] = -lights[:, 0]
sfs_light = lights[sfs_index, :]
# (Subject, Feature space) - Alphabetical order
mean_depth_error_results = np.zeros([len(photoface_subjects), len(feature_spaces)])
mean_angular_error_results = np.zeros([len(photoface_subjects), len(feature_spaces)])
# (Subject, Feature space) - Alphabetical order
std_depth_error_results = np.zeros([len(photoface_subjects), len(feature_spaces)])
std_angular_error_results = np.zeros([len(photoface_subjects), len(feature_spaces)])
# 5 feature spaces + ground truth
normals = dict(zip(photoface_subjects, [{}, {}, {}, {}, {}, {}, {}]))
for s in normals.values():
s.update(zip(['ground_truth'] + feature_spaces, [None] * (len(feature_spaces) + 1)))
for i, subject_id in enumerate(photoface_subjects):
print "Running experiment for {0}".format(subject_id)
subject_images = auto_import(
'/vol/atlas/databases/alex_images/{0}*.ppm'.format(subject_id))
# Create a 4 channel image where each channel is the greyscale of an image
ground_truth_images = MaskedNDImage(
np.concatenate([im.as_greyscale().pixels
for im in subject_images], axis=2))
# Choose the third image as the reconstruction candidate
intensity_image = subject_images[sfs_index].as_greyscale()
# The first image is the only landmarked one
intensity_image.landmarks = subject_images[0].landmarks
# Pass landmarks to all ground truth images
ground_truth_images.landmarks['PTS'] = intensity_image.landmarks['PTS']
# Label with correct labels
labeller([ground_truth_images, intensity_image],
'PTS', ibug_68_closed_mouth)
# Constrain to mask
ground_truth_images.constrain_mask_to_landmarks(
group='ibug_68_closed_mouth', label='all')
intensity_image.constrain_mask_to_landmarks(
group='ibug_68_closed_mouth', label='all')
intensity_image.crop_to_landmarks(group='ibug_68_closed_mouth',
label='all', boundary=2)
ground_truth_images.crop_to_landmarks(group='ibug_68_closed_mouth',
label='all', boundary=2)
temp_texture = subject_images[sfs_index]
# Perform Photometric Stereo
ground_truth_normals, ground_truth_albedo = ps(ground_truth_images, lights)
ground_truth_depth = frankotchellappa(-ground_truth_normals.pixels[:, :, 0],
ground_truth_normals.pixels[:, :, 1])
ground_truth_depth_image = DepthImage((ground_truth_depth - np.min(ground_truth_depth)) / 2,
texture=temp_texture)
normals[subject_id]['ground_truth'] = ground_truth_normals
# TODO: save images
#ground_truth_depth_image.view(mode='mesh')
#save_result_images(subject_id, 'all', 'groundtruth')
for k, feature_space in enumerate(feature_spaces):
print "Running {0} for {1}".format(feature_space, subject_id)
model_path = '/vol/atlas/pts08/cvpr/frgc_spring2003_sfs_tps_{0}_{1}.pkl'.format(subject_id, feature_space)
with open(model_path, 'rb') as f:
model = cPickle.load(f)
normal_model = model['appearance_model']
reference_frame = model['template']
mean_normals = model['mean_normals']
reference_frame = model['template']
try:
intrinsic_mean_normals = model['intrinsic_mean_normals']
except Exception:
intrinsic_mean_normals = None
# Perform SFS
warped_intensity_image = MaskedNDImage(intensity_image.pixels.copy(),
mask=intensity_image.mask)
initial_estimate_image = warped_intensity_image.from_vector(
mean_normals.copy(), n_channels=3)
mapping_object = build_mapping_object(feature_space,
initial_estimate_image,
intrinsic_mean_normals)
# Normalise the image so that it has unit albedo
#warped_intensity_image.masked_pixels /= ground_truth_albedo.masked_pixels
#warped_intensity_image.masked_pixels = \
# ((warped_intensity_image.masked_pixels - np.nanmin(warped_intensity_image.masked_pixels)) /
# (np.nanmax(warped_intensity_image.masked_pixels) - np.nanmin(warped_intensity_image.masked_pixels)))
reconstructed_normals = sfs(warped_intensity_image,
initial_estimate_image,
normal_model,
sfs_light, n_iters=50,
mapping_object=mapping_object)
normals[subject_id][feature_space] = reconstructed_normals
reconstructed_depth = frankotchellappa(
-reconstructed_normals.pixels[:, :, 0],
reconstructed_normals.pixels[:, :, 1])
reconstructed_depth_image = DepthImage((reconstructed_depth - np.min(reconstructed_depth)) / 2,
texture=temp_texture)
# TODO: save images
#reconstructed_depth_image.view(mode='mesh')
#save_result_images(subject_id, feature_space, 'sfs')
depth_differences = np.abs(reconstructed_depth.flatten() -
ground_truth_depth.flatten())
mean_depth_error_results[i, k] = np.mean(depth_differences)
ground_truth_normal_vec = ground_truth_normals.as_vector(keep_channels=True)
recon_normal_vec = reconstructed_normals.as_vector(keep_channels=True)
angular_differences = np.arccos(np.clip(np.sum(recon_normal_vec *
ground_truth_normal_vec, axis=-1), -1, 1))
mean_angular_error_results[i, k] = np.mean(angular_differences)
print "{0}_{1}: Mean Depth error: {2}".format(subject_id, feature_space, mean_depth_error_results[i, k])
print "{0}_{1}: Mean Angular error: {2}".format(subject_id, feature_space, mean_angular_error_results[i, k])
std_depth_error_results[i, k] = np.std(depth_differences)
std_angular_error_results[i, k] = np.std(angular_differences)
# Save out error results
with open('/vol/atlas/pts08/cvpr/results/photoface/mean_depth_errors.pkl', 'wb') as f:
cPickle.dump(mean_depth_error_results, f, protocol=2)
with open('/vol/atlas/pts08/cvpr/results/photoface/mean_angular_errors.pkl', 'wb') as f:
cPickle.dump(mean_angular_error_results, f, protocol=2)
with open('/vol/atlas/pts08/cvpr/results/photoface/std_depth_errors.pkl', 'wb') as f:
cPickle.dump(std_depth_error_results, f, protocol=2)
with open('/vol/atlas/pts08/cvpr/results/photoface/std_angular_errors.pkl', 'wb') as f:
cPickle.dump(std_angular_error_results, f, protocol=2)
with open('/vol/atlas/pts08/cvpr/results/photoface/all_result_dict.pkl', 'wb') as f:
cPickle.dump(normals, f, protocol=2)