-
Notifications
You must be signed in to change notification settings - Fork 9
/
train_vin.py
executable file
·220 lines (198 loc) · 6.52 KB
/
train_vin.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
from trainer.start import *
from utils.exp_base import *
from train_nih import *
@dataclass
class VinConfig(Config):
n_ep: int = 100
data_conf: VinDataConfig = VinDataConfig(bs=64, split='v3')
net_conf: UnionModelConfig = None
n_eval_ep_cycle: int = 3
pre_conf: 'VinConfig' = None
@property
def name(self):
if self.save_dir is not None:
return self.save_dir
a = f'{self.data_conf.name}'
b = f'{self.net_conf.name}'
if self.optimizier == 'pylonadam':
b += f'_pylonadam_lr({",".join(str(lr) for lr in self.lr)})'
else:
b += f'_lr{self.lr}'
b += f'term{self.lr_term}rop{self.rop_patience}fac{self.rop_factor}'
if self.fp16:
b += f'_fp16'
c = f'{self.seed}'
return '/'.join([a, b, c])
def make_experiment(self):
return VinExperiment(self)
class VinExperiment(Experiment):
def __init__(self, conf: VinConfig) -> None:
super().__init__(conf, Trainer)
self.conf = conf
def make_dataset(self):
self.data = self.conf.data_conf.make_dataset()
self.train_loader = ConvertLoader(
self.data.make_loader(self.data.train, shuffle=True),
device=self.conf.device,
)
self.val_loader = ConvertLoader(
self.data.make_loader(self.data.val, shuffle=False),
device=self.conf.device,
)
self.test_loader = ConvertLoader(
self.data.make_loader(self.data.test, shuffle=False),
device=self.conf.device,
)
def make_callbacks(self, trainer: Trainer):
cls_id_to_name = self.data.test.id_to_cls
return super().make_callbacks(trainer) + [
ValidateCb(
self.val_loader,
n_ep_cycle=self.conf.n_eval_ep_cycle,
name='val',
callbacks=[
AvgCb(trainer.metrics),
AUROCCb(
keys=('pred', 'classification'),
cls_id_to_name=cls_id_to_name,
),
LocalizationAccCb(
keys=('pred_seg', 'bboxes'),
cls_id_to_name=cls_id_to_name,
conf=LocalizationAccConfig(intersect_thresholds=[]),
),
],
),
]
def vin_baseline(seed, size=256, bs=64):
return [
VinConfig(
seed=seed,
data_conf=VinDataConfig(bs=bs,
trans_conf=XRayTransformConfig(size=size)),
net_conf=BaselineModelConfig(n_out=15),
)
]
def vin_li2018(seed, size=256, bs=64):
return [
VinConfig(
seed=seed,
data_conf=VinDataConfig(bs=bs,
trans_conf=XRayTransformConfig(size=size)),
net_conf=Li2018Config(n_out=15),
)
]
def vin_fpn(seed,
size=256,
bs=64,
segment_block='custom',
use_norm='batchnorm',
n_group=None):
"""
Args:
segment_block: 'original', 'custom'
use_norm: 'batchnorm', 'groupnorm' (on with 'custom')
"""
return [
VinConfig(
seed=seed,
data_conf=VinDataConfig(bs=bs,
trans_conf=XRayTransformConfig(size=size)),
net_conf=FPNConfig(n_out=15,
segment_block=segment_block,
use_norm=use_norm,
n_group=n_group),
)
]
def vin_pylon(seed, size=256, bs=64, up_type='2layer', **kwargs):
return [
VinConfig(
seed=seed,
data_conf=VinDataConfig(bs=bs,
trans_conf=XRayTransformConfig(size=size)),
net_conf=PylonConfig(n_in=1, n_out=15, up_type=up_type, **kwargs),
)
]
def vin_baseline_transfer(seed, size=256, bs=64):
out = []
pre_conf = nih_baseline(seed, size, bs)[0]
out.append(
VinConfig(
seed=seed,
data_conf=VinDataConfig(bs=bs,
trans_conf=XRayTransformConfig(size=size)),
net_conf=BaselineModelConfig(
n_out=15,
pretrain_conf=PretrainConfig(
pretrain_name='nih',
path=get_pretrain_path(pre_conf.name),
),
),
pre_conf=pre_conf,
))
return out
def vin_pylon_transfer(seed, size=256, bs=64, up_type='2layer', **kwargs):
out = []
pre_conf = nih_pylon(seed, size, bs, up_type, **kwargs)[0]
out.append(
VinConfig(
seed=seed,
data_conf=VinDataConfig(bs=bs,
trans_conf=XRayTransformConfig(size=size)),
net_conf=PylonConfig(
n_in=1,
n_out=15,
up_type=up_type,
**kwargs,
pretrain_conf=PretrainConfig(
pretrain_name='nih',
path=get_pretrain_path(pre_conf.name),
),
),
pre_conf=pre_conf,
))
return out
def vin_pylon_transfer_two_phase(seed,
size=256,
bs=64,
up_type='2layer',
**kwargs):
out = []
# train on NIH
pre_conf = nih_pylon(seed, size, bs, up_type, **kwargs)[0]
# train only the decoder
data_conf = VinDataConfig(bs=bs, trans_conf=XRayTransformConfig(size=size))
first_phase_conf = VinConfig(
seed=seed,
data_conf=data_conf,
net_conf=PylonConfig(
n_in=1,
n_out=15,
up_type=up_type,
**kwargs,
pretrain_conf=PretrainConfig(
pretrain_name='nih',
path=get_pretrain_path(pre_conf.name),
),
freeze='enc',
),
pre_conf=pre_conf,
)
# train all
out.append(
VinConfig(
seed=seed,
data_conf=data_conf,
net_conf=PylonConfig(
n_in=1,
n_out=15,
up_type=up_type,
**kwargs,
pretrain_conf=PretrainConfig(
pretrain_name='nih,twophase',
path=get_pretrain_path(first_phase_conf.name),
),
),
pre_conf=first_phase_conf,
))
return out