Skip to content

Commit 00d8085

Browse files
authored
Add SwinIR for x2 SR (#17)
1 parent 7f9785e commit 00d8085

2 files changed

Lines changed: 66 additions & 1 deletion

File tree

deepinv_bench/benchmarks/div2k_super_resolution_2x/objective.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Objective(BaseObjective):
1313
"div2k_super_resolution_2x"
1414
)
1515

16-
requirements = ["deepinv", "datasets"]
16+
requirements = ["deepinv", "datasets", "timm"]
1717

1818
# Minimal version of benchopt required to run this benchmark.
1919
# Bump it up if the benchmark depends on a new feature of benchopt.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from benchopt import BaseSolver
2+
3+
import torch
4+
import deepinv as dinv
5+
6+
WEIGHTS_BASE_URL = "https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/"
7+
8+
# Architecture and pretrained weights of official SwinIR x2 variants.
9+
VARIANTS = {
10+
"lightweight": dict(
11+
kwargs=dict(
12+
embed_dim=60,
13+
depths=(6, 6, 6, 6),
14+
num_heads=(6, 6, 6, 6),
15+
upsampler="pixelshuffledirect",
16+
),
17+
weights="002_lightweightSR_DIV2K_s64w8_SwinIR-S_x2.pth",
18+
),
19+
"medium": dict(
20+
kwargs=dict(
21+
embed_dim=180,
22+
depths=(6, 6, 6, 6, 6, 6),
23+
num_heads=(6, 6, 6, 6, 6, 6),
24+
upsampler="pixelshuffle",
25+
),
26+
weights="001_classicalSR_DF2K_s64w8_SwinIR-M_x2.pth",
27+
),
28+
}
29+
30+
31+
class Solver(BaseSolver):
32+
name = "SwinIR"
33+
34+
parameters = {
35+
"variant": ["lightweight", "medium"],
36+
}
37+
38+
def set_objective(self, train_dataset=None, physics=None):
39+
device = dinv.utils.get_freer_gpu() if torch.cuda.is_available() else "cpu"
40+
41+
variant = VARIANTS[self.variant]
42+
self.model = dinv.models.SwinIR(
43+
img_size=64,
44+
in_chans=3,
45+
window_size=8,
46+
mlp_ratio=2,
47+
upscale=2,
48+
img_range=1.0,
49+
resi_connection="1conv",
50+
pretrained=None,
51+
**variant["kwargs"],
52+
)
53+
pretrained_weights = dinv.models.utils.load_state_dict_from_url(
54+
WEIGHTS_BASE_URL + variant["weights"],
55+
map_location=lambda storage, loc: storage,
56+
)
57+
self.model.load_state_dict(pretrained_weights["params"])
58+
self.model = self.model.to(device)
59+
self.model.device = device
60+
61+
def run(self, _):
62+
pass
63+
64+
def get_result(self):
65+
return dict(model=self.model)

0 commit comments

Comments
 (0)