Spaces:
Runtime error
Runtime error
Update Generater.py
Browse files- Generater.py +39 -237
Generater.py
CHANGED
@@ -1,240 +1,42 @@
|
|
1 |
-
|
2 |
import numpy as np
|
3 |
-
# import os
|
4 |
import paddle
|
5 |
-
import paddle.optimizer
|
6 |
-
import paddle.nn as nn
|
7 |
-
# from tqdm import tqdm
|
8 |
-
# from paddle.io import Dataset
|
9 |
-
# from paddle.io import DataLoader
|
10 |
import paddle.nn.functional as F
|
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 |
-
@staticmethod
|
51 |
-
def get_bias(data_dict, name):
|
52 |
-
return data_dict[name][1]
|
53 |
-
|
54 |
-
@staticmethod
|
55 |
-
def get_fc_weight(data_dict, name):
|
56 |
-
return data_dict[name][0]
|
57 |
-
|
58 |
-
def make_layers(self, cfg, data_dict, batch_norm=False) -> nn.Sequential:
|
59 |
-
layers = []
|
60 |
-
in_channels = 3
|
61 |
-
block = 1
|
62 |
-
number = 1
|
63 |
-
for v in cfg:
|
64 |
-
if v == 'M':
|
65 |
-
layers += [nn.MaxPool2D(kernel_size=2, stride=2)]
|
66 |
-
block += 1
|
67 |
-
number = 1
|
68 |
-
else:
|
69 |
-
conv2d = nn.Conv2D(in_channels, v, kernel_size=3, padding=1)
|
70 |
-
""" set value """
|
71 |
-
weight = paddle.to_tensor(
|
72 |
-
self.get_conv_filter(data_dict, f'conv{block}_{number}'))
|
73 |
-
weight = weight.transpose((3, 2, 0, 1))
|
74 |
-
bias = paddle.to_tensor(
|
75 |
-
self.get_bias(data_dict, f'conv{block}_{number}'))
|
76 |
-
conv2d.weight.set_value(weight)
|
77 |
-
conv2d.bias.set_value(bias)
|
78 |
-
number += 1
|
79 |
-
if batch_norm:
|
80 |
-
layers += [conv2d, nn.BatchNorm2D(v), nn.ReLU()]
|
81 |
-
else:
|
82 |
-
layers += [conv2d, nn.ReLU()]
|
83 |
-
in_channels = v
|
84 |
-
# print("number",block)
|
85 |
-
return nn.Sequential(*layers)
|
86 |
-
|
87 |
-
|
88 |
-
class InvertedresBlock(nn.Layer):
|
89 |
-
def __init__(self,
|
90 |
-
in_channels: int,
|
91 |
-
expansion: float,
|
92 |
-
out_channels: int,
|
93 |
-
bias_attr=False):
|
94 |
-
super().__init__()
|
95 |
-
self.in_channels = in_channels
|
96 |
-
self.expansion = expansion
|
97 |
-
self.out_channels = out_channels
|
98 |
-
self.bottle_channels = round(self.expansion * self.out_channels)
|
99 |
-
self.body = nn.Sequential(
|
100 |
-
# pw
|
101 |
-
Conv2DNormLReLU(self.in_channels,
|
102 |
-
self.bottle_channels,
|
103 |
-
kernel_size=1,
|
104 |
-
bias_attr=bias_attr),
|
105 |
-
# dw
|
106 |
-
nn.Conv2D(self.bottle_channels,
|
107 |
-
self.bottle_channels,
|
108 |
-
kernel_size=3,
|
109 |
-
stride=1,
|
110 |
-
padding=0,
|
111 |
-
groups=self.bottle_channels,
|
112 |
-
bias_attr=True),
|
113 |
-
nn.GroupNorm(1, self.bottle_channels),
|
114 |
-
nn.LeakyReLU(0.2),
|
115 |
-
# pw & linear
|
116 |
-
nn.Conv2D(self.bottle_channels,
|
117 |
-
self.out_channels,
|
118 |
-
kernel_size=1,
|
119 |
-
padding=0,
|
120 |
-
bias_attr=False),
|
121 |
-
nn.GroupNorm(1, self.out_channels),
|
122 |
-
)
|
123 |
-
|
124 |
-
|
125 |
-
def forward(self, x0):
|
126 |
-
x = self.body(x0)
|
127 |
-
if self.in_channels == self.out_channels:
|
128 |
-
out = paddle.add(x0, x)
|
129 |
-
else:
|
130 |
-
out = x
|
131 |
-
return x
|
132 |
-
class Conv2DNormLReLU(nn.Layer):
|
133 |
-
def __init__(self,
|
134 |
-
in_channels: int,
|
135 |
-
out_channels: int,
|
136 |
-
kernel_size: int = 3,
|
137 |
-
stride: int = 1,
|
138 |
-
padding: int = 1,
|
139 |
-
bias_attr=False) -> None:
|
140 |
-
super().__init__()
|
141 |
-
self.conv = nn.Conv2D(in_channels,
|
142 |
-
out_channels,
|
143 |
-
kernel_size,
|
144 |
-
stride,
|
145 |
-
padding,
|
146 |
-
bias_attr=bias_attr)
|
147 |
-
# NOTE layer norm is crucial for animegan!
|
148 |
-
self.norm = nn.GroupNorm(1, out_channels)
|
149 |
-
self.lrelu = nn.LeakyReLU(0.2)
|
150 |
-
|
151 |
-
def forward(self, x):
|
152 |
-
x = self.conv(x)
|
153 |
-
x = self.norm(x)
|
154 |
-
x = self.lrelu(x)
|
155 |
-
return x
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
class Generater(nn.Layer):
|
160 |
-
def __init__(self):
|
161 |
-
super().__init__()
|
162 |
-
self.VGG = VGG19()
|
163 |
-
self.A = nn.Sequential(InvertedresBlock(512, 2, 256),
|
164 |
-
InvertedresBlock(256, 2, 256),
|
165 |
-
InvertedresBlock(256, 2, 256),
|
166 |
-
InvertedresBlock(256, 2, 256),
|
167 |
-
Conv2DNormLReLU(256, 128))
|
168 |
-
self.B = nn.Sequential(nn.Upsample(scale_factor=2, mode='bilinear'),
|
169 |
-
Conv2DNormLReLU(128, 128),
|
170 |
-
Conv2DNormLReLU(128, 128))
|
171 |
-
self.C = nn.Sequential(nn.Upsample(scale_factor=2, mode='bilinear'),
|
172 |
-
Conv2DNormLReLU(128, 128),
|
173 |
-
Conv2DNormLReLU(128, 128))
|
174 |
-
self.D = nn.Sequential(nn.Upsample(scale_factor=2, mode='bilinear'),
|
175 |
-
Conv2DNormLReLU(128, 64),
|
176 |
-
Conv2DNormLReLU(64, 64),
|
177 |
-
Conv2DNormLReLU(64, 32, 7, padding=3))
|
178 |
-
|
179 |
-
self.out = nn.Sequential(nn.Conv2D(32, 3, 1, bias_attr=False),
|
180 |
-
nn.Tanh())
|
181 |
-
# ,nn.Sigmoid())
|
182 |
-
def style_projection(self,content_feature,style_feature,alpha = 0.7):
|
183 |
-
def scatter_numpy(dim, index, src):
|
184 |
-
dst = src.copy()
|
185 |
-
idx_xsection_shape = index.shape[:dim] + index.shape[dim + 1:]
|
186 |
-
# print("idx_xsection_shape",idx_xsection_shape)#(b,c)
|
187 |
-
dst_xsection_shape = dst.shape[:dim] + dst.shape[dim + 1:]
|
188 |
-
def make_slice(arr, dim, i):
|
189 |
-
slc = [slice(None)] * arr.ndim
|
190 |
-
slc[dim] = i
|
191 |
-
return tuple(slc)
|
192 |
-
|
193 |
-
# We use index and dim parameters to create idx
|
194 |
-
# idx is in a form that can be used as a NumPy advanced index for scattering of src param.
|
195 |
-
idx = [[
|
196 |
-
*np.indices(idx_xsection_shape).reshape(index.ndim - 1, -1), index[make_slice(index, dim, i)].reshape(1, -1)[0]
|
197 |
-
] for i in range(index.shape[dim])]
|
198 |
-
idx = list(np.concatenate(idx, axis=1))
|
199 |
-
# print("idx",idx)
|
200 |
-
# idx.insert(dim, idx.pop())
|
201 |
-
|
202 |
-
if not np.isscalar(src):
|
203 |
-
src_idx = list(idx)#使idx和src_idx并不是同一个内存空间
|
204 |
-
src_idx.pop(dim)
|
205 |
-
src_idx.insert(dim, np.repeat(np.arange(index.shape[dim]), np.prod(idx_xsection_shape)))
|
206 |
-
dst[tuple(idx)] = src[tuple(src_idx)]
|
207 |
-
else:
|
208 |
-
dst[idx] = src
|
209 |
-
return dst
|
210 |
-
b,c,h,w = content_feature.shape
|
211 |
-
style_feature = F.interpolate(x=style_feature, size=content_feature.shape[-2:],mode="BILINEAR")
|
212 |
-
content_feat = content_feature.reshape([b,c,h*w]).numpy()
|
213 |
-
style_feat = style_feature.reshape([b,c,h*w]).numpy()
|
214 |
-
# print("content_feat",content_feat.shape,b,c)
|
215 |
-
# content_feat = np.reshape(content_feat, (b,c, -1))#(b,c,-1)
|
216 |
-
# style_feat = np.reshape(style_feat, (b,c, -1))#(b,c,-1)
|
217 |
-
# print(content_feat)
|
218 |
-
content_feat_index = np.argsort(content_feat, axis=2)
|
219 |
-
style_feat = np.sort(style_feat, axis=2)
|
220 |
-
# print("content_feat_index",content_feat_index)
|
221 |
-
# print("style_feat",style_feat)
|
222 |
-
fr_feat = scatter_numpy(dim=2, index=content_feat_index, src=style_feat)
|
223 |
-
fr_feat = fr_feat * alpha + content_feat * (1 - alpha)
|
224 |
-
fr_feat = np.reshape(fr_feat, (b,c,h,w))
|
225 |
-
fr_feat = paddle.to_tensor(fr_feat)
|
226 |
-
return fr_feat
|
227 |
-
# @paddle.jit.to_static
|
228 |
-
def forward(self,real_image,style_image,alpha):
|
229 |
-
alpha = alpha.numpy()[0]
|
230 |
-
# print("real_image",real_image.shape)
|
231 |
-
content_feature = self.VGG(real_image)
|
232 |
-
# print("content_feat",content_feature.shape)
|
233 |
-
style_feature = self.VGG(style_image)
|
234 |
-
fr_feat = self.style_projection(content_feature,style_feature,alpha)
|
235 |
-
a = self.A(fr_feat)
|
236 |
-
b = self.B(a)
|
237 |
-
c = self.C(b)
|
238 |
-
d = self.D(c)
|
239 |
-
out = self.out(d)
|
240 |
-
return out
|
|
|
1 |
+
import cv2
|
2 |
import numpy as np
|
|
|
3 |
import paddle
|
|
|
|
|
|
|
|
|
|
|
4 |
import paddle.nn.functional as F
|
5 |
+
from Generater import Generater
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
|
9 |
+
generator = Generater()
|
10 |
+
# # oslist =os.listdir("纹理")
|
11 |
+
# # print(oslist)
|
12 |
+
G_path ='Gmodel_state33003.pdparams'
|
13 |
+
layer_state_dictg = paddle.load(G_path)
|
14 |
+
generator.set_state_dict(layer_state_dictg)#导入训练好的参数文件
|
15 |
+
|
16 |
+
def style_transfer(content_img,style_img):
|
17 |
+
g_input = content_img.astype('float32') / 127.5 - 1 # 归一化
|
18 |
+
g_input = g_input[np.newaxis, ...].transpose(0, 3, 1, 2) # NHWC -> NCHW
|
19 |
+
g_input = paddle.to_tensor(g_input) # numpy -> tensor
|
20 |
+
h,w = g_input.shape[-2:]
|
21 |
+
p = max([h,w])
|
22 |
+
g_input = F.interpolate(g_input,scale_factor=(256/p))
|
23 |
+
|
24 |
+
g_input_s = style_img.astype('float32') / 127.5 - 1 # 归一化
|
25 |
+
g_input_s = g_input_s[np.newaxis, ...].transpose(0, 3, 1, 2) # NHWC -> NCHW
|
26 |
+
g_input_s = paddle.to_tensor(g_input_s) # numpy -> tensor
|
27 |
+
h,w = g_input_s.shape[-2:]
|
28 |
+
p = max([h,w])
|
29 |
+
g_input_s = F.interpolate(g_input_s,scale_factor=(256/p))
|
30 |
+
|
31 |
+
i = paddle.to_tensor([1.])
|
32 |
+
g_output = generator(g_input,g_input_s,i)
|
33 |
+
g_output = g_output.detach().numpy() # tensor -> numpy
|
34 |
+
g_output = g_output.transpose(0, 2, 3, 1)[0] # NCHW -> NHWC
|
35 |
+
g_output = (g_output+1) *127.5 # 反归一化
|
36 |
+
g_output = g_output.astype(np.uint8)
|
37 |
+
output = g_output
|
38 |
+
# cv2.imwrite(os.path.join("./test", str(i.numpy()[0])+'qt.png'), g_output)#保存图片到本地
|
39 |
+
return output
|
40 |
+
|
41 |
+
interface = gr.Interface(fn=style_transfer, inputs=["image","image"], outputs="image")
|
42 |
+
interface.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|