-
Notifications
You must be signed in to change notification settings - Fork 13
/
test.py
192 lines (153 loc) · 6.69 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#############################################################
# File: test.py
# Created Date: Saturday July 3rd 2021
# Author: Chen Xuanhong
# Email: [email protected]
# Last Modified: Sunday, 23rd April 2023 2:56:31 pm
# Modified By: Chen Xuanhong
# Copyright (c) 2021 Shanghai Jiao Tong University
#############################################################
import os
import argparse
from torch.backends import cudnn
from utilities.json_config import readConfig
from utilities.reporter import Reporter
import warnings
warnings.filterwarnings('ignore')
def str2bool(v):
return v.lower() in ('true')
####################################################################################
# To configure the seting of training\finetune\test
#
####################################################################################
def getParameters():
parser = argparse.ArgumentParser()
# general settings
parser.add_argument('-v', '--version', type=str, default='OmniSR_X2_DF2K',
help="version name for train, test, finetune")
parser.add_argument('-c', '--cuda', type=int, default=0) # >0 if it is set as -1, program will use CPU
parser.add_argument('-s', '--checkpoint_epoch', type=int, default=885,
help="checkpoint epoch for test phase or finetune phase")
# test
parser.add_argument('-t', '--test_script_name', type=str, default='tester_Matlab')
parser.add_argument('-b', '--batch_size', type=int, default=1)
parser.add_argument('-n', '--node_ip', type=str, default='localhost')
parser.add_argument('--test_dataset_name', type=str, default='Urban100',
choices=['DIV2K', 'B100', 'Urban100', 'Set5', 'Set14', "Manga109"])
return parser.parse_args()
ignoreKey = [
"dataloader_workers",
"log_root_path",
"project_root",
"project_summary",
"project_checkpoints",
"project_samples",
"project_scripts",
"reporter_path",
"use_specified_data",
"specified_data_paths",
"dataset_path",
"cuda",
"test_script_name",
"test_dataloader",
"test_dataset_path",
"save_test_result",
"test_batch_size",
"node_name",
"checkpoint_epoch",
"test_dataset_path",
"test_dataset_name",
"patch_test"]
####################################################################################
# This function will create the related directories before the
# training\fintune\test starts
# Your_log_root (version name)
# |---summary/...
# |---samples/... (save evaluated images)
# |---checkpoints/...
# |---scripts/...
#
####################################################################################
def createDirs(sys_state):
# the base dir
if not os.path.exists(sys_state["log_root_path"]):
os.makedirs(sys_state["log_root_path"])
# create dirs
sys_state["project_root"] = os.path.join(sys_state["log_root_path"],
sys_state["version"])
project_root = sys_state["project_root"]
if not os.path.exists(project_root):
os.makedirs(project_root)
sys_state["project_summary"] = os.path.join(project_root, "summary")
if not os.path.exists(sys_state["project_summary"]):
os.makedirs(sys_state["project_summary"])
sys_state["project_checkpoints"] = os.path.join(project_root, "checkpoints")
if not os.path.exists(sys_state["project_checkpoints"]):
os.makedirs(sys_state["project_checkpoints"])
sys_state["project_samples"] = os.path.join(project_root, "samples")
if not os.path.exists(sys_state["project_samples"]):
os.makedirs(sys_state["project_samples"])
sys_state["project_scripts"] = os.path.join(project_root, "scripts")
if not os.path.exists(sys_state["project_scripts"]):
os.makedirs(sys_state["project_scripts"])
sys_state["reporter_path"] = os.path.join(project_root,sys_state["version"]+"_report")
def main():
config = getParameters()
# speed up the program
cudnn.benchmark = True
sys_state = {}
# set the GPU number
if config.cuda >= 0:
os.environ["CUDA_VISIBLE_DEVICES"] = str(config.cuda)
# read system environment paths
env_config = readConfig('env/env.json')
env_config = env_config["path"]
sys_state["env_config"] = env_config
# obtain all configurations in argparse
config_dic = vars(config)
for config_key in config_dic.keys():
sys_state[config_key] = config_dic[config_key]
#=======================Test Phase=========================#
# TODO modify below lines to obtain the configuration
sys_state["log_root_path"] = env_config["train_log_root"]
sys_state["test_samples_path"] = os.path.join(env_config["test_log_root"],
sys_state["version"] , "samples")
if not os.path.exists(sys_state["test_samples_path"]):
os.makedirs(sys_state["test_samples_path"])
# Create dirs
createDirs(sys_state)
config_json = os.path.join(sys_state["project_root"], env_config["config_json_name"])
# Read model_config.json
json_obj = readConfig(config_json)
for item in json_obj.items():
if item[0] in ignoreKey:
pass
else:
sys_state[item[0]] = item[1]
# Get checkpoints
data_key = config.test_dataset_name.lower()
sys_state["test_dataset_path"] = env_config["test_dataset_paths"][data_key]
sys_state["test_dataset_names"] = config.test_dataset_name
# TODO get the checkpoint file path
sys_state["ckp_name"] = {}
# Get the test configurations
sys_state["com_base"] = "train_logs.%s.scripts."%sys_state["version"]
# make a reporter
report_path = os.path.join(env_config["test_log_root"], sys_state["version"],
sys_state["version"]+"_report")
reporter = Reporter(report_path)
reporter.writeConfig(sys_state)
# Display the test information
# TODO modify below lines to display your configuration information
moduleName = "test_scripts." + sys_state["test_script_name"]
print("Start to run test script: {}".format(moduleName))
print("Test version: %s"%sys_state["version"])
print("Test Script Name: %s"%sys_state["test_script_name"])
package = __import__(moduleName, fromlist=True)
testerClass = getattr(package, 'Tester')
tester = testerClass(sys_state,reporter)
tester.test()
if __name__ == '__main__':
main()