forked from EvelynFan/FaceFormer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
274 lines (230 loc) · 12.1 KB
/
demo.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import numpy as np
import scipy.io.wavfile as wav
import librosa
import os,sys,shutil,argparse,copy,pickle
import math,scipy
from faceformer import Faceformer
from transformers import Wav2Vec2FeatureExtractor,Wav2Vec2Processor
import time
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.ao.quantization import (
get_default_qconfig_mapping,
get_default_qat_qconfig_mapping,
QConfigMapping,
)
import torch.quantization.quantize_fx as quantize_fx
import copy
import cv2
import tempfile
from subprocess import call
os.environ['PYOPENGL_PLATFORM'] = 'osmesa' # egl
import pyrender
from psbody.mesh import Mesh
import trimesh
import random
from torch.profiler import profile, record_function, ProfilerActivity
@torch.no_grad()
def test_model(args):
if not os.path.exists(args.result_path):
os.makedirs(args.result_path)
if args.set_seed:
torch.manual_seed(42)
torch.cuda.manual_seed(42)
random.seed(42)
np.random.seed(42)
print("Setting seed to 42...")
#build model
model = Faceformer(args)
model.load_state_dict(torch.load(os.path.join(args.dataset, '{}.pth'.format(args.model_name)), map_location=torch.device(args.device)))
model = model.to(torch.device(args.device))
model.eval()
template_file = os.path.join(args.dataset, args.template_path)
with open(template_file, 'rb') as fin:
templates = pickle.load(fin,encoding='latin1')
train_subjects_list = [i for i in args.train_subjects.split(" ")]
one_hot_labels = np.eye(len(train_subjects_list))
iter = train_subjects_list.index(args.condition)
one_hot = one_hot_labels[iter]
one_hot = np.reshape(one_hot,(-1,one_hot.shape[0]))
one_hot = torch.FloatTensor(one_hot).to(device=args.device)
temp = templates[args.subject]
template = temp.reshape((-1))
template = np.reshape(template,(-1,template.shape[0]))
template = torch.FloatTensor(template).to(device=args.device)
wav_path = args.wav_path
test_name = os.path.basename(wav_path).split(".")[0]
speech_array, sampling_rate = librosa.load(os.path.join(wav_path), sr=16000)
processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h")
audio_feature = np.squeeze(processor(speech_array,sampling_rate=16000).input_values)
audio_feature = np.reshape(audio_feature,(-1,audio_feature.shape[0]))
audio_feature = torch.FloatTensor(audio_feature).to(device=args.device)
print("Model size before quantization: ")
print_size_of_model(model)
if args.int8_quantization == "dynamic_fx":
raise NotImplementedError("dynamic_fx quantization is not supported because model is not traceable.")
print("Doing int8 quantization...")
model = transform_model_to_int8_fx(model, audio_feature)
elif args.int8_quantization == "dynamic_eager":
print("Doing dynamic_eager int8 quantization...")
model = transform_model_to_int8_eager(model)
print(model)
print("Model size after quantization: ")
print_size_of_model(model)
print("Starting to predict...")
start_time = time.time()
#TODO consider using intel ipex...
with profile(activities=[ProfilerActivity.CPU],
profile_memory=True,
record_shapes=True,
with_stack=True,
experimental_config=torch._C._profiler._ExperimentalConfig(verbose=True),
on_trace_ready=torch.profiler.tensorboard_trace_handler(f'./logs/faceformer_{args.int8_quantization}')) as prof:
prediction = model.predict(audio_feature, template, one_hot, args.optimize_last_layer)
print(prof.key_averages(group_by_stack_n=5).table(sort_by="cpu_time_total", row_limit=20))
print("Time for prediction: {}".format(time.time()-start_time))
prediction = prediction.squeeze() # (seq_len, V*3)
np.save(os.path.join(args.result_path, test_name), prediction.detach().cpu().numpy())
def transform_model_to_int8_eager(model):
model_int8 = torch.ao.quantization.quantize_dynamic(
model, # the original model
{torch.nn.Linear}, # a set of layers to dynamically quantize
dtype=torch.qint8) # the target dtype for quantized weights
return model_int8
def print_size_of_model(model):
torch.save(model.state_dict(), "temp.p")
print('Size (MB):', os.path.getsize("temp.p")/1e6)
os.remove('temp.p')
def transform_model_to_int8_fx(model, input_fp32):
model_to_quantize = copy.deepcopy(model)
model_to_quantize.eval()
qconfig_mapping = QConfigMapping().set_global(torch.ao.quantization.default_dynamic_qconfig)
# a tuple of one or more example inputs are needed to trace the model
example_inputs = (input_fp32)
# prepare
model_prepared = quantize_fx.prepare_fx(model_to_quantize, qconfig_mapping, example_inputs)
# no calibration needed when we only have dynamic/weight_only quantization
# quantize
model_quantized = quantize_fx.convert_fx(model_prepared)
return model_quantized
# The implementation of rendering is borrowed from VOCA: https://github.com/TimoBolkart/voca/blob/master/utils/rendering.py
def render_mesh_helper(args,mesh, t_center, rot=np.zeros(3), tex_img=None, z_offset=0):
if args.dataset == "BIWI":
camera_params = {'c': np.array([400, 400]),
'k': np.array([-0.19816071, 0.92822711, 0, 0, 0]),
'f': np.array([4754.97941935 / 8, 4754.97941935 / 8])}
elif args.dataset == "vocaset":
camera_params = {'c': np.array([400, 400]),
'k': np.array([-0.19816071, 0.92822711, 0, 0, 0]),
'f': np.array([4754.97941935 / 2, 4754.97941935 / 2])}
frustum = {'near': 0.01, 'far': 3.0, 'height': 800, 'width': 800}
mesh_copy = Mesh(mesh.v, mesh.f)
mesh_copy.v[:] = cv2.Rodrigues(rot)[0].dot((mesh_copy.v-t_center).T).T+t_center
intensity = 2.0
rgb_per_v = None
primitive_material = pyrender.material.MetallicRoughnessMaterial(
alphaMode='BLEND',
baseColorFactor=[0.3, 0.3, 0.3, 1.0],
metallicFactor=0.8,
roughnessFactor=0.8
)
tri_mesh = trimesh.Trimesh(vertices=mesh_copy.v, faces=mesh_copy.f, vertex_colors=rgb_per_v)
render_mesh = pyrender.Mesh.from_trimesh(tri_mesh, material=primitive_material,smooth=True)
if args.background_black:
scene = pyrender.Scene(ambient_light=[.2, .2, .2], bg_color=[0, 0, 0])
else:
scene = pyrender.Scene(ambient_light=[.2, .2, .2], bg_color=[255, 255, 255])
camera = pyrender.IntrinsicsCamera(fx=camera_params['f'][0],
fy=camera_params['f'][1],
cx=camera_params['c'][0],
cy=camera_params['c'][1],
znear=frustum['near'],
zfar=frustum['far'])
scene.add(render_mesh, pose=np.eye(4))
camera_pose = np.eye(4)
camera_pose[:3,3] = np.array([0, 0, 1.0-z_offset])
scene.add(camera, pose=[[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 1],
[0, 0, 0, 1]])
angle = np.pi / 6.0
pos = camera_pose[:3,3]
light_color = np.array([1., 1., 1.])
light = pyrender.DirectionalLight(color=light_color, intensity=intensity)
light_pose = np.eye(4)
light_pose[:3,3] = pos
scene.add(light, pose=light_pose.copy())
light_pose[:3,3] = cv2.Rodrigues(np.array([angle, 0, 0]))[0].dot(pos)
scene.add(light, pose=light_pose.copy())
light_pose[:3,3] = cv2.Rodrigues(np.array([-angle, 0, 0]))[0].dot(pos)
scene.add(light, pose=light_pose.copy())
light_pose[:3,3] = cv2.Rodrigues(np.array([0, -angle, 0]))[0].dot(pos)
scene.add(light, pose=light_pose.copy())
light_pose[:3,3] = cv2.Rodrigues(np.array([0, angle, 0]))[0].dot(pos)
scene.add(light, pose=light_pose.copy())
flags = pyrender.RenderFlags.SKIP_CULL_FACES
try:
r = pyrender.OffscreenRenderer(viewport_width=frustum['width'], viewport_height=frustum['height'])
color, _ = r.render(scene, flags=flags)
except:
print('pyrender: Failed rendering frame')
color = np.zeros((frustum['height'], frustum['width'], 3), dtype='uint8')
return color[..., ::-1]
def render_sequence(args):
wav_path = args.wav_path
test_name = os.path.basename(wav_path).split(".")[0]
predicted_vertices_path = os.path.join(args.result_path,test_name+".npy")
if args.dataset == "BIWI":
template_file = os.path.join(args.dataset, args.render_template_path, "BIWI.ply")
elif args.dataset == "vocaset":
template_file = os.path.join(args.dataset, args.render_template_path, "FLAME_sample.ply")
print("rendering: ", test_name)
template = Mesh(filename=template_file)
predicted_vertices = np.load(predicted_vertices_path)
predicted_vertices = np.reshape(predicted_vertices,(-1,args.vertice_dim//3,3))
output_path = args.output_path
if not os.path.exists(output_path):
os.makedirs(output_path)
num_frames = predicted_vertices.shape[0]
tmp_video_file = tempfile.NamedTemporaryFile('w', suffix='.mp4', dir=output_path)
writer = cv2.VideoWriter(tmp_video_file.name, cv2.VideoWriter_fourcc(*'mp4v'), args.fps, (800, 800), True)
center = np.mean(predicted_vertices[0], axis=0)
for i_frame in range(num_frames):
render_mesh = Mesh(predicted_vertices[i_frame], template.f)
pred_img = render_mesh_helper(args,render_mesh, center)
pred_img = pred_img.astype(np.uint8)
writer.write(pred_img)
writer.release()
file_name = test_name+"_"+args.subject+"_condition_"+args.condition
video_fname = os.path.join(output_path, file_name+'.mp4')
cmd = ('ffmpeg' + ' -i {0} -pix_fmt yuv420p -qscale 0 {1}'.format(
tmp_video_file.name, video_fname)).split()
call(cmd)
def main():
parser = argparse.ArgumentParser(description='FaceFormer: Speech-Driven 3D Facial Animation with Transformers')
parser.add_argument("--model_name", type=str, default="biwi")
parser.add_argument("--dataset", type=str, default="BIWI", help='vocaset or BIWI')
parser.add_argument("--fps", type=float, default=25, help='frame rate - 30 for vocaset; 25 for BIWI')
parser.add_argument("--feature_dim", type=int, default=128, help='64 for vocaset; 128 for BIWI')
parser.add_argument("--period", type=int, default=25, help='period in PPE - 30 for vocaset; 25 for BIWI')
parser.add_argument("--vertice_dim", type=int, default=23370*3, help='number of vertices - 5023*3 for vocaset; 23370*3 for BIWI')
parser.add_argument("--device", type=str, default="cuda")
parser.add_argument("--train_subjects", type=str, default="F2 F3 F4 M3 M4 M5")
parser.add_argument("--test_subjects", type=str, default="F1 F5 F6 F7 F8 M1 M2 M6")
parser.add_argument("--output_path", type=str, default="demo/output", help='path of the rendered video sequence')
parser.add_argument("--wav_path", type=str, default="demo/wav/test.wav", help='path of the input audio signal')
parser.add_argument("--result_path", type=str, default="demo/result", help='path of the predictions')
parser.add_argument("--condition", type=str, default="M3", help='select a conditioning subject from train_subjects')
parser.add_argument("--subject", type=str, default="M1", help='select a subject from test_subjects or train_subjects')
parser.add_argument("--background_black", type=bool, default=True, help='whether to use black background')
parser.add_argument("--template_path", type=str, default="templates.pkl", help='path of the personalized templates')
parser.add_argument("--render_template_path", type=str, default="templates", help='path of the mesh in BIWI/FLAME topology')
parser.add_argument("--int8_quantization", type=str, default="", help='')
parser.add_argument("--optimize_last_layer", type=bool, default=False, help='Dont calculate linear layer for all')
parser.add_argument("--set_seed", type=bool, default=False, help='')
args = parser.parse_args()
test_model(args)
render_sequence(args)
if __name__=="__main__":
main()