Spaces:
Sleeping
Sleeping
Update videoretalking/third_part/GPEN/face_parse/parse_model.py
Browse files
videoretalking/third_part/GPEN/face_parse/parse_model.py
CHANGED
@@ -1,77 +1,77 @@
|
|
1 |
-
'''
|
2 |
-
@Created by chaofengc (chaofenghust@gmail.com)
|
3 |
-
|
4 |
-
@Modified by yangxy (yangtao9009@gmail.com)
|
5 |
-
'''
|
6 |
-
|
7 |
-
from face_parse.blocks import *
|
8 |
-
import torch
|
9 |
-
from torch import nn
|
10 |
-
import numpy as np
|
11 |
-
|
12 |
-
def define_P(in_size=512, out_size=512, min_feat_size=32, relu_type='LeakyReLU', isTrain=False, weight_path=None):
|
13 |
-
net = ParseNet(in_size, out_size, min_feat_size, 64, 19, norm_type='bn', relu_type=relu_type, ch_range=[32, 256])
|
14 |
-
if not isTrain:
|
15 |
-
net.eval()
|
16 |
-
if weight_path is not None:
|
17 |
-
net.load_state_dict(torch.load(weight_path))
|
18 |
-
return net
|
19 |
-
|
20 |
-
|
21 |
-
class ParseNet(nn.Module):
|
22 |
-
def __init__(self,
|
23 |
-
in_size=128,
|
24 |
-
out_size=128,
|
25 |
-
min_feat_size=32,
|
26 |
-
base_ch=64,
|
27 |
-
parsing_ch=19,
|
28 |
-
res_depth=10,
|
29 |
-
relu_type='prelu',
|
30 |
-
norm_type='bn',
|
31 |
-
ch_range=[32, 512],
|
32 |
-
):
|
33 |
-
super().__init__()
|
34 |
-
self.res_depth = res_depth
|
35 |
-
act_args = {'norm_type': norm_type, 'relu_type': relu_type}
|
36 |
-
min_ch, max_ch = ch_range
|
37 |
-
|
38 |
-
ch_clip = lambda x: max(min_ch, min(x, max_ch))
|
39 |
-
min_feat_size = min(in_size, min_feat_size)
|
40 |
-
|
41 |
-
down_steps = int(np.log2(in_size//min_feat_size))
|
42 |
-
up_steps = int(np.log2(out_size//min_feat_size))
|
43 |
-
|
44 |
-
# =============== define encoder-body-decoder ====================
|
45 |
-
self.encoder = []
|
46 |
-
self.encoder.append(ConvLayer(3, base_ch, 3, 1))
|
47 |
-
head_ch = base_ch
|
48 |
-
for i in range(down_steps):
|
49 |
-
cin, cout = ch_clip(head_ch), ch_clip(head_ch * 2)
|
50 |
-
self.encoder.append(ResidualBlock(cin, cout, scale='down', **act_args))
|
51 |
-
head_ch = head_ch * 2
|
52 |
-
|
53 |
-
self.body = []
|
54 |
-
for i in range(res_depth):
|
55 |
-
self.body.append(ResidualBlock(ch_clip(head_ch), ch_clip(head_ch), **act_args))
|
56 |
-
|
57 |
-
self.decoder = []
|
58 |
-
for i in range(up_steps):
|
59 |
-
cin, cout = ch_clip(head_ch), ch_clip(head_ch // 2)
|
60 |
-
self.decoder.append(ResidualBlock(cin, cout, scale='up', **act_args))
|
61 |
-
head_ch = head_ch // 2
|
62 |
-
|
63 |
-
self.encoder = nn.Sequential(*self.encoder)
|
64 |
-
self.body = nn.Sequential(*self.body)
|
65 |
-
self.decoder = nn.Sequential(*self.decoder)
|
66 |
-
self.out_img_conv = ConvLayer(ch_clip(head_ch), 3)
|
67 |
-
self.out_mask_conv = ConvLayer(ch_clip(head_ch), parsing_ch)
|
68 |
-
|
69 |
-
def forward(self, x):
|
70 |
-
feat = self.encoder(x)
|
71 |
-
x = feat + self.body(feat)
|
72 |
-
x = self.decoder(x)
|
73 |
-
out_img = self.out_img_conv(x)
|
74 |
-
out_mask = self.out_mask_conv(x)
|
75 |
-
return out_mask, out_img
|
76 |
-
|
77 |
-
|
|
|
1 |
+
'''
|
2 |
+
@Created by chaofengc (chaofenghust@gmail.com)
|
3 |
+
|
4 |
+
@Modified by yangxy (yangtao9009@gmail.com)
|
5 |
+
'''
|
6 |
+
|
7 |
+
from videoretalking.third_part.GPEN.face_parse.blocks import *
|
8 |
+
import torch
|
9 |
+
from torch import nn
|
10 |
+
import numpy as np
|
11 |
+
|
12 |
+
def define_P(in_size=512, out_size=512, min_feat_size=32, relu_type='LeakyReLU', isTrain=False, weight_path=None):
|
13 |
+
net = ParseNet(in_size, out_size, min_feat_size, 64, 19, norm_type='bn', relu_type=relu_type, ch_range=[32, 256])
|
14 |
+
if not isTrain:
|
15 |
+
net.eval()
|
16 |
+
if weight_path is not None:
|
17 |
+
net.load_state_dict(torch.load(weight_path))
|
18 |
+
return net
|
19 |
+
|
20 |
+
|
21 |
+
class ParseNet(nn.Module):
|
22 |
+
def __init__(self,
|
23 |
+
in_size=128,
|
24 |
+
out_size=128,
|
25 |
+
min_feat_size=32,
|
26 |
+
base_ch=64,
|
27 |
+
parsing_ch=19,
|
28 |
+
res_depth=10,
|
29 |
+
relu_type='prelu',
|
30 |
+
norm_type='bn',
|
31 |
+
ch_range=[32, 512],
|
32 |
+
):
|
33 |
+
super().__init__()
|
34 |
+
self.res_depth = res_depth
|
35 |
+
act_args = {'norm_type': norm_type, 'relu_type': relu_type}
|
36 |
+
min_ch, max_ch = ch_range
|
37 |
+
|
38 |
+
ch_clip = lambda x: max(min_ch, min(x, max_ch))
|
39 |
+
min_feat_size = min(in_size, min_feat_size)
|
40 |
+
|
41 |
+
down_steps = int(np.log2(in_size//min_feat_size))
|
42 |
+
up_steps = int(np.log2(out_size//min_feat_size))
|
43 |
+
|
44 |
+
# =============== define encoder-body-decoder ====================
|
45 |
+
self.encoder = []
|
46 |
+
self.encoder.append(ConvLayer(3, base_ch, 3, 1))
|
47 |
+
head_ch = base_ch
|
48 |
+
for i in range(down_steps):
|
49 |
+
cin, cout = ch_clip(head_ch), ch_clip(head_ch * 2)
|
50 |
+
self.encoder.append(ResidualBlock(cin, cout, scale='down', **act_args))
|
51 |
+
head_ch = head_ch * 2
|
52 |
+
|
53 |
+
self.body = []
|
54 |
+
for i in range(res_depth):
|
55 |
+
self.body.append(ResidualBlock(ch_clip(head_ch), ch_clip(head_ch), **act_args))
|
56 |
+
|
57 |
+
self.decoder = []
|
58 |
+
for i in range(up_steps):
|
59 |
+
cin, cout = ch_clip(head_ch), ch_clip(head_ch // 2)
|
60 |
+
self.decoder.append(ResidualBlock(cin, cout, scale='up', **act_args))
|
61 |
+
head_ch = head_ch // 2
|
62 |
+
|
63 |
+
self.encoder = nn.Sequential(*self.encoder)
|
64 |
+
self.body = nn.Sequential(*self.body)
|
65 |
+
self.decoder = nn.Sequential(*self.decoder)
|
66 |
+
self.out_img_conv = ConvLayer(ch_clip(head_ch), 3)
|
67 |
+
self.out_mask_conv = ConvLayer(ch_clip(head_ch), parsing_ch)
|
68 |
+
|
69 |
+
def forward(self, x):
|
70 |
+
feat = self.encoder(x)
|
71 |
+
x = feat + self.body(feat)
|
72 |
+
x = self.decoder(x)
|
73 |
+
out_img = self.out_img_conv(x)
|
74 |
+
out_mask = self.out_mask_conv(x)
|
75 |
+
return out_mask, out_img
|
76 |
+
|
77 |
+
|