-
Notifications
You must be signed in to change notification settings - Fork 199
/
syncnet.py
258 lines (205 loc) · 9.59 KB
/
syncnet.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
import torch
from torch import nn
from torch.nn import functional as F
from torch.utils.data import DataLoader
import cv2
import os
import numpy as np
from torch import optim
import random
import argparse
class Dataset(object):
def __init__(self, dataset_dir, mode):
self.img_path_list = []
self.lms_path_list = []
for i in range(len(os.listdir(dataset_dir+"/full_body_img/"))):
img_path = os.path.join(dataset_dir+"/full_body_img/", str(i)+".jpg")
lms_path = os.path.join(dataset_dir+"/landmarks/", str(i)+".lms")
self.img_path_list.append(img_path)
self.lms_path_list.append(lms_path)
if mode=="wenet":
audio_feats_path = dataset_dir+"/aud_wenet.npy"
if mode=="hubert":
audio_feats_path = dataset_dir+"/aud_hu.npy"
self.mode = mode
self.audio_feats = np.load(audio_feats_path)
self.audio_feats = self.audio_feats.astype(np.float32)
def __len__(self):
return self.audio_feats.shape[0]-1
def get_audio_features(self, features, index):
left = index - 8
right = index + 8
pad_left = 0
pad_right = 0
if left < 0:
pad_left = -left
left = 0
if right > features.shape[0]:
pad_right = right - features.shape[0]
right = features.shape[0]
auds = torch.from_numpy(features[left:right])
if pad_left > 0:
auds = torch.cat([torch.zeros_like(auds[:pad_left]), auds], dim=0)
if pad_right > 0:
auds = torch.cat([auds, torch.zeros_like(auds[:pad_right])], dim=0) # [8, 16]
return auds
def process_img(self, img, lms_path, img_ex, lms_path_ex):
lms_list = []
with open(lms_path, "r") as f:
lines = f.read().splitlines()
for line in lines:
arr = line.split(" ")
arr = np.array(arr, dtype=np.float32)
lms_list.append(arr)
lms = np.array(lms_list, dtype=np.int32)
xmin = lms[1][0]
ymin = lms[52][1]
xmax = lms[31][0]
width = xmax - xmin
ymax = ymin + width
crop_img = img[ymin:ymax, xmin:xmax]
crop_img = cv2.resize(crop_img, (168, 168), cv2.INTER_AREA)
img_real = crop_img[4:164, 4:164].copy()
img_real_ori = img_real.copy()
img_real_ori = img_real_ori.transpose(2,0,1).astype(np.float32)
img_real_T = torch.from_numpy(img_real_ori / 255.0)
return img_real_T
def __getitem__(self, idx):
img = cv2.imread(self.img_path_list[idx])
lms_path = self.lms_path_list[idx]
ex_int = random.randint(0, self.__len__()-1)
img_ex = cv2.imread(self.img_path_list[ex_int])
lms_path_ex = self.lms_path_list[ex_int]
img_real_T = self.process_img(img, lms_path, img_ex, lms_path_ex)
audio_feat = self.get_audio_features(self.audio_feats, idx) #
# print(audio_feat.shape)
if self.mode=="wenet":
audio_feat = audio_feat.reshape(256,16,32)
if self.mode=="hubert":
audio_feat = audio_feat.reshape(32,32,32)
y = torch.ones(1).float()
return img_real_T, audio_feat, y
class Conv2d(nn.Module):
def __init__(self, cin, cout, kernel_size, stride, padding, residual=False, *args, **kwargs):
super().__init__(*args, **kwargs)
self.conv_block = nn.Sequential(
nn.Conv2d(cin, cout, kernel_size, stride, padding),
nn.BatchNorm2d(cout)
)
self.act = nn.ReLU()
self.residual = residual
def forward(self, x):
out = self.conv_block(x)
if self.residual:
out += x
return self.act(out)
class nonorm_Conv2d(nn.Module):
def __init__(self, cin, cout, kernel_size, stride, padding, residual=False, *args, **kwargs):
super().__init__(*args, **kwargs)
self.conv_block = nn.Sequential(
nn.Conv2d(cin, cout, kernel_size, stride, padding),
)
self.act = nn.LeakyReLU(0.01, inplace=True)
def forward(self, x):
out = self.conv_block(x)
return self.act(out)
class Conv2dTranspose(nn.Module):
def __init__(self, cin, cout, kernel_size, stride, padding, output_padding=0, *args, **kwargs):
super().__init__(*args, **kwargs)
self.conv_block = nn.Sequential(
nn.ConvTranspose2d(cin, cout, kernel_size, stride, padding, output_padding),
nn.BatchNorm2d(cout)
)
self.act = nn.ReLU()
def forward(self, x):
out = self.conv_block(x)
return self.act(out)
class SyncNet_color(nn.Module):
def __init__(self, mode):
super(SyncNet_color, self).__init__()
self.face_encoder = nn.Sequential(
Conv2d(3, 32, kernel_size=(7, 7), stride=1, padding=3),
Conv2d(32, 64, kernel_size=5, stride=2, padding=1),
Conv2d(64, 64, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(64, 64, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(64, 128, kernel_size=3, stride=2, padding=1),
Conv2d(128, 128, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(128, 128, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(128, 128, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(128, 256, kernel_size=3, stride=2, padding=1),
Conv2d(256, 256, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(256, 256, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(256, 512, kernel_size=3, stride=2, padding=1),
Conv2d(512, 512, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(512, 512, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(512, 512, kernel_size=3, stride=2, padding=1),
Conv2d(512, 512, kernel_size=3, stride=1, padding=0),
Conv2d(512, 512, kernel_size=1, stride=1, padding=0),)
p1 = 256
p2 = (1, 2)
if mode == "hubert":
p1 = 32
p2 = (2, 2)
self.audio_encoder = nn.Sequential(
Conv2d(p1, 256, kernel_size=3, stride=1, padding=1),
Conv2d(256, 256, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(256, 256, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(256, 256, kernel_size=3, stride=p2, padding=1),
Conv2d(256, 256, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(256, 256, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(256, 256, kernel_size=3, stride=2, padding=2),
Conv2d(256, 256, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(256, 256, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(256, 512, kernel_size=3, stride=2, padding=1),
Conv2d(512, 512, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(512, 512, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(512, 512, kernel_size=3, stride=1, padding=0),
Conv2d(512, 512, kernel_size=1, stride=1, padding=0),)
def forward(self, face_sequences, audio_sequences): # audio_sequences := (B, dim, T)
face_embedding = self.face_encoder(face_sequences)
audio_embedding = self.audio_encoder(audio_sequences)
audio_embedding = audio_embedding.view(audio_embedding.size(0), -1)
face_embedding = face_embedding.view(face_embedding.size(0), -1)
audio_embedding = F.normalize(audio_embedding, p=2, dim=1)
face_embedding = F.normalize(face_embedding, p=2, dim=1)
return audio_embedding, face_embedding
logloss = nn.BCELoss()
def cosine_loss(a, v, y):
d = nn.functional.cosine_similarity(a, v)
loss = logloss(d.unsqueeze(1), y)
return loss
def train(save_dir, dataset_dir, mode):
if not os.path.exists(save_dir):
os.mkdir(save_dir)
train_dataset = Dataset(dataset_dir, mode=mode)
train_data_loader = DataLoader(
train_dataset, batch_size=16, shuffle=True,
num_workers=4)
model = SyncNet_color(mode).cuda()
optimizer = optim.Adam([p for p in model.parameters() if p.requires_grad],
lr=0.001)
for epoch in range(40):
for batch in train_data_loader:
imgT, audioT, y = batch
imgT = imgT.cuda()
audioT = audioT.cuda()
y = y.cuda()
audio_embedding, face_embedding = model(imgT, audioT)
loss = cosine_loss(audio_embedding, face_embedding, y)
loss.backward()
optimizer.step()
print(epoch, loss.item())
torch.save(model.state_dict(), os.path.join(save_dir, str(epoch)+'.pth'))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--save_dir', type=str)
parser.add_argument('--dataset_dir', type=str)
parser.add_argument('--asr', type=str)
opt = parser.parse_args()
# syncnet = SyncNet_color(mode=opt.asr)
# img = torch.zeros([1,3,160,160])
# # audio = torch.zeros([1,128,16,32])
# audio = torch.zeros([1,16,32,32])
# audio_embedding, face_embedding = syncnet(img, audio)
# print(audio_embedding.shape, face_embedding.shape)
train(opt.save_dir, opt.dataset_dir, opt.asr)