-
Notifications
You must be signed in to change notification settings - Fork 351
/
demo.py
134 lines (107 loc) · 6.11 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
"""
Copyright Snap Inc. 2021. This sample code is made available by Snap Inc. for informational purposes only.
No license, whether implied or otherwise, is granted in or to such code (including any rights to copy, modify,
publish, distribute and/or commercialize such code), unless you have entered into a separate agreement for such rights.
Such code is provided as-is, without warranty of any kind, express or implied, including any warranties of merchantability,
title, fitness for a particular purpose, non-infringement, or that such code is free of defects, errors or viruses.
In no event will Snap Inc. be liable for any damages or losses of any kind arising from the sample code or your use thereof.
"""
import sys
import yaml
from argparse import ArgumentParser
from tqdm import tqdm
import imageio
import numpy as np
from skimage.transform import resize
from skimage import img_as_ubyte
import torch
from sync_batchnorm import DataParallelWithCallback
from modules.generator import Generator
from modules.region_predictor import RegionPredictor
from modules.avd_network import AVDNetwork
from animate import get_animation_region_params
import matplotlib
matplotlib.use('Agg')
if sys.version_info[0] < 3:
raise Exception("You must use Python 3 or higher. Recommended version is Python 3.7")
def load_checkpoints(config_path, checkpoint_path, cpu=False):
with open(config_path) as f:
config = yaml.load(f)
generator = Generator(num_regions=config['model_params']['num_regions'],
num_channels=config['model_params']['num_channels'],
**config['model_params']['generator_params'])
if not cpu:
generator.cuda()
region_predictor = RegionPredictor(num_regions=config['model_params']['num_regions'],
num_channels=config['model_params']['num_channels'],
estimate_affine=config['model_params']['estimate_affine'],
**config['model_params']['region_predictor_params'])
if not cpu:
region_predictor.cuda()
avd_network = AVDNetwork(num_regions=config['model_params']['num_regions'],
**config['model_params']['avd_network_params'])
if not cpu:
avd_network.cuda()
if cpu:
checkpoint = torch.load(checkpoint_path, map_location=torch.device('cpu'))
else:
checkpoint = torch.load(checkpoint_path)
generator.load_state_dict(checkpoint['generator'])
region_predictor.load_state_dict(checkpoint['region_predictor'])
if 'avd_network' in checkpoint:
avd_network.load_state_dict(checkpoint['avd_network'])
if not cpu:
generator = DataParallelWithCallback(generator)
region_predictor = DataParallelWithCallback(region_predictor)
avd_network = DataParallelWithCallback(avd_network)
generator.eval()
region_predictor.eval()
avd_network.eval()
return generator, region_predictor, avd_network
def make_animation(source_image, driving_video, generator, region_predictor, avd_network,
animation_mode='standard', cpu=False):
with torch.no_grad():
predictions = []
source = torch.tensor(source_image[np.newaxis].astype(np.float32)).permute(0, 3, 1, 2)
if not cpu:
source = source.cuda()
driving = torch.tensor(np.array(driving_video)[np.newaxis].astype(np.float32)).permute(0, 4, 1, 2, 3)
source_region_params = region_predictor(source)
driving_region_params_initial = region_predictor(driving[:, :, 0])
for frame_idx in tqdm(range(driving.shape[2])):
driving_frame = driving[:, :, frame_idx]
if not cpu:
driving_frame = driving_frame.cuda()
driving_region_params = region_predictor(driving_frame)
new_region_params = get_animation_region_params(source_region_params, driving_region_params,
driving_region_params_initial, avd_network=avd_network,
mode=animation_mode)
out = generator(source, source_region_params=source_region_params, driving_region_params=new_region_params)
predictions.append(np.transpose(out['prediction'].data.cpu().numpy(), [0, 2, 3, 1])[0])
return predictions
def main(opt):
source_image = imageio.imread(opt.source_image)
reader = imageio.get_reader(opt.driving_video)
fps = reader.get_meta_data()['fps']
reader.close()
driving_video = imageio.mimread(opt.driving_video, memtest=False)
source_image = resize(source_image, opt.img_shape)[..., :3]
driving_video = [resize(frame, opt.img_shape)[..., :3] for frame in driving_video]
generator, region_predictor, avd_network = load_checkpoints(config_path=opt.config,
checkpoint_path=opt.checkpoint, cpu=opt.cpu)
predictions = make_animation(source_image, driving_video, generator, region_predictor, avd_network,
animation_mode=opt.mode, cpu=opt.cpu)
imageio.mimsave(opt.result_video, [img_as_ubyte(frame) for frame in predictions], fps=fps)
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--config", required=True, help="path to config")
parser.add_argument("--checkpoint", default='ted384.pth', help="path to checkpoint to restore")
parser.add_argument("--source_image", default='sup-mat/source.png', help="path to source image")
parser.add_argument("--driving_video", default='sup-mat/driving.mp4', help="path to driving video")
parser.add_argument("--result_video", default='result.mp4', help="path to output")
parser.add_argument("--mode", default='avd', choices=['standard', 'relative', 'avd'],
help="Animation mode")
parser.add_argument("--img_shape", default="384,384", type=lambda x: list(map(int, x.split(','))),
help='Shape of image, that the model was trained on.')
parser.add_argument("--cpu", dest="cpu", action="store_true", help="cpu mode.")
main(parser.parse_args())