-
Notifications
You must be signed in to change notification settings - Fork 11
/
test.py
308 lines (287 loc) · 17.1 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# -*- encoding: utf-8 -*-
"""
@author : yykzjh
@Contact : 1378453948@qq.com
@DateTime : 2023/12/30 17:05
@Version : 1.0
@License : (C)Copyright 2023
"""
import os
import argparse
import torch
from lib import utils, dataloaders, models, metrics, testers
params_3D_CBCT_Tooth = {
# —————————————————————————————————————————————— Launch Initialization —————————————————————————————————————————————————
"CUDA_VISIBLE_DEVICES": "6",
"seed": 1777777,
"cuda": True,
"benchmark": False,
"deterministic": True,
# ————————————————————————————————————————————— Preprocessing ————————————————————————————————————————————————————
"resample_spacing": [0.5, 0.5, 0.5],
"clip_lower_bound": -1412,
"clip_upper_bound": 17943,
"samples_train": 2048,
"crop_size": (160, 160, 96),
"crop_threshold": 0.5,
# —————————————————————————————————————————————— Data Augmentation ——————————————————————————————————————————————————————
"augmentation_probability": 0.3,
"augmentation_method": "Choice",
"open_elastic_transform": True,
"elastic_transform_sigma": 20,
"elastic_transform_alpha": 1,
"open_gaussian_noise": True,
"gaussian_noise_mean": 0,
"gaussian_noise_std": 0.01,
"open_random_flip": True,
"open_random_rescale": True,
"random_rescale_min_percentage": 0.5,
"random_rescale_max_percentage": 1.5,
"open_random_rotate": True,
"random_rotate_min_angle": -50,
"random_rotate_max_angle": 50,
"open_random_shift": True,
"random_shift_max_percentage": 0.3,
"normalize_mean": 0.05029342141696459,
"normalize_std": 0.028477091559295814,
# ————————————————————————————————————————————— Data Loading ——————————————————————————————————————————————————————
"dataset_name": "3D-CBCT-Tooth",
"dataset_path": r"./datasets/3D-CBCT-Tooth",
"create_data": False,
"batch_size": 1,
"num_workers": 2,
# ————————————————————————————————————————————— Model ——————————————————————————————————————————————————————
"model_name": "PMFSNet",
"in_channels": 1,
"classes": 2,
"index_to_class_dict":
{
0: "background",
1: "foreground"
},
"resume": None,
"pretrain": None,
"high_frequency": 0.9,
"low_frequency": 0.1,
# —————————————————————————————————————————————— Optimizer ——————————————————————————————————————————————————————
"optimizer_name": "Adam",
"learning_rate": 0.0005,
"weight_decay": 0.00005,
"momentum": 0.8,
# ——————————————————————————————————————————— Learning Rate Scheduler —————————————————————————————————————————————————————
"lr_scheduler_name": "ReduceLROnPlateau",
"gamma": 0.1,
"step_size": 9,
"milestones": [1, 3, 5, 7, 8, 9],
"T_max": 2,
"T_0": 2,
"T_mult": 2,
"mode": "max",
"patience": 1,
"factor": 0.5,
# ———————————————————————————————————————————— Loss And Metric ———————————————————————————————————————————————————————
"metric_names": ["HD", "ASSD", "IoU", "SO", "DSC"],
"loss_function_name": "DiceLoss",
"class_weight": [0.00551122, 0.99448878],
"sigmoid_normalization": False,
"dice_loss_mode": "extension",
"dice_mode": "standard",
# ————————————————————————————————————————————— Training ——————————————————————————————————————————————————————
"optimize_params": False,
"use_amp": False,
"run_dir": r"./runs",
"start_epoch": 0,
"end_epoch": 20,
"best_dice": 0.60,
"update_weight_freq": 32,
"terminal_show_freq": 256,
"save_epoch_freq": 4,
# ———————————————————————————————————————————— Testing ———————————————————————————————————————————————————————
"crop_stride": [32, 32, 32]
}
params_MMOTU = {
# —————————————————————————————————————————————— Launch Initialization ———————————————————————————————————————————————————
"CUDA_VISIBLE_DEVICES": "0",
"seed": 1777777,
"cuda": True,
"benchmark": False,
"deterministic": True,
# ————————————————————————————————————————————— Preprocessing ————————————————————————————————————————————————————
"resize_shape": (224, 224),
# —————————————————————————————————————————————— Data Augmentation ——————————————————————————————————————————————————————
"augmentation_p": 0.12097393901893663,
"color_jitter": 0.4203933474361258,
"random_rotation_angle": 30,
"normalize_means": (0.22250386, 0.21844882, 0.21521868),
"normalize_stds": (0.21923075, 0.21622984, 0.21370508),
# ————————————————————————————————————————————— Data Loading ——————————————————————————————————————————————————————
"dataset_name": "MMOTU",
"dataset_path": r"./datasets/MMOTU",
"batch_size": 32,
"num_workers": 2,
# ————————————————————————————————————————————— Model ——————————————————————————————————————————————————————
"model_name": "PMFSNet",
"in_channels": 3,
"classes": 2,
"index_to_class_dict":
{
0: "background",
1: "foreground"
},
"resume": None,
"pretrain": None,
# —————————————————————————————————————————————— Optimizer ——————————————————————————————————————————————————————
"optimizer_name": "AdamW",
"learning_rate": 0.01,
"weight_decay": 0.00001,
"momentum": 0.7725414416309884,
# ——————————————————————————————————————————— Learning Rate Scheduler —————————————————————————————————————————————————————
"lr_scheduler_name": "CosineAnnealingLR",
"gamma": 0.8689275449032848,
"step_size": 5,
"milestones": [10, 30, 60, 100, 120, 140, 160, 170],
"T_max": 200,
"T_0": 10,
"T_mult": 5,
"mode": "max",
"patience": 1,
"factor": 0.97,
# ———————————————————————————————————————————— Loss And Metric ———————————————————————————————————————————————————————
"metric_names": ["DSC", "IoU"],
"loss_function_name": "DiceLoss",
"class_weight": [0.2350689696563569, 1 - 0.2350689696563569],
"sigmoid_normalization": False,
"dice_loss_mode": "extension",
"dice_mode": "standard",
# ————————————————————————————————————————————— Training ——————————————————————————————————————————————————————
"optimize_params": False,
"run_dir": r"./runs",
"start_epoch": 0,
"end_epoch": 2000,
"best_metric": 0,
"terminal_show_freq": 8,
"save_epoch_freq": 500,
}
params_ISIC_2018 = {
# —————————————————————————————————————————————— Launch Initialization ———————————————————————————————————————————————————
"CUDA_VISIBLE_DEVICES": "0",
"seed": 1777777,
"cuda": True,
"benchmark": False,
"deterministic": True,
# ————————————————————————————————————————————— Preprocessing ————————————————————————————————————————————————————
"resize_shape": (224, 224),
# —————————————————————————————————————————————— Data Augmentation ——————————————————————————————————————————————————————
"augmentation_p": 0.1,
"color_jitter": 0.37,
"random_rotation_angle": 15,
"normalize_means": (0.50297405, 0.54711632, 0.71049083),
"normalize_stds": (0.18653496, 0.17118206, 0.17080363),
# ————————————————————————————————————————————— Data Loading ——————————————————————————————————————————————————————
"dataset_name": "ISIC-2018",
"dataset_path": r"./datasets/ISIC-2018",
"batch_size": 32,
"num_workers": 2,
# ————————————————————————————————————————————— Model ——————————————————————————————————————————————————————
"model_name": "PMFSNet",
"in_channels": 3,
"classes": 2,
"index_to_class_dict":
{
0: "background",
1: "foreground"
},
"resume": None,
"pretrain": None,
# —————————————————————————————————————————————— Optimizer ——————————————————————————————————————————————————————
"optimizer_name": "AdamW",
"learning_rate": 0.005,
"weight_decay": 0.000001,
"momentum": 0.9657205586290213,
# ——————————————————————————————————————————— Learning Rate Scheduler —————————————————————————————————————————————————————
"lr_scheduler_name": "CosineAnnealingWarmRestarts",
"gamma": 0.9582311026945434,
"step_size": 20,
"milestones": [1, 3, 5, 7, 8, 9],
"T_max": 100,
"T_0": 5,
"T_mult": 5,
"mode": "max",
"patience": 20,
"factor": 0.3,
# ———————————————————————————————————————————— Loss And Metric ———————————————————————————————————————————————————————
"metric_names": ["DSC", "IoU", "JI", "ACC"],
"loss_function_name": "DiceLoss",
"class_weight": [0.029, 1 - 0.029],
"sigmoid_normalization": False,
"dice_loss_mode": "extension",
"dice_mode": "standard",
# ————————————————————————————————————————————— Training ——————————————————————————————————————————————————————
"optimize_params": False,
"run_dir": r"./runs",
"start_epoch": 0,
"end_epoch": 150,
"best_metric": 0,
"terminal_show_freq": 20,
"save_epoch_freq": 50,
}
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--dataset", type=str, default="3D-CBCT-Tooth", help="dataset name")
parser.add_argument("--model", type=str, default="PMFSNet", help="model name")
parser.add_argument("--pretrain_weight", type=str, default=None, help="pre-trained weight file path")
parser.add_argument("--dimension", type=str, default="3d", help="dimension of dataset images and models")
parser.add_argument("--scaling_version", type=str, default="TINY", help="scaling version of PMFSNet")
args = parser.parse_args()
return args
def main():
# analyse console arguments
args = parse_args()
# select the dictionary of hyperparameters used for training
if args.dataset == "3D-CBCT-Tooth":
params = params_3D_CBCT_Tooth
elif args.dataset == "MMOTU":
params = params_MMOTU
elif args.dataset == "ISIC-2018":
params = params_ISIC_2018
else:
raise RuntimeError(f"No {args.dataset} dataset available")
# update the dictionary of hyperparameters used for training
params["dataset_name"] = args.dataset
params["dataset_path"] = os.path.join(r"./datasets", ("NC-release-data-checked" if args.dataset == "3D-CBCT-Tooth" else args.dataset))
params["model_name"] = args.model
if args.pretrain_weight is None:
raise RuntimeError("model weights cannot be None")
params["pretrain"] = args.pretrain_weight
params["dimension"] = args.dimension
params["scaling_version"] = args.scaling_version
# launch initialization
os.environ["CUDA_VISIBLE_DEVICES"] = params["CUDA_VISIBLE_DEVICES"]
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"
utils.reproducibility(params["seed"], params["deterministic"], params["benchmark"])
# get the cuda device
if params["cuda"]:
params["device"] = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
else:
params["device"] = torch.device("cpu")
print(params["device"])
print("Complete the initialization of configuration")
# initialize the dataloader
valid_loader = dataloaders.get_test_dataloader(params)
print("Complete the initialization of dataloader")
# initialize the model
model = models.get_model(params)
print("Complete the initialization of model:{}".format(params["model_name"]))
# initialize the metrics
metric = metrics.get_metric(params)
print("Complete the initialization of metrics")
# initialize the tester
tester = testers.get_tester(params, model, metric)
print("Complete the initialization of tester")
# load training weights
tester.load()
print("Complete loading training weights")
# evaluate valid set
tester.evaluation(valid_loader)
if __name__ == '__main__':
main()