Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

FaceEnhancer.py 13 KB

You have to be logged in to leave a comment. Sign In
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
  1. import operator
  2. from pathlib import Path
  3. import cv2
  4. import numpy as np
  5. from core.leras import nn
  6. class FaceEnhancer(object):
  7. """
  8. x4 face enhancer
  9. """
  10. def __init__(self, place_model_on_cpu=False, run_on_cpu=False):
  11. nn.initialize(data_format="NHWC")
  12. tf = nn.tf
  13. class FaceEnhancer (nn.ModelBase):
  14. def __init__(self, name='FaceEnhancer'):
  15. super().__init__(name=name)
  16. def on_build(self):
  17. self.conv1 = nn.Conv2D (3, 64, kernel_size=3, strides=1, padding='SAME')
  18. self.dense1 = nn.Dense (1, 64, use_bias=False)
  19. self.dense2 = nn.Dense (1, 64, use_bias=False)
  20. self.e0_conv0 = nn.Conv2D (64, 64, kernel_size=3, strides=1, padding='SAME')
  21. self.e0_conv1 = nn.Conv2D (64, 64, kernel_size=3, strides=1, padding='SAME')
  22. self.e1_conv0 = nn.Conv2D (64, 112, kernel_size=3, strides=1, padding='SAME')
  23. self.e1_conv1 = nn.Conv2D (112, 112, kernel_size=3, strides=1, padding='SAME')
  24. self.e2_conv0 = nn.Conv2D (112, 192, kernel_size=3, strides=1, padding='SAME')
  25. self.e2_conv1 = nn.Conv2D (192, 192, kernel_size=3, strides=1, padding='SAME')
  26. self.e3_conv0 = nn.Conv2D (192, 336, kernel_size=3, strides=1, padding='SAME')
  27. self.e3_conv1 = nn.Conv2D (336, 336, kernel_size=3, strides=1, padding='SAME')
  28. self.e4_conv0 = nn.Conv2D (336, 512, kernel_size=3, strides=1, padding='SAME')
  29. self.e4_conv1 = nn.Conv2D (512, 512, kernel_size=3, strides=1, padding='SAME')
  30. self.center_conv0 = nn.Conv2D (512, 512, kernel_size=3, strides=1, padding='SAME')
  31. self.center_conv1 = nn.Conv2D (512, 512, kernel_size=3, strides=1, padding='SAME')
  32. self.center_conv2 = nn.Conv2D (512, 512, kernel_size=3, strides=1, padding='SAME')
  33. self.center_conv3 = nn.Conv2D (512, 512, kernel_size=3, strides=1, padding='SAME')
  34. self.d4_conv0 = nn.Conv2D (1024, 512, kernel_size=3, strides=1, padding='SAME')
  35. self.d4_conv1 = nn.Conv2D (512, 512, kernel_size=3, strides=1, padding='SAME')
  36. self.d3_conv0 = nn.Conv2D (848, 512, kernel_size=3, strides=1, padding='SAME')
  37. self.d3_conv1 = nn.Conv2D (512, 512, kernel_size=3, strides=1, padding='SAME')
  38. self.d2_conv0 = nn.Conv2D (704, 288, kernel_size=3, strides=1, padding='SAME')
  39. self.d2_conv1 = nn.Conv2D (288, 288, kernel_size=3, strides=1, padding='SAME')
  40. self.d1_conv0 = nn.Conv2D (400, 160, kernel_size=3, strides=1, padding='SAME')
  41. self.d1_conv1 = nn.Conv2D (160, 160, kernel_size=3, strides=1, padding='SAME')
  42. self.d0_conv0 = nn.Conv2D (224, 96, kernel_size=3, strides=1, padding='SAME')
  43. self.d0_conv1 = nn.Conv2D (96, 96, kernel_size=3, strides=1, padding='SAME')
  44. self.out1x_conv0 = nn.Conv2D (96, 48, kernel_size=3, strides=1, padding='SAME')
  45. self.out1x_conv1 = nn.Conv2D (48, 3, kernel_size=3, strides=1, padding='SAME')
  46. self.dec2x_conv0 = nn.Conv2D (96, 96, kernel_size=3, strides=1, padding='SAME')
  47. self.dec2x_conv1 = nn.Conv2D (96, 96, kernel_size=3, strides=1, padding='SAME')
  48. self.out2x_conv0 = nn.Conv2D (96, 48, kernel_size=3, strides=1, padding='SAME')
  49. self.out2x_conv1 = nn.Conv2D (48, 3, kernel_size=3, strides=1, padding='SAME')
  50. self.dec4x_conv0 = nn.Conv2D (96, 72, kernel_size=3, strides=1, padding='SAME')
  51. self.dec4x_conv1 = nn.Conv2D (72, 72, kernel_size=3, strides=1, padding='SAME')
  52. self.out4x_conv0 = nn.Conv2D (72, 36, kernel_size=3, strides=1, padding='SAME')
  53. self.out4x_conv1 = nn.Conv2D (36, 3 , kernel_size=3, strides=1, padding='SAME')
  54. def forward(self, inp):
  55. bgr, param, param1 = inp
  56. x = self.conv1(bgr)
  57. a = self.dense1(param)
  58. a = tf.reshape(a, (-1,1,1,64) )
  59. b = self.dense2(param1)
  60. b = tf.reshape(b, (-1,1,1,64) )
  61. x = tf.nn.leaky_relu(x+a+b, 0.1)
  62. x = tf.nn.leaky_relu(self.e0_conv0(x), 0.1)
  63. x = e0 = tf.nn.leaky_relu(self.e0_conv1(x), 0.1)
  64. x = tf.nn.avg_pool(x, [1,2,2,1], [1,2,2,1], "VALID")
  65. x = tf.nn.leaky_relu(self.e1_conv0(x), 0.1)
  66. x = e1 = tf.nn.leaky_relu(self.e1_conv1(x), 0.1)
  67. x = tf.nn.avg_pool(x, [1,2,2,1], [1,2,2,1], "VALID")
  68. x = tf.nn.leaky_relu(self.e2_conv0(x), 0.1)
  69. x = e2 = tf.nn.leaky_relu(self.e2_conv1(x), 0.1)
  70. x = tf.nn.avg_pool(x, [1,2,2,1], [1,2,2,1], "VALID")
  71. x = tf.nn.leaky_relu(self.e3_conv0(x), 0.1)
  72. x = e3 = tf.nn.leaky_relu(self.e3_conv1(x), 0.1)
  73. x = tf.nn.avg_pool(x, [1,2,2,1], [1,2,2,1], "VALID")
  74. x = tf.nn.leaky_relu(self.e4_conv0(x), 0.1)
  75. x = e4 = tf.nn.leaky_relu(self.e4_conv1(x), 0.1)
  76. x = tf.nn.avg_pool(x, [1,2,2,1], [1,2,2,1], "VALID")
  77. x = tf.nn.leaky_relu(self.center_conv0(x), 0.1)
  78. x = tf.nn.leaky_relu(self.center_conv1(x), 0.1)
  79. x = tf.nn.leaky_relu(self.center_conv2(x), 0.1)
  80. x = tf.nn.leaky_relu(self.center_conv3(x), 0.1)
  81. x = tf.concat( [nn.resize2d_bilinear(x), e4], -1 )
  82. x = tf.nn.leaky_relu(self.d4_conv0(x), 0.1)
  83. x = tf.nn.leaky_relu(self.d4_conv1(x), 0.1)
  84. x = tf.concat( [nn.resize2d_bilinear(x), e3], -1 )
  85. x = tf.nn.leaky_relu(self.d3_conv0(x), 0.1)
  86. x = tf.nn.leaky_relu(self.d3_conv1(x), 0.1)
  87. x = tf.concat( [nn.resize2d_bilinear(x), e2], -1 )
  88. x = tf.nn.leaky_relu(self.d2_conv0(x), 0.1)
  89. x = tf.nn.leaky_relu(self.d2_conv1(x), 0.1)
  90. x = tf.concat( [nn.resize2d_bilinear(x), e1], -1 )
  91. x = tf.nn.leaky_relu(self.d1_conv0(x), 0.1)
  92. x = tf.nn.leaky_relu(self.d1_conv1(x), 0.1)
  93. x = tf.concat( [nn.resize2d_bilinear(x), e0], -1 )
  94. x = tf.nn.leaky_relu(self.d0_conv0(x), 0.1)
  95. x = d0 = tf.nn.leaky_relu(self.d0_conv1(x), 0.1)
  96. x = tf.nn.leaky_relu(self.out1x_conv0(x), 0.1)
  97. x = self.out1x_conv1(x)
  98. out1x = bgr + tf.nn.tanh(x)
  99. x = d0
  100. x = tf.nn.leaky_relu(self.dec2x_conv0(x), 0.1)
  101. x = tf.nn.leaky_relu(self.dec2x_conv1(x), 0.1)
  102. x = d2x = nn.resize2d_bilinear(x)
  103. x = tf.nn.leaky_relu(self.out2x_conv0(x), 0.1)
  104. x = self.out2x_conv1(x)
  105. out2x = nn.resize2d_bilinear(out1x) + tf.nn.tanh(x)
  106. x = d2x
  107. x = tf.nn.leaky_relu(self.dec4x_conv0(x), 0.1)
  108. x = tf.nn.leaky_relu(self.dec4x_conv1(x), 0.1)
  109. x = d4x = nn.resize2d_bilinear(x)
  110. x = tf.nn.leaky_relu(self.out4x_conv0(x), 0.1)
  111. x = self.out4x_conv1(x)
  112. out4x = nn.resize2d_bilinear(out2x) + tf.nn.tanh(x)
  113. return out4x
  114. model_path = Path(__file__).parent / "FaceEnhancer.npy"
  115. if not model_path.exists():
  116. raise Exception("Unable to load FaceEnhancer.npy")
  117. with tf.device ('/CPU:0' if place_model_on_cpu else nn.tf_default_device_name):
  118. self.model = FaceEnhancer()
  119. self.model.load_weights (model_path)
  120. with tf.device ('/CPU:0' if run_on_cpu else nn.tf_default_device_name):
  121. self.model.build_for_run ([ (tf.float32, nn.get4Dshape (192,192,3) ),
  122. (tf.float32, (None,1,) ),
  123. (tf.float32, (None,1,) ),
  124. ])
  125. def enhance (self, inp_img, is_tanh=False, preserve_size=True):
  126. if not is_tanh:
  127. inp_img = np.clip( inp_img * 2 -1, -1, 1 )
  128. param = np.array([0.2])
  129. param1 = np.array([1.0])
  130. up_res = 4
  131. patch_size = 192
  132. patch_size_half = patch_size // 2
  133. ih,iw,ic = inp_img.shape
  134. h,w,c = ih,iw,ic
  135. th,tw = h*up_res, w*up_res
  136. t_padding = 0
  137. b_padding = 0
  138. l_padding = 0
  139. r_padding = 0
  140. if h < patch_size:
  141. t_padding = (patch_size-h)//2
  142. b_padding = (patch_size-h) - t_padding
  143. if w < patch_size:
  144. l_padding = (patch_size-w)//2
  145. r_padding = (patch_size-w) - l_padding
  146. if t_padding != 0:
  147. inp_img = np.concatenate ([ np.zeros ( (t_padding,w,c), dtype=np.float32 ), inp_img ], axis=0 )
  148. h,w,c = inp_img.shape
  149. if b_padding != 0:
  150. inp_img = np.concatenate ([ inp_img, np.zeros ( (b_padding,w,c), dtype=np.float32 ) ], axis=0 )
  151. h,w,c = inp_img.shape
  152. if l_padding != 0:
  153. inp_img = np.concatenate ([ np.zeros ( (h,l_padding,c), dtype=np.float32 ), inp_img ], axis=1 )
  154. h,w,c = inp_img.shape
  155. if r_padding != 0:
  156. inp_img = np.concatenate ([ inp_img, np.zeros ( (h,r_padding,c), dtype=np.float32 ) ], axis=1 )
  157. h,w,c = inp_img.shape
  158. i_max = w-patch_size+1
  159. j_max = h-patch_size+1
  160. final_img = np.zeros ( (h*up_res,w*up_res,c), dtype=np.float32 )
  161. final_img_div = np.zeros ( (h*up_res,w*up_res,1), dtype=np.float32 )
  162. x = np.concatenate ( [ np.linspace (0,1,patch_size_half*up_res), np.linspace (1,0,patch_size_half*up_res) ] )
  163. x,y = np.meshgrid(x,x)
  164. patch_mask = (x*y)[...,None]
  165. j=0
  166. while j < j_max:
  167. i = 0
  168. while i < i_max:
  169. patch_img = inp_img[j:j+patch_size, i:i+patch_size,:]
  170. x = self.model.run( [ patch_img[None,...], [param], [param1] ] )[0]
  171. final_img [j*up_res:(j+patch_size)*up_res, i*up_res:(i+patch_size)*up_res,:] += x*patch_mask
  172. final_img_div[j*up_res:(j+patch_size)*up_res, i*up_res:(i+patch_size)*up_res,:] += patch_mask
  173. if i == i_max-1:
  174. break
  175. i = min( i+patch_size_half, i_max-1)
  176. if j == j_max-1:
  177. break
  178. j = min( j+patch_size_half, j_max-1)
  179. final_img_div[final_img_div==0] = 1.0
  180. final_img /= final_img_div
  181. if t_padding+b_padding+l_padding+r_padding != 0:
  182. final_img = final_img [t_padding*up_res:(h-b_padding)*up_res, l_padding*up_res:(w-r_padding)*up_res,:]
  183. if preserve_size:
  184. final_img = cv2.resize (final_img, (iw,ih), interpolation=cv2.INTER_LANCZOS4)
  185. if not is_tanh:
  186. final_img = np.clip( final_img/2+0.5, 0, 1 )
  187. return final_img
  188. """
  189. def enhance (self, inp_img, is_tanh=False, preserve_size=True):
  190. if not is_tanh:
  191. inp_img = np.clip( inp_img * 2 -1, -1, 1 )
  192. param = np.array([0.2])
  193. param1 = np.array([1.0])
  194. up_res = 4
  195. patch_size = 192
  196. patch_size_half = patch_size // 2
  197. h,w,c = inp_img.shape
  198. th,tw = h*up_res, w*up_res
  199. preupscale_rate = 1.0
  200. if h < patch_size or w < patch_size:
  201. preupscale_rate = 1.0 / ( max(h,w) / patch_size )
  202. if preupscale_rate != 1.0:
  203. inp_img = cv2.resize (inp_img, ( int(w*preupscale_rate), int(h*preupscale_rate) ), interpolation=cv2.INTER_LANCZOS4)
  204. h,w,c = inp_img.shape
  205. i_max = w-patch_size+1
  206. j_max = h-patch_size+1
  207. final_img = np.zeros ( (h*up_res,w*up_res,c), dtype=np.float32 )
  208. final_img_div = np.zeros ( (h*up_res,w*up_res,1), dtype=np.float32 )
  209. x = np.concatenate ( [ np.linspace (0,1,patch_size_half*up_res), np.linspace (1,0,patch_size_half*up_res) ] )
  210. x,y = np.meshgrid(x,x)
  211. patch_mask = (x*y)[...,None]
  212. j=0
  213. while j < j_max:
  214. i = 0
  215. while i < i_max:
  216. patch_img = inp_img[j:j+patch_size, i:i+patch_size,:]
  217. x = self.model.run( [ patch_img[None,...], [param], [param1] ] )[0]
  218. final_img [j*up_res:(j+patch_size)*up_res, i*up_res:(i+patch_size)*up_res,:] += x*patch_mask
  219. final_img_div[j*up_res:(j+patch_size)*up_res, i*up_res:(i+patch_size)*up_res,:] += patch_mask
  220. if i == i_max-1:
  221. break
  222. i = min( i+patch_size_half, i_max-1)
  223. if j == j_max-1:
  224. break
  225. j = min( j+patch_size_half, j_max-1)
  226. final_img_div[final_img_div==0] = 1.0
  227. final_img /= final_img_div
  228. if preserve_size:
  229. final_img = cv2.resize (final_img, (w,h), interpolation=cv2.INTER_LANCZOS4)
  230. else:
  231. if preupscale_rate != 1.0:
  232. final_img = cv2.resize (final_img, (tw,th), interpolation=cv2.INTER_LANCZOS4)
  233. if not is_tanh:
  234. final_img = np.clip( final_img/2+0.5, 0, 1 )
  235. return final_img
  236. """
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...