Spaces:
Paused
Paused
Fix inference + scaling, update Gradio
Browse files- LICENSE +236 -0
- ModelLoader.py +27 -19
- README.md +15 -1
- app.py +18 -5
- examples/test.jpg +0 -0
- test.py +4 -2
- util/get_transform.py +16 -0
LICENSE
ADDED
@@ -0,0 +1,236 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2021 Junlin Han
|
4 |
+
Copyright (c) 2024 Yarflam on Hugging Face (same license)
|
5 |
+
|
6 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 |
+
of this software and associated documentation files (the "Software"), to deal
|
8 |
+
in the Software without restriction, including without limitation the rights
|
9 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 |
+
copies of the Software, and to permit persons to whom the Software is
|
11 |
+
furnished to do so, subject to the following conditions:
|
12 |
+
|
13 |
+
The above copyright notice and this permission notice shall be included in all
|
14 |
+
copies or substantial portions of the Software.
|
15 |
+
|
16 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22 |
+
SOFTWARE.
|
23 |
+
|
24 |
+
--------------------------- LICENSE FOR CUT -------------------------------
|
25 |
+
Copyright (c) 2020, Taesung Park and Jun-Yan Zhu
|
26 |
+
All rights reserved.
|
27 |
+
|
28 |
+
Redistribution and use in source and binary forms, with or without
|
29 |
+
modification, are permitted provided that the following conditions are met:
|
30 |
+
|
31 |
+
* Redistributions of source code must retain the above copyright notice, this
|
32 |
+
list of conditions and the following disclaimer.
|
33 |
+
|
34 |
+
* Redistributions in binary form must reproduce the above copyright notice,
|
35 |
+
this list of conditions and the following disclaimer in the documentation
|
36 |
+
and/or other materials provided with the distribution.
|
37 |
+
|
38 |
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
39 |
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
40 |
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
41 |
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
42 |
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
43 |
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
44 |
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
45 |
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
46 |
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
47 |
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
48 |
+
|
49 |
+
--------------------------- LICENSE FOR CycleGAN -------------------------------
|
50 |
+
-------------------https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix------
|
51 |
+
Copyright (c) 2017, Jun-Yan Zhu and Taesung Park
|
52 |
+
All rights reserved.
|
53 |
+
|
54 |
+
Redistribution and use in source and binary forms, with or without
|
55 |
+
modification, are permitted provided that the following conditions are met:
|
56 |
+
|
57 |
+
* Redistributions of source code must retain the above copyright notice, this
|
58 |
+
list of conditions and the following disclaimer.
|
59 |
+
|
60 |
+
* Redistributions in binary form must reproduce the above copyright notice,
|
61 |
+
this list of conditions and the following disclaimer in the documentation
|
62 |
+
and/or other materials provided with the distribution.
|
63 |
+
|
64 |
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
65 |
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
66 |
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
67 |
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
68 |
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
69 |
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
70 |
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
71 |
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
72 |
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
73 |
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
74 |
+
|
75 |
+
--------------------------- LICENSE FOR stylegan2-pytorch ----------------------
|
76 |
+
----------------https://github.com/rosinality/stylegan2-pytorch/----------------
|
77 |
+
MIT License
|
78 |
+
|
79 |
+
Copyright (c) 2019 Kim Seonghyeon
|
80 |
+
|
81 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
82 |
+
of this software and associated documentation files (the "Software"), to deal
|
83 |
+
in the Software without restriction, including without limitation the rights
|
84 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
85 |
+
copies of the Software, and to permit persons to whom the Software is
|
86 |
+
furnished to do so, subject to the following conditions:
|
87 |
+
|
88 |
+
The above copyright notice and this permission notice shall be included in all
|
89 |
+
copies or substantial portions of the Software.
|
90 |
+
|
91 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
92 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
93 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
94 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
95 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
96 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
97 |
+
SOFTWARE.
|
98 |
+
|
99 |
+
|
100 |
+
--------------------------- LICENSE FOR pix2pix --------------------------------
|
101 |
+
BSD License
|
102 |
+
|
103 |
+
For pix2pix software
|
104 |
+
Copyright (c) 2016, Phillip Isola and Jun-Yan Zhu
|
105 |
+
All rights reserved.
|
106 |
+
|
107 |
+
Redistribution and use in source and binary forms, with or without
|
108 |
+
modification, are permitted provided that the following conditions are met:
|
109 |
+
|
110 |
+
* Redistributions of source code must retain the above copyright notice, this
|
111 |
+
list of conditions and the following disclaimer.
|
112 |
+
|
113 |
+
* Redistributions in binary form must reproduce the above copyright notice,
|
114 |
+
this list of conditions and the following disclaimer in the documentation
|
115 |
+
and/or other materials provided with the distribution.
|
116 |
+
|
117 |
+
----------------------------- LICENSE FOR DCGAN --------------------------------
|
118 |
+
BSD License
|
119 |
+
|
120 |
+
For dcgan.torch software
|
121 |
+
|
122 |
+
Copyright (c) 2015, Facebook, Inc. All rights reserved.
|
123 |
+
|
124 |
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
125 |
+
|
126 |
+
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
127 |
+
|
128 |
+
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.
|
129 |
+
|
130 |
+
Neither the name Facebook nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
131 |
+
|
132 |
+
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.
|
133 |
+
|
134 |
+
--------------------------- LICENSE FOR StyleGAN2 ------------------------------
|
135 |
+
--------------------------- Inherited from stylegan2-pytorch -------------------
|
136 |
+
Copyright (c) 2019, NVIDIA Corporation. All rights reserved.
|
137 |
+
|
138 |
+
|
139 |
+
Nvidia Source Code License-NC
|
140 |
+
|
141 |
+
=======================================================================
|
142 |
+
|
143 |
+
1. Definitions
|
144 |
+
|
145 |
+
"Licensor" means any person or entity that distributes its Work.
|
146 |
+
|
147 |
+
"Software" means the original work of authorship made available under
|
148 |
+
this License.
|
149 |
+
|
150 |
+
"Work" means the Software and any additions to or derivative works of
|
151 |
+
the Software that are made available under this License.
|
152 |
+
|
153 |
+
"Nvidia Processors" means any central processing unit (CPU), graphics
|
154 |
+
processing unit (GPU), field-programmable gate array (FPGA),
|
155 |
+
application-specific integrated circuit (ASIC) or any combination
|
156 |
+
thereof designed, made, sold, or provided by Nvidia or its affiliates.
|
157 |
+
|
158 |
+
The terms "reproduce," "reproduction," "derivative works," and
|
159 |
+
"distribution" have the meaning as provided under U.S. copyright law;
|
160 |
+
provided, however, that for the purposes of this License, derivative
|
161 |
+
works shall not include works that remain separable from, or merely
|
162 |
+
link (or bind by name) to the interfaces of, the Work.
|
163 |
+
|
164 |
+
Works, including the Software, are "made available" under this License
|
165 |
+
by including in or with the Work either (a) a copyright notice
|
166 |
+
referencing the applicability of this License to the Work, or (b) a
|
167 |
+
copy of this License.
|
168 |
+
|
169 |
+
2. License Grants
|
170 |
+
|
171 |
+
2.1 Copyright Grant. Subject to the terms and conditions of this
|
172 |
+
License, each Licensor grants to you a perpetual, worldwide,
|
173 |
+
non-exclusive, royalty-free, copyright license to reproduce,
|
174 |
+
prepare derivative works of, publicly display, publicly perform,
|
175 |
+
sublicense and distribute its Work and any resulting derivative
|
176 |
+
works in any form.
|
177 |
+
|
178 |
+
3. Limitations
|
179 |
+
|
180 |
+
3.1 Redistribution. You may reproduce or distribute the Work only
|
181 |
+
if (a) you do so under this License, (b) you include a complete
|
182 |
+
copy of this License with your distribution, and (c) you retain
|
183 |
+
without modification any copyright, patent, trademark, or
|
184 |
+
attribution notices that are present in the Work.
|
185 |
+
|
186 |
+
3.2 Derivative Works. You may specify that additional or different
|
187 |
+
terms apply to the use, reproduction, and distribution of your
|
188 |
+
derivative works of the Work ("Your Terms") only if (a) Your Terms
|
189 |
+
provide that the use limitation in Section 3.3 applies to your
|
190 |
+
derivative works, and (b) you identify the specific derivative
|
191 |
+
works that are subject to Your Terms. Notwithstanding Your Terms,
|
192 |
+
this License (including the redistribution requirements in Section
|
193 |
+
3.1) will continue to apply to the Work itself.
|
194 |
+
|
195 |
+
3.3 Use Limitation. The Work and any derivative works thereof only
|
196 |
+
may be used or intended for use non-commercially. The Work or
|
197 |
+
derivative works thereof may be used or intended for use by Nvidia
|
198 |
+
or its affiliates commercially or non-commercially. As used herein,
|
199 |
+
"non-commercially" means for research or evaluation purposes only.
|
200 |
+
|
201 |
+
3.4 Patent Claims. If you bring or threaten to bring a patent claim
|
202 |
+
against any Licensor (including any claim, cross-claim or
|
203 |
+
counterclaim in a lawsuit) to enforce any patents that you allege
|
204 |
+
are infringed by any Work, then your rights under this License from
|
205 |
+
such Licensor (including the grants in Sections 2.1 and 2.2) will
|
206 |
+
terminate immediately.
|
207 |
+
|
208 |
+
3.5 Trademarks. This License does not grant any rights to use any
|
209 |
+
Licensor's or its affiliates' names, logos, or trademarks, except
|
210 |
+
as necessary to reproduce the notices described in this License.
|
211 |
+
|
212 |
+
3.6 Termination. If you violate any term of this License, then your
|
213 |
+
rights under this License (including the grants in Sections 2.1 and
|
214 |
+
2.2) will terminate immediately.
|
215 |
+
|
216 |
+
4. Disclaimer of Warranty.
|
217 |
+
|
218 |
+
THE WORK IS PROVIDED "AS IS" WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
219 |
+
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WARRANTIES OR CONDITIONS OF
|
220 |
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE OR
|
221 |
+
NON-INFRINGEMENT. YOU BEAR THE RISK OF UNDERTAKING ANY ACTIVITIES UNDER
|
222 |
+
THIS LICENSE.
|
223 |
+
|
224 |
+
5. Limitation of Liability.
|
225 |
+
|
226 |
+
EXCEPT AS PROHIBITED BY APPLICABLE LAW, IN NO EVENT AND UNDER NO LEGAL
|
227 |
+
THEORY, WHETHER IN TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE
|
228 |
+
SHALL ANY LICENSOR BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY DIRECT,
|
229 |
+
INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
|
230 |
+
OR RELATED TO THIS LICENSE, THE USE OR INABILITY TO USE THE WORK
|
231 |
+
(INCLUDING BUT NOT LIMITED TO LOSS OF GOODWILL, BUSINESS INTERRUPTION,
|
232 |
+
LOST PROFITS OR DATA, COMPUTER FAILURE OR MALFUNCTION, OR ANY OTHER
|
233 |
+
COMMERCIAL DAMAGES OR LOSSES), EVEN IF THE LICENSOR HAS BEEN ADVISED OF
|
234 |
+
THE POSSIBILITY OF SUCH DAMAGES.
|
235 |
+
|
236 |
+
=======================================================================
|
ModelLoader.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
from models import create_model
|
2 |
from util.get_transform import get_transform
|
|
|
3 |
from PIL import Image
|
4 |
import os
|
5 |
|
@@ -14,41 +15,43 @@ class Options(object):
|
|
14 |
setattr(self, key, kwargs[key])
|
15 |
|
16 |
class ModelLoader:
|
17 |
-
def __init__(self) -> None:
|
18 |
self.opt = Options({
|
19 |
'isGradio': True, # Custom
|
20 |
-
'name': 'original',
|
21 |
-
'checkpoints_dir': ckp_path,
|
22 |
-
'gpu_ids': [],
|
23 |
-
'init_gain': 0.02,
|
24 |
-
'init_type': 'xavier',
|
25 |
-
'input_nc': 3,
|
26 |
'output_nc': 3,
|
27 |
'isTrain': False,
|
28 |
'model': 'cwr',
|
29 |
'nce_idt': False,
|
30 |
'nce_layers': '0',
|
31 |
-
'ndf': 64,
|
32 |
'netD': 'basic',
|
33 |
'netG': 'resnet_9blocks',
|
34 |
'netF': 'mlp_sample',
|
35 |
'netF_nc': 256,
|
36 |
-
'ngf': 64,
|
37 |
-
'no_antialias_up':
|
38 |
-
'no_antialias':
|
39 |
'no_dropout': True,
|
40 |
'normD': 'instance',
|
41 |
'normG': 'instance',
|
42 |
-
'preprocess': '
|
43 |
-
'
|
|
|
44 |
'batch_size': 1, # test code only supports batch_size = 1
|
45 |
-
'serial_batches':
|
46 |
'no_flip': True, # no flip; comment this line if results on flipped images are needed.
|
47 |
'display_id': -1, # no visdom display; the test code saves the results to a HTML file.
|
48 |
'direction': 'AtoB', # inference
|
49 |
'flip_equivariance': False,
|
50 |
-
'load_size': 1680,
|
51 |
-
'crop_size': 512,
|
|
|
52 |
})
|
53 |
self.transform = get_transform(self.opt, grayscale=False)
|
54 |
self.model = None
|
@@ -62,9 +65,14 @@ class ModelLoader:
|
|
62 |
# Loading
|
63 |
print('Loading the image %s' % src)
|
64 |
source = Image.open(src).convert('RGB')
|
65 |
-
img = self.transform(source)
|
66 |
print(img.shape)
|
67 |
# Inference
|
68 |
-
self.model.set_input({
|
|
|
|
|
|
|
69 |
self.model.forward()
|
70 |
-
|
|
|
|
|
|
1 |
from models import create_model
|
2 |
from util.get_transform import get_transform
|
3 |
+
from util.util import tensor2im
|
4 |
from PIL import Image
|
5 |
import os
|
6 |
|
|
|
15 |
setattr(self, key, kwargs[key])
|
16 |
|
17 |
class ModelLoader:
|
18 |
+
def __init__(self, gpu_ids='', max_img_wh=512) -> None:
|
19 |
self.opt = Options({
|
20 |
'isGradio': True, # Custom
|
21 |
+
'name': 'original', # Checkpoints name
|
22 |
+
'checkpoints_dir': ckp_path, # Checkpoint folder
|
23 |
+
'gpu_ids': gpu_ids.split(',') if gpu_ids else [],
|
24 |
+
'init_gain': 0.02, # Scaling Factor
|
25 |
+
'init_type': 'xavier', # list: 'normal', 'xavier', 'kaiming', 'orthogonal'
|
26 |
+
'input_nc': 3, # 3 -> RGB, 1 -> Grayscale
|
27 |
'output_nc': 3,
|
28 |
'isTrain': False,
|
29 |
'model': 'cwr',
|
30 |
'nce_idt': False,
|
31 |
'nce_layers': '0',
|
32 |
+
'ndf': 64, # Nb of discrim filters in the first conv layer
|
33 |
'netD': 'basic',
|
34 |
'netG': 'resnet_9blocks',
|
35 |
'netF': 'mlp_sample',
|
36 |
'netF_nc': 256,
|
37 |
+
'ngf': 64, # Nb of gen filters in the last conv layer
|
38 |
+
'no_antialias_up': False,
|
39 |
+
'no_antialias': False,
|
40 |
'no_dropout': True,
|
41 |
'normD': 'instance',
|
42 |
'normG': 'instance',
|
43 |
+
'preprocess': 'yarflam_auto', # see more: util.get_transform
|
44 |
+
'dataroot': 'placeholder',
|
45 |
+
'num_threads': 1, # test code only supports num_threads = 1
|
46 |
'batch_size': 1, # test code only supports batch_size = 1
|
47 |
+
'serial_batches': False, # disable data shuffling; comment this line if results on randomly chosen images are needed.
|
48 |
'no_flip': True, # no flip; comment this line if results on flipped images are needed.
|
49 |
'display_id': -1, # no visdom display; the test code saves the results to a HTML file.
|
50 |
'direction': 'AtoB', # inference
|
51 |
'flip_equivariance': False,
|
52 |
+
'load_size': 1680, # not used
|
53 |
+
'crop_size': 512, # not used
|
54 |
+
'yarflam_img_wh': max_img_wh, # max width|height + auto scale down
|
55 |
})
|
56 |
self.transform = get_transform(self.opt, grayscale=False)
|
57 |
self.model = None
|
|
|
65 |
# Loading
|
66 |
print('Loading the image %s' % src)
|
67 |
source = Image.open(src).convert('RGB')
|
68 |
+
img = self.transform(source).unsqueeze(0)
|
69 |
print(img.shape)
|
70 |
# Inference
|
71 |
+
self.model.set_input({
|
72 |
+
'A': img, 'A_paths': src,
|
73 |
+
'B': img, 'B_paths': src
|
74 |
+
})
|
75 |
self.model.forward()
|
76 |
+
out_data = list(self.model.get_current_visuals().items())[1][1]
|
77 |
+
out_img = Image.fromarray(tensor2im(out_data))
|
78 |
+
return out_img
|
README.md
CHANGED
@@ -10,4 +10,18 @@ pinned: false
|
|
10 |
short_description: Contrastive UnderWater Restoration
|
11 |
---
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
short_description: Contrastive UnderWater Restoration
|
11 |
---
|
12 |
|
13 |
+
# UnderWater
|
14 |
+
|
15 |
+
Improve underwater photos.
|
16 |
+
|
17 |
+
Contrastive UnderWater Restoration (CWR) - Gradio integration.
|
18 |
+
|
19 |
+
Source: https://github.com/JunlinHan/CWR
|
20 |
+
|
21 |
+
## Author
|
22 |
+
|
23 |
+
- Yarflam - Gradio integration
|
24 |
+
|
25 |
+
## License
|
26 |
+
|
27 |
+
MIT License - [see more](LICENSE)
|
app.py
CHANGED
@@ -1,11 +1,24 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
demo = gr.Interface(
|
7 |
-
fn=
|
8 |
-
inputs=
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
10 |
)
|
11 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from ModelLoader import ModelLoader
|
3 |
+
import os
|
4 |
|
5 |
+
max_img_wh = 1024 # Set the maximum image size in pixels
|
6 |
+
|
7 |
+
def inference(image, use_gpu):
|
8 |
+
gpu_ids = '0' if use_gpu else ''
|
9 |
+
model = ModelLoader(gpu_ids=gpu_ids, max_img_wh=max_img_wh)
|
10 |
+
model.load()
|
11 |
+
output_img = model.inference(src=image)
|
12 |
+
return output_img
|
13 |
|
14 |
demo = gr.Interface(
|
15 |
+
fn=inference,
|
16 |
+
inputs=[
|
17 |
+
gr.inputs.Image(type="pil", label="Input Image"),
|
18 |
+
gr.inputs.Checkbox(label="Use GPU", value=True) # Precheck the GPU checkbox
|
19 |
+
],
|
20 |
+
outputs=gr.outputs.Image(type="pil", label="Output Image"),
|
21 |
+
title="Model Demo",
|
22 |
+
description="Upload an image to see the model output."
|
23 |
)
|
24 |
demo.launch()
|
examples/test.jpg
ADDED
test.py
CHANGED
@@ -2,10 +2,12 @@ from ModelLoader import ModelLoader
|
|
2 |
import os
|
3 |
|
4 |
def main():
|
5 |
-
model = ModelLoader()
|
6 |
model.load()
|
7 |
# Test
|
8 |
sample = os.path.join(os.path.dirname(__file__), 'examples', 'rawimg.png')
|
9 |
-
|
|
|
|
|
10 |
|
11 |
main()
|
|
|
2 |
import os
|
3 |
|
4 |
def main():
|
5 |
+
model = ModelLoader(gpu_ids='', max_img_wh=512)
|
6 |
model.load()
|
7 |
# Test
|
8 |
sample = os.path.join(os.path.dirname(__file__), 'examples', 'rawimg.png')
|
9 |
+
# sample = os.path.join(os.path.dirname(__file__), 'examples', 'test.jpg')
|
10 |
+
img = model.inference(src=sample)
|
11 |
+
img.show() # PIL Image
|
12 |
|
13 |
main()
|
util/get_transform.py
CHANGED
@@ -18,6 +18,9 @@ def get_transform(opt, params=None, grayscale=False, method=Image.BICUBIC, conve
|
|
18 |
elif 'scale_shortside' in opt.preprocess:
|
19 |
transform_list.append(transforms.Lambda(lambda img: __scale_shortside(img, opt.load_size, opt.crop_size, method)))
|
20 |
|
|
|
|
|
|
|
21 |
if 'zoom' in opt.preprocess:
|
22 |
if params is None:
|
23 |
transform_list.append(transforms.Lambda(lambda img: __random_zoom(img, opt.load_size, opt.crop_size, method)))
|
@@ -110,6 +113,19 @@ def __scale_width(img, target_width, crop_width, method=Image.BICUBIC):
|
|
110 |
h = int(max(target_width * oh / ow, crop_width))
|
111 |
return img.resize((w, h), method)
|
112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
def __crop(img, pos, size):
|
115 |
ow, oh = img.size
|
|
|
18 |
elif 'scale_shortside' in opt.preprocess:
|
19 |
transform_list.append(transforms.Lambda(lambda img: __scale_shortside(img, opt.load_size, opt.crop_size, method)))
|
20 |
|
21 |
+
if opt.preprocess == 'yarflam_auto':
|
22 |
+
transform_list.append(transforms.Lambda(lambda img: __scale_yarflam(img, opt.yarflam_img_wh, method)))
|
23 |
+
|
24 |
if 'zoom' in opt.preprocess:
|
25 |
if params is None:
|
26 |
transform_list.append(transforms.Lambda(lambda img: __random_zoom(img, opt.load_size, opt.crop_size, method)))
|
|
|
113 |
h = int(max(target_width * oh / ow, crop_width))
|
114 |
return img.resize((w, h), method)
|
115 |
|
116 |
+
def __scale_yarflam(img, target_wh, method=Image.BICUBIC):
|
117 |
+
ow, oh = img.size
|
118 |
+
if max(ow, oh) <= target_wh:
|
119 |
+
return img
|
120 |
+
if ow > target_wh and oh > target_wh:
|
121 |
+
ratio = target_wh / max(ow, oh)
|
122 |
+
w, h = int(ow * ratio), int(oh * ratio)
|
123 |
+
elif ow > target_wh:
|
124 |
+
w, h = target_wh, int((oh / ow) * target_wh)
|
125 |
+
else:
|
126 |
+
w, h = int((ow / oh) * target_wh), target_wh
|
127 |
+
return img.resize((w, h), method)
|
128 |
+
|
129 |
|
130 |
def __crop(img, pos, size):
|
131 |
ow, oh = img.size
|