Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
S
stable-diffusion-webui
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
stable-diffusion-webui
Commits
948eff4b
Commit
948eff4b
authored
Sep 20, 2022
by
C43H66N12O12S2
Committed by
AUTOMATIC1111
Sep 20, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make swinir actually useful
parent
7267b7d2
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
5 deletions
+23
-5
swinir.py
modules/swinir.py
+23
-5
No files found.
swinir.py
→
modules/
swinir.py
View file @
948eff4b
...
...
@@ -12,7 +12,13 @@ import modules.images
from
modules.shared
import
cmd_opts
,
opts
,
device
from
modules.swinir_arch
import
SwinIR
as
net
precision_scope
=
torch
.
autocast
if
cmd_opts
.
precision
==
"autocast"
else
contextlib
.
nullcontext
def
load_model
(
task
=
"realsr"
,
large_model
=
True
,
model_path
=
next
(
os
.
listdir
(
cmd_opts
.
esrgan_models_path
))):
def
load_model
(
task
=
"realsr"
,
large_model
=
True
,
model_path
=
"C:/sd/ESRGANn/4x-large.pth"
,
scale
=
4
):
try
:
modules
.
shared
.
sd_upscalers
.
append
(
UpscalerSwin
(
"McSwinnySwin"
))
except
Exception
:
print
(
f
"Error loading ESRGAN model"
,
file
=
sys
.
stderr
)
print
(
traceback
.
format_exc
(),
file
=
sys
.
stderr
)
if
not
large_model
:
# use 'nearest+conv' to avoid block artifacts
model
=
net
(
upscale
=
scale
,
in_chans
=
3
,
img_size
=
64
,
window_size
=
8
,
...
...
@@ -26,12 +32,16 @@ def load_model(task = "realsr", large_model = True, model_path=next(os.listdir(c
mlp_ratio
=
2
,
upsampler
=
'nearest+conv'
,
resi_connection
=
'3conv'
)
pretrained_model
=
torch
.
load
(
model_path
)
model
.
load_state_dict
(
pretrained_model
,
strict
=
True
)
model
.
load_state_dict
(
pretrained_model
[
"params_ema"
]
,
strict
=
True
)
return
model
.
half
()
.
to
(
device
)
def
upscale
(
img
,
tile
=
opts
.
ESRGAN_tile
,
tile_overlap
=
opts
.
ESRGAN_tile_overlap
,
window_size
=
8
,
scale
=
4
):
img
=
cv2
.
imread
(
img
,
cv2
.
IMREAD_COLOR
)
.
astype
(
np
.
float16
)
/
255.
img
=
np
.
array
(
img
)
img
=
img
[:,
:,
::
-
1
]
img
=
np
.
moveaxis
(
img
,
2
,
0
)
/
255
img
=
torch
.
from_numpy
(
img
)
.
float
()
img
=
img
.
unsqueeze
(
0
)
.
to
(
device
)
model
=
load_model
()
with
torch
.
no_grad
(),
precision_scope
(
"cuda"
):
_
,
_
,
h_old
,
w_old
=
img
.
size
()
...
...
@@ -45,7 +55,7 @@ def upscale(img, tile=opts.ESRGAN_tile, tile_overlap=opts.ESRGAN_tile_overlap, w
if
output
.
ndim
==
3
:
output
=
np
.
transpose
(
output
[[
2
,
1
,
0
],
:,
:],
(
1
,
2
,
0
))
# CHW-RGB to HCW-BGR
output
=
(
output
*
255.0
)
.
round
()
.
astype
(
np
.
uint8
)
# float32 to uint8
return
output
return
Image
.
fromarray
(
output
,
'RGB'
)
def
inference
(
img
,
model
,
tile
,
tile_overlap
,
window_size
,
scale
):
...
...
@@ -72,3 +82,11 @@ def inference(img, model, tile, tile_overlap, window_size, scale):
output
=
E
.
div_
(
W
)
return
output
class
UpscalerSwin
(
modules
.
images
.
Upscaler
):
def
__init__
(
self
,
title
):
self
.
name
=
title
def
do_upscale
(
self
,
img
):
img
=
upscale
(
img
)
return
img
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment