-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.py
executable file
·55 lines (40 loc) · 1.59 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import argparse
from model import VDSR
from utils.common import *
parser = argparse.ArgumentParser()
parser.add_argument('--scale', type=int, default=2, help='-')
parser.add_argument("--ckpt-path", type=str, default="", help='-')
parser.add_argument("--image-path", type=str, default="dataset/test1.png", help='-')
FLAGS, unparsed = parser.parse_known_args()
image_path = FLAGS.image_path
scale = FLAGS.scale
if scale not in [2, 3, 4]:
raise ValueError("must be 2, 3 or 4")
ckpt_path = FLAGS.ckpt_path
if (ckpt_path == "") or (ckpt_path == "default"):
ckpt_path = "checkpoint/VDSR.pt"
sigma = 0.3 if scale == 2 else 0.2
# -----------------------------------------------------------
# demo
# -----------------------------------------------------------
def main():
device = "cuda" if torch.cuda.is_available() else "cpu"
lr_image = read_image(image_path)
bicubic_image = upscale(lr_image, scale)
write_image("bicubic.png", bicubic_image)
lr_image = gaussian_blur(lr_image, sigma=sigma)
bicubic_image = upscale(lr_image, scale)
bicubic_image = rgb2ycbcr(bicubic_image)
bicubic_image = norm01(bicubic_image)
bicubic_image = torch.unsqueeze(bicubic_image, dim=0)
model = VDSR(device)
model.load_weights(ckpt_path)
with torch.no_grad():
bicubic_image = bicubic_image.to(device)
sr_image = model.predict(bicubic_image)[0]
sr_image = denorm01(sr_image)
sr_image = sr_image.type(torch.uint8)
sr_image = ycbcr2rgb(sr_image)
write_image("sr.png", sr_image)
if __name__ == "__main__":
main()