chauhuynh90 commited on
Commit
59bc898
·
1 Parent(s): 712814a

Create utils/facial_makeup.py

Browse files
Files changed (1) hide show
  1. utils/facial_makeup.py +442 -0
utils/facial_makeup.py ADDED
@@ -0,0 +1,442 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+ import torchvision
5
+ import torch.utils.model_zoo as modelzoo
6
+
7
+ # from modules.bn import InPlaceABNSync as BatchNorm2d
8
+ import torchvision.transforms as transforms
9
+ import cv2
10
+ import numpy as np
11
+ from skimage.filters import gaussian
12
+ from PIL import Image
13
+
14
+ def sharpen(img):
15
+ img = img * 1.0
16
+ gauss_out = gaussian(img, sigma=5, multichannel=True)
17
+
18
+ alpha = 1.5
19
+ img_out = (img - gauss_out) * alpha + img
20
+
21
+ img_out = img_out / 255.0
22
+
23
+ mask_1 = img_out < 0
24
+ mask_2 = img_out > 1
25
+
26
+ img_out = img_out * (1 - mask_1)
27
+ img_out = img_out * (1 - mask_2) + mask_2
28
+ img_out = np.clip(img_out, 0, 1)
29
+ img_out = img_out * 255
30
+ return np.array(img_out, dtype=np.uint8)
31
+
32
+
33
+ def hair(image, parsing, part=17, color=[230, 50, 20]):
34
+ b, g, r = color #[10, 50, 250] # [10, 250, 10]
35
+ tar_color = np.zeros_like(image)
36
+ tar_color[:, :, 0] = b
37
+ tar_color[:, :, 1] = g
38
+ tar_color[:, :, 2] = r
39
+
40
+ image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
41
+ tar_hsv = cv2.cvtColor(tar_color, cv2.COLOR_BGR2HSV)
42
+
43
+ if part == 12 or part == 13:
44
+ image_hsv[:, :, 0:2] = tar_hsv[:, :, 0:2]
45
+ else:
46
+ image_hsv[:, :, 0:1] = tar_hsv[:, :, 0:1]
47
+
48
+ changed = cv2.cvtColor(image_hsv, cv2.COLOR_HSV2BGR)
49
+
50
+ if part == 17:
51
+ changed = sharpen(changed)
52
+
53
+ changed[parsing != part] = image[parsing != part]
54
+ return changed
55
+
56
+ def evaluate(image_path, cp='cp/79999_iter.pth'):
57
+
58
+ # if not os.path.exists(respth):
59
+ # os.makedirs(respth)
60
+
61
+ n_classes = 19
62
+ net = BiSeNet(n_classes=n_classes)
63
+ net.cuda()
64
+ net.load_state_dict(torch.load(cp))
65
+ net.eval()
66
+
67
+ to_tensor = transforms.Compose([
68
+ transforms.ToTensor(),
69
+ transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
70
+ ])
71
+
72
+ with torch.no_grad():
73
+ img = Image.open(image_path)
74
+ image = img.resize((512, 512), Image.BILINEAR)
75
+ img = to_tensor(image)
76
+ img = torch.unsqueeze(img, 0)
77
+ img = img.cuda()
78
+ out = net(img)[0]
79
+ parsing = out.squeeze(0).cpu().numpy().argmax(0)
80
+
81
+ return parsing
82
+
83
+ resnet18_url = 'https://download.pytorch.org/models/resnet18-5c106cde.pth'
84
+
85
+
86
+ def conv3x3(in_planes, out_planes, stride=1):
87
+ """3x3 convolution with padding"""
88
+ return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
89
+ padding=1, bias=False)
90
+
91
+
92
+ class BasicBlock(nn.Module):
93
+ def __init__(self, in_chan, out_chan, stride=1):
94
+ super(BasicBlock, self).__init__()
95
+ self.conv1 = conv3x3(in_chan, out_chan, stride)
96
+ self.bn1 = nn.BatchNorm2d(out_chan)
97
+ self.conv2 = conv3x3(out_chan, out_chan)
98
+ self.bn2 = nn.BatchNorm2d(out_chan)
99
+ self.relu = nn.ReLU(inplace=True)
100
+ self.downsample = None
101
+ if in_chan != out_chan or stride != 1:
102
+ self.downsample = nn.Sequential(
103
+ nn.Conv2d(in_chan, out_chan,
104
+ kernel_size=1, stride=stride, bias=False),
105
+ nn.BatchNorm2d(out_chan),
106
+ )
107
+
108
+ def forward(self, x):
109
+ residual = self.conv1(x)
110
+ residual = F.relu(self.bn1(residual))
111
+ residual = self.conv2(residual)
112
+ residual = self.bn2(residual)
113
+
114
+ shortcut = x
115
+ if self.downsample is not None:
116
+ shortcut = self.downsample(x)
117
+
118
+ out = shortcut + residual
119
+ out = self.relu(out)
120
+ return out
121
+
122
+
123
+ def create_layer_basic(in_chan, out_chan, bnum, stride=1):
124
+ layers = [BasicBlock(in_chan, out_chan, stride=stride)]
125
+ for i in range(bnum-1):
126
+ layers.append(BasicBlock(out_chan, out_chan, stride=1))
127
+ return nn.Sequential(*layers)
128
+
129
+
130
+ class Resnet18(nn.Module):
131
+ def __init__(self):
132
+ super(Resnet18, self).__init__()
133
+ self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
134
+ bias=False)
135
+ self.bn1 = nn.BatchNorm2d(64)
136
+ self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
137
+ self.layer1 = create_layer_basic(64, 64, bnum=2, stride=1)
138
+ self.layer2 = create_layer_basic(64, 128, bnum=2, stride=2)
139
+ self.layer3 = create_layer_basic(128, 256, bnum=2, stride=2)
140
+ self.layer4 = create_layer_basic(256, 512, bnum=2, stride=2)
141
+ self.init_weight()
142
+
143
+ def forward(self, x):
144
+ x = self.conv1(x)
145
+ x = F.relu(self.bn1(x))
146
+ x = self.maxpool(x)
147
+
148
+ x = self.layer1(x)
149
+ feat8 = self.layer2(x) # 1/8
150
+ feat16 = self.layer3(feat8) # 1/16
151
+ feat32 = self.layer4(feat16) # 1/32
152
+ return feat8, feat16, feat32
153
+
154
+ def init_weight(self):
155
+ state_dict = modelzoo.load_url(resnet18_url)
156
+ self_state_dict = self.state_dict()
157
+ for k, v in state_dict.items():
158
+ if 'fc' in k: continue
159
+ self_state_dict.update({k: v})
160
+ self.load_state_dict(self_state_dict)
161
+
162
+ def get_params(self):
163
+ wd_params, nowd_params = [], []
164
+ for name, module in self.named_modules():
165
+ if isinstance(module, (nn.Linear, nn.Conv2d)):
166
+ wd_params.append(module.weight)
167
+ if not module.bias is None:
168
+ nowd_params.append(module.bias)
169
+ elif isinstance(module, nn.BatchNorm2d):
170
+ nowd_params += list(module.parameters())
171
+ return wd_params, nowd_params
172
+
173
+ class ConvBNReLU(nn.Module):
174
+ def __init__(self, in_chan, out_chan, ks=3, stride=1, padding=1, *args, **kwargs):
175
+ super(ConvBNReLU, self).__init__()
176
+ self.conv = nn.Conv2d(in_chan,
177
+ out_chan,
178
+ kernel_size = ks,
179
+ stride = stride,
180
+ padding = padding,
181
+ bias = False)
182
+ self.bn = nn.BatchNorm2d(out_chan)
183
+ self.init_weight()
184
+
185
+ def forward(self, x):
186
+ x = self.conv(x)
187
+ x = F.relu(self.bn(x))
188
+ return x
189
+
190
+ def init_weight(self):
191
+ for ly in self.children():
192
+ if isinstance(ly, nn.Conv2d):
193
+ nn.init.kaiming_normal_(ly.weight, a=1)
194
+ if not ly.bias is None: nn.init.constant_(ly.bias, 0)
195
+
196
+ class BiSeNetOutput(nn.Module):
197
+ def __init__(self, in_chan, mid_chan, n_classes, *args, **kwargs):
198
+ super(BiSeNetOutput, self).__init__()
199
+ self.conv = ConvBNReLU(in_chan, mid_chan, ks=3, stride=1, padding=1)
200
+ self.conv_out = nn.Conv2d(mid_chan, n_classes, kernel_size=1, bias=False)
201
+ self.init_weight()
202
+
203
+ def forward(self, x):
204
+ x = self.conv(x)
205
+ x = self.conv_out(x)
206
+ return x
207
+
208
+ def init_weight(self):
209
+ for ly in self.children():
210
+ if isinstance(ly, nn.Conv2d):
211
+ nn.init.kaiming_normal_(ly.weight, a=1)
212
+ if not ly.bias is None: nn.init.constant_(ly.bias, 0)
213
+
214
+ def get_params(self):
215
+ wd_params, nowd_params = [], []
216
+ for name, module in self.named_modules():
217
+ if isinstance(module, nn.Linear) or isinstance(module, nn.Conv2d):
218
+ wd_params.append(module.weight)
219
+ if not module.bias is None:
220
+ nowd_params.append(module.bias)
221
+ elif isinstance(module, nn.BatchNorm2d):
222
+ nowd_params += list(module.parameters())
223
+ return wd_params, nowd_params
224
+
225
+
226
+ class AttentionRefinementModule(nn.Module):
227
+ def __init__(self, in_chan, out_chan, *args, **kwargs):
228
+ super(AttentionRefinementModule, self).__init__()
229
+ self.conv = ConvBNReLU(in_chan, out_chan, ks=3, stride=1, padding=1)
230
+ self.conv_atten = nn.Conv2d(out_chan, out_chan, kernel_size= 1, bias=False)
231
+ self.bn_atten = nn.BatchNorm2d(out_chan)
232
+ self.sigmoid_atten = nn.Sigmoid()
233
+ self.init_weight()
234
+
235
+ def forward(self, x):
236
+ feat = self.conv(x)
237
+ atten = F.avg_pool2d(feat, feat.size()[2:])
238
+ atten = self.conv_atten(atten)
239
+ atten = self.bn_atten(atten)
240
+ atten = self.sigmoid_atten(atten)
241
+ out = torch.mul(feat, atten)
242
+ return out
243
+
244
+ def init_weight(self):
245
+ for ly in self.children():
246
+ if isinstance(ly, nn.Conv2d):
247
+ nn.init.kaiming_normal_(ly.weight, a=1)
248
+ if not ly.bias is None: nn.init.constant_(ly.bias, 0)
249
+
250
+
251
+ class ContextPath(nn.Module):
252
+ def __init__(self, *args, **kwargs):
253
+ super(ContextPath, self).__init__()
254
+ self.resnet = Resnet18()
255
+ self.arm16 = AttentionRefinementModule(256, 128)
256
+ self.arm32 = AttentionRefinementModule(512, 128)
257
+ self.conv_head32 = ConvBNReLU(128, 128, ks=3, stride=1, padding=1)
258
+ self.conv_head16 = ConvBNReLU(128, 128, ks=3, stride=1, padding=1)
259
+ self.conv_avg = ConvBNReLU(512, 128, ks=1, stride=1, padding=0)
260
+
261
+ self.init_weight()
262
+
263
+ def forward(self, x):
264
+ H0, W0 = x.size()[2:]
265
+ feat8, feat16, feat32 = self.resnet(x)
266
+ H8, W8 = feat8.size()[2:]
267
+ H16, W16 = feat16.size()[2:]
268
+ H32, W32 = feat32.size()[2:]
269
+
270
+ avg = F.avg_pool2d(feat32, feat32.size()[2:])
271
+ avg = self.conv_avg(avg)
272
+ avg_up = F.interpolate(avg, (H32, W32), mode='nearest')
273
+
274
+ feat32_arm = self.arm32(feat32)
275
+ feat32_sum = feat32_arm + avg_up
276
+ feat32_up = F.interpolate(feat32_sum, (H16, W16), mode='nearest')
277
+ feat32_up = self.conv_head32(feat32_up)
278
+
279
+ feat16_arm = self.arm16(feat16)
280
+ feat16_sum = feat16_arm + feat32_up
281
+ feat16_up = F.interpolate(feat16_sum, (H8, W8), mode='nearest')
282
+ feat16_up = self.conv_head16(feat16_up)
283
+
284
+ return feat8, feat16_up, feat32_up # x8, x8, x16
285
+
286
+ def init_weight(self):
287
+ for ly in self.children():
288
+ if isinstance(ly, nn.Conv2d):
289
+ nn.init.kaiming_normal_(ly.weight, a=1)
290
+ if not ly.bias is None: nn.init.constant_(ly.bias, 0)
291
+
292
+ def get_params(self):
293
+ wd_params, nowd_params = [], []
294
+ for name, module in self.named_modules():
295
+ if isinstance(module, (nn.Linear, nn.Conv2d)):
296
+ wd_params.append(module.weight)
297
+ if not module.bias is None:
298
+ nowd_params.append(module.bias)
299
+ elif isinstance(module, nn.BatchNorm2d):
300
+ nowd_params += list(module.parameters())
301
+ return wd_params, nowd_params
302
+
303
+
304
+ ### This is not used, since I replace this with the resnet feature with the same size
305
+ class SpatialPath(nn.Module):
306
+ def __init__(self, *args, **kwargs):
307
+ super(SpatialPath, self).__init__()
308
+ self.conv1 = ConvBNReLU(3, 64, ks=7, stride=2, padding=3)
309
+ self.conv2 = ConvBNReLU(64, 64, ks=3, stride=2, padding=1)
310
+ self.conv3 = ConvBNReLU(64, 64, ks=3, stride=2, padding=1)
311
+ self.conv_out = ConvBNReLU(64, 128, ks=1, stride=1, padding=0)
312
+ self.init_weight()
313
+
314
+ def forward(self, x):
315
+ feat = self.conv1(x)
316
+ feat = self.conv2(feat)
317
+ feat = self.conv3(feat)
318
+ feat = self.conv_out(feat)
319
+ return feat
320
+
321
+ def init_weight(self):
322
+ for ly in self.children():
323
+ if isinstance(ly, nn.Conv2d):
324
+ nn.init.kaiming_normal_(ly.weight, a=1)
325
+ if not ly.bias is None: nn.init.constant_(ly.bias, 0)
326
+
327
+ def get_params(self):
328
+ wd_params, nowd_params = [], []
329
+ for name, module in self.named_modules():
330
+ if isinstance(module, nn.Linear) or isinstance(module, nn.Conv2d):
331
+ wd_params.append(module.weight)
332
+ if not module.bias is None:
333
+ nowd_params.append(module.bias)
334
+ elif isinstance(module, nn.BatchNorm2d):
335
+ nowd_params += list(module.parameters())
336
+ return wd_params, nowd_params
337
+
338
+
339
+ class FeatureFusionModule(nn.Module):
340
+ def __init__(self, in_chan, out_chan, *args, **kwargs):
341
+ super(FeatureFusionModule, self).__init__()
342
+ self.convblk = ConvBNReLU(in_chan, out_chan, ks=1, stride=1, padding=0)
343
+ self.conv1 = nn.Conv2d(out_chan,
344
+ out_chan//4,
345
+ kernel_size = 1,
346
+ stride = 1,
347
+ padding = 0,
348
+ bias = False)
349
+ self.conv2 = nn.Conv2d(out_chan//4,
350
+ out_chan,
351
+ kernel_size = 1,
352
+ stride = 1,
353
+ padding = 0,
354
+ bias = False)
355
+ self.relu = nn.ReLU(inplace=True)
356
+ self.sigmoid = nn.Sigmoid()
357
+ self.init_weight()
358
+
359
+ def forward(self, fsp, fcp):
360
+ fcat = torch.cat([fsp, fcp], dim=1)
361
+ feat = self.convblk(fcat)
362
+ atten = F.avg_pool2d(feat, feat.size()[2:])
363
+ atten = self.conv1(atten)
364
+ atten = self.relu(atten)
365
+ atten = self.conv2(atten)
366
+ atten = self.sigmoid(atten)
367
+ feat_atten = torch.mul(feat, atten)
368
+ feat_out = feat_atten + feat
369
+ return feat_out
370
+
371
+ def init_weight(self):
372
+ for ly in self.children():
373
+ if isinstance(ly, nn.Conv2d):
374
+ nn.init.kaiming_normal_(ly.weight, a=1)
375
+ if not ly.bias is None: nn.init.constant_(ly.bias, 0)
376
+
377
+ def get_params(self):
378
+ wd_params, nowd_params = [], []
379
+ for name, module in self.named_modules():
380
+ if isinstance(module, nn.Linear) or isinstance(module, nn.Conv2d):
381
+ wd_params.append(module.weight)
382
+ if not module.bias is None:
383
+ nowd_params.append(module.bias)
384
+ elif isinstance(module, nn.BatchNorm2d):
385
+ nowd_params += list(module.parameters())
386
+ return wd_params, nowd_params
387
+
388
+
389
+ class BiSeNet(nn.Module):
390
+ def __init__(self, n_classes, *args, **kwargs):
391
+ super(BiSeNet, self).__init__()
392
+ self.cp = ContextPath()
393
+ ## here self.sp is deleted
394
+ self.ffm = FeatureFusionModule(256, 256)
395
+ self.conv_out = BiSeNetOutput(256, 256, n_classes)
396
+ self.conv_out16 = BiSeNetOutput(128, 64, n_classes)
397
+ self.conv_out32 = BiSeNetOutput(128, 64, n_classes)
398
+ self.init_weight()
399
+
400
+ def forward(self, x):
401
+ H, W = x.size()[2:]
402
+ feat_res8, feat_cp8, feat_cp16 = self.cp(x) # here return res3b1 feature
403
+ feat_sp = feat_res8 # use res3b1 feature to replace spatial path feature
404
+ feat_fuse = self.ffm(feat_sp, feat_cp8)
405
+
406
+ feat_out = self.conv_out(feat_fuse)
407
+ feat_out16 = self.conv_out16(feat_cp8)
408
+ feat_out32 = self.conv_out32(feat_cp16)
409
+
410
+ feat_out = F.interpolate(feat_out, (H, W), mode='bilinear', align_corners=True)
411
+ feat_out16 = F.interpolate(feat_out16, (H, W), mode='bilinear', align_corners=True)
412
+ feat_out32 = F.interpolate(feat_out32, (H, W), mode='bilinear', align_corners=True)
413
+ return feat_out, feat_out16, feat_out32
414
+
415
+ def init_weight(self):
416
+ for ly in self.children():
417
+ if isinstance(ly, nn.Conv2d):
418
+ nn.init.kaiming_normal_(ly.weight, a=1)
419
+ if not ly.bias is None: nn.init.constant_(ly.bias, 0)
420
+
421
+ def get_params(self):
422
+ wd_params, nowd_params, lr_mul_wd_params, lr_mul_nowd_params = [], [], [], []
423
+ for name, child in self.named_children():
424
+ child_wd_params, child_nowd_params = child.get_params()
425
+ if isinstance(child, FeatureFusionModule) or isinstance(child, BiSeNetOutput):
426
+ lr_mul_wd_params += child_wd_params
427
+ lr_mul_nowd_params += child_nowd_params
428
+ else:
429
+ wd_params += child_wd_params
430
+ nowd_params += child_nowd_params
431
+ return wd_params, nowd_params, lr_mul_wd_params, lr_mul_nowd_params
432
+
433
+
434
+ if __name__ == "__main__":
435
+ net = BiSeNet(19)
436
+ net.cuda()
437
+ net.eval()
438
+ in_ten = torch.randn(16, 3, 640, 480).cuda()
439
+ out, out16, out32 = net(in_ten)
440
+ print(out.shape)
441
+
442
+ net.get_params()