akhaliq3 commited on
Commit
f7acea1
1 Parent(s): ff27b25

spaces demo

Browse files
LICENSE ADDED
@@ -0,0 +1,351 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Tencent is pleased to support the open source community by making GFPGAN available.
2
+
3
+ Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
4
+
5
+ GFPGAN is licensed under the Apache License Version 2.0 except for the third-party components listed below.
6
+
7
+
8
+ Terms of the Apache License Version 2.0:
9
+ ---------------------------------------------
10
+ Apache License
11
+
12
+ Version 2.0, January 2004
13
+
14
+ http://www.apache.org/licenses/
15
+
16
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
17
+ 1. Definitions.
18
+
19
+ “License” shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
20
+
21
+ “Licensor” shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
22
+
23
+ “Legal Entity” shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, “control” means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
24
+
25
+ “You” (or “Your”) shall mean an individual or Legal Entity exercising permissions granted by this License.
26
+
27
+ “Source” form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
28
+
29
+ “Object” form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
30
+
31
+ “Work” shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
32
+
33
+ “Derivative Works” shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
34
+
35
+ “Contribution” shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, “submitted” means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as “Not a Contribution.”
36
+
37
+ “Contributor” shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
38
+
39
+ 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
40
+
41
+ 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
42
+
43
+ 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
44
+
45
+ You must give any other recipients of the Work or Derivative Works a copy of this License; and
46
+
47
+ You must cause any modified files to carry prominent notices stating that You changed the files; and
48
+
49
+ You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
50
+
51
+ If the Work includes a “NOTICE” text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
52
+
53
+ You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
54
+
55
+ 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
56
+
57
+ 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
58
+
59
+ 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
60
+
61
+ 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
62
+
63
+ 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
64
+
65
+ END OF TERMS AND CONDITIONS
66
+
67
+
68
+
69
+ Other dependencies and licenses:
70
+
71
+
72
+ Open Source Software licensed under the Apache 2.0 license and Other Licenses of the Third-Party Components therein:
73
+ ---------------------------------------------
74
+ 1. basicsr
75
+ Copyright 2018-2020 BasicSR Authors
76
+
77
+
78
+ This BasicSR project is released under the Apache 2.0 license.
79
+
80
+ A copy of Apache 2.0 is included in this file.
81
+
82
+ StyleGAN2
83
+ The codes are modified from the repository stylegan2-pytorch. Many thanks to the author - Kim Seonghyeon 😊 for translating from the official TensorFlow codes to PyTorch ones. Here is the license of stylegan2-pytorch.
84
+ The official repository is https://github.com/NVlabs/stylegan2, and here is the NVIDIA license.
85
+ DFDNet
86
+ The codes are largely modified from the repository DFDNet. Their license is Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
87
+
88
+ Terms of the Nvidia License:
89
+ ---------------------------------------------
90
+
91
+ 1. Definitions
92
+
93
+ "Licensor" means any person or entity that distributes its Work.
94
+
95
+ "Software" means the original work of authorship made available under
96
+ this License.
97
+
98
+ "Work" means the Software and any additions to or derivative works of
99
+ the Software that are made available under this License.
100
+
101
+ "Nvidia Processors" means any central processing unit (CPU), graphics
102
+ processing unit (GPU), field-programmable gate array (FPGA),
103
+ application-specific integrated circuit (ASIC) or any combination
104
+ thereof designed, made, sold, or provided by Nvidia or its affiliates.
105
+
106
+ The terms "reproduce," "reproduction," "derivative works," and
107
+ "distribution" have the meaning as provided under U.S. copyright law;
108
+ provided, however, that for the purposes of this License, derivative
109
+ works shall not include works that remain separable from, or merely
110
+ link (or bind by name) to the interfaces of, the Work.
111
+
112
+ Works, including the Software, are "made available" under this License
113
+ by including in or with the Work either (a) a copyright notice
114
+ referencing the applicability of this License to the Work, or (b) a
115
+ copy of this License.
116
+
117
+ 2. License Grants
118
+
119
+ 2.1 Copyright Grant. Subject to the terms and conditions of this
120
+ License, each Licensor grants to you a perpetual, worldwide,
121
+ non-exclusive, royalty-free, copyright license to reproduce,
122
+ prepare derivative works of, publicly display, publicly perform,
123
+ sublicense and distribute its Work and any resulting derivative
124
+ works in any form.
125
+
126
+ 3. Limitations
127
+
128
+ 3.1 Redistribution. You may reproduce or distribute the Work only
129
+ if (a) you do so under this License, (b) you include a complete
130
+ copy of this License with your distribution, and (c) you retain
131
+ without modification any copyright, patent, trademark, or
132
+ attribution notices that are present in the Work.
133
+
134
+ 3.2 Derivative Works. You may specify that additional or different
135
+ terms apply to the use, reproduction, and distribution of your
136
+ derivative works of the Work ("Your Terms") only if (a) Your Terms
137
+ provide that the use limitation in Section 3.3 applies to your
138
+ derivative works, and (b) you identify the specific derivative
139
+ works that are subject to Your Terms. Notwithstanding Your Terms,
140
+ this License (including the redistribution requirements in Section
141
+ 3.1) will continue to apply to the Work itself.
142
+
143
+ 3.3 Use Limitation. The Work and any derivative works thereof only
144
+ may be used or intended for use non-commercially. The Work or
145
+ derivative works thereof may be used or intended for use by Nvidia
146
+ or its affiliates commercially or non-commercially. As used herein,
147
+ "non-commercially" means for research or evaluation purposes only.
148
+
149
+ 3.4 Patent Claims. If you bring or threaten to bring a patent claim
150
+ against any Licensor (including any claim, cross-claim or
151
+ counterclaim in a lawsuit) to enforce any patents that you allege
152
+ are infringed by any Work, then your rights under this License from
153
+ such Licensor (including the grants in Sections 2.1 and 2.2) will
154
+ terminate immediately.
155
+
156
+ 3.5 Trademarks. This License does not grant any rights to use any
157
+ Licensor's or its affiliates' names, logos, or trademarks, except
158
+ as necessary to reproduce the notices described in this License.
159
+
160
+ 3.6 Termination. If you violate any term of this License, then your
161
+ rights under this License (including the grants in Sections 2.1 and
162
+ 2.2) will terminate immediately.
163
+
164
+ 4. Disclaimer of Warranty.
165
+
166
+ THE WORK IS PROVIDED "AS IS" WITHOUT WARRANTIES OR CONDITIONS OF ANY
167
+ KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WARRANTIES OR CONDITIONS OF
168
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE OR
169
+ NON-INFRINGEMENT. YOU BEAR THE RISK OF UNDERTAKING ANY ACTIVITIES UNDER
170
+ THIS LICENSE.
171
+
172
+ 5. Limitation of Liability.
173
+
174
+ EXCEPT AS PROHIBITED BY APPLICABLE LAW, IN NO EVENT AND UNDER NO LEGAL
175
+ THEORY, WHETHER IN TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE
176
+ SHALL ANY LICENSOR BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY DIRECT,
177
+ INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
178
+ OR RELATED TO THIS LICENSE, THE USE OR INABILITY TO USE THE WORK
179
+ (INCLUDING BUT NOT LIMITED TO LOSS OF GOODWILL, BUSINESS INTERRUPTION,
180
+ LOST PROFITS OR DATA, COMPUTER FAILURE OR MALFUNCTION, OR ANY OTHER
181
+ COMMERCIAL DAMAGES OR LOSSES), EVEN IF THE LICENSOR HAS BEEN ADVISED OF
182
+ THE POSSIBILITY OF SUCH DAMAGES.
183
+
184
+ MIT License
185
+
186
+ Copyright (c) 2019 Kim Seonghyeon
187
+
188
+ Permission is hereby granted, free of charge, to any person obtaining a copy
189
+ of this software and associated documentation files (the "Software"), to deal
190
+ in the Software without restriction, including without limitation the rights
191
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
192
+ copies of the Software, and to permit persons to whom the Software is
193
+ furnished to do so, subject to the following conditions:
194
+
195
+ The above copyright notice and this permission notice shall be included in all
196
+ copies or substantial portions of the Software.
197
+
198
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
199
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
200
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
201
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
202
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
203
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
204
+ SOFTWARE.
205
+
206
+
207
+
208
+ Open Source Software licensed under the BSD 3-Clause license:
209
+ ---------------------------------------------
210
+ 1. torchvision
211
+ Copyright (c) Soumith Chintala 2016,
212
+ All rights reserved.
213
+
214
+ 2. torch
215
+ Copyright (c) 2016- Facebook, Inc (Adam Paszke)
216
+ Copyright (c) 2014- Facebook, Inc (Soumith Chintala)
217
+ Copyright (c) 2011-2014 Idiap Research Institute (Ronan Collobert)
218
+ Copyright (c) 2012-2014 Deepmind Technologies (Koray Kavukcuoglu)
219
+ Copyright (c) 2011-2012 NEC Laboratories America (Koray Kavukcuoglu)
220
+ Copyright (c) 2011-2013 NYU (Clement Farabet)
221
+ Copyright (c) 2006-2010 NEC Laboratories America (Ronan Collobert, Leon Bottou, Iain Melvin, Jason Weston)
222
+ Copyright (c) 2006 Idiap Research Institute (Samy Bengio)
223
+ Copyright (c) 2001-2004 Idiap Research Institute (Ronan Collobert, Samy Bengio, Johnny Mariethoz)
224
+
225
+
226
+ Terms of the BSD 3-Clause License:
227
+ ---------------------------------------------
228
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
229
+
230
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
231
+
232
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
233
+
234
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
235
+
236
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
237
+
238
+
239
+
240
+ Open Source Software licensed under the BSD 3-Clause License and Other Licenses of the Third-Party Components therein:
241
+ ---------------------------------------------
242
+ 1. numpy
243
+ Copyright (c) 2005-2020, NumPy Developers.
244
+ All rights reserved.
245
+
246
+ A copy of BSD 3-Clause License is included in this file.
247
+
248
+ The NumPy repository and source distributions bundle several libraries that are
249
+ compatibly licensed. We list these here.
250
+
251
+ Name: Numpydoc
252
+ Files: doc/sphinxext/numpydoc/*
253
+ License: BSD-2-Clause
254
+ For details, see doc/sphinxext/LICENSE.txt
255
+
256
+ Name: scipy-sphinx-theme
257
+ Files: doc/scipy-sphinx-theme/*
258
+ License: BSD-3-Clause AND PSF-2.0 AND Apache-2.0
259
+ For details, see doc/scipy-sphinx-theme/LICENSE.txt
260
+
261
+ Name: lapack-lite
262
+ Files: numpy/linalg/lapack_lite/*
263
+ License: BSD-3-Clause
264
+ For details, see numpy/linalg/lapack_lite/LICENSE.txt
265
+
266
+ Name: tempita
267
+ Files: tools/npy_tempita/*
268
+ License: MIT
269
+ For details, see tools/npy_tempita/license.txt
270
+
271
+ Name: dragon4
272
+ Files: numpy/core/src/multiarray/dragon4.c
273
+ License: MIT
274
+ For license text, see numpy/core/src/multiarray/dragon4.c
275
+
276
+
277
+
278
+ Open Source Software licensed under the MIT license:
279
+ ---------------------------------------------
280
+ 1. facexlib
281
+ Copyright (c) 2020 Xintao Wang
282
+
283
+ 2. opencv-python
284
+ Copyright (c) Olli-Pekka Heinisuo
285
+ Please note that only files in cv2 package are used.
286
+
287
+
288
+ Terms of the MIT License:
289
+ ---------------------------------------------
290
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
291
+
292
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
293
+
294
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
295
+
296
+
297
+
298
+ Open Source Software licensed under the MIT license and Other Licenses of the Third-Party Components therein:
299
+ ---------------------------------------------
300
+ 1. tqdm
301
+ Copyright (c) 2013 noamraph
302
+
303
+ `tqdm` is a product of collaborative work.
304
+ Unless otherwise stated, all authors (see commit logs) retain copyright
305
+ for their respective work, and release the work under the MIT licence
306
+ (text below).
307
+
308
+ Exceptions or notable authors are listed below
309
+ in reverse chronological order:
310
+
311
+ * files: *
312
+ MPLv2.0 2015-2020 (c) Casper da Costa-Luis
313
+ [casperdcl](https://github.com/casperdcl).
314
+ * files: tqdm/_tqdm.py
315
+ MIT 2016 (c) [PR #96] on behalf of Google Inc.
316
+ * files: tqdm/_tqdm.py setup.py README.rst MANIFEST.in .gitignore
317
+ MIT 2013 (c) Noam Yorav-Raphael, original author.
318
+
319
+ [PR #96]: https://github.com/tqdm/tqdm/pull/96
320
+
321
+
322
+ Mozilla Public Licence (MPL) v. 2.0 - Exhibit A
323
+ -----------------------------------------------
324
+
325
+ This Source Code Form is subject to the terms of the
326
+ Mozilla Public License, v. 2.0.
327
+ If a copy of the MPL was not distributed with this file,
328
+ You can obtain one at https://mozilla.org/MPL/2.0/.
329
+
330
+
331
+ MIT License (MIT)
332
+ -----------------
333
+
334
+ Copyright (c) 2013 noamraph
335
+
336
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
337
+ this software and associated documentation files (the "Software"), to deal in
338
+ the Software without restriction, including without limitation the rights to
339
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
340
+ the Software, and to permit persons to whom the Software is furnished to do so,
341
+ subject to the following conditions:
342
+
343
+ The above copyright notice and this permission notice shall be included in all
344
+ copies or substantial portions of the Software.
345
+
346
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
347
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
348
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
349
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
350
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
351
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
MANIFEST.in ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
1
+ include assets/*
2
+ include inputs/*
3
+ include scripts/*.py
4
+ include inference_gfpgan.py
5
+ include VERSION
6
+ include LICENSE
7
+ include requirements.txt
8
+ include gfpgan/weights/README.md
PaperModel.md ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Installation
2
+
3
+ We now provide a *clean* version of GFPGAN, which does not require customized CUDA extensions. See [here](README.md#installation) for this easier installation.<br>
4
+ If you want want to use the original model in our paper, please follow the instructions below.
5
+
6
+ 1. Clone repo
7
+
8
+ ```bash
9
+ git clone https://github.com/xinntao/GFPGAN.git
10
+ cd GFPGAN
11
+ ```
12
+
13
+ 1. Install dependent packages
14
+
15
+ As StyleGAN2 uses customized PyTorch C++ extensions, you need to **compile them during installation** or **load them just-in-time(JIT)**.
16
+ You can refer to [BasicSR-INSTALL.md](https://github.com/xinntao/BasicSR/blob/master/INSTALL.md) for more details.
17
+
18
+ **Option 1: Load extensions just-in-time(JIT)** (For those just want to do simple inferences, may have less issues)
19
+
20
+ ```bash
21
+ # Install basicsr - https://github.com/xinntao/BasicSR
22
+ # We use BasicSR for both training and inference
23
+ pip install basicsr
24
+
25
+ # Install facexlib - https://github.com/xinntao/facexlib
26
+ # We use face detection and face restoration helper in the facexlib package
27
+ pip install facexlib
28
+
29
+ pip install -r requirements.txt
30
+ python setup.py develop
31
+
32
+ # remember to set BASICSR_JIT=True before your running commands
33
+ ```
34
+
35
+ **Option 2: Compile extensions during installation** (For those need to train/inference for many times)
36
+
37
+ ```bash
38
+ # Install basicsr - https://github.com/xinntao/BasicSR
39
+ # We use BasicSR for both training and inference
40
+ # Set BASICSR_EXT=True to compile the cuda extensions in the BasicSR - It may take several minutes to compile, please be patient
41
+ # Add -vvv for detailed log prints
42
+ BASICSR_EXT=True pip install basicsr -vvv
43
+
44
+ # Install facexlib - https://github.com/xinntao/facexlib
45
+ # We use face detection and face restoration helper in the facexlib package
46
+ pip install facexlib
47
+
48
+ pip install -r requirements.txt
49
+ python setup.py develop
50
+ ```
51
+
52
+ ## :zap: Quick Inference
53
+
54
+ Download pre-trained models: [GFPGANv1.pth](https://github.com/TencentARC/GFPGAN/releases/download/v0.1.0/GFPGANv1.pth)
55
+
56
+ ```bash
57
+ wget https://github.com/TencentARC/GFPGAN/releases/download/v0.1.0/GFPGANv1.pth -P experiments/pretrained_models
58
+ ```
59
+
60
+ - Option 1: Load extensions just-in-time(JIT)
61
+
62
+ ```bash
63
+ BASICSR_JIT=True python inference_gfpgan.py --model_path experiments/pretrained_models/GFPGANv1.pth --test_path inputs/whole_imgs --save_root results --arch original --channel 1
64
+
65
+ # for aligned images
66
+ BASICSR_JIT=True python inference_gfpgan.py --model_path experiments/pretrained_models/GFPGANv1.pth --test_path inputs/cropped_faces --save_root results --arch original --channel 1 --aligned
67
+ ```
68
+
69
+ - Option 2: Have successfully compiled extensions during installation
70
+
71
+ ```bash
72
+ python inference_gfpgan.py --model_path experiments/pretrained_models/GFPGANv1.pth --test_path inputs/whole_imgs --save_root results --arch original --channel 1
73
+
74
+ # for aligned images
75
+ python inference_gfpgan.py --model_path experiments/pretrained_models/GFPGANv1.pth --test_path inputs/cropped_faces --save_root results --arch original --channel 1 --aligned
76
+ ```
VERSION ADDED
@@ -0,0 +1 @@
 
1
+ 0.2.1
experiments/.DS_Store ADDED
Binary file (6.15 kB). View file
experiments/pretrained_models/README.md ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
1
+ # Pre-trained Models and Other Data
2
+
3
+ Download pre-trained models and other data. Put them in this folder.
4
+
5
+ 1. [Pretrained StyleGAN2 model: StyleGAN2_512_Cmul1_FFHQ_B12G4_scratch_800k.pth](https://github.com/TencentARC/GFPGAN/releases/download/v0.1.0/StyleGAN2_512_Cmul1_FFHQ_B12G4_scratch_800k.pth)
6
+ 1. [Component locations of FFHQ: FFHQ_eye_mouth_landmarks_512.pth](https://github.com/TencentARC/GFPGAN/releases/download/v0.1.0/FFHQ_eye_mouth_landmarks_512.pth)
7
+ 1. [A simple ArcFace model: arcface_resnet18.pth](https://github.com/TencentARC/GFPGAN/releases/download/v0.1.0/arcface_resnet18.pth)
gfpgan/__init__.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
1
+ # flake8: noqa
2
+ from .archs import *
3
+ from .data import *
4
+ from .models import *
5
+ from .utils import *
6
+ from .version import __gitsha__, __version__
gfpgan/archs/__init__.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
1
+ import importlib
2
+ from basicsr.utils import scandir
3
+ from os import path as osp
4
+
5
+ # automatically scan and import arch modules for registry
6
+ # scan all the files that end with '_arch.py' under the archs folder
7
+ arch_folder = osp.dirname(osp.abspath(__file__))
8
+ arch_filenames = [osp.splitext(osp.basename(v))[0] for v in scandir(arch_folder) if v.endswith('_arch.py')]
9
+ # import all the arch modules
10
+ _arch_modules = [importlib.import_module(f'gfpgan.archs.{file_name}') for file_name in arch_filenames]
gfpgan/archs/arcface_arch.py ADDED
@@ -0,0 +1,197 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch.nn as nn
2
+ from basicsr.utils.registry import ARCH_REGISTRY
3
+
4
+
5
+ def conv3x3(in_planes, out_planes, stride=1):
6
+ """3x3 convolution with padding"""
7
+ return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False)
8
+
9
+
10
+ class BasicBlock(nn.Module):
11
+ expansion = 1
12
+
13
+ def __init__(self, inplanes, planes, stride=1, downsample=None):
14
+ super(BasicBlock, self).__init__()
15
+ self.conv1 = conv3x3(inplanes, planes, stride)
16
+ self.bn1 = nn.BatchNorm2d(planes)
17
+ self.relu = nn.ReLU(inplace=True)
18
+ self.conv2 = conv3x3(planes, planes)
19
+ self.bn2 = nn.BatchNorm2d(planes)
20
+ self.downsample = downsample
21
+ self.stride = stride
22
+
23
+ def forward(self, x):
24
+ residual = x
25
+
26
+ out = self.conv1(x)
27
+ out = self.bn1(out)
28
+ out = self.relu(out)
29
+
30
+ out = self.conv2(out)
31
+ out = self.bn2(out)
32
+
33
+ if self.downsample is not None:
34
+ residual = self.downsample(x)
35
+
36
+ out += residual
37
+ out = self.relu(out)
38
+
39
+ return out
40
+
41
+
42
+ class IRBlock(nn.Module):
43
+ expansion = 1
44
+
45
+ def __init__(self, inplanes, planes, stride=1, downsample=None, use_se=True):
46
+ super(IRBlock, self).__init__()
47
+ self.bn0 = nn.BatchNorm2d(inplanes)
48
+ self.conv1 = conv3x3(inplanes, inplanes)
49
+ self.bn1 = nn.BatchNorm2d(inplanes)
50
+ self.prelu = nn.PReLU()
51
+ self.conv2 = conv3x3(inplanes, planes, stride)
52
+ self.bn2 = nn.BatchNorm2d(planes)
53
+ self.downsample = downsample
54
+ self.stride = stride
55
+ self.use_se = use_se
56
+ if self.use_se:
57
+ self.se = SEBlock(planes)
58
+
59
+ def forward(self, x):
60
+ residual = x
61
+ out = self.bn0(x)
62
+ out = self.conv1(out)
63
+ out = self.bn1(out)
64
+ out = self.prelu(out)
65
+
66
+ out = self.conv2(out)
67
+ out = self.bn2(out)
68
+ if self.use_se:
69
+ out = self.se(out)
70
+
71
+ if self.downsample is not None:
72
+ residual = self.downsample(x)
73
+
74
+ out += residual
75
+ out = self.prelu(out)
76
+
77
+ return out
78
+
79
+
80
+ class Bottleneck(nn.Module):
81
+ expansion = 4
82
+
83
+ def __init__(self, inplanes, planes, stride=1, downsample=None):
84
+ super(Bottleneck, self).__init__()
85
+ self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
86
+ self.bn1 = nn.BatchNorm2d(planes)
87
+ self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
88
+ self.bn2 = nn.BatchNorm2d(planes)
89
+ self.conv3 = nn.Conv2d(planes, planes * self.expansion, kernel_size=1, bias=False)
90
+ self.bn3 = nn.BatchNorm2d(planes * self.expansion)
91
+ self.relu = nn.ReLU(inplace=True)
92
+ self.downsample = downsample
93
+ self.stride = stride
94
+
95
+ def forward(self, x):
96
+ residual = x
97
+
98
+ out = self.conv1(x)
99
+ out = self.bn1(out)
100
+ out = self.relu(out)
101
+
102
+ out = self.conv2(out)
103
+ out = self.bn2(out)
104
+ out = self.relu(out)
105
+
106
+ out = self.conv3(out)
107
+ out = self.bn3(out)
108
+
109
+ if self.downsample is not None:
110
+ residual = self.downsample(x)
111
+
112
+ out += residual
113
+ out = self.relu(out)
114
+
115
+ return out
116
+
117
+
118
+ class SEBlock(nn.Module):
119
+
120
+ def __init__(self, channel, reduction=16):
121
+ super(SEBlock, self).__init__()
122
+ self.avg_pool = nn.AdaptiveAvgPool2d(1)
123
+ self.fc = nn.Sequential(
124
+ nn.Linear(channel, channel // reduction), nn.PReLU(), nn.Linear(channel // reduction, channel),
125
+ nn.Sigmoid())
126
+
127
+ def forward(self, x):
128
+ b, c, _, _ = x.size()
129
+ y = self.avg_pool(x).view(b, c)
130
+ y = self.fc(y).view(b, c, 1, 1)
131
+ return x * y
132
+
133
+
134
+ @ARCH_REGISTRY.register()
135
+ class ResNetArcFace(nn.Module):
136
+
137
+ def __init__(self, block, layers, use_se=True):
138
+ if block == 'IRBlock':
139
+ block = IRBlock
140
+ self.inplanes = 64
141
+ self.use_se = use_se
142
+ super(ResNetArcFace, self).__init__()
143
+ self.conv1 = nn.Conv2d(1, 64, kernel_size=3, padding=1, bias=False)
144
+ self.bn1 = nn.BatchNorm2d(64)
145
+ self.prelu = nn.PReLU()
146
+ self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
147
+ self.layer1 = self._make_layer(block, 64, layers[0])
148
+ self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
149
+ self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
150
+ self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
151
+ self.bn4 = nn.BatchNorm2d(512)
152
+ self.dropout = nn.Dropout()
153
+ self.fc5 = nn.Linear(512 * 8 * 8, 512)
154
+ self.bn5 = nn.BatchNorm1d(512)
155
+
156
+ for m in self.modules():
157
+ if isinstance(m, nn.Conv2d):
158
+ nn.init.xavier_normal_(m.weight)
159
+ elif isinstance(m, nn.BatchNorm2d) or isinstance(m, nn.BatchNorm1d):
160
+ nn.init.constant_(m.weight, 1)
161
+ nn.init.constant_(m.bias, 0)
162
+ elif isinstance(m, nn.Linear):
163
+ nn.init.xavier_normal_(m.weight)
164
+ nn.init.constant_(m.bias, 0)
165
+
166
+ def _make_layer(self, block, planes, blocks, stride=1):
167
+ downsample = None
168
+ if stride != 1 or self.inplanes != planes * block.expansion:
169
+ downsample = nn.Sequential(
170
+ nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False),
171
+ nn.BatchNorm2d(planes * block.expansion),
172
+ )
173
+ layers = []
174
+ layers.append(block(self.inplanes, planes, stride, downsample, use_se=self.use_se))
175
+ self.inplanes = planes
176
+ for _ in range(1, blocks):
177
+ layers.append(block(self.inplanes, planes, use_se=self.use_se))
178
+
179
+ return nn.Sequential(*layers)
180
+
181
+ def forward(self, x):
182
+ x = self.conv1(x)
183
+ x = self.bn1(x)
184
+ x = self.prelu(x)
185
+ x = self.maxpool(x)
186
+
187
+ x = self.layer1(x)
188
+ x = self.layer2(x)
189
+ x = self.layer3(x)
190
+ x = self.layer4(x)
191
+ x = self.bn4(x)
192
+ x = self.dropout(x)
193
+ x = x.view(x.size(0), -1)
194
+ x = self.fc5(x)
195
+ x = self.bn5(x)
196
+
197
+ return x
gfpgan/archs/gfpganv1_arch.py ADDED
@@ -0,0 +1,417 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import random
3
+ import torch
4
+ from basicsr.archs.stylegan2_arch import (ConvLayer, EqualConv2d, EqualLinear, ResBlock, ScaledLeakyReLU,
5
+ StyleGAN2Generator)
6
+ from basicsr.ops.fused_act import FusedLeakyReLU
7
+ from basicsr.utils.registry import ARCH_REGISTRY
8
+ from torch import nn
9
+ from torch.nn import functional as F
10
+
11
+
12
+ class StyleGAN2GeneratorSFT(StyleGAN2Generator):
13
+ """StyleGAN2 Generator.
14
+
15
+ Args:
16
+ out_size (int): The spatial size of outputs.
17
+ num_style_feat (int): Channel number of style features. Default: 512.
18
+ num_mlp (int): Layer number of MLP style layers. Default: 8.
19
+ channel_multiplier (int): Channel multiplier for large networks of
20
+ StyleGAN2. Default: 2.
21
+ resample_kernel (list[int]): A list indicating the 1D resample kernel
22
+ magnitude. A cross production will be applied to extent 1D resample
23
+ kenrel to 2D resample kernel. Default: [1, 3, 3, 1].
24
+ lr_mlp (float): Learning rate multiplier for mlp layers. Default: 0.01.
25
+ """
26
+
27
+ def __init__(self,
28
+ out_size,
29
+ num_style_feat=512,
30
+ num_mlp=8,
31
+ channel_multiplier=2,
32
+ resample_kernel=(1, 3, 3, 1),
33
+ lr_mlp=0.01,
34
+ narrow=1,
35
+ sft_half=False):
36
+ super(StyleGAN2GeneratorSFT, self).__init__(
37
+ out_size,
38
+ num_style_feat=num_style_feat,
39
+ num_mlp=num_mlp,
40
+ channel_multiplier=channel_multiplier,
41
+ resample_kernel=resample_kernel,
42
+ lr_mlp=lr_mlp,
43
+ narrow=narrow)
44
+ self.sft_half = sft_half
45
+
46
+ def forward(self,
47
+ styles,
48
+ conditions,
49
+ input_is_latent=False,
50
+ noise=None,
51
+ randomize_noise=True,
52
+ truncation=1,
53
+ truncation_latent=None,
54
+ inject_index=None,
55
+ return_latents=False):
56
+ """Forward function for StyleGAN2Generator.
57
+
58
+ Args:
59
+ styles (list[Tensor]): Sample codes of styles.
60
+ input_is_latent (bool): Whether input is latent style.
61
+ Default: False.
62
+ noise (Tensor | None): Input noise or None. Default: None.
63
+ randomize_noise (bool): Randomize noise, used when 'noise' is
64
+ False. Default: True.
65
+ truncation (float): TODO. Default: 1.
66
+ truncation_latent (Tensor | None): TODO. Default: None.
67
+ inject_index (int | None): The injection index for mixing noise.
68
+ Default: None.
69
+ return_latents (bool): Whether to return style latents.
70
+ Default: False.
71
+ """
72
+ # style codes -> latents with Style MLP layer
73
+ if not input_is_latent:
74
+ styles = [self.style_mlp(s) for s in styles]
75
+ # noises
76
+ if noise is None:
77
+ if randomize_noise:
78
+ noise = [None] * self.num_layers # for each style conv layer
79
+ else: # use the stored noise
80
+ noise = [getattr(self.noises, f'noise{i}') for i in range(self.num_layers)]
81
+ # style truncation
82
+ if truncation < 1:
83
+ style_truncation = []
84
+ for style in styles:
85
+ style_truncation.append(truncation_latent + truncation * (style - truncation_latent))
86
+ styles = style_truncation
87
+ # get style latent with injection
88
+ if len(styles) == 1:
89
+ inject_index = self.num_latent
90
+
91
+ if styles[0].ndim < 3:
92
+ # repeat latent code for all the layers
93
+ latent = styles[0].unsqueeze(1).repeat(1, inject_index, 1)
94
+ else: # used for encoder with different latent code for each layer
95
+ latent = styles[0]
96
+ elif len(styles) == 2: # mixing noises
97
+ if inject_index is None:
98
+ inject_index = random.randint(1, self.num_latent - 1)
99
+ latent1 = styles[0].unsqueeze(1).repeat(1, inject_index, 1)
100
+ latent2 = styles[1].unsqueeze(1).repeat(1, self.num_latent - inject_index, 1)
101
+ latent = torch.cat([latent1, latent2], 1)
102
+
103
+ # main generation
104
+ out = self.constant_input(latent.shape[0])
105
+ out = self.style_conv1(out, latent[:, 0], noise=noise[0])
106
+ skip = self.to_rgb1(out, latent[:, 1])
107
+
108
+ i = 1
109
+ for conv1, conv2, noise1, noise2, to_rgb in zip(self.style_convs[::2], self.style_convs[1::2], noise[1::2],
110
+ noise[2::2], self.to_rgbs):
111
+ out = conv1(out, latent[:, i], noise=noise1)
112
+
113
+ # the conditions may have fewer levels
114
+ if i < len(conditions):
115
+ # SFT part to combine the conditions
116
+ if self.sft_half:
117
+ out_same, out_sft = torch.split(out, int(out.size(1) // 2), dim=1)
118
+ out_sft = out_sft * conditions[i - 1] + conditions[i]
119
+ out = torch.cat([out_same, out_sft], dim=1)
120
+ else:
121
+ out = out * conditions[i - 1] + conditions[i]
122
+
123
+ out = conv2(out, latent[:, i + 1], noise=noise2)
124
+ skip = to_rgb(out, latent[:, i + 2], skip)
125
+ i += 2
126
+
127
+ image = skip
128
+
129
+ if return_latents:
130
+ return image, latent
131
+ else:
132
+ return image, None
133
+
134
+
135
+ class ConvUpLayer(nn.Module):
136
+ """Conv Up Layer. Bilinear upsample + Conv.
137
+
138
+ Args:
139
+ in_channels (int): Channel number of the input.
140
+ out_channels (int): Channel number of the output.
141
+ kernel_size (int): Size of the convolving kernel.
142
+ stride (int): Stride of the convolution. Default: 1
143
+ padding (int): Zero-padding added to both sides of the input.
144
+ Default: 0.
145
+ bias (bool): If ``True``, adds a learnable bias to the output.
146
+ Default: ``True``.
147
+ bias_init_val (float): Bias initialized value. Default: 0.
148
+ activate (bool): Whether use activateion. Default: True.
149
+ """
150
+
151
+ def __init__(self,
152
+ in_channels,
153
+ out_channels,
154
+ kernel_size,
155
+ stride=1,
156
+ padding=0,
157
+ bias=True,
158
+ bias_init_val=0,
159
+ activate=True):
160
+ super(ConvUpLayer, self).__init__()
161
+ self.in_channels = in_channels
162
+ self.out_channels = out_channels
163
+ self.kernel_size = kernel_size
164
+ self.stride = stride
165
+ self.padding = padding
166
+ self.scale = 1 / math.sqrt(in_channels * kernel_size**2)
167
+
168
+ self.weight = nn.Parameter(torch.randn(out_channels, in_channels, kernel_size, kernel_size))
169
+
170
+ if bias and not activate:
171
+ self.bias = nn.Parameter(torch.zeros(out_channels).fill_(bias_init_val))
172
+ else:
173
+ self.register_parameter('bias', None)
174
+
175
+ # activation
176
+ if activate:
177
+ if bias:
178
+ self.activation = FusedLeakyReLU(out_channels)
179
+ else:
180
+ self.activation = ScaledLeakyReLU(0.2)
181
+ else:
182
+ self.activation = None
183
+
184
+ def forward(self, x):
185
+ # bilinear upsample
186
+ out = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=False)
187
+ # conv
188
+ out = F.conv2d(
189
+ out,
190
+ self.weight * self.scale,
191
+ bias=self.bias,
192
+ stride=self.stride,
193
+ padding=self.padding,
194
+ )
195
+ # activation
196
+ if self.activation is not None:
197
+ out = self.activation(out)
198
+ return out
199
+
200
+
201
+ class ResUpBlock(nn.Module):
202
+ """Residual block with upsampling.
203
+
204
+ Args:
205
+ in_channels (int): Channel number of the input.
206
+ out_channels (int): Channel number of the output.
207
+ """
208
+
209
+ def __init__(self, in_channels, out_channels):
210
+ super(ResUpBlock, self).__init__()
211
+
212
+ self.conv1 = ConvLayer(in_channels, in_channels, 3, bias=True, activate=True)
213
+ self.conv2 = ConvUpLayer(in_channels, out_channels, 3, stride=1, padding=1, bias=True, activate=True)
214
+ self.skip = ConvUpLayer(in_channels, out_channels, 1, bias=False, activate=False)
215
+
216
+ def forward(self, x):
217
+ out = self.conv1(x)
218
+ out = self.conv2(out)
219
+ skip = self.skip(x)
220
+ out = (out + skip) / math.sqrt(2)
221
+ return out
222
+
223
+
224
+ @ARCH_REGISTRY.register()
225
+ class GFPGANv1(nn.Module):
226
+ """Unet + StyleGAN2 decoder with SFT."""
227
+
228
+ def __init__(
229
+ self,
230
+ out_size,
231
+ num_style_feat=512,
232
+ channel_multiplier=1,
233
+ resample_kernel=(1, 3, 3, 1),
234
+ decoder_load_path=None,
235
+ fix_decoder=True,
236
+ # for stylegan decoder
237
+ num_mlp=8,
238
+ lr_mlp=0.01,
239
+ input_is_latent=False,
240
+ different_w=False,
241
+ narrow=1,
242
+ sft_half=False):
243
+
244
+ super(GFPGANv1, self).__init__()
245
+ self.input_is_latent = input_is_latent
246
+ self.different_w = different_w
247
+ self.num_style_feat = num_style_feat
248
+
249
+ unet_narrow = narrow * 0.5
250
+ channels = {
251
+ '4': int(512 * unet_narrow),
252
+ '8': int(512 * unet_narrow),
253
+ '16': int(512 * unet_narrow),
254
+ '32': int(512 * unet_narrow),
255
+ '64': int(256 * channel_multiplier * unet_narrow),
256
+ '128': int(128 * channel_multiplier * unet_narrow),
257
+ '256': int(64 * channel_multiplier * unet_narrow),
258
+ '512': int(32 * channel_multiplier * unet_narrow),
259
+ '1024': int(16 * channel_multiplier * unet_narrow)
260
+ }
261
+
262
+ self.log_size = int(math.log(out_size, 2))
263
+ first_out_size = 2**(int(math.log(out_size, 2)))
264
+
265
+ self.conv_body_first = ConvLayer(3, channels[f'{first_out_size}'], 1, bias=True, activate=True)
266
+
267
+ # downsample
268
+ in_channels = channels[f'{first_out_size}']
269
+ self.conv_body_down = nn.ModuleList()
270
+ for i in range(self.log_size, 2, -1):
271
+ out_channels = channels[f'{2**(i - 1)}']
272
+ self.conv_body_down.append(ResBlock(in_channels, out_channels, resample_kernel))
273
+ in_channels = out_channels
274
+
275
+ self.final_conv = ConvLayer(in_channels, channels['4'], 3, bias=True, activate=True)
276
+
277
+ # upsample
278
+ in_channels = channels['4']
279
+ self.conv_body_up = nn.ModuleList()
280
+ for i in range(3, self.log_size + 1):
281
+ out_channels = channels[f'{2**i}']
282
+ self.conv_body_up.append(ResUpBlock(in_channels, out_channels))
283
+ in_channels = out_channels
284
+
285
+ # to RGB
286
+ self.toRGB = nn.ModuleList()
287
+ for i in range(3, self.log_size + 1):
288
+ self.toRGB.append(EqualConv2d(channels[f'{2**i}'], 3, 1, stride=1, padding=0, bias=True, bias_init_val=0))
289
+
290
+ if different_w:
291
+ linear_out_channel = (int(math.log(out_size, 2)) * 2 - 2) * num_style_feat
292
+ else:
293
+ linear_out_channel = num_style_feat
294
+
295
+ self.final_linear = EqualLinear(
296
+ channels['4'] * 4 * 4, linear_out_channel, bias=True, bias_init_val=0, lr_mul=1, activation=None)
297
+
298
+ self.stylegan_decoder = StyleGAN2GeneratorSFT(
299
+ out_size=out_size,
300
+ num_style_feat=num_style_feat,
301
+ num_mlp=num_mlp,
302
+ channel_multiplier=channel_multiplier,
303
+ resample_kernel=resample_kernel,
304
+ lr_mlp=lr_mlp,
305
+ narrow=narrow,
306
+ sft_half=sft_half)
307
+
308
+ if decoder_load_path:
309
+ self.stylegan_decoder.load_state_dict(
310
+ torch.load(decoder_load_path, map_location=lambda storage, loc: storage)['params_ema'])
311
+ if fix_decoder:
312
+ for _, param in self.stylegan_decoder.named_parameters():
313
+ param.requires_grad = False
314
+
315
+ # for SFT
316
+ self.condition_scale = nn.ModuleList()
317
+ self.condition_shift = nn.ModuleList()
318
+ for i in range(3, self.log_size + 1):
319
+ out_channels = channels[f'{2**i}']
320
+ if sft_half:
321
+ sft_out_channels = out_channels
322
+ else:
323
+ sft_out_channels = out_channels * 2
324
+ self.condition_scale.append(
325
+ nn.Sequential(
326
+ EqualConv2d(out_channels, out_channels, 3, stride=1, padding=1, bias=True, bias_init_val=0),
327
+ ScaledLeakyReLU(0.2),
328
+ EqualConv2d(out_channels, sft_out_channels, 3, stride=1, padding=1, bias=True, bias_init_val=1)))
329
+ self.condition_shift.append(
330
+ nn.Sequential(
331
+ EqualConv2d(out_channels, out_channels, 3, stride=1, padding=1, bias=True, bias_init_val=0),
332
+ ScaledLeakyReLU(0.2),
333
+ EqualConv2d(out_channels, sft_out_channels, 3, stride=1, padding=1, bias=True, bias_init_val=0)))
334
+
335
+ def forward(self,
336
+ x,
337
+ return_latents=False,
338
+ save_feat_path=None,
339
+ load_feat_path=None,
340
+ return_rgb=True,
341
+ randomize_noise=True):
342
+ conditions = []
343
+ unet_skips = []
344
+ out_rgbs = []
345
+
346
+ # encoder
347
+ feat = self.conv_body_first(x)
348
+ for i in range(self.log_size - 2):
349
+ feat = self.conv_body_down[i](feat)
350
+ unet_skips.insert(0, feat)
351
+
352
+ feat = self.final_conv(feat)
353
+
354
+ # style code
355
+ style_code = self.final_linear(feat.view(feat.size(0), -1))
356
+ if self.different_w:
357
+ style_code = style_code.view(style_code.size(0), -1, self.num_style_feat)
358
+
359
+ # decode
360
+ for i in range(self.log_size - 2):
361
+ # add unet skip
362
+ feat = feat + unet_skips[i]
363
+ # ResUpLayer
364
+ feat = self.conv_body_up[i](feat)
365
+ # generate scale and shift for SFT layer
366
+ scale = self.condition_scale[i](feat)
367
+ conditions.append(scale.clone())
368
+ shift = self.condition_shift[i](feat)
369
+ conditions.append(shift.clone())
370
+ # generate rgb images
371
+ if return_rgb:
372
+ out_rgbs.append(self.toRGB[i](feat))
373
+
374
+ if save_feat_path is not None:
375
+ torch.save(conditions, save_feat_path)
376
+ if load_feat_path is not None:
377
+ conditions = torch.load(load_feat_path)
378
+ conditions = [v.cuda() for v in conditions]
379
+
380
+ # decoder
381
+ image, _ = self.stylegan_decoder([style_code],
382
+ conditions,
383
+ return_latents=return_latents,
384
+ input_is_latent=self.input_is_latent,
385
+ randomize_noise=randomize_noise)
386
+
387
+ return image, out_rgbs
388
+
389
+
390
+ @ARCH_REGISTRY.register()
391
+ class FacialComponentDiscriminator(nn.Module):
392
+
393
+ def __init__(self):
394
+ super(FacialComponentDiscriminator, self).__init__()
395
+
396
+ self.conv1 = ConvLayer(3, 64, 3, downsample=False, resample_kernel=(1, 3, 3, 1), bias=True, activate=True)
397
+ self.conv2 = ConvLayer(64, 128, 3, downsample=True, resample_kernel=(1, 3, 3, 1), bias=True, activate=True)
398
+ self.conv3 = ConvLayer(128, 128, 3, downsample=False, resample_kernel=(1, 3, 3, 1), bias=True, activate=True)
399
+ self.conv4 = ConvLayer(128, 256, 3, downsample=True, resample_kernel=(1, 3, 3, 1), bias=True, activate=True)
400
+ self.conv5 = ConvLayer(256, 256, 3, downsample=False, resample_kernel=(1, 3, 3, 1), bias=True, activate=True)
401
+ self.final_conv = ConvLayer(256, 1, 3, bias=True, activate=False)
402
+
403
+ def forward(self, x, return_feats=False):
404
+ feat = self.conv1(x)
405
+ feat = self.conv3(self.conv2(feat))
406
+ rlt_feats = []
407
+ if return_feats:
408
+ rlt_feats.append(feat.clone())
409
+ feat = self.conv5(self.conv4(feat))
410
+ if return_feats:
411
+ rlt_feats.append(feat.clone())
412
+ out = self.final_conv(feat)
413
+
414
+ if return_feats:
415
+ return out, rlt_feats
416
+ else:
417
+ return out, None
gfpgan/archs/gfpganv1_clean_arch.py ADDED
@@ -0,0 +1,304 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import random
3
+ import torch
4
+ from torch import nn
5
+ from torch.nn import functional as F
6
+
7
+ from .stylegan2_clean_arch import StyleGAN2GeneratorClean
8
+
9
+
10
+ class StyleGAN2GeneratorCSFT(StyleGAN2GeneratorClean):
11
+ """StyleGAN2 Generator.
12
+
13
+ Args:
14
+ out_size (int): The spatial size of outputs.
15
+ num_style_feat (int): Channel number of style features. Default: 512.
16
+ num_mlp (int): Layer number of MLP style layers. Default: 8.
17
+ channel_multiplier (int): Channel multiplier for large networks of
18
+ StyleGAN2. Default: 2.
19
+ """
20
+
21
+ def __init__(self, out_size, num_style_feat=512, num_mlp=8, channel_multiplier=2, narrow=1, sft_half=False):
22
+ super(StyleGAN2GeneratorCSFT, self).__init__(
23
+ out_size,
24
+ num_style_feat=num_style_feat,
25
+ num_mlp=num_mlp,
26
+ channel_multiplier=channel_multiplier,
27
+ narrow=narrow)
28
+
29
+ self.sft_half = sft_half
30
+
31
+ def forward(self,
32
+ styles,
33
+ conditions,
34
+ input_is_latent=False,
35
+ noise=None,
36
+ randomize_noise=True,
37
+ truncation=1,
38
+ truncation_latent=None,
39
+ inject_index=None,
40
+ return_latents=False):
41
+ """Forward function for StyleGAN2Generator.
42
+
43
+ Args:
44
+ styles (list[Tensor]): Sample codes of styles.
45
+ input_is_latent (bool): Whether input is latent style.
46
+ Default: False.
47
+ noise (Tensor | None): Input noise or None. Default: None.
48
+ randomize_noise (bool): Randomize noise, used when 'noise' is
49
+ False. Default: True.
50
+ truncation (float): TODO. Default: 1.
51
+ truncation_latent (Tensor | None): TODO. Default: None.
52
+ inject_index (int | None): The injection index for mixing noise.
53
+ Default: None.
54
+ return_latents (bool): Whether to return style latents.
55
+ Default: False.
56
+ """
57
+ # style codes -> latents with Style MLP layer
58
+ if not input_is_latent:
59
+ styles = [self.style_mlp(s) for s in styles]
60
+ # noises
61
+ if noise is None:
62
+ if randomize_noise:
63
+ noise = [None] * self.num_layers # for each style conv layer
64
+ else: # use the stored noise
65
+ noise = [getattr(self.noises, f'noise{i}') for i in range(self.num_layers)]
66
+ # style truncation
67
+ if truncation < 1:
68
+ style_truncation = []
69
+ for style in styles:
70
+ style_truncation.append(truncation_latent + truncation * (style - truncation_latent))
71
+ styles = style_truncation
72
+ # get style latent with injection
73
+ if len(styles) == 1:
74
+ inject_index = self.num_latent
75
+
76
+ if styles[0].ndim < 3:
77
+ # repeat latent code for all the layers
78
+ latent = styles[0].unsqueeze(1).repeat(1, inject_index, 1)
79
+ else: # used for encoder with different latent code for each layer
80
+ latent = styles[0]
81
+ elif len(styles) == 2: # mixing noises
82
+ if inject_index is None:
83
+ inject_index = random.randint(1, self.num_latent - 1)
84
+ latent1 = styles[0].unsqueeze(1).repeat(1, inject_index, 1)
85
+ latent2 = styles[1].unsqueeze(1).repeat(1, self.num_latent - inject_index, 1)
86
+ latent = torch.cat([latent1, latent2], 1)
87
+
88
+ # main generation
89
+ out = self.constant_input(latent.shape[0])
90
+ out = self.style_conv1(out, latent[:, 0], noise=noise[0])
91
+ skip = self.to_rgb1(out, latent[:, 1])
92
+
93
+ i = 1
94
+ for conv1, conv2, noise1, noise2, to_rgb in zip(self.style_convs[::2], self.style_convs[1::2], noise[1::2],
95
+ noise[2::2], self.to_rgbs):
96
+ out = conv1(out, latent[:, i], noise=noise1)
97
+
98
+ # the conditions may have fewer levels
99
+ if i < len(conditions):
100
+ # SFT part to combine the conditions
101
+ if self.sft_half:
102
+ out_same, out_sft = torch.split(out, int(out.size(1) // 2), dim=1)
103
+ out_sft = out_sft * conditions[i - 1] + conditions[i]
104
+ out = torch.cat([out_same, out_sft], dim=1)
105
+ else:
106
+ out = out * conditions[i - 1] + conditions[i]
107
+
108
+ out = conv2(out, latent[:, i + 1], noise=noise2)
109
+ skip = to_rgb(out, latent[:, i + 2], skip)
110
+ i += 2
111
+
112
+ image = skip
113
+
114
+ if return_latents:
115
+ return image, latent
116
+ else:
117
+ return image, None
118
+
119
+
120
+ class ResBlock(nn.Module):
121
+ """Residual block with upsampling/downsampling.
122
+
123
+ Args:
124
+ in_channels (int): Channel number of the input.
125
+ out_channels (int): Channel number of the output.
126
+ """
127
+
128
+ def __init__(self, in_channels, out_channels, mode='down'):
129
+ super(ResBlock, self).__init__()
130
+
131
+ self.conv1 = nn.Conv2d(in_channels, in_channels, 3, 1, 1)
132
+ self.conv2 = nn.Conv2d(in_channels, out_channels, 3, 1, 1)
133
+ self.skip = nn.Conv2d(in_channels, out_channels, 1, bias=False)
134
+ if mode == 'down':
135
+ self.scale_factor = 0.5
136
+ elif mode == 'up':
137
+ self.scale_factor = 2
138
+
139
+ def forward(self, x):
140
+ out = F.leaky_relu_(self.conv1(x), negative_slope=0.2)
141
+ # upsample/downsample
142
+ out = F.interpolate(out, scale_factor=self.scale_factor, mode='bilinear', align_corners=False)
143
+ out = F.leaky_relu_(self.conv2(out), negative_slope=0.2)
144
+ # skip
145
+ x = F.interpolate(x, scale_factor=self.scale_factor, mode='bilinear', align_corners=False)
146
+ skip = self.skip(x)
147
+ out = out + skip
148
+ return out
149
+
150
+
151
+ class GFPGANv1Clean(nn.Module):
152
+ """GFPGANv1 Clean version."""
153
+
154
+ def __init__(
155
+ self,
156
+ out_size,
157
+ num_style_feat=512,
158
+ channel_multiplier=1,
159
+ decoder_load_path=None,
160
+ fix_decoder=True,
161
+ # for stylegan decoder
162
+ num_mlp=8,
163
+ input_is_latent=False,
164
+ different_w=False,
165
+ narrow=1,
166
+ sft_half=False):
167
+
168
+ super(GFPGANv1Clean, self).__init__()
169
+ self.input_is_latent = input_is_latent
170
+ self.different_w = different_w
171
+ self.num_style_feat = num_style_feat
172
+
173
+ unet_narrow = narrow * 0.5
174
+ channels = {
175
+ '4': int(512 * unet_narrow),
176
+ '8': int(512 * unet_narrow),
177
+ '16': int(512 * unet_narrow),
178
+ '32': int(512 * unet_narrow),
179
+ '64': int(256 * channel_multiplier * unet_narrow),
180
+ '128': int(128 * channel_multiplier * unet_narrow),
181
+ '256': int(64 * channel_multiplier * unet_narrow),
182
+ '512': int(32 * channel_multiplier * unet_narrow),
183
+ '1024': int(16 * channel_multiplier * unet_narrow)
184
+ }
185
+
186
+ self.log_size = int(math.log(out_size, 2))
187
+ first_out_size = 2**(int(math.log(out_size, 2)))
188
+
189
+ self.conv_body_first = nn.Conv2d(3, channels[f'{first_out_size}'], 1)
190
+
191
+ # downsample
192
+ in_channels = channels[f'{first_out_size}']
193
+ self.conv_body_down = nn.ModuleList()
194
+ for i in range(self.log_size, 2, -1):
195
+ out_channels = channels[f'{2**(i - 1)}']
196
+ self.conv_body_down.append(ResBlock(in_channels, out_channels, mode='down'))
197
+ in_channels = out_channels
198
+
199
+ self.final_conv = nn.Conv2d(in_channels, channels['4'], 3, 1, 1)
200
+
201
+ # upsample
202
+ in_channels = channels['4']
203
+ self.conv_body_up = nn.ModuleList()
204
+ for i in range(3, self.log_size + 1):
205
+ out_channels = channels[f'{2**i}']
206
+ self.conv_body_up.append(ResBlock(in_channels, out_channels, mode='up'))
207
+ in_channels = out_channels
208
+
209
+ # to RGB
210
+ self.toRGB = nn.ModuleList()
211
+ for i in range(3, self.log_size + 1):
212
+ self.toRGB.append(nn.Conv2d(channels[f'{2**i}'], 3, 1))
213
+
214
+ if different_w:
215
+ linear_out_channel = (int(math.log(out_size, 2)) * 2 - 2) * num_style_feat
216
+ else:
217
+ linear_out_channel = num_style_feat
218
+
219
+ self.final_linear = nn.Linear(channels['4'] * 4 * 4, linear_out_channel)
220
+
221
+ self.stylegan_decoder = StyleGAN2GeneratorCSFT(
222
+ out_size=out_size,
223
+ num_style_feat=num_style_feat,
224
+ num_mlp=num_mlp,
225
+ channel_multiplier=channel_multiplier,
226
+ narrow=narrow,
227
+ sft_half=sft_half)
228
+
229
+ if decoder_load_path:
230
+ self.stylegan_decoder.load_state_dict(
231
+ torch.load(decoder_load_path, map_location=lambda storage, loc: storage)['params_ema'])
232
+ if fix_decoder:
233
+ for name, param in self.stylegan_decoder.named_parameters():
234
+ param.requires_grad = False
235
+
236
+ # for SFT
237
+ self.condition_scale = nn.ModuleList()
238
+ self.condition_shift = nn.ModuleList()
239
+ for i in range(3, self.log_size + 1):
240
+ out_channels = channels[f'{2**i}']
241
+ if sft_half:
242
+ sft_out_channels = out_channels
243
+ else:
244
+ sft_out_channels = out_channels * 2
245
+ self.condition_scale.append(
246
+ nn.Sequential(
247
+ nn.Conv2d(out_channels, out_channels, 3, 1, 1), nn.LeakyReLU(0.2, True),
248
+ nn.Conv2d(out_channels, sft_out_channels, 3, 1, 1)))
249
+ self.condition_shift.append(
250
+ nn.Sequential(
251
+ nn.Conv2d(out_channels, out_channels, 3, 1, 1), nn.LeakyReLU(0.2, True),
252
+ nn.Conv2d(out_channels, sft_out_channels, 3, 1, 1)))
253
+
254
+ def forward(self,
255
+ x,
256
+ return_latents=False,
257
+ save_feat_path=None,
258
+ load_feat_path=None,
259
+ return_rgb=True,
260
+ randomize_noise=True):
261
+ conditions = []
262
+ unet_skips = []
263
+ out_rgbs = []
264
+
265
+ # encoder
266
+ feat = F.leaky_relu_(self.conv_body_first(x), negative_slope=0.2)
267
+ for i in range(self.log_size - 2):
268
+ feat = self.conv_body_down[i](feat)
269
+ unet_skips.insert(0, feat)
270
+ feat = F.leaky_relu_(self.final_conv(feat), negative_slope=0.2)
271
+
272
+ # style code
273
+ style_code = self.final_linear(feat.view(feat.size(0), -1))
274
+ if self.different_w:
275
+ style_code = style_code.view(style_code.size(0), -1, self.num_style_feat)
276
+ # decode
277
+ for i in range(self.log_size - 2):
278
+ # add unet skip
279
+ feat = feat + unet_skips[i]
280
+ # ResUpLayer
281
+ feat = self.conv_body_up[i](feat)
282
+ # generate scale and shift for SFT layer
283
+ scale = self.condition_scale[i](feat)
284
+ conditions.append(scale.clone())
285
+ shift = self.condition_shift[i](feat)
286
+ conditions.append(shift.clone())
287
+ # generate rgb images
288
+ if return_rgb:
289
+ out_rgbs.append(self.toRGB[i](feat))
290
+
291
+ if save_feat_path is not None:
292
+ torch.save(conditions, save_feat_path)
293
+ if load_feat_path is not None:
294
+ conditions = torch.load(load_feat_path)
295
+ conditions = [v.cuda() for v in conditions]
296
+
297
+ # decoder
298
+ image, _ = self.stylegan_decoder([style_code],
299
+ conditions,
300
+ return_latents=return_latents,
301
+ input_is_latent=self.input_is_latent,
302
+ randomize_noise=randomize_noise)
303
+
304
+ return image, out_rgbs
gfpgan/archs/stylegan2_clean_arch.py ADDED
@@ -0,0 +1,377 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import random
3
+ import torch
4
+ from basicsr.archs.arch_util import default_init_weights
5
+ from basicsr.utils.registry import ARCH_REGISTRY
6
+ from torch import nn
7
+ from torch.nn import functional as F
8
+
9
+
10
+ class NormStyleCode(nn.Module):
11
+
12
+ def forward(self, x):
13
+ """Normalize the style codes.
14
+
15
+ Args:
16
+ x (Tensor): Style codes with shape (b, c).
17
+
18
+ Returns:
19
+ Tensor: Normalized tensor.
20
+ """
21
+ return x * torch.rsqrt(torch.mean(x**2, dim=1, keepdim=True) + 1e-8)
22
+
23
+
24
+ class ModulatedConv2d(nn.Module):
25
+ """Modulated Conv2d used in StyleGAN2.
26
+
27
+ There is no bias in ModulatedConv2d.
28
+
29
+ Args:
30
+ in_channels (int): Channel number of the input.
31
+ out_channels (int): Channel number of the output.
32
+ kernel_size (int): Size of the convolving kernel.
33
+ num_style_feat (int): Channel number of style features.
34
+ demodulate (bool): Whether to demodulate in the conv layer.
35
+ Default: True.
36
+ sample_mode (str | None): Indicating 'upsample', 'downsample' or None.
37
+ Default: None.
38
+ eps (float): A value added to the denominator for numerical stability.
39
+ Default: 1e-8.
40
+ """
41
+
42
+ def __init__(self,
43
+ in_channels,
44
+ out_channels,
45
+ kernel_size,
46
+ num_style_feat,
47
+ demodulate=True,
48
+ sample_mode=None,
49
+ eps=1e-8):
50
+ super(ModulatedConv2d, self).__init__()
51
+ self.in_channels = in_channels
52
+ self.out_channels = out_channels
53
+ self.kernel_size = kernel_size
54
+ self.demodulate = demodulate
55
+ self.sample_mode = sample_mode
56
+ self.eps = eps
57
+
58
+ # modulation inside each modulated conv
59
+ self.modulation = nn.Linear(num_style_feat, in_channels, bias=True)
60
+ # initialization
61
+ default_init_weights(self.modulation, scale=1, bias_fill=1, a=0, mode='fan_in', nonlinearity='linear')
62
+
63
+ self.weight = nn.Parameter(
64
+ torch.randn(1, out_channels, in_channels, kernel_size, kernel_size) /
65
+ math.sqrt(in_channels * kernel_size**2))
66
+ self.padding = kernel_size // 2
67
+
68
+ def forward(self, x, style):
69
+ """Forward function.
70
+
71
+ Args:
72
+ x (Tensor): Tensor with shape (b, c, h, w).
73
+ style (Tensor): Tensor with shape (b, num_style_feat).
74
+
75
+ Returns:
76
+ Tensor: Modulated tensor after convolution.
77
+ """
78
+ b, c, h, w = x.shape # c = c_in
79
+ # weight modulation
80
+ style = self.modulation(style).view(b, 1, c, 1, 1)
81
+ # self.weight: (1, c_out, c_in, k, k); style: (b, 1, c, 1, 1)
82
+ weight = self.weight * style # (b, c_out, c_in, k, k)
83
+
84
+ if self.demodulate:
85
+ demod = torch.rsqrt(weight.pow(2).sum([2, 3, 4]) + self.eps)
86
+ weight = weight * demod.view(b, self.out_channels, 1, 1, 1)
87
+
88
+ weight = weight.view(b * self.out_channels, c, self.kernel_size, self.kernel_size)
89
+
90
+ if self.sample_mode == 'upsample':
91
+ x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=False)
92
+ elif self.sample_mode == 'downsample':
93
+ x = F.interpolate(x, scale_factor=0.5, mode='bilinear', align_corners=False)
94
+
95
+ b, c, h, w = x.shape
96
+ x = x.view(1, b * c, h, w)
97
+ # weight: (b*c_out, c_in, k, k), groups=b
98
+ out = F.conv2d(x, weight, padding=self.padding, groups=b)
99
+ out = out.view(b, self.out_channels, *out.shape[2:4])
100
+
101
+ return out
102
+
103
+ def __repr__(self):
104
+ return (f'{self.__class__.__name__}(in_channels={self.in_channels}, '
105
+ f'out_channels={self.out_channels}, '
106
+ f'kernel_size={self.kernel_size}, '
107
+ f'demodulate={self.demodulate}, sample_mode={self.sample_mode})')
108
+
109
+
110
+ class StyleConv(nn.Module):
111
+ """Style conv.
112
+
113
+ Args:
114
+ in_channels (int): Channel number of the input.
115
+ out_channels (int): Channel number of the output.
116
+ kernel_size (int): Size of the convolving kernel.
117
+ num_style_feat (int): Channel number of style features.
118
+ demodulate (bool): Whether demodulate in the conv layer. Default: True.
119
+ sample_mode (str | None): Indicating 'upsample', 'downsample' or None.
120
+ Default: None.
121
+ """
122
+
123
+ def __init__(self, in_channels, out_channels, kernel_size, num_style_feat, demodulate=True, sample_mode=None):
124
+ super(StyleConv, self).__init__()
125
+ self.modulated_conv = ModulatedConv2d(
126
+ in_channels, out_channels, kernel_size, num_style_feat, demodulate=demodulate, sample_mode=sample_mode)
127
+ self.weight = nn.Parameter(torch.zeros(1)) # for noise injection
128
+ self.bias = nn.Parameter(torch.zeros(1, out_channels, 1, 1))
129
+ self.activate = nn.LeakyReLU(negative_slope=0.2, inplace=True)
130
+
131
+ def forward(self, x, style, noise=None):
132
+ # modulate
133
+ out = self.modulated_conv(x, style) * 2**0.5 # for conversion
134
+ # noise injection
135
+ if noise is None:
136
+ b, _, h, w = out.shape
137
+ noise = out.new_empty(b, 1, h, w).normal_()
138
+ out = out + self.weight * noise
139
+ # add bias
140
+ out = out + self.bias
141
+ # activation
142
+ out = self.activate(out)
143
+ return out
144
+
145
+
146
+ class ToRGB(nn.Module):
147
+ """To RGB from features.
148
+
149
+ Args:
150
+ in_channels (int): Channel number of input.
151
+ num_style_feat (int): Channel number of style features.
152
+ upsample (bool): Whether to upsample. Default: True.
153
+ """
154
+
155
+ def __init__(self, in_channels, num_style_feat, upsample=True):
156
+ super(ToRGB, self).__init__()
157
+ self.upsample = upsample
158
+ self.modulated_conv = ModulatedConv2d(
159
+ in_channels, 3, kernel_size=1, num_style_feat=num_style_feat, demodulate=False, sample_mode=None)
160
+ self.bias = nn.Parameter(torch.zeros(1, 3, 1, 1))
161
+
162
+ def forward(self, x, style, skip=None):
163
+ """Forward function.
164
+
165
+ Args:
166
+ x (Tensor): Feature tensor with shape (b, c, h, w).
167
+ style (Tensor): Tensor with shape (b, num_style_feat).
168
+ skip (Tensor): Base/skip tensor. Default: None.
169
+
170
+ Returns:
171
+ Tensor: RGB images.
172
+ """
173
+ out = self.modulated_conv(x, style)
174
+ out = out + self.bias
175
+ if skip is not None:
176
+ if self.upsample:
177
+ skip = F.interpolate(skip, scale_factor=2, mode='bilinear', align_corners=False)
178
+ out = out + skip
179
+ return out
180
+
181
+
182
+ class ConstantInput(nn.Module):
183
+ """Constant input.
184
+
185
+ Args:
186
+ num_channel (int): Channel number of constant input.
187
+ size (int): Spatial size of constant input.
188
+ """
189
+
190
+ def __init__(self, num_channel, size):
191
+ super(ConstantInput, self).__init__()
192
+ self.weight = nn.Parameter(torch.randn(1, num_channel, size, size))
193
+
194
+ def forward(self, batch):
195
+ out = self.weight.repeat(batch, 1, 1, 1)
196
+ return out
197
+
198
+
199
+ @ARCH_REGISTRY.register()
200
+ class StyleGAN2GeneratorClean(nn.Module):
201
+ """Clean version of StyleGAN2 Generator.
202
+
203
+ Args:
204
+ out_size (int): The spatial size of outputs.
205
+ num_style_feat (int): Channel number of style features. Default: 512.
206
+ num_mlp (int): Layer number of MLP style layers. Default: 8.
207
+ channel_multiplier (int): Channel multiplier for large networks of
208
+ StyleGAN2. Default: 2.
209
+ narrow (float): Narrow ratio for channels. Default: 1.0.
210
+ """
211
+
212
+ def __init__(self, out_size, num_style_feat=512, num_mlp=8, channel_multiplier=2, narrow=1):
213
+ super(StyleGAN2GeneratorClean, self).__init__()
214
+ # Style MLP layers
215
+ self.num_style_feat = num_style_feat
216
+ style_mlp_layers = [NormStyleCode()]
217
+ for i in range(num_mlp):
218
+ style_mlp_layers.extend(
219
+ [nn.Linear(num_style_feat, num_style_feat, bias=True),
220
+ nn.LeakyReLU(negative_slope=0.2, inplace=True)])
221
+ self.style_mlp = nn.Sequential(*style_mlp_layers)
222
+ # initialization
223
+ default_init_weights(self.style_mlp, scale=1, bias_fill=0, a=0.2, mode='fan_in', nonlinearity='leaky_relu')
224
+
225
+ channels = {
226
+ '4': int(512 * narrow),
227
+ '8': int(512 * narrow),
228
+ '16': int(512 * narrow),
229
+ '32': int(512 * narrow),
230
+ '64': int(256 * channel_multiplier * narrow),
231
+ '128': int(128 * channel_multiplier * narrow),
232
+ '256': int(64 * channel_multiplier * narrow),
233
+ '512': int(32 * channel_multiplier * narrow),
234
+ '1024': int(16 * channel_multiplier * narrow)
235
+ }
236
+ self.channels = channels
237
+
238
+ self.constant_input = ConstantInput(channels['4'], size=4)
239
+ self.style_conv1 = StyleConv(
240
+ channels['4'],
241
+ channels['4'],
242
+ kernel_size=3,
243
+ num_style_feat=num_style_feat,
244
+ demodulate=True,
245
+ sample_mode=None)
246
+ self.to_rgb1 = ToRGB(channels['4'], num_style_feat, upsample=False)
247
+
248
+ self.log_size = int(math.log(out_size, 2))
249
+ self.num_layers = (self.log_size - 2) * 2 + 1
250
+ self.num_latent = self.log_size * 2 - 2
251
+
252
+ self.style_convs = nn.ModuleList()
253
+ self.to_rgbs = nn.ModuleList()
254
+ self.noises = nn.Module()
255
+
256
+ in_channels = channels['4']
257
+ # noise
258
+ for layer_idx in range(self.num_layers):
259
+ resolution = 2**((layer_idx + 5) // 2)
260
+ shape = [1, 1, resolution, resolution]
261
+ self.noises.register_buffer(f'noise{layer_idx}', torch.randn(*shape))
262
+ # style convs and to_rgbs
263
+ for i in range(3, self.log_size + 1):
264
+ out_channels = channels[f'{2**i}']
265
+ self.style_convs.append(
266
+ StyleConv(
267
+ in_channels,
268
+ out_channels,
269
+ kernel_size=3,
270
+ num_style_feat=num_style_feat,
271
+ demodulate=True,
272
+ sample_mode='upsample'))
273
+ self.style_convs.append(
274
+ StyleConv(
275
+ out_channels,
276
+ out_channels,
277
+ kernel_size=3,
278
+ num_style_feat=num_style_feat,
279
+ demodulate=True,
280
+ sample_mode=None))
281
+ self.to_rgbs.append(ToRGB(out_channels, num_style_feat, upsample=True))
282
+ in_channels = out_channels
283
+
284
+ def make_noise(self):
285
+ """Make noise for noise injection."""
286
+ device = self.constant_input.weight.device
287
+ noises = [torch.randn(1, 1, 4, 4, device=device)]
288
+
289
+ for i in range(3, self.log_size + 1):
290
+ for _ in range(2):
291
+ noises.append(torch.randn(1, 1, 2**i, 2**i, device=device))
292
+
293
+ return noises
294
+
295
+ def get_latent(self, x):
296
+ return self.style_mlp(x)
297
+
298
+ def mean_latent(self, num_latent):
299
+ latent_in = torch.randn(num_latent, self.num_style_feat, device=self.constant_input.weight.device)
300
+ latent = self.style_mlp(latent_in).mean(0, keepdim=True)
301
+ return latent
302
+
303
+ def forward(self,
304
+ styles,
305
+ input_is_latent=False,
306
+ noise=None,
307
+ randomize_noise=True,
308
+ truncation=1,
309
+ truncation_latent=None,
310
+ inject_index=None,
311
+ return_latents=False):
312
+ """Forward function for StyleGAN2Generator.
313
+
314
+ Args:
315
+ styles (list[Tensor]): Sample codes of styles.
316
+ input_is_latent (bool): Whether input is latent style.
317
+ Default: False.
318
+ noise (Tensor | None): Input noise or None. Default: None.
319
+ randomize_noise (bool): Randomize noise, used when 'noise' is
320
+ False. Default: True.
321
+ truncation (float): TODO. Default: 1.
322
+ truncation_latent (Tensor | None): TODO. Default: None.
323
+ inject_index (int | None): The injection index for mixing noise.
324
+ Default: None.
325
+ return_latents (bool): Whether to return style latents.
326
+ Default: False.
327
+ """
328
+ # style codes -> latents with Style MLP layer
329
+ if not input_is_latent:
330
+ styles = [self.style_mlp(s) for s in styles]
331
+ # noises
332
+ if noise is None:
333
+ if randomize_noise:
334
+ noise = [None] * self.num_layers # for each style conv layer
335
+ else: # use the stored noise
336
+ noise = [getattr(self.noises, f'noise{i}') for i in range(self.num_layers)]
337
+ # style truncation
338
+ if truncation < 1:
339
+ style_truncation = []
340
+ for style in styles:
341
+ style_truncation.append(truncation_latent + truncation * (style - truncation_latent))
342
+ styles = style_truncation
343
+ # get style latent with injection
344
+ if len(styles) == 1:
345
+ inject_index = self.num_latent
346
+
347
+ if styles[0].ndim < 3:
348
+ # repeat latent code for all the layers
349
+ latent = styles[0].unsqueeze(1).repeat(1, inject_index, 1)
350
+ else: # used for encoder with different latent code for each layer
351
+ latent = styles[0]
352
+ elif len(styles) == 2: # mixing noises
353
+ if inject_index is None:
354
+ inject_index = random.randint(1, self.num_latent - 1)
355
+ latent1 = styles[0].unsqueeze(1).repeat(1, inject_index, 1)
356
+ latent2 = styles[1].unsqueeze(1).repeat(1, self.num_latent - inject_index, 1)
357
+ latent = torch.cat([latent1, latent2], 1)
358
+
359
+ # main generation
360
+ out = self.constant_input(latent.shape[0])
361
+ out = self.style_conv1(out, latent[:, 0], noise=noise[0])
362
+ skip = self.to_rgb1(out, latent[:, 1])
363
+
364
+ i = 1
365
+ for conv1, conv2, noise1, noise2, to_rgb in zip(self.style_convs[::2], self.style_convs[1::2], noise[1::2],
366
+ noise[2::2], self.to_rgbs):
367
+ out = conv1(out, latent[:, i], noise=noise1)
368
+ out = conv2(out, latent[:, i + 1], noise=noise2)
369
+ skip = to_rgb(out, latent[:, i + 2], skip)
370
+ i += 2
371
+
372
+ image = skip
373
+
374
+ if return_latents:
375
+ return image, latent
376
+ else:
377
+ return image, None
gfpgan/data/__init__.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
1
+ import importlib
2
+ from basicsr.utils import scandir
3
+ from os import path as osp
4
+
5
+ # automatically scan and import dataset modules for registry
6
+ # scan all the files that end with '_dataset.py' under the data folder
7
+ data_folder = osp.dirname(osp.abspath(__file__))
8
+ dataset_filenames = [osp.splitext(osp.basename(v))[0] for v in scandir(data_folder) if v.endswith('_dataset.py')]
9
+ # import all the dataset modules
10
+ _dataset_modules = [importlib.import_module(f'gfpgan.data.{file_name}') for file_name in dataset_filenames]
gfpgan/data/ffhq_degradation_dataset.py ADDED
@@ -0,0 +1,212 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import math
3
+ import numpy as np
4
+ import os.path as osp
5
+ import torch
6
+ import torch.utils.data as data
7
+ from basicsr.data import degradations as degradations
8
+ from basicsr.data.data_util import paths_from_folder
9
+ from basicsr.data.transforms import augment
10
+ from basicsr.utils import FileClient, get_root_logger, imfrombytes, img2tensor
11
+ from basicsr.utils.registry import DATASET_REGISTRY
12
+ from torchvision.transforms.functional import (adjust_brightness, adjust_contrast, adjust_hue, adjust_saturation,
13
+ normalize)
14
+
15
+
16
+ @DATASET_REGISTRY.register()
17
+ class FFHQDegradationDataset(data.Dataset):
18
+
19
+ def __init__(self, opt):
20
+ super(FFHQDegradationDataset, self).__init__()
21
+ self.opt = opt
22
+ # file client (io backend)
23
+ self.file_client = None
24
+ self.io_backend_opt = opt['io_backend']
25
+
26
+ self.gt_folder = opt['dataroot_gt']
27
+ self.mean = opt['mean']
28
+ self.std = opt['std']
29
+ self.out_size = opt['out_size']
30
+
31
+ self.crop_components = opt.get('crop_components', False) # facial components
32
+ self.eye_enlarge_ratio = opt.get('eye_enlarge_ratio', 1)
33
+
34
+ if self.crop_components:
35
+ self.components_list = torch.load(opt.get('component_path'))
36
+
37
+ if self.io_backend_opt['type'] == 'lmdb':
38
+ self.io_backend_opt['db_paths'] = self.gt_folder
39
+ if not self.gt_folder.endswith('.lmdb'):
40
+ raise ValueError(f"'dataroot_gt' should end with '.lmdb', but received {self.gt_folder}")
41
+ with open(osp.join(self.gt_folder, 'meta_info.txt')) as fin:
42
+ self.paths = [line.split('.')[0] for line in fin]
43
+ else:
44
+ self.paths = paths_from_folder(self.gt_folder)
45
+
46
+ # degradations
47
+ self.blur_kernel_size = opt['blur_kernel_size']
48
+ self.kernel_list = opt['kernel_list']
49
+ self.kernel_prob = opt['kernel_prob']
50
+ self.blur_sigma = opt['blur_sigma']
51
+ self.downsample_range = opt['downsample_range']
52
+ self.noise_range = opt['noise_range']
53
+ self.jpeg_range = opt['jpeg_range']
54
+
55
+ # color jitter
56
+ self.color_jitter_prob = opt.get('color_jitter_prob')
57
+ self.color_jitter_pt_prob = opt.get('color_jitter_pt_prob')
58
+ self.color_jitter_shift = opt.get('color_jitter_shift', 20)
59
+ # to gray
60
+ self.gray_prob = opt.get('gray_prob')
61
+
62
+ logger = get_root_logger()
63
+ logger.info(f'Blur: blur_kernel_size {self.blur_kernel_size}, '
64
+ f'sigma: [{", ".join(map(str, self.blur_sigma))}]')
65
+ logger.info(f'Downsample: downsample_range [{", ".join(map(str, self.downsample_range))}]')
66
+ logger.info(f'Noise: [{", ".join(map(str, self.noise_range))}]')
67
+ logger.info(f'JPEG compression: [{", ".join(map(str, self.jpeg_range))}]')
68
+
69
+ if self.color_jitter_prob is not None:
70
+ logger.info(f'Use random color jitter. Prob: {self.color_jitter_prob}, '
71
+ f'shift: {self.color_jitter_shift}')
72
+ if self.gray_prob is not None:
73
+ logger.info(f'Use random gray. Prob: {self.gray_prob}')
74
+
75
+ self.color_jitter_shift /= 255.
76
+
77
+ @staticmethod
78
+ def color_jitter(img, shift):
79
+ jitter_val = np.random.uniform(-shift, shift, 3).astype(np.float32)
80
+ img = img + jitter_val
81
+ img = np.clip(img, 0, 1)
82
+ return img
83
+
84
+ @staticmethod
85
+ def color_jitter_pt(img, brightness, contrast, saturation, hue):
86
+ fn_idx = torch.randperm(4)
87
+ for fn_id in fn_idx:
88
+ if fn_id == 0 and brightness is not None:
89
+ brightness_factor = torch.tensor(1.0).uniform_(brightness[0], brightness[1]).item()
90
+ img = adjust_brightness(img, brightness_factor)
91
+
92
+ if fn_id == 1 and contrast is not None:
93
+ contrast_factor = torch.tensor(1.0).uniform_(contrast[0], contrast[1]).item()
94
+ img = adjust_contrast(img, contrast_factor)
95
+
96
+ if fn_id == 2 and saturation is not None:
97
+ saturation_factor = torch.tensor(1.0).uniform_(saturation[0], saturation[1]).item()
98
+ img = adjust_saturation(img, saturation_factor)
99
+
100
+ if fn_id == 3 and hue is not None:
101
+ hue_factor = torch.tensor(1.0).uniform_(hue[0], hue[1]).item()
102
+ img = adjust_hue(img, hue_factor)
103
+ return img
104
+
105
+ def get_component_coordinates(self, index, status):
106
+ components_bbox = self.components_list[f'{index:08d}']
107
+ if status[0]: # hflip
108
+ # exchange right and left eye
109
+ tmp = components_bbox['left_eye']
110
+ components_bbox['left_eye'] = components_bbox['right_eye']
111
+ components_bbox['right_eye'] = tmp
112
+ # modify the width coordinate
113
+ components_bbox['left_eye'][0] = self.out_size - components_bbox['left_eye'][0]
114
+ components_bbox['right_eye'][0] = self.out_size - components_bbox['right_eye'][0]
115
+ components_bbox['mouth'][0] = self.out_size - components_bbox['mouth'][0]
116
+
117
+ # get coordinates
118
+ locations = []
119
+ for part in ['left_eye', 'right_eye', 'mouth']:
120
+ mean = components_bbox[part][0:2]
121
+ half_len = components_bbox[part][2]
122
+ if 'eye' in part:
123
+ half_len *= self.eye_enlarge_ratio
124
+ loc = np.hstack((mean - half_len + 1, mean + half_len))
125
+ loc = torch.from_numpy(loc).float()
126
+ locations.append(loc)
127
+ return locations
128
+
129
+ def __getitem__(self, index):
130
+ if self.file_client is None:
131
+ self.file_client = FileClient(self.io_backend_opt.pop('type'), **self.io_backend_opt)
132
+
133
+ # load gt image
134
+ gt_path = self.paths[index]
135
+ img_bytes = self.file_client.get(gt_path)
136
+ img_gt = imfrombytes(img_bytes, float32=True)
137
+
138
+ # random horizontal flip
139
+ img_gt, status = augment(img_gt, hflip=self.opt['use_hflip'], rotation=False, return_status=True)
140
+ h, w, _ = img_gt.shape
141
+
142
+ if self.crop_components:
143
+ locations = self.get_component_coordinates(index, status)
144
+ loc_left_eye, loc_right_eye, loc_mouth = locations
145
+
146
+ # ------------------------ generate lq image ------------------------ #
147
+ # blur
148
+ kernel = degradations.random_mixed_kernels(
149
+ self.kernel_list,
150
+ self.kernel_prob,
151
+ self.blur_kernel_size,
152
+ self.blur_sigma,
153
+ self.blur_sigma, [-math.pi, math.pi],
154
+ noise_range=None)
155
+ img_lq = cv2.filter2D(img_gt, -1, kernel)
156
+ # downsample
157
+ scale = np.random.uniform(self.downsample_range[0], self.downsample_range[1])
158
+ img_lq = cv2.resize(img_lq, (int(w // scale), int(h // scale)), interpolation=cv2.INTER_LINEAR)
159
+ # noise
160
+ if self.noise_range is not None:
161
+ img_lq = degradations.random_add_gaussian_noise(img_lq, self.noise_range)
162
+ # jpeg compression
163
+ if self.jpeg_range is not None:
164
+ img_lq = degradations.random_add_jpg_compression(img_lq, self.jpeg_range)
165
+
166
+ # resize to original size
167
+ img_lq = cv2.resize(img_lq, (w, h), interpolation=cv2.INTER_LINEAR)
168
+
169
+ # random color jitter (only for lq)
170
+ if self.color_jitter_prob is not None and (np.random.uniform() < self.color_jitter_prob):
171
+ img_lq = self.color_jitter(img_lq, self.color_jitter_shift)
172
+ # random to gray (only for lq)
173
+ if self.gray_prob and np.random.uniform() < self.gray_prob:
174
+ img_lq = cv2.cvtColor(img_lq, cv2.COLOR_BGR2GRAY)
175
+ img_lq = np.tile(img_lq[:, :, None], [1, 1, 3])
176
+ if self.opt.get('gt_gray'):
177
+ img_gt = cv2.cvtColor(img_gt, cv2.COLOR_BGR2GRAY)
178
+ img_gt = np.tile(img_gt[:, :, None], [1, 1, 3])
179
+
180
+ # BGR to RGB, HWC to CHW, numpy to tensor
181
+ img_gt, img_lq = img2tensor([img_gt, img_lq], bgr2rgb=True, float32=True)
182
+
183
+ # random color jitter (pytorch version) (only for lq)
184
+ if self.color_jitter_pt_prob is not None and (np.random.uniform() < self.color_jitter_pt_prob):
185
+ brightness = self.opt.get('brightness', (0.5, 1.5))
186
+ contrast = self.opt.get('contrast', (0.5, 1.5))
187
+ saturation = self.opt.get('saturation', (0, 1.5))
188
+ hue = self.opt.get('hue', (-0.1, 0.1))
189
+ img_lq = self.color_jitter_pt(img_lq, brightness, contrast, saturation, hue)
190
+
191
+ # round and clip
192
+ img_lq = torch.clamp((img_lq * 255.0).round(), 0, 255) / 255.
193
+
194
+ # normalize
195
+ normalize(img_gt, self.mean, self.std, inplace=True)
196
+ normalize(img_lq, self.mean, self.std, inplace=True)
197
+
198
+ if self.crop_components:
199
+ return_dict = {
200
+ 'lq': img_lq,
201
+ 'gt': img_gt,
202
+ 'gt_path': gt_path,
203
+ 'loc_left_eye': loc_left_eye,
204
+ 'loc_right_eye': loc_right_eye,
205
+ 'loc_mouth': loc_mouth
206
+ }
207
+ return return_dict
208
+ else:
209
+ return {'lq': img_lq, 'gt': img_gt, 'gt_path': gt_path}
210
+
211
+ def __len__(self):
212
+ return len(self.paths)
gfpgan/models/__init__.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
1
+ import importlib
2
+ from basicsr.utils import scandir
3
+ from os import path as osp
4
+
5
+ # automatically scan and import model modules for registry
6
+ # scan all the files that end with '_model.py' under the model folder
7
+ model_folder = osp.dirname(osp.abspath(__file__))
8
+ model_filenames = [osp.splitext(osp.basename(v))[0] for v in scandir(model_folder) if v.endswith('_model.py')]
9
+ # import all the model modules
10
+ _model_modules = [importlib.import_module(f'gfpgan.models.{file_name}') for file_name in model_filenames]
gfpgan/models/gfpgan_model.py ADDED
@@ -0,0 +1,561 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import os.path as osp
3
+ import torch
4
+ from basicsr.archs import build_network
5
+ from basicsr.losses import build_loss
6
+ from basicsr.losses.losses import r1_penalty
7
+ from basicsr.metrics import calculate_metric
8
+ from basicsr.models.base_model import BaseModel
9
+ from basicsr.utils import get_root_logger, imwrite, tensor2img
10
+ from basicsr.utils.registry import MODEL_REGISTRY
11
+ from collections import OrderedDict
12
+ from torch.nn import functional as F
13
+ from torchvision.ops import roi_align
14
+ from tqdm import tqdm
15
+
16
+
17
+ @MODEL_REGISTRY.register()
18
+ class GFPGANModel(BaseModel):
19
+ """GFPGAN model for <Towards real-world blind face restoratin with generative facial prior>"""
20
+
21
+ def __init__(self, opt):
22
+ super(GFPGANModel, self).__init__(opt)
23
+ self.idx = 0
24
+
25
+ # define network
26
+ self.net_g = build_network(opt['network_g'])
27
+ self.net_g = self.model_to_device(self.net_g)
28
+ self.print_network(self.net_g)
29
+
30
+ # load pretrained model
31
+ load_path = self.opt['path'].get('pretrain_network_g', None)
32
+ if load_path is not None:
33
+ param_key = self.opt['path'].get('param_key_g', 'params')
34
+ self.load_network(self.net_g, load_path, self.opt['path'].get('strict_load_g', True), param_key)
35
+
36
+ self.log_size = int(math.log(self.opt['network_g']['out_size'], 2))
37
+
38
+ if self.is_train:
39
+ self.init_training_settings()
40
+
41
+ def init_training_settings(self):
42
+ train_opt = self.opt['train']
43
+
44
+ # ----------- define net_d ----------- #
45
+ self.net_d = build_network(self.opt['network_d'])
46
+ self.net_d = self.model_to_device(self.net_d)
47
+ self.print_network(self.net_d)
48
+ # load pretrained model
49
+ load_path = self.opt['path'].get('pretrain_network_d', None)
50
+ if load_path is not None:
51
+ self.load_network(self.net_d, load_path, self.opt['path'].get('strict_load_d', True))
52
+
53
+ # ----------- define net_g with Exponential Moving Average (EMA) ----------- #
54
+ # net_g_ema only used for testing on one GPU and saving
55
+ # There is no need to wrap with DistributedDataParallel
56
+ self.net_g_ema = build_network(self.opt['network_g']).to(self.device)
57
+ # load pretrained model
58
+ load_path = self.opt['path'].get('pretrain_network_g', None)
59
+ if load_path is not None:
60
+ self.load_network(self.net_g_ema, load_path, self.opt['path'].get('strict_load_g', True), 'params_ema')
61
+ else:
62
+ self.model_ema(0) # copy net_g weight
63
+
64
+ self.net_g.train()
65
+ self.net_d.train()
66
+ self.net_g_ema.eval()
67
+
68
+ # ----------- facial components networks ----------- #
69
+ if ('network_d_left_eye' in self.opt and 'network_d_right_eye' in self.opt and 'network_d_mouth' in self.opt):
70
+ self.use_facial_disc = True
71
+ else:
72
+ self.use_facial_disc = False
73
+
74
+ if self.use_facial_disc:
75
+ # left eye
76
+ self.net_d_left_eye = build_network(self.opt['network_d_left_eye'])
77
+ self.net_d_left_eye = self.model_to_device(self.net_d_left_eye)
78
+ self.print_network(self.net_d_left_eye)
79
+ load_path = self.opt['path'].get('pretrain_network_d_left_eye')
80
+ if load_path is not None:
81
+ self.load_network(self.net_d_left_eye, load_path, True, 'params')
82
+ # right eye
83
+ self.net_d_right_eye = build_network(self.opt['network_d_right_eye'])
84
+ self.net_d_right_eye = self.model_to_device(self.net_d_right_eye)
85
+ self.print_network(self.net_d_right_eye)
86
+ load_path = self.opt['path'].get('pretrain_network_d_right_eye')
87
+ if load_path is not None:
88
+ self.load_network(self.net_d_right_eye, load_path, True, 'params')
89
+ # mouth
90
+ self.net_d_mouth = build_network(self.opt['network_d_mouth'])
91
+ self.net_d_mouth = self.model_to_device(self.net_d_mouth)
92
+ self.print_network(self.net_d_mouth)
93
+ load_path = self.opt['path'].get('pretrain_network_d_mouth')
94
+ if load_path is not None:
95
+ self.load_network(self.net_d_mouth, load_path, True, 'params')
96
+
97
+ self.net_d_left_eye.train()
98
+ self.net_d_right_eye.train()
99
+ self.net_d_mouth.train()
100
+
101
+ # ----------- define facial component gan loss ----------- #
102
+ self.cri_component = build_loss(train_opt['gan_component_opt']).to(self.device)
103
+
104
+ # ----------- define losses ----------- #
105
+ if train_opt.get('pixel_opt'):
106
+ self.cri_pix = build_loss(train_opt['pixel_opt']).to(self.device)
107
+ else:
108
+ self.cri_pix = None
109
+
110
+ if train_opt.get('perceptual_opt'):
111
+ self.cri_perceptual = build_loss(train_opt['perceptual_opt']).to(self.device)
112
+ else:
113
+ self.cri_perceptual = None
114
+
115
+ # L1 loss used in pyramid loss, component style loss and identity loss
116
+ self.cri_l1 = build_loss(train_opt['L1_opt']).to(self.device)
117
+
118
+ # gan loss (wgan)
119
+ self.cri_gan = build_loss(train_opt['gan_opt']).to(self.device)
120
+
121
+ # ----------- define identity loss ----------- #
122
+ if 'network_identity' in self.opt:
123
+ self.use_identity = True
124
+ else:
125
+ self.use_identity = False
126
+
127
+ if self.use_identity:
128
+ # define identity network
129
+ self.network_identity = build_network(self.opt['network_identity'])
130
+ self.network_identity = self.model_to_device(self.network_identity)
131
+ self.print_network(self.network_identity)
132
+ load_path = self.opt['path'].get('pretrain_network_identity')
133
+ if load_path is not None:
134
+ self.load_network(self.network_identity, load_path, True, None)
135
+ self.network_identity.eval()
136
+ for param in self.network_identity.parameters():
137
+ param.requires_grad = False
138
+
139
+ # regularization weights
140
+ self.r1_reg_weight = train_opt['r1_reg_weight'] # for discriminator
141
+ self.net_d_iters = train_opt.get('net_d_iters', 1)
142
+ self.net_d_init_iters = train_opt.get('net_d_init_iters', 0)
143
+ self.net_d_reg_every = train_opt['net_d_reg_every']
144
+
145
+ # set up optimizers and schedulers
146
+ self.setup_optimizers()
147
+ self.setup_schedulers()
148
+
149
+ def setup_optimizers(self):
150
+ train_opt = self.opt['train']
151
+
152
+ # ----------- optimizer g ----------- #
153
+ net_g_reg_ratio = 1
154
+ normal_params = []
155
+ for _, param in self.net_g.named_parameters():
156
+ normal_params.append(param)
157
+ optim_params_g = [{ # add normal params first
158
+ 'params': normal_params,
159
+ 'lr': train_opt['optim_g']['lr']
160
+ }]
161
+ optim_type = train_opt['optim_g'].pop('type')
162
+ lr = train_opt['optim_g']['lr'] * net_g_reg_ratio
163
+ betas = (0**net_g_reg_ratio, 0.99**net_g_reg_ratio)
164
+ self.optimizer_g = self.get_optimizer(optim_type, optim_params_g, lr, betas=betas)
165
+ self.optimizers.append(self.optimizer_g)
166
+
167
+ # ----------- optimizer d ----------- #
168
+ net_d_reg_ratio = self.net_d_reg_every / (self.net_d_reg_every + 1)
169
+ normal_params = []
170
+ for _, param in self.net_d.named_parameters():
171
+ normal_params.append(param)
172
+ optim_params_d = [{ # add normal params first
173
+ 'params': normal_params,
174
+ 'lr': train_opt['optim_d']['lr']
175
+ }]
176
+ optim_type = train_opt['optim_d'].pop('type')
177
+ lr = train_opt['optim_d']['lr'] * net_d_reg_ratio
178
+ betas = (0**net_d_reg_ratio, 0.99**net_d_reg_ratio)
179
+ self.optimizer_d = self.get_optimizer(optim_type, optim_params_d, lr, betas=betas)
180
+ self.optimizers.append(self.optimizer_d)
181
+
182
+ if self.use_facial_disc:
183
+ # setup optimizers for facial component discriminators
184
+ optim_type = train_opt['optim_component'].pop('type')
185
+ lr = train_opt['optim_component']['lr']
186
+ # left eye
187
+ self.optimizer_d_left_eye = self.get_optimizer(
188
+ optim_type, self.net_d_left_eye.parameters(), lr, betas=(0.9, 0.99))
189
+ self.optimizers.append(self.optimizer_d_left_eye)
190
+ # right eye
191
+ self.optimizer_d_right_eye = self.get_optimizer(
192
+ optim_type, self.net_d_right_eye.parameters(), lr, betas=(0.9, 0.99))
193
+ self.optimizers.append(self.optimizer_d_right_eye)
194
+ # mouth
195
+ self.optimizer_d_mouth = self.get_optimizer(
196
+ optim_type, self.net_d_mouth.parameters(), lr, betas=(0.9, 0.99))
197
+ self.optimizers.append(self.optimizer_d_mouth)
198
+
199
+ def feed_data(self, data):
200
+ self.lq = data['lq'].to(self.device)
201
+ if 'gt' in data:
202
+ self.gt = data['gt'].to(self.device)
203
+
204
+ if 'loc_left_eye' in data:
205
+ # get facial component locations, shape (batch, 4)
206
+ self.loc_left_eyes = data['loc_left_eye']
207
+ self.loc_right_eyes = data['loc_right_eye']
208
+ self.loc_mouths = data['loc_mouth']
209
+
210
+ # uncomment to check data
211
+ # import torchvision
212
+ # if self.opt['rank'] == 0:
213
+ # import os
214
+ # os.makedirs('tmp/gt', exist_ok=True)
215
+ # os.makedirs('tmp/lq', exist_ok=True)
216
+ # print(self.idx)
217
+ # torchvision.utils.save_image(
218
+ # self.gt, f'tmp/gt/gt_{self.idx}.png', nrow=4, padding=2, normalize=True, range=(-1, 1))
219
+ # torchvision.utils.save_image(
220
+ # self.lq, f'tmp/lq/lq{self.idx}.png', nrow=4, padding=2, normalize=True, range=(-1, 1))
221
+ # self.idx = self.idx + 1
222
+
223
+ def construct_img_pyramid(self):
224
+ pyramid_gt = [self.gt]
225
+ down_img = self.gt
226
+ for _ in range(0, self.log_size - 3):
227
+ down_img = F.interpolate(down_img, scale_factor=0.5, mode='bilinear', align_corners=False)
228
+ pyramid_gt.insert(0, down_img)
229
+ return pyramid_gt
230
+
231
+ def get_roi_regions(self, eye_out_size=80, mouth_out_size=120):
232
+ # hard code
233
+ face_ratio = int(self.opt['network_g']['out_size'] / 512)
234
+ eye_out_size *= face_ratio
235
+ mouth_out_size *= face_ratio
236
+
237
+ rois_eyes = []
238
+ rois_mouths = []
239
+ for b in range(self.loc_left_eyes.size(0)): # loop for batch size
240
+ # left eye and right eye
241
+ img_inds = self.loc_left_eyes.new_full((2, 1), b)
242
+ bbox = torch.stack([self.loc_left_eyes[b, :], self.loc_right_eyes[b, :]], dim=0) # shape: (2, 4)
243
+ rois = torch.cat([img_inds, bbox], dim=-1) # shape: (2, 5)
244
+ rois_eyes.append(rois)
245
+ # mouse
246
+ img_inds = self.loc_left_eyes.new_full((1, 1), b)
247
+ rois = torch.cat([img_inds, self.loc_mouths[b:b + 1, :]], dim=-1) # shape: (1, 5)
248
+ rois_mouths.append(rois)
249
+
250
+ rois_eyes = torch.cat(rois_eyes, 0).to(self.device)
251
+ rois_mouths = torch.cat(rois_mouths, 0).to(self.device)
252
+
253
+ # real images
254
+ all_eyes = roi_align(self.gt, boxes=rois_eyes, output_size=eye_out_size) * face_ratio
255
+ self.left_eyes_gt = all_eyes[0::2, :, :, :]
256
+ self.right_eyes_gt = all_eyes[1::2, :, :, :]
257
+ self.mouths_gt = roi_align(self.gt, boxes=rois_mouths, output_size=mouth_out_size) * face_ratio
258
+ # output
259
+ all_eyes = roi_align(self.output, boxes=rois_eyes, output_size=eye_out_size) * face_ratio
260
+ self.left_eyes = all_eyes[0::2, :, :, :]
261
+ self.right_eyes = all_eyes[1::2, :, :, :]
262
+ self.mouths = roi_align(self.output, boxes=rois_mouths, output_size=mouth_out_size) * face_ratio
263
+
264
+ def _gram_mat(self, x):
265
+ """Calculate Gram matrix.
266
+
267
+ Args:
268
+ x (torch.Tensor): Tensor with shape of (n, c, h, w).
269
+
270
+ Returns:
271
+ torch.Tensor: Gram matrix.
272
+ """
273
+ n, c, h, w = x.size()
274
+ features = x.view(n, c, w * h)
275
+ features_t = features.transpose(1, 2)
276
+ gram = features.bmm(features_t) / (c * h * w)
277
+ return gram
278
+
279
+ def gray_resize_for_identity(self, out, size=128):
280
+ out_gray = (0.2989 * out[:, 0, :, :] + 0.5870 * out[:, 1, :, :] + 0.1140 * out[:, 2, :, :])
281
+ out_gray = out_gray.unsqueeze(1)
282
+ out_gray = F.interpolate(out_gray, (size, size), mode='bilinear', align_corners=False)
283
+ return out_gray
284
+
285
+ def optimize_parameters(self, current_iter):
286
+ # optimize net_g
287
+ for p in self.net_d.parameters():
288
+ p.requires_grad = False
289
+ self.optimizer_g.zero_grad()
290
+
291
+ if self.use_facial_disc:
292
+ for p in self.net_d_left_eye.parameters():
293
+ p.requires_grad = False
294
+ for p in self.net_d_right_eye.parameters():
295
+ p.requires_grad = False
296
+ for p in self.net_d_mouth.parameters():
297
+ p.requires_grad = False
298
+
299
+ # image pyramid loss weight
300
+ if current_iter < self.opt['train'].get('remove_pyramid_loss', float('inf')):
301
+ pyramid_loss_weight = self.opt['train'].get('pyramid_loss_weight', 1)
302
+ else:
303
+ pyramid_loss_weight = 1e-12 # very small loss
304
+ if pyramid_loss_weight > 0:
305
+ self.output, out_rgbs = self.net_g(self.lq, return_rgb=True)
306
+ pyramid_gt = self.construct_img_pyramid()
307
+ else:
308
+ self.output, out_rgbs = self.net_g(self.lq, return_rgb=False)
309
+
310
+ # get roi-align regions
311
+ if self.use_facial_disc:
312
+ self.get_roi_regions(eye_out_size=80, mouth_out_size=120)
313
+
314
+ l_g_total = 0
315
+ loss_dict = OrderedDict()
316
+ if (current_iter % self.net_d_iters == 0 and current_iter > self.net_d_init_iters):
317
+ # pixel loss
318
+ if self.cri_pix:
319
+ l_g_pix = self.cri_pix(self.output, self.gt)
320
+ l_g_total += l_g_pix
321
+ loss_dict['l_g_pix'] = l_g_pix
322
+
323
+ # image pyramid loss
324
+ if pyramid_loss_weight > 0:
325
+ for i in range(0, self.log_size - 2):
326
+ l_pyramid = self.cri_l1(out_rgbs[i], pyramid_gt[i]) * pyramid_loss_weight
327
+ l_g_total += l_pyramid
328
+ loss_dict[f'l_p_{2**(i+3)}'] = l_pyramid
329
+
330
+ # perceptual loss
331
+ if self.cri_perceptual:
332
+ l_g_percep, l_g_style = self.cri_perceptual(self.output, self.gt)
333
+ if l_g_percep is not None:
334
+ l_g_total += l_g_percep
335
+ loss_dict['l_g_percep'] = l_g_percep
336
+ if l_g_style is not None:
337
+ l_g_total += l_g_style
338
+ loss_dict['l_g_style'] = l_g_style
339
+
340
+ # gan loss
341
+ fake_g_pred = self.net_d(self.output)
342
+ l_g_gan = self.cri_gan(fake_g_pred, True, is_disc=False)
343
+ l_g_total += l_g_gan
344
+ loss_dict['l_g_gan'] = l_g_gan
345
+
346
+ # facial component loss
347
+ if self.use_facial_disc:
348
+ # left eye
349
+ fake_left_eye, fake_left_eye_feats = self.net_d_left_eye(self.left_eyes, return_feats=True)
350
+ l_g_gan = self.cri_component(fake_left_eye, True, is_disc=False)
351
+ l_g_total += l_g_gan
352
+ loss_dict['l_g_gan_left_eye'] = l_g_gan
353
+ # right eye
354
+ fake_right_eye, fake_right_eye_feats = self.net_d_right_eye(self.right_eyes, return_feats=True)
355
+ l_g_gan = self.cri_component(fake_right_eye, True, is_disc=False)
356
+ l_g_total += l_g_gan
357
+ loss_dict['l_g_gan_right_eye'] = l_g_gan
358
+ # mouth
359
+ fake_mouth, fake_mouth_feats = self.net_d_mouth(self.mouths, return_feats=True)
360
+ l_g_gan = self.cri_component(fake_mouth, True, is_disc=False)
361
+ l_g_total += l_g_gan
362
+ loss_dict['l_g_gan_mouth'] = l_g_gan
363
+
364
+ if self.opt['train'].get('comp_style_weight', 0) > 0:
365
+ # get gt feat
366
+ _, real_left_eye_feats = self.net_d_left_eye(self.left_eyes_gt, return_feats=True)
367
+ _, real_right_eye_feats = self.net_d_right_eye(self.right_eyes_gt, return_feats=True)
368
+ _, real_mouth_feats = self.net_d_mouth(self.mouths_gt, return_feats=True)
369
+
370
+ def _comp_style(feat, feat_gt, criterion):
371
+ return criterion(self._gram_mat(feat[0]), self._gram_mat(
372
+ feat_gt[0].detach())) * 0.5 + criterion(
373
+ self._gram_mat(feat[1]), self._gram_mat(feat_gt[1].detach()))
374
+
375
+ # facial component style loss
376
+ comp_style_loss = 0
377
+ comp_style_loss += _comp_style(fake_left_eye_feats, real_left_eye_feats, self.cri_l1)
378
+ comp_style_loss += _comp_style(fake_right_eye_feats, real_right_eye_feats, self.cri_l1)
379
+ comp_style_loss += _comp_style(fake_mouth_feats, real_mouth_feats, self.cri_l1)
380
+ comp_style_loss = comp_style_loss * self.opt['train']['comp_style_weight']
381
+ l_g_total += comp_style_loss
382
+ loss_dict['l_g_comp_style_loss'] = comp_style_loss
383
+
384
+ # identity loss
385
+ if self.use_identity:
386
+ identity_weight = self.opt['train']['identity_weight']
387
+ # get gray images and resize
388
+ out_gray = self.gray_resize_for_identity(self.output)
389
+ gt_gray = self.gray_resize_for_identity(self.gt)
390
+
391
+ identity_gt = self.network_identity(gt_gray).detach()
392
+ identity_out = self.network_identity(out_gray)
393
+ l_identity = self.cri_l1(identity_out, identity_gt) * identity_weight
394
+ l_g_total += l_identity
395
+ loss_dict['l_identity'] = l_identity
396
+
397
+ l_g_total.backward()
398
+ self.optimizer_g.step()
399
+
400
+ # EMA
401
+ self.model_ema(decay=0.5**(32 / (10 * 1000)))
402
+
403
+ # ----------- optimize net_d ----------- #
404
+ for p in self.net_d.parameters():
405
+ p.requires_grad = True
406
+ self.optimizer_d.zero_grad()
407
+ if self.use_facial_disc:
408
+ for p in self.net_d_left_eye.parameters():
409
+ p.requires_grad = True
410
+ for p in self.net_d_right_eye.parameters():
411
+ p.requires_grad = True
412
+ for p in self.net_d_mouth.parameters():
413
+ p.requires_grad = True
414
+ self.optimizer_d_left_eye.zero_grad()
415
+ self.optimizer_d_right_eye.zero_grad()
416
+ self.optimizer_d_mouth.zero_grad()
417
+
418
+ fake_d_pred = self.net_d(self.output.detach())
419
+ real_d_pred = self.net_d(self.gt)
420
+ l_d = self.cri_gan(real_d_pred, True, is_disc=True) + self.cri_gan(fake_d_pred, False, is_disc=True)
421
+ loss_dict['l_d'] = l_d
422
+ # In wgan, real_score should be positive and fake_score should benegative
423
+ loss_dict['real_score'] = real_d_pred.detach().mean()
424
+ loss_dict['fake_score'] = fake_d_pred.detach().mean()
425
+ l_d.backward()
426
+
427
+ if current_iter % self.net_d_reg_every == 0:
428
+ self.gt.requires_grad = True
429
+ real_pred = self.net_d(self.gt)
430
+ l_d_r1 = r1_penalty(real_pred, self.gt)
431
+ l_d_r1 = (self.r1_reg_weight / 2 * l_d_r1 * self.net_d_reg_every + 0 * real_pred[0])
432
+ loss_dict['l_d_r1'] = l_d_r1.detach().mean()
433
+ l_d_r1.backward()
434
+
435
+ self.optimizer_d.step()
436
+
437
+ if self.use_facial_disc:
438
+ # lefe eye
439
+ fake_d_pred, _ = self.net_d_left_eye(self.left_eyes.detach())
440
+ real_d_pred, _ = self.net_d_left_eye(self.left_eyes_gt)
441
+ l_d_left_eye = self.cri_component(
442
+ real_d_pred, True, is_disc=True) + self.cri_gan(
443
+ fake_d_pred, False, is_disc=True)
444
+ loss_dict['l_d_left_eye'] = l_d_left_eye
445
+ l_d_left_eye.backward()
446
+ # right eye
447
+ fake_d_pred, _ = self.net_d_right_eye(self.right_eyes.detach())
448
+ real_d_pred, _ = self.net_d_right_eye(self.right_eyes_gt)
449
+ l_d_right_eye = self.cri_component(
450
+ real_d_pred, True, is_disc=True) + self.cri_gan(
451
+ fake_d_pred, False, is_disc=True)
452
+ loss_dict['l_d_right_eye'] = l_d_right_eye
453
+ l_d_right_eye.backward()
454
+ # mouth
455
+ fake_d_pred, _ = self.net_d_mouth(self.mouths.detach())
456
+ real_d_pred, _ = self.net_d_mouth(self.mouths_gt)
457
+ l_d_mouth = self.cri_component(
458
+ real_d_pred, True, is_disc=True) + self.cri_gan(
459
+ fake_d_pred, False, is_disc=True)
460
+ loss_dict['l_d_mouth'] = l_d_mouth
461
+ l_d_mouth.backward()
462
+
463
+ self.optimizer_d_left_eye.step()
464
+ self.optimizer_d_right_eye.step()
465
+ self.optimizer_d_mouth.step()
466
+
467
+ self.log_dict = self.reduce_loss_dict(loss_dict)
468
+
469
+ def test(self):
470
+ with torch.no_grad():
471
+ if hasattr(self, 'net_g_ema'):
472
+ self.net_g_ema.eval()
473
+ self.output, _ = self.net_g_ema(self.lq)
474
+ else:
475
+ logger = get_root_logger()
476
+ logger.warning('Do not have self.net_g_ema, use self.net_g.')
477
+ self.net_g.eval()
478
+ self.output, _ = self.net_g(self.lq)
479
+ self.net_g.train()
480
+
481
+ def dist_validation(self, dataloader, current_iter, tb_logger, save_img):
482
+ if self.opt['rank'] == 0:
483
+ self.nondist_validation(dataloader, current_iter, tb_logger, save_img)
484
+
485
+ def nondist_validation(self, dataloader, current_iter, tb_logger, save_img):
486
+ dataset_name = dataloader.dataset.opt['name']
487
+ with_metrics = self.opt['val'].get('metrics') is not None
488
+ if with_metrics:
489
+ self.metric_results = {metric: 0 for metric in self.opt['val']['metrics'].keys()}
490
+ pbar = tqdm(total=len(dataloader), unit='image')
491
+
492
+ for idx, val_data in enumerate(dataloader):
493
+ img_name = osp.splitext(osp.basename(val_data['lq_path'][0]))[0]
494
+ self.feed_data(val_data)
495
+ self.test()
496
+
497
+ visuals = self.get_current_visuals()
498
+ sr_img = tensor2img([visuals['sr']], min_max=(-1, 1))
499
+ gt_img = tensor2img([visuals['gt']], min_max=(-1, 1))
500
+
501
+ if 'gt' in visuals:
502
+ gt_img = tensor2img([visuals['gt']], min_max=(-1, 1))
503
+ del self.gt
504
+ # tentative for out of GPU memory
505
+ del self.lq
506
+ del self.output
507
+ torch.cuda.empty_cache()
508
+
509
+ if save_img:
510
+ if self.opt['is_train']:
511
+ save_img_path = osp.join(self.opt['path']['visualization'], img_name,
512
+ f'{img_name}_{current_iter}.png')
513
+ else:
514
+ if self.opt['val']['suffix']:
515
+ save_img_path = osp.join(self.opt['path']['visualization'], dataset_name,
516
+ f'{img_name}_{self.opt["val"]["suffix"]}.png')
517
+ else:
518
+ save_img_path = osp.join(self.opt['path']['visualization'], dataset_name,
519
+ f'{img_name}_{self.opt["name"]}.png')
520
+ imwrite(sr_img, save_img_path)
521
+
522
+ if with_metrics:
523
+ # calculate metrics
524
+ for name, opt_ in self.opt['val']['metrics'].items():
525
+ metric_data = dict(img1=sr_img, img2=gt_img)
526
+ self.metric_results[name] += calculate_metric(metric_data, opt_)
527
+ pbar.update(1)
528
+ pbar.set_description(f'Test {img_name}')
529
+ pbar.close()
530
+
531
+ if with_metrics:
532
+ for metric in self.metric_results.keys():
533
+ self.metric_results[metric] /= (idx + 1)
534
+
535
+ self._log_validation_metric_values(current_iter, dataset_name, tb_logger)
536
+
537
+ def _log_validation_metric_values(self, current_iter, dataset_name, tb_logger):
538
+ log_str = f'Validation {dataset_name}\n'
539
+ for metric, value in self.metric_results.items():
540
+ log_str += f'\t # {metric}: {value:.4f}\n'
541
+ logger = get_root_logger()
542
+ logger.info(log_str)
543
+ if tb_logger:
544
+ for metric, value in self.metric_results.items():
545
+ tb_logger.add_scalar(f'metrics/{metric}', value, current_iter)
546
+
547
+ def get_current_visuals(self):
548
+ out_dict = OrderedDict()
549
+ out_dict['gt'] = self.gt.detach().cpu()
550
+ out_dict['sr'] = self.output.detach().cpu()
551
+ return out_dict
552
+
553
+ def save(self, epoch, current_iter):
554
+ self.save_network([self.net_g, self.net_g_ema], 'net_g', current_iter, param_key=['params', 'params_ema'])
555
+ self.save_network(self.net_d, 'net_d', current_iter)
556
+ # save component discriminators
557
+ if self.use_facial_disc:
558
+ self.save_network(self.net_d_left_eye, 'net_d_left_eye', current_iter)
559
+ self.save_network(self.net_d_right_eye, 'net_d_right_eye', current_iter)
560
+ self.save_network(self.net_d_mouth, 'net_d_mouth', current_iter)
561
+ self.save_training_state(epoch, current_iter)
gfpgan/train.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ # flake8: noqa
2
+ import os.path as osp
3
+ from basicsr.train import train_pipeline
4
+
5
+ import gfpgan.archs
6
+ import gfpgan.data
7
+ import gfpgan.models
8
+
9
+ if __name__ == '__main__':
10
+ root_path = osp.abspath(osp.join(__file__, osp.pardir, osp.pardir))
11
+ train_pipeline(root_path)
gfpgan/utils.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import os
3
+ import torch
4
+ from basicsr.utils import img2tensor, tensor2img
5
+ from facexlib.utils.face_restoration_helper import FaceRestoreHelper
6
+ from torch.hub import download_url_to_file, get_dir
7
+ from torchvision.transforms.functional import normalize
8
+ from urllib.parse import urlparse
9
+
10
+ from gfpgan.archs.gfpganv1_arch import GFPGANv1
11
+ from gfpgan.archs.gfpganv1_clean_arch import GFPGANv1Clean
12
+
13
+ ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
14
+
15
+
16
+ class GFPGANer():
17
+
18
+ def __init__(self, model_path, upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=None):
19
+ self.upscale = upscale
20
+ self.bg_upsampler = bg_upsampler
21
+
22
+ # initialize model
23
+ self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
24
+ # initialize the GFP-GAN
25
+ if arch == 'clean':
26
+ self.gfpgan = GFPGANv1Clean(
27
+ out_size=512,
28
+ num_style_feat=512,
29
+ channel_multiplier=channel_multiplier,
30
+ decoder_load_path=None,
31
+ fix_decoder=False,
32
+ num_mlp=8,
33
+ input_is_latent=True,
34
+ different_w=True,
35
+ narrow=1,
36
+ sft_half=True)
37
+ else:
38
+ self.gfpgan = GFPGANv1(
39
+ out_size=512,
40
+ num_style_feat=512,
41
+ channel_multiplier=channel_multiplier,
42
+ decoder_load_path=None,
43
+ fix_decoder=True,
44
+ num_mlp=8,
45
+ input_is_latent=True,
46
+ different_w=True,
47
+ narrow=1,
48
+ sft_half=True)
49
+ # initialize face helper
50
+ self.face_helper = FaceRestoreHelper(
51
+ upscale,
52
+ face_size=512,
53
+ crop_ratio=(1, 1),
54
+ det_model='retinaface_resnet50',
55
+ save_ext='png',
56
+ device=self.device)
57
+
58
+ if model_path.startswith('https://'):
59
+ model_path = load_file_from_url(url=model_path, model_dir='gfpgan/weights', progress=True, file_name=None)
60
+ loadnet = torch.load(model_path)
61
+ if 'params_ema' in loadnet:
62
+ keyname = 'params_ema'
63
+ else:
64
+ keyname = 'params'
65
+ self.gfpgan.load_state_dict(loadnet[keyname], strict=True)
66
+ self.gfpgan.eval()
67
+ self.gfpgan = self.gfpgan.to(self.device)
68
+
69
+ @torch.no_grad()
70
+ def enhance(self, img, has_aligned=False, only_center_face=False, paste_back=True):
71
+ self.face_helper.clean_all()
72
+
73
+ if has_aligned:
74
+ img = cv2.resize(img, (512, 512))
75
+ self.face_helper.cropped_faces = [img]
76
+ else:
77
+ self.face_helper.read_image(img)
78
+ # get face landmarks for each face
79
+ self.face_helper.get_face_landmarks_5(only_center_face=only_center_face)
80
+ # align and warp each face
81
+ self.face_helper.align_warp_face()
82
+
83
+ # face restoration
84
+ for cropped_face in self.face_helper.cropped_faces:
85
+ # prepare data
86
+ cropped_face_t = img2tensor(cropped_face / 255., bgr2rgb=True, float32=True)
87
+ normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)
88
+ cropped_face_t = cropped_face_t.unsqueeze(0).to(self.device)
89
+
90
+ try:
91
+ output = self.gfpgan(cropped_face_t, return_rgb=False)[0]
92
+ # convert to image
93
+ restored_face = tensor2img(output.squeeze(0), rgb2bgr=True, min_max=(-1, 1))
94
+ except RuntimeError as error:
95
+ print(f'\tFailed inference for GFPGAN: {error}.')
96
+ restored_face = cropped_face
97
+
98
+ restored_face = restored_face.astype('uint8')
99
+ self.face_helper.add_restored_face(restored_face)
100
+
101
+ if not has_aligned and paste_back:
102
+
103
+ if self.bg_upsampler is not None:
104
+ # Now only support RealESRGAN
105
+ bg_img = self.bg_upsampler.enhance(img, outscale=self.upscale)[0]
106
+ else:
107
+ bg_img = None
108
+
109
+ self.face_helper.get_inverse_affine(None)
110
+ # paste each restored face to the input image
111
+ restored_img = self.face_helper.paste_faces_to_input_image(upsample_img=bg_img)
112
+ return self.face_helper.cropped_faces, self.face_helper.restored_faces, restored_img
113
+ else:
114
+ return self.face_helper.cropped_faces, self.face_helper.restored_faces, None
115
+
116
+
117
+ def load_file_from_url(url, model_dir=None, progress=True, file_name=None):
118
+ """Ref:https://github.com/1adrianb/face-alignment/blob/master/face_alignment/utils.py
119
+ """
120
+ if model_dir is None:
121
+ hub_dir = get_dir()
122
+ model_dir = os.path.join(hub_dir, 'checkpoints')
123
+
124
+ os.makedirs(os.path.join(ROOT_DIR, model_dir), exist_ok=True)
125
+
126
+ parts = urlparse(url)
127
+ filename = os.path.basename(parts.path)
128
+ if file_name is not None:
129
+ filename = file_name
130
+ cached_file = os.path.abspath(os.path.join(ROOT_DIR, model_dir, filename))
131
+ if not os.path.exists(cached_file):
132
+ print(f'Downloading: "{url}" to {cached_file}\n')
133
+ download_url_to_file(url, cached_file, hash_prefix=None, progress=progress)
134
+ return cached_file
gfpgan/weights/README.md ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ # Weights
2
+
3
+ Put the downloaded weights to this folder.
inference_gfpgan.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import cv2
3
+ import glob
4
+ import numpy as np
5
+ import os
6
+ import torch
7
+ from basicsr.utils import imwrite
8
+
9
+ from gfpgan import GFPGANer
10
+
11
+
12
+ def main():
13
+ parser = argparse.ArgumentParser()
14
+
15
+ parser.add_argument('--upscale', type=int, default=2)
16
+ parser.add_argument('--arch', type=str, default='clean')
17
+ parser.add_argument('--channel', type=int, default=2)
18
+ parser.add_argument('--model_path', type=str, default='experiments/pretrained_models/GFPGANCleanv1-NoCE-C2.pth')
19
+ parser.add_argument('--bg_upsampler', type=str, default='realesrgan')
20
+ parser.add_argument('--bg_tile', type=int, default=400)
21
+ parser.add_argument('--test_path', type=str, default='inputs/whole_imgs')
22
+ parser.add_argument('--suffix', type=str, default=None, help='Suffix of the restored faces')
23
+ parser.add_argument('--only_center_face', action='store_true')
24
+ parser.add_argument('--aligned', action='store_true')
25
+ parser.add_argument('--paste_back', action='store_false')
26
+ parser.add_argument('--save_root', type=str, default='results')
27
+
28
+ args = parser.parse_args()
29
+ if args.test_path.endswith('/'):
30
+ args.test_path = args.test_path[:-1]
31
+ os.makedirs(args.save_root, exist_ok=True)
32
+
33
+ # background upsampler
34
+ if args.bg_upsampler == 'realesrgan':
35
+ if not torch.cuda.is_available(): # CPU
36
+ import warnings
37
+ warnings.warn('The unoptimized RealESRGAN is very slow on CPU. We do not use it. '
38
+ 'If you really want to use it, please modify the corresponding codes.')
39
+ bg_upsampler = None
40
+ else:
41
+ from realesrgan import RealESRGANer
42
+ bg_upsampler = RealESRGANer(
43
+ scale=2,
44
+ model_path='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth',
45
+ tile=args.bg_tile,
46
+ tile_pad=10,
47
+ pre_pad=0,
48
+ half=True) # need to set False in CPU mode
49
+ else:
50
+ bg_upsampler = None
51
+ # set up GFPGAN restorer
52
+ restorer = GFPGANer(
53
+ model_path=args.model_path,
54
+ upscale=args.upscale,
55
+ arch=args.arch,
56
+ channel_multiplier=args.channel,
57
+ bg_upsampler=bg_upsampler)
58
+
59
+ img_list = sorted(glob.glob(os.path.join(args.test_path, '*')))
60
+ for img_path in img_list:
61
+ # read image
62
+ img_name = os.path.basename(img_path)
63
+ print(f'Processing {img_name} ...')
64
+ basename, ext = os.path.splitext(img_name)
65
+ input_img = cv2.imread(img_path, cv2.IMREAD_COLOR)
66
+
67
+ cropped_faces, restored_faces, restored_img = restorer.enhance(
68
+ input_img, has_aligned=args.aligned, only_center_face=args.only_center_face, paste_back=args.paste_back)
69
+
70
+ # save faces
71
+ for idx, (cropped_face, restored_face) in enumerate(zip(cropped_faces, restored_faces)):
72
+ # save cropped face
73
+ save_crop_path = os.path.join(args.save_root, 'cropped_faces', f'{basename}_{idx:02d}.png')
74
+ imwrite(cropped_face, save_crop_path)
75
+ # save restored face
76
+ if args.suffix is not None:
77
+ save_face_name = f'{basename}_{idx:02d}_{args.suffix}.png'
78
+ else:
79
+ save_face_name = f'{basename}_{idx:02d}.png'
80
+ save_restore_path = os.path.join(args.save_root, 'restored_faces', save_face_name)
81
+ imwrite(restored_face, save_restore_path)
82
+ # save cmp image
83
+ cmp_img = np.concatenate((cropped_face, restored_face), axis=1)
84
+ imwrite(cmp_img, os.path.join(args.save_root, 'cmp', f'{basename}_{idx:02d}.png'))
85
+
86
+ # save restored img
87
+ if restored_img is not None:
88
+ if args.suffix is not None:
89
+ save_restore_path = os.path.join(args.save_root, 'restored_imgs', f'{basename}_{args.suffix}{ext}')
90
+ else:
91
+ save_restore_path = os.path.join(args.save_root, 'restored_imgs', img_name)
92
+ imwrite(restored_img, save_restore_path)
93
+
94
+ print(f'Results are in the [{args.save_root}] folder.')
95
+
96
+
97
+ if __name__ == '__main__':
98
+ main()
options/train_gfpgan_v1.yml ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # general settings
2
+ name: train_GFPGANv1_512
3
+ model_type: GFPGANModel
4
+ num_gpu: 4
5
+ manual_seed: 0
6
+
7
+ # dataset and data loader settings
8
+ datasets:
9
+ train:
10
+ name: FFHQ
11
+ type: FFHQDegradationDataset
12
+ # dataroot_gt: datasets/ffhq/ffhq_512.lmdb
13
+ dataroot_gt: datasets/ffhq/ffhq_512
14
+ io_backend:
15
+ # type: lmdb
16
+ type: disk
17
+
18
+ use_hflip: true
19
+ mean: [0.5, 0.5, 0.5]
20
+ std: [0.5, 0.5, 0.5]
21
+ out_size: 512
22
+
23
+ blur_kernel_size: 41
24
+ kernel_list: ['iso', 'aniso']
25
+ kernel_prob: [0.5, 0.5]
26
+ blur_sigma: [0.1, 10]
27
+ downsample_range: [0.8, 8]
28
+ noise_range: [0, 20]
29
+ jpeg_range: [60, 100]
30
+
31
+ # color jitter and gray
32
+ color_jitter_prob: 0.3
33
+ color_jitter_shift: 20
34
+ color_jitter_pt_prob: 0.3
35
+ gray_prob: 0.01
36
+
37
+ # If you do not want colorization, please set
38
+ # color_jitter_prob: ~
39
+ # color_jitter_pt_prob: ~
40
+ # gray_prob: 0.01
41
+ # gt_gray: True
42
+
43
+ crop_components: true
44
+ component_path: experiments/pretrained_models/FFHQ_eye_mouth_landmarks_512.pth
45
+ eye_enlarge_ratio: 1.4
46
+
47
+ # data loader
48
+ use_shuffle: true
49
+ num_worker_per_gpu: 6
50
+ batch_size_per_gpu: 3
51
+ dataset_enlarge_ratio: 1
52
+ prefetch_mode: ~
53
+
54
+ val:
55
+ # Please modify accordingly to use your own validation
56
+ # Or comment the val block if do not need validation during training
57
+ name: validation
58
+ type: PairedImageDataset
59
+ dataroot_lq: datasets/faces/validation/input
60
+ dataroot_gt: datasets/faces/validation/reference
61
+ io_backend:
62
+ type: disk
63
+ mean: [0.5, 0.5, 0.5]
64
+ std: [0.5, 0.5, 0.5]
65
+ scale: 1
66
+
67
+ # network structures
68
+ network_g:
69
+ type: GFPGANv1
70
+ out_size: 512
71
+ num_style_feat: 512
72
+ channel_multiplier: 1
73
+ resample_kernel: [1, 3, 3, 1]
74
+ decoder_load_path: experiments/pretrained_models/StyleGAN2_512_Cmul1_FFHQ_B12G4_scratch_800k.pth
75
+ fix_decoder: true
76
+ num_mlp: 8
77
+ lr_mlp: 0.01
78
+ input_is_latent: true
79
+ different_w: true
80
+ narrow: 1
81
+ sft_half: true
82
+
83
+ network_d:
84
+ type: StyleGAN2Discriminator
85
+ out_size: 512
86
+ channel_multiplier: 1
87
+ resample_kernel: [1, 3, 3, 1]
88
+
89
+ network_d_left_eye:
90
+ type: FacialComponentDiscriminator
91
+
92
+ network_d_right_eye:
93
+ type: FacialComponentDiscriminator
94
+
95
+ network_d_mouth:
96
+ type: FacialComponentDiscriminator
97
+
98
+ network_identity:
99
+ type: ResNetArcFace
100
+ block: IRBlock
101
+ layers: [2, 2, 2, 2]
102
+ use_se: False
103
+
104
+ # path
105
+ path:
106
+ pretrain_network_g: ~
107
+ param_key_g: params_ema
108
+ strict_load_g: ~
109
+ pretrain_network_d: ~
110
+ pretrain_network_d_left_eye: ~
111
+ pretrain_network_d_right_eye: ~
112
+ pretrain_network_d_mouth: ~
113
+ pretrain_network_identity: experiments/pretrained_models/arcface_resnet18.pth
114
+ # resume
115
+ resume_state: ~
116
+ ignore_resume_networks: ['network_identity']
117
+
118
+ # training settings
119
+ train:
120
+ optim_g:
121
+ type: Adam
122
+ lr: !!float 2e-3
123
+ optim_d:
124
+ type: Adam
125
+ lr: !!float 2e-3
126
+ optim_component:
127
+ type: Adam
128
+ lr: !!float 2e-3
129
+
130
+ scheduler:
131
+ type: MultiStepLR
132
+ milestones: [600000, 700000]
133
+ gamma: 0.5
134
+
135
+ total_iter: 800000
136
+ warmup_iter: -1 # no warm up
137
+
138
+ # losses
139
+ # pixel loss
140
+ pixel_opt:
141
+ type: L1Loss
142
+ loss_weight: !!float 1e-1
143
+ reduction: mean
144
+ # L1 loss used in pyramid loss, component style loss and identity loss
145
+ L1_opt:
146
+ type: L1Loss
147
+ loss_weight: 1
148
+ reduction: mean
149
+
150
+ # image pyramid loss
151
+ pyramid_loss_weight: 1
152
+ remove_pyramid_loss: 50000
153
+ # perceptual loss (content and style losses)
154
+ perceptual_opt:
155
+ type: PerceptualLoss
156
+ layer_weights:
157
+ # before relu
158
+ 'conv1_2': 0.1
159
+ 'conv2_2': 0.1
160
+ 'conv3_4': 1
161
+ 'conv4_4': 1
162
+ 'conv5_4': 1
163
+ vgg_type: vgg19
164
+ use_input_norm: true
165
+ perceptual_weight: !!float 1
166
+ style_weight: 50
167
+ range_norm: true
168
+ criterion: l1
169
+ # gan loss
170
+ gan_opt:
171
+ type: GANLoss
172
+ gan_type: wgan_softplus
173
+ loss_weight: !!float 1e-1
174
+ # r1 regularization for discriminator
175
+ r1_reg_weight: 10
176
+ # facial component loss
177
+ gan_component_opt:
178
+ type: GANLoss
179
+ gan_type: vanilla
180
+ real_label_val: 1.0
181
+ fake_label_val: 0.0
182
+ loss_weight: !!float 1
183
+ comp_style_weight: 200
184
+ # identity loss
185
+ identity_weight: 10
186
+
187
+ net_d_iters: 1
188
+ net_d_init_iters: 0
189
+ net_d_reg_every: 16
190
+
191
+ # validation settings
192
+ val:
193
+ val_freq: !!float 5e3
194
+ save_img: true
195
+
196
+ metrics:
197
+ psnr: # metric name, can be arbitrary
198
+ type: calculate_psnr
199
+ crop_border: 0
200
+ test_y_channel: false
201
+
202
+ # logging settings
203
+ logger:
204
+ print_freq: 100
205
+ save_checkpoint_freq: !!float 5e3
206
+ use_tb_logger: true
207
+ wandb:
208
+ project: ~
209
+ resume_id: ~
210
+
211
+ # dist training settings
212
+ dist_params:
213
+ backend: nccl
214
+ port: 29500
215
+
216
+ find_unused_parameters: true
options/train_gfpgan_v1_simple.yml ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # general settings
2
+ name: train_GFPGANv1_512_simple
3
+ model_type: GFPGANModel
4
+ num_gpu: 4
5
+ manual_seed: 0
6
+
7
+ # dataset and data loader settings
8
+ datasets:
9
+ train:
10
+ name: FFHQ
11
+ type: FFHQDegradationDataset
12
+ # dataroot_gt: datasets/ffhq/ffhq_512.lmdb
13
+ dataroot_gt: datasets/ffhq/ffhq_512
14
+ io_backend:
15
+ # type: lmdb
16
+ type: disk
17
+
18
+ use_hflip: true
19
+ mean: [0.5, 0.5, 0.5]
20
+ std: [0.5, 0.5, 0.5]
21
+ out_size: 512
22
+
23
+ blur_kernel_size: 41
24
+ kernel_list: ['iso', 'aniso']
25
+ kernel_prob: [0.5, 0.5]
26
+ blur_sigma: [0.1, 10]
27
+ downsample_range: [0.8, 8]
28
+ noise_range: [0, 20]
29
+ jpeg_range: [60, 100]
30
+
31
+ # color jitter and gray
32
+ color_jitter_prob: 0.3
33
+ color_jitter_shift: 20
34
+ color_jitter_pt_prob: 0.3
35
+ gray_prob: 0.01
36
+
37
+ # If you do not want colorization, please set
38
+ # color_jitter_prob: ~
39
+ # color_jitter_pt_prob: ~
40
+ # gray_prob: 0.01
41
+ # gt_gray: True
42
+
43
+ # crop_components: false
44
+ # component_path: experiments/pretrained_models/FFHQ_eye_mouth_landmarks_512.pth
45
+ # eye_enlarge_ratio: 1.4
46
+
47
+ # data loader
48
+ use_shuffle: true
49
+ num_worker_per_gpu: 6
50
+ batch_size_per_gpu: 3
51
+ dataset_enlarge_ratio: 1
52
+ prefetch_mode: ~
53
+
54
+ val:
55
+ # Please modify accordingly to use your own validation
56
+ # Or comment the val block if do not need validation during training
57
+ name: validation
58
+ type: PairedImageDataset
59
+ dataroot_lq: datasets/faces/validation/input
60
+ dataroot_gt: datasets/faces/validation/reference
61
+ io_backend:
62
+ type: disk
63
+ mean: [0.5, 0.5, 0.5]
64
+ std: [0.5, 0.5, 0.5]
65
+ scale: 1
66
+
67
+ # network structures
68
+ network_g:
69
+ type: GFPGANv1
70
+ out_size: 512
71
+ num_style_feat: 512
72
+ channel_multiplier: 1
73
+ resample_kernel: [1, 3, 3, 1]
74
+ decoder_load_path: experiments/pretrained_models/StyleGAN2_512_Cmul1_FFHQ_B12G4_scratch_800k.pth
75
+ fix_decoder: true
76
+ num_mlp: 8
77
+ lr_mlp: 0.01
78
+ input_is_latent: true
79
+ different_w: true
80
+ narrow: 1
81
+ sft_half: true
82
+
83
+ network_d:
84
+ type: StyleGAN2Discriminator
85
+ out_size: 512
86
+ channel_multiplier: 1
87
+ resample_kernel: [1, 3, 3, 1]
88
+
89
+ # network_d_left_eye:
90
+ # type: FacialComponentDiscriminator
91
+
92
+ # network_d_right_eye:
93
+ # type: FacialComponentDiscriminator
94
+
95
+ # network_d_mouth:
96
+ # type: FacialComponentDiscriminator
97
+
98
+ network_identity:
99
+ type: ResNetArcFace
100
+ block: IRBlock
101
+ layers: [2, 2, 2, 2]
102
+ use_se: False
103
+
104
+ # path
105
+ path:
106
+ pretrain_network_g: ~
107
+ param_key_g: params_ema
108
+ strict_load_g: ~
109
+ pretrain_network_d: ~
110
+ # pretrain_network_d_left_eye: ~
111
+ # pretrain_network_d_right_eye: ~
112
+ # pretrain_network_d_mouth: ~
113
+ pretrain_network_identity: experiments/pretrained_models/arcface_resnet18.pth
114
+ # resume
115
+ resume_state: ~
116
+ ignore_resume_networks: ['network_identity']
117
+
118
+ # training settings
119
+ train:
120
+ optim_g:
121
+ type: Adam
122
+ lr: !!float 2e-3
123
+ optim_d:
124
+ type: Adam
125
+ lr: !!float 2e-3
126
+ optim_component:
127
+ type: Adam
128
+ lr: !!float 2e-3
129
+
130
+ scheduler:
131
+ type: MultiStepLR
132
+ milestones: [600000, 700000]
133
+ gamma: 0.5
134
+
135
+ total_iter: 800000
136
+ warmup_iter: -1 # no warm up
137
+
138
+ # losses
139
+ # pixel loss
140
+ pixel_opt:
141
+ type: L1Loss
142
+ loss_weight: !!float 1e-1
143
+ reduction: mean
144
+ # L1 loss used in pyramid loss, component style loss and identity loss
145
+ L1_opt:
146
+ type: L1Loss
147
+ loss_weight: 1
148
+ reduction: mean
149
+
150
+ # image pyramid loss
151
+ pyramid_loss_weight: 1
152
+ remove_pyramid_loss: 50000
153
+ # perceptual loss (content and style losses)
154
+ perceptual_opt:
155
+ type: PerceptualLoss
156
+ layer_weights:
157
+ # before relu
158
+ 'conv1_2': 0.1
159
+ 'conv2_2': 0.1
160
+ 'conv3_4': 1
161
+ 'conv4_4': 1
162
+ 'conv5_4': 1
163
+ vgg_type: vgg19
164
+ use_input_norm: true
165
+ perceptual_weight: !!float 1
166
+ style_weight: 50
167
+ range_norm: true
168
+ criterion: l1
169
+ # gan loss
170
+ gan_opt:
171
+ type: GANLoss
172
+ gan_type: wgan_softplus
173
+ loss_weight: !!float 1e-1
174
+ # r1 regularization for discriminator
175
+ r1_reg_weight: 10
176
+ # facial component loss
177
+ # gan_component_opt:
178
+ # type: GANLoss
179
+ # gan_type: vanilla
180
+ # real_label_val: 1.0
181
+ # fake_label_val: 0.0
182
+ # loss_weight: !!float 1
183
+ # comp_style_weight: 200
184
+ # identity loss
185
+ identity_weight: 10
186
+
187
+ net_d_iters: 1
188
+ net_d_init_iters: 0
189
+ net_d_reg_every: 16
190
+
191
+ # validation settings
192
+ val:
193
+ val_freq: !!float 5e3
194
+ save_img: true
195
+
196
+ metrics:
197
+ psnr: # metric name, can be arbitrary
198
+ type: calculate_psnr
199
+ crop_border: 0
200
+ test_y_channel: false
201
+
202
+ # logging settings
203
+ logger:
204
+ print_freq: 100
205
+ save_checkpoint_freq: !!float 5e3
206
+ use_tb_logger: true
207
+ wandb:
208
+ project: ~
209
+ resume_id: ~
210
+
211
+ # dist training settings
212
+ dist_params:
213
+ backend: nccl
214
+ port: 29500
215
+
216
+ find_unused_parameters: true
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ basicsr>=1.3.3.10
2
+ facexlib>=0.2.0.2
3
+ lmdb
4
+ numpy
5
+ opencv-python
6
+ pyyaml
7
+ tb-nightly
8
+ torch>=1.7
9
+ torchvision
10
+ tqdm
11
+ yapf
scripts/parse_landmark.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import json
3
+ import numpy as np
4
+ import torch
5
+ from basicsr.utils import FileClient, imfrombytes
6
+ from collections import OrderedDict
7
+
8
+ print('Load JSON metadata...')
9
+ # use the json file in FFHQ dataset
10
+ with open('ffhq-dataset-v2.json', 'rb') as f:
11
+ json_data = json.load(f, object_pairs_hook=OrderedDict)
12
+
13
+ print('Open LMDB file...')
14
+ # read ffhq images
15
+ file_client = FileClient('lmdb', db_paths='datasets/ffhq/ffhq_512.lmdb')
16
+ with open('datasets/ffhq/ffhq_512.lmdb/meta_info.txt') as fin:
17
+ paths = [line.split('.')[0] for line in fin]
18
+
19
+ save_img = False
20
+ scale = 0.5 # 0.5 for official FFHQ (512x512), 1 for others
21
+ enlarge_ratio = 1.4 # only for eyes
22
+ save_dict = {}
23
+
24
+ for item_idx, item in enumerate(json_data.values()):
25
+ print(f'\r{item_idx} / {len(json_data)}, {item["image"]["file_path"]} ', end='', flush=True)
26
+
27
+ # parse landmarks
28
+ lm = np.array(item['image']['face_landmarks'])
29
+ lm = lm * scale
30
+
31
+ item_dict = {}
32
+ # get image
33
+ if save_img:
34
+ img_bytes = file_client.get(paths[item_idx])
35
+ img = imfrombytes(img_bytes, float32=True)
36
+
37
+ map_left_eye = list(range(36, 42))
38
+ map_right_eye = list(range(42, 48))
39
+ map_mouth = list(range(48, 68))
40
+
41
+ # eye_left
42
+ mean_left_eye = np.mean(lm[map_left_eye], 0) # (x, y)
43
+ half_len_left_eye = np.max((np.max(np.max(lm[map_left_eye], 0) - np.min(lm[map_left_eye], 0)) / 2, 16))
44
+ item_dict['left_eye'] = [mean_left_eye[0], mean_left_eye[1], half_len_left_eye]
45
+ # mean_left_eye[0] = 512 - mean_left_eye[0] # for testing flip
46
+ half_len_left_eye *= enlarge_ratio
47
+ loc_left_eye = np.hstack((mean_left_eye - half_len_left_eye + 1, mean_left_eye + half_len_left_eye)).astype(int)
48
+ if save_img:
49
+ eye_left_img = img[loc_left_eye[1]:loc_left_eye[3], loc_left_eye[0]:loc_left_eye[2], :]
50
+ cv2.imwrite(f'tmp/{item_idx:08d}_eye_left.png', eye_left_img * 255)
51
+
52
+ # eye_right
53
+ mean_right_eye = np.mean(lm[map_right_eye], 0)
54
+ half_len_right_eye = np.max((np.max(np.max(lm[map_right_eye], 0) - np.min(lm[map_right_eye], 0)) / 2, 16))
55
+ item_dict['right_eye'] = [mean_right_eye[0], mean_right_eye[1], half_len_right_eye]
56
+ # mean_right_eye[0] = 512 - mean_right_eye[0] # # for testing flip
57
+ half_len_right_eye *= enlarge_ratio
58
+ loc_right_eye = np.hstack(
59
+ (mean_right_eye - half_len_right_eye + 1, mean_right_eye + half_len_right_eye)).astype(int)
60
+ if save_img:
61
+ eye_right_img = img[loc_right_eye[1]:loc_right_eye[3], loc_right_eye[0]:loc_right_eye[2], :]
62
+ cv2.imwrite(f'tmp/{item_idx:08d}_eye_right.png', eye_right_img * 255)
63
+
64
+ # mouth
65
+ mean_mouth = np.mean(lm[map_mouth], 0)
66
+ half_len_mouth = np.max((np.max(np.max(lm[map_mouth], 0) - np.min(lm[map_mouth], 0)) / 2, 16))
67
+ item_dict['mouth'] = [mean_mouth[0], mean_mouth[1], half_len_mouth]
68
+ # mean_mouth[0] = 512 - mean_mouth[0] # for testing flip
69
+ loc_mouth = np.hstack((mean_mouth - half_len_mouth + 1, mean_mouth + half_len_mouth)).astype(int)
70
+ if save_img:
71
+ mouth_img = img[loc_mouth[1]:loc_mouth[3], loc_mouth[0]:loc_mouth[2], :]
72
+ cv2.imwrite(f'tmp/{item_idx:08d}_mouth.png', mouth_img * 255)
73
+
74
+ save_dict[f'{item_idx:08d}'] = item_dict
75
+
76
+ print('Save...')
77
+ torch.save(save_dict, './FFHQ_eye_mouth_landmarks_512.pth')
setup.cfg ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [flake8]
2
+ ignore =
3
+ # line break before binary operator (W503)
4
+ W503,
5
+ # line break after binary operator (W504)
6
+ W504,
7
+ max-line-length=120
8
+
9
+ [yapf]
10
+ based_on_style = pep8
11
+ column_limit = 120
12
+ blank_line_before_nested_class_or_def = true
13
+ split_before_expression_after_opening_paren = true
14
+
15
+ [isort]
16
+ line_length = 120
17
+ multi_line_output = 0
18
+ known_standard_library = pkg_resources,setuptools
19
+ known_first_party = gfpgan
20
+ known_third_party = basicsr,cv2,facexlib,numpy,torch,torchvision,tqdm
21
+ no_lines_before = STDLIB,LOCALFOLDER
22
+ default_section = THIRDPARTY
setup.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ from setuptools import find_packages, setup
4
+
5
+ import os
6
+ import subprocess
7
+ import time
8
+
9
+ version_file = 'gfpgan/version.py'
10
+
11
+
12
+ def readme():
13
+ with open('README.md', encoding='utf-8') as f:
14
+ content = f.read()
15
+ return content
16
+
17
+
18
+ def get_git_hash():
19
+
20
+ def _minimal_ext_cmd(cmd):
21
+ # construct minimal environment
22
+ env = {}
23
+ for k in ['SYSTEMROOT', 'PATH', 'HOME']:
24
+ v = os.environ.get(k)
25
+ if v is not None:
26
+ env[k] = v
27
+ # LANGUAGE is used on win32
28
+ env['LANGUAGE'] = 'C'
29
+ env['LANG'] = 'C'
30
+ env['LC_ALL'] = 'C'
31
+ out = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=env).communicate()[0]
32
+ return out
33
+
34
+ try:
35
+ out = _minimal_ext_cmd(['git', 'rev-parse', 'HEAD'])
36
+ sha = out.strip().decode('ascii')
37
+ except OSError:
38
+ sha = 'unknown'
39
+
40
+ return sha
41
+
42
+
43
+ def get_hash():
44
+ if os.path.exists('.git'):
45
+ sha = get_git_hash()[:7]
46
+ elif os.path.exists(version_file):
47
+ try:
48
+ from facexlib.version import __version__
49
+ sha = __version__.split('+')[-1]
50
+ except ImportError:
51
+ raise ImportError('Unable to get git version')
52
+ else:
53
+ sha = 'unknown'
54
+
55
+ return sha
56
+
57
+
58
+ def write_version_py():
59
+ content = """# GENERATED VERSION FILE
60
+ # TIME: {}
61
+ __version__ = '{}'
62
+ __gitsha__ = '{}'
63
+ version_info = ({})
64
+ """
65
+ sha = get_hash()
66
+ with open('VERSION', 'r') as f:
67
+ SHORT_VERSION = f.read().strip()
68
+ VERSION_INFO = ', '.join([x if x.isdigit() else f'"{x}"' for x in SHORT_VERSION.split('.')])
69
+
70
+ version_file_str = content.format(time.asctime(), SHORT_VERSION, sha, VERSION_INFO)
71
+ with open(version_file, 'w') as f:
72
+ f.write(version_file_str)
73
+
74
+
75
+ def get_version():
76
+ with open(version_file, 'r') as f:
77
+ exec(compile(f.read(), version_file, 'exec'))
78
+ return locals()['__version__']
79
+
80
+
81
+ def get_requirements(filename='requirements.txt'):
82
+ here = os.path.dirname(os.path.realpath(__file__))
83
+ with open(os.path.join(here, filename), 'r') as f:
84
+ requires = [line.replace('\n', '') for line in f.readlines()]
85
+ return requires
86
+
87
+
88
+ if __name__ == '__main__':
89
+ write_version_py()
90
+ setup(
91
+ name='gfpgan',
92
+ version=get_version(),
93
+ description='GFPGAN aims at developing Practical Algorithms for Real-world Face Restoration',
94
+ long_description=readme(),
95
+ long_description_content_type='text/markdown',
96
+ author='Xintao Wang',
97
+ author_email='xintao.wang@outlook.com',
98
+ keywords='computer vision, pytorch, image restoration, super-resolution, face restoration, gan, gfpgan',
99
+ url='https://github.com/TencentARC/GFPGAN',
100
+ include_package_data=True,
101
+ packages=find_packages(exclude=('options', 'datasets', 'experiments', 'results', 'tb_logger', 'wandb')),
102
+ classifiers=[
103
+ 'Development Status :: 4 - Beta',
104
+ 'License :: OSI Approved :: Apache Software License',
105
+ 'Operating System :: OS Independent',
106
+ 'Programming Language :: Python :: 3',
107
+ 'Programming Language :: Python :: 3.7',
108
+ 'Programming Language :: Python :: 3.8',
109
+ ],
110
+ license='Apache License Version 2.0',
111
+ setup_requires=['cython', 'numpy'],
112
+ install_requires=get_requirements(),
113
+ zip_safe=False)