-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
132 lines (109 loc) · 4.07 KB
/
test.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import argparse
import os
import numpy as np
import torch
from litsr.data import DownsampledDataset
from litsr.utils import mkdir, read_yaml
from matplotlib import pyplot as plt
from pytorch_lightning import seed_everything
from tqdm import tqdm
from models import load_model
seed_everything(123)
def test_pipeline(args):
# setup scales and datasets
test_datasets = (
[_ for _ in args.datasets.split(",")]
if args.datasets
else ["sr-geo-15", "Google-15"]
)
# config ckpt path
exp_path = os.path.dirname(os.path.dirname(args.checkpoint))
ckpt_path = args.checkpoint
# read config
config = read_yaml(os.path.join(exp_path, "hparams.yaml"))
# create model
model = load_model(config, ckpt_path, strict=False)
model.eval()
# set gpu
if args.gpus:
model.cuda()
scales = args.scales.split(",") if args.scales else [2, 3, 4]
scales = [float(s) for s in scales]
for dataset_name in test_datasets:
for scale in scales:
# config result path
rslt_path = os.path.join(
exp_path,
"results",
dataset_name,
"x" + str(scale),
)
mkdir(rslt_path)
print(
"==== Dataset {}, Scale Factor x{:.2f} ====".format(dataset_name, scale)
)
dataset = DownsampledDataset(
datapath="load/benchmark/{0}/HR".format(dataset_name),
scale=scale,
is_train=False,
cache="bin",
rgb_range=config.data_module.args.rgb_range,
mean=config.data_module.args.get("mean"),
std=config.data_module.args.get("std"),
return_img_name=True,
)
dataloader = torch.utils.data.DataLoader(
dataset, batch_size=1, shuffle=False
)
psnrs, ssims, run_times = [], [], []
for batch_idx, batch in tqdm(enumerate(dataloader), total=len(dataset)):
if args.gpus:
lr, hr, name = batch
batch = (lr.cuda(), hr.cuda(), name)
with torch.no_grad():
rslt = model.test_step(
batch,
batch_idx,
patch_sz=48,
overlap=8,
thresholds=None,
self_ensemble=False,
)
file_path = os.path.join(rslt_path, rslt["name"])
if "log_img" in rslt.keys():
plt.imsave(file_path, rslt["log_img"])
if "log_img_sr" in rslt.keys():
plt.imsave(file_path, rslt["log_img_sr"])
if "log_img_smap" in rslt.keys():
plt.imsave(
file_path.replace(".png", "_smap.png"),
rslt["log_img_smap"][:, :, 0],
cmap="gray",
)
psnrs.append(rslt["val_psnr"])
ssims.append(rslt["val_ssim"])
run_times.append(rslt["time"])
mean_psnr = np.array(psnrs).mean()
mean_ssim = np.array(ssims).mean()
mean_runtime = np.array(run_times[1:]).mean()
print("- PSNR: {:.4f}".format(mean_psnr))
print("- SSIM: {:.4f}".format(mean_ssim))
print("- Runtime : {:.4f}".format(mean_runtime))
print("=" * 42)
def getTestParser():
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--checkpoint", type=str, help="checkpoint index")
parser.add_argument(
"-g",
"--gpus",
default="0",
type=str,
help="indices of GPUs to enable (default: all)",
)
parser.add_argument("--datasets", default="", type=str, help="dataset names")
parser.add_argument("--scales", default="", type=str, help="scale factors")
return parser
test_parser = getTestParser()
if __name__ == "__main__":
args = test_parser.parse_args()
test_pipeline(args)