-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo.py
64 lines (48 loc) · 2.01 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
55
56
57
58
59
60
61
62
63
64
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '3'
from utils.common import *
from model import SRCNN
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--scale', type=int, default=2, help='-')
parser.add_argument('--architecture', type=str, default="915", help='-')
parser.add_argument("--ckpt-path", type=str, default="", help='-')
parser.add_argument("--image-path", type=str, default="dataset/test1.png", help='-')
# -----------------------------------------------------------
# global variables
# -----------------------------------------------------------
FLAGS, unparsed = parser.parse_known_args()
image_path = FLAGS.image_path
architecture = FLAGS.architecture
if architecture not in ["915", "935", "955"]:
raise ValueError("architecture must be 915, 935 or 955")
scale = FLAGS.scale
if scale not in [2, 3, 4]:
raise ValueError("scale must be 2, 3, or 4")
ckpt_path = FLAGS.ckpt_path
if (ckpt_path == "") or (ckpt_path == "default"):
ckpt_path = f"checkpoint/SRCNN{architecture}/SRCNN-{architecture}.h5"
sigma = 0.3 if scale == 2 else 0.2
pad = int(architecture[1]) // 2 + 6
# -----------------------------------------------------------
# demo
# -----------------------------------------------------------
def main():
lr_image = read_image(image_path)
bicubic_image = upscale(lr_image, scale)
bicubic_image = bicubic_image[pad:-pad, pad:-pad]
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 = tf.expand_dims(bicubic_image, axis=0)
model = SRCNN(architecture)
model.load_weights(ckpt_path)
sr_image = model.predict(bicubic_image)[0]
sr_image = denorm01(sr_image)
sr_image = tf.cast(sr_image, tf.uint8)
sr_image = ycbcr2rgb(sr_image)
write_image("sr.png", sr_image)
if __name__ == "__main__":
main()