-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathmodels.py
173 lines (157 loc) · 8.88 KB
/
models.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
import time
import torch.nn as nn
import tha2.poser.modes.mode_20
import tha3.poser.modes.standard_float
import tha3.poser.modes.separable_float
import tha3.poser.modes.standard_half
import tha3.poser.modes.separable_half
from torch.nn.functional import interpolate
from args import args
from collections import OrderedDict
class TalkingAnimeLight(nn.Module):
def __init__(self):
super(TalkingAnimeLight, self).__init__()
self.face_morpher = tha2.poser.modes.mode_20.load_face_morpher('pretrained/face_morpher.pt')
self.two_algo_face_rotator = tha2.poser.modes.mode_20.load_face_rotater('pretrained/two_algo_face_rotator.pt')
self.combiner = tha2.poser.modes.mode_20.load_combiner('pretrained/combiner.pt')
self.face_cache = OrderedDict()
self.tot = 0
self.hit = 0
def forward(self, image, mouth_eye_vector, pose_vector, mouth_eye_vector_c, ratio=None):
x = image.clone()
if args.perf == 'model':
tic = time.perf_counter()
input_hash = hash(tuple(mouth_eye_vector_c))
cached = self.face_cache.get(input_hash)
self.tot += 1
if cached is None:
mouth_eye_morp_image = self.face_morpher(image[:, :, 32:224, 32:224], mouth_eye_vector)
self.face_cache[input_hash] = mouth_eye_morp_image.detach()
if len(self.face_cache) > args.max_gpu_cache_len:
self.face_cache.popitem(last=False)
else:
self.hit += 1
mouth_eye_morp_image = cached
self.face_cache.move_to_end(input_hash)
if args.debug and ratio is not None:
ratio.value = self.hit / self.tot
if args.perf == 'model':
print(" - face_morpher", (time.perf_counter() - tic) * 1000)
tic = time.perf_counter()
x[:, :, 32:224, 32:224] = mouth_eye_morp_image
rotate_image = self.two_algo_face_rotator(x, pose_vector)[:2]
if args.perf == 'model':
print(" - rotator", (time.perf_counter() - tic) * 1000)
tic = time.perf_counter()
output_image = self.combiner(rotate_image[0], rotate_image[1], pose_vector)
if args.perf == 'model':
print(" - combiner", (time.perf_counter() - tic) * 1000)
tic = time.perf_counter()
return output_image
class TalkingAnime3(nn.Module):
def __init__(self):
super(TalkingAnime3, self).__init__()
if args.model == "standard_float":
if args.eyebrow:
self.eyebrow_decomposer = tha3.poser.modes.standard_float.load_eyebrow_decomposer(
'data/models/standard_float/eyebrow_decomposer.pt')
self.eyebrow_morphing_combiner = tha3.poser.modes.standard_float.load_eyebrow_morphing_combiner(
'data/models/standard_float/eyebrow_morphing_combiner.pt')
self.face_morpher = tha3.poser.modes.standard_float.load_face_morpher(
'data/models/standard_float/face_morpher.pt')
self.two_algo_face_body_rotator = tha3.poser.modes.standard_float.load_two_algo_generator(
'data/models/standard_float/two_algo_face_body_rotator.pt')
self.editor = tha3.poser.modes.standard_float.load_editor('data/models/standard_float/editor.pt')
elif args.model == "standard_half":
if args.eyebrow:
self.eyebrow_decomposer = tha3.poser.modes.standard_half.load_eyebrow_decomposer(
'data/models/standard_half/eyebrow_decomposer.pt')
self.eyebrow_morphing_combiner = tha3.poser.modes.standard_half.load_eyebrow_morphing_combiner(
'data/models/standard_half/eyebrow_morphing_combiner.pt')
self.face_morpher = tha3.poser.modes.standard_half.load_face_morpher(
'data/models/standard_half/face_morpher.pt')
self.two_algo_face_body_rotator = tha3.poser.modes.standard_half.load_two_algo_generator(
'data/models/standard_half/two_algo_face_body_rotator.pt')
self.editor = tha3.poser.modes.standard_half.load_editor('data/models/standard_half/editor.pt')
elif args.model == "separable_float":
if args.eyebrow:
self.eyebrow_decomposer = tha3.poser.modes.separable_float.load_eyebrow_decomposer(
'data/models/separable_float/eyebrow_decomposer.pt')
self.eyebrow_morphing_combiner = tha3.poser.modes.separable_float.load_eyebrow_morphing_combiner(
'data/models/separable_float/eyebrow_morphing_combiner.pt')
self.face_morpher = tha3.poser.modes.separable_float.load_face_morpher(
'data/models/separable_float/face_morpher.pt')
self.two_algo_face_body_rotator = tha3.poser.modes.separable_float.load_two_algo_generator(
'data/models/separable_float/two_algo_face_body_rotator.pt')
self.editor = tha3.poser.modes.separable_float.load_editor('data/models/separable_float/editor.pt')
elif args.model == "separable_half":
if args.eyebrow:
self.eyebrow_decomposer = tha3.poser.modes.separable_half.load_eyebrow_decomposer(
'data/models/separable_half/eyebrow_decomposer.pt')
self.eyebrow_morphing_combiner = tha3.poser.modes.separable_half.load_eyebrow_morphing_combiner(
'data/models/separable_half/eyebrow_morphing_combiner.pt')
self.face_morpher = tha3.poser.modes.separable_half.load_face_morpher(
'data/models/separable_half/face_morpher.pt')
self.two_algo_face_body_rotator = tha3.poser.modes.separable_half.load_two_algo_generator(
'data/models/separable_half/two_algo_face_body_rotator.pt')
self.editor = tha3.poser.modes.separable_half.load_editor('data/models/separable_half/editor.pt')
else:
raise RuntimeError("Invalid model: '%s'" % args.model)
self.face_cache = OrderedDict()
self.tot = 0
self.hit = 0
def forward(self, image, mouth_eye_vector, pose_vector, eyebrow_vector, mouth_eye_vector_c, eyebrow_vector_c,
ratio=None):
if args.perf == 'model':
tic = time.perf_counter()
x = image.clone()
if args.eyebrow:
input_hash = hash(tuple(eyebrow_vector_c + mouth_eye_vector_c))
else:
input_hash = hash(tuple(mouth_eye_vector_c))
cached = self.face_cache.get(input_hash)
self.tot += 1
if cached is None:
face_image = x[:, :, 32:32 + 192, (32 + 128):(32 + 192 + 128)].clone()
if args.eyebrow:
eyebrow_morp_image = self.eyebrow_decomposer(x[:, :, 64:192, 64 + 128:192 + 128].clone())
eyebrow_morp_image = \
self.eyebrow_morphing_combiner(eyebrow_morp_image[3], eyebrow_morp_image[0], eyebrow_vector)[2]
face_image[:, :, 32:32 + 128, 32:32 + 128] = eyebrow_morp_image
mouth_eye_morp_image = self.face_morpher(face_image, mouth_eye_vector)[0]
self.face_cache[input_hash] = mouth_eye_morp_image.detach()
if len(self.face_cache) > args.max_gpu_cache_len:
self.face_cache.popitem(last=False)
else:
self.hit += 1
mouth_eye_morp_image = cached
self.face_cache.move_to_end(input_hash)
if args.debug and ratio is not None:
ratio.value = self.hit / self.tot
if args.perf == 'model':
print(" - face_morpher", (time.perf_counter() - tic) * 1000)
tic = time.perf_counter()
x[:, :, 32:32 + 192, (32 + 128):(32 + 192 + 128)] = mouth_eye_morp_image
x_half = interpolate(x, size=(256, 256), mode='bilinear', align_corners=False)
rotate_image = self.two_algo_face_body_rotator(x_half, pose_vector)
if args.perf == 'model':
print(" - rotator", (time.perf_counter() - tic) * 1000)
tic = time.perf_counter()
output_image = self.editor(x,
interpolate(rotate_image[1], size=(512, 512), mode='bilinear', align_corners=False),
interpolate(rotate_image[2], size=(512, 512), mode='bilinear', align_corners=False),
pose_vector)[0]
if args.perf == 'model':
print(" - editor", (time.perf_counter() - tic) * 1000)
tic = time.perf_counter()
return output_image
class TalkingAnime(nn.Module):
def __init__(self):
super(TalkingAnime, self).__init__()
def forward(self, image, mouth_eye_vector, pose_vector):
x = image.clone()
mouth_eye_morp_image = self.face_morpher(image[:, :, 32:224, 32:224], mouth_eye_vector)
x[:, :, 32:224, 32:224] = mouth_eye_morp_image
rotate_image = self.two_algo_face_rotator(x, pose_vector)[:2]
output_image = self.combiner(rotate_image[0], rotate_image[1], pose_vector)
return output_image