forked from cmb-chula/pylon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_voc.py
208 lines (183 loc) · 6.15 KB
/
train_voc.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
from trainer.start import *
from utils.exp_base import *
@dataclass
class VOCConfig(Config):
n_ep: int = 40
data_conf: VOCDataConfig = VOCDataConfig(bs=64)
net_conf: UnionModelConfig = None
pre_conf: 'VOCConfig' = 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 VOCExperiment(self)
class VOCExperiment(Experiment):
def __init__(self, conf: VOCConfig) -> 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,
)
def make_callbacks(self, trainer: Trainer):
cls_id_to_name = self.data.val.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 test_auc(self):
cls_id_to_name = self.data.val.id_to_cls
callbacks = [
ProgressCb('test'),
AvgCb('loss'),
AUROCCb(
keys=('pred', 'classification'),
cls_id_to_name=cls_id_to_name,
),
]
trainer = self.load_trainer()
predictor = ValidatePredictor(trainer, callbacks)
out, extras = predictor.predict(self.val_loader)
out.update(extras)
print(out)
path = f'eval_auc/{self.conf.name}.csv'
dirname = os.path.dirname(path)
if not os.path.exists(dirname):
os.makedirs(dirname)
df = DataFrame([out])
df.to_csv(path, index=False)
# group the seeds, it will be correct with the last seed
group_seeds(dirname)
def test_loc(self):
cls_id_to_name = self.data.val.id_to_cls
callbacks = [
ProgressCb('test'),
LocalizationAccCb(
keys=('pred_seg', 'bboxes'),
cls_id_to_name=cls_id_to_name,
conf=LocalizationAccConfig(
intersect_thresholds=(0.1, 0.25, 0.5),
# mode='iou',
mode='iobb_or_iou',
),
)
]
trainer = self.load_trainer()
predictor = ValidatePredictor(trainer, callbacks)
out, extras = predictor.predict(self.val_loader)
out.update(extras)
print(out)
path = f'eval_loc/{self.conf.name}.csv'
dirname = os.path.dirname(path)
if not os.path.exists(dirname):
os.makedirs(dirname)
df = DataFrame([out])
df.to_csv(path, index=False)
# group the seeds, it will be correct with the last seed
group_seeds(dirname)
def generate_all_heatmap(self):
raise NotImplementedError()
def generate_picked_heatmap(self):
raise NotImplementedError()
def voc_baseline(seed, size=256, bs=64):
return [
VOCConfig(
seed=seed,
data_conf=VOCDataConfig(bs=bs,
trans_conf=VOCTransformConfig(size=size)),
net_conf=BaselineModelConfig(n_out=21, n_in=3),
)
]
def voc_li2018(seed, size=256, bs=64):
return [
VOCConfig(
seed=seed,
data_conf=VOCDataConfig(bs=bs,
trans_conf=VOCTransformConfig(size=size)),
net_conf=Li2018Config(n_out=21, n_in=3),
)
]
def voc_fpn(seed,
size=256,
bs=64,
segment_block='custom',
use_norm='batchnorm',
n_group=None):
return [
VOCConfig(
seed=seed,
data_conf=VOCDataConfig(bs=bs,
trans_conf=VOCTransformConfig(size=size)),
net_conf=FPNConfig(n_out=21,
n_in=3,
segment_block=segment_block,
use_norm=use_norm,
n_group=n_group),
)
]
def voc_pylon_two_phase(seed, size=256, bs=64, up_type='2layer', **kwargs):
data_conf = VOCDataConfig(bs=bs, trans_conf=VOCTransformConfig(size=size))
out = []
# train only the decoder
first_phase_config = VOCConfig(
seed=seed,
data_conf=data_conf,
net_conf=PylonConfig(
n_in=3,
n_out=21,
up_type=up_type,
**kwargs,
freeze='enc',
),
)
# train the whole network
out.append(
VOCConfig(
seed=seed,
data_conf=data_conf,
net_conf=PylonConfig(
n_in=3,
n_out=21,
up_type=up_type,
**kwargs,
pretrain_conf=PretrainConfig(
pretrain_name='twophase',
path=get_pretrain_path(first_phase_config.name),
),
),
pre_conf=first_phase_config,
))
return out