forked from vdurnov/xview2_1st_place_solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_senet154.py
260 lines (244 loc) · 9.23 KB
/
example_senet154.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
from pathlib import Path
from torch.utils.data import DataLoader
from torch.optim import AdamW
from torch.optim.lr_scheduler import MultiStepLR
from torch.cuda import amp
from torch import nn
from metadamagenet.utils import set_random_seeds
from metadamagenet.dataset import LocalizationDataset, ClassificationDataset
from metadamagenet.models import Localizer, Classifier
from metadamagenet.models.unet import SeNet154Unet
from metadamagenet.losses import WeightedSum, BinaryFocalLoss2d, BinaryDiceLossWithLogits, DiceLoss, \
SegmentationCCE
from metadamagenet.metrics import xview2
from metadamagenet.runner import Trainer, ValidationInTrainingParams
from metadamagenet.augment import Random, VFlip, Rotate90, Shift, RotateAndScale, BestCrop, OneOf, RGBShift, HSVShift, \
Clahe, GaussianNoise, Blur, Saturation, Brightness, Contrast, ElasticTransform, Dilation
train_dir = Path('/datasets/xview2/train')
test_dir = Path('/datasets/xview2/test')
class SeNet154Localizer(Localizer[SeNet154Unet]):
pass
class SeNet154Classifier(Classifier[SeNet154Unet]):
pass
def train_localizer(seed: int):
set_random_seeds(321 + seed)
transform = nn.Sequential(
Random(VFlip(), p=0.4),
Random(Rotate90(), p=0.9),
Random(Shift(y=(.2, .8), x=(.2, .8)), p=.3),
Random(RotateAndScale(center_y=(0.3, 0.7), center_x=(0.3, 0.7), angle=(-10., 10.), scale=(.9, 1.1)), p=0.6),
BestCrop(samples=5, dsize=(480, 480), size_range=(0.42, 0.52)),
Random(RGBShift().only_on('img'), p=0.05),
Random(HSVShift().only_on('img'), p=0.04),
OneOf(
(OneOf(
(Clahe().only_on('img'), 0.08),
(GaussianNoise().only_on('img'), 0.08),
(Blur().only_on('img'), 0.08)), 0.08),
(OneOf(
(Saturation().only_on('img'), 0.08),
(Brightness().only_on('img'), 0.08),
(Contrast().only_on('img'), 0.08)), 0.08)
),
Random(ElasticTransform(), p=0.05)
)
model = SeNet154Localizer(SeNet154Unet(pretrained_backbone=True))
optimizer = AdamW(model.parameters(), lr=0.00015, weight_decay=1e-6)
lr_scheduler = MultiStepLR(optimizer,
milestones=[3, 7, 11, 15, 19, 23, 27, 33, 41, 50, 60, 70, 90, 110, 130, 150, 170, 180,
190],
gamma=0.5)
Trainer(
model=model,
version='1',
seed=0,
dataloader=DataLoader(
dataset=LocalizationDataset(train_dir),
batch_size=14,
num_workers=6,
shuffle=True,
drop_last=True
),
transform=transform,
optimizer=optimizer,
lr_scheduler=lr_scheduler,
loss=WeightedSum(
(BinaryDiceLossWithLogits(), 1.),
(BinaryFocalLoss2d(), 14.)
),
score=xview2.localization_score,
clip_grad_norm=0.999,
epochs=30,
validation_params=ValidationInTrainingParams(
dataloader=DataLoader(
dataset=LocalizationDataset(test_dir),
batch_size=4,
num_workers=6,
shuffle=False,
drop_last=False
),
transform=None,
interval=1,
)
)
def train_classifier(seed: int):
set_random_seeds(123123 + seed)
# from dpn92-classifier-train
transform = transform = nn.Sequential(
Random(VFlip(), p=0.5),
Random(Rotate90(), p=0.9999),
Random(Shift(y=(.2, .8), x=(.2, .8)), p=.5),
Random(RotateAndScale(center_y=(0.3, 0.7), center_x=(0.3, 0.7), angle=(-10., 10.), scale=(.9, 1.1)), p=0.95),
BestCrop(samples=10, dsize=(512, 512), size_range=(0.4, 0.6)),
OneOf(
(RGBShift().only_on('img_pre'), 0.1),
(RGBShift().only_on('img_post'), 0.1),
),
OneOf(
(HSVShift().only_on('img_pre'), 0.1),
(HSVShift().only_on('img_post'), 0.1),
),
OneOf(
(OneOf(
(Clahe().only_on('img_pre'), 0.1),
(GaussianNoise().only_on('img_pre'), 0.1),
(Blur().only_on('img_pre'), 0.1)), 0.1),
(OneOf(
(Saturation().only_on('img_pre'), 0.1),
(Brightness().only_on('img_pre'), 0.1),
(Contrast().only_on('img_pre'), 0.1)), 0.1)
),
OneOf(
(OneOf(
(Clahe().only_on('img_post'), 0.1),
(GaussianNoise().only_on('img_post'), 0.1),
(Blur().only_on('img_post'), 0.1)), 0.1),
(OneOf(
(Saturation().only_on('img_post'), 0.1),
(Brightness().only_on('img_post'), 0.1),
(Contrast().only_on('img_post'), 0.1)), 0.1)
),
Random(ElasticTransform().only_on('img_pre'), p=0.1),
Random(ElasticTransform().only_on('img_post'), p=0.1),
Random(Dilation().only_on('msk'), p=0.9)
)
model = SeNet154Classifier(SeNet154Localizer.from_pretrained(version='1', seed=seed).unet)
optimizer = AdamW(model.parameters(), lr=0.0001, weight_decay=1e-6)
lr_scheduler = MultiStepLR(optimizer,
milestones=[3, 5, 9, 13, 17, 21, 25, 29, 33, 47, 50, 60, 70, 90, 110,
130, 150, 170, 180, 190],
gamma=0.5)
Trainer(
model=model,
version='1',
seed=0,
dataloader=DataLoader(
dataset=ClassificationDataset(train_dir),
batch_size=8,
num_workers=6,
shuffle=True,
drop_last=True,
),
transform=transform,
optimizer=optimizer,
lr_scheduler=lr_scheduler,
loss=WeightedSum(
(DiceLoss(class_weights=[0.1, 0.1, 0.6, 0.3, 0.2]), 1),
(SegmentationCCE(), 8)
),
score=xview2.classification_score,
clip_grad_norm=0.999,
epochs=16,
grad_scaler=amp.GradScaler(),
validation_params=ValidationInTrainingParams(
dataloader=DataLoader(
dataset=ClassificationDataset(test_dir),
batch_size=2,
num_workers=6,
shuffle=False,
drop_last=False
),
transform=None,
interval=2,
)
)
def tune_classifier(seed: int):
set_random_seeds(531 + seed)
# from dpn92-classifier-tune
transform = nn.Sequential(
Random(VFlip(), p=0.3),
Random(Rotate90(), p=0.7),
Random(Shift(), p=.01),
Random(RotateAndScale(), p=0.5),
BestCrop(samples=10, dsize=(512, 512), size_range=(0.4, 0.6)),
OneOf(
(RGBShift().only_on('img_pre'), 0.01),
(RGBShift().only_on('img_post'), 0.01),
),
OneOf(
(HSVShift().only_on('img_pre'), 0.01),
(HSVShift().only_on('img_post'), 0.01),
),
OneOf(
(OneOf(
(Clahe().only_on('img_pre'), 0.01),
(GaussianNoise().only_on('img_pre'), 0.01),
(Blur().only_on('img_pre'), 0.01)), 0.01),
(OneOf(
(Saturation().only_on('img_pre'), 0.01),
(Brightness().only_on('img_pre'), 0.01),
(Contrast().only_on('img_pre'), 0.01)), 0.01)
),
OneOf(
(OneOf(
(Clahe().only_on('img_post'), 0.01),
(GaussianNoise().only_on('img_post'), 0.01),
(Blur().only_on('img_post'), 0.01)), 0.01),
(OneOf(
(Saturation().only_on('img_post'), 0.01),
(Brightness().only_on('img_post'), 0.01),
(Contrast().only_on('img_post'), 0.01)), 0.01)
),
Random(ElasticTransform().only_on('img_pre'), p=0.01),
Random(ElasticTransform().only_on('img_post'), p=0.01),
Random(Dilation().only_on('msk'), p=0.9)
)
model = SeNet154Classifier.from_pretrained(version='1', seed=0)
optimizer = AdamW(model.parameters(), lr=0.000008, weight_decay=1e-6)
lr_scheduler = MultiStepLR(optimizer,
milestones=[1, 2, 3, 4, 5, 7, 9, 11, 17, 23, 29, 33, 47, 50, 60, 70,
90, 110, 130, 150, 170, 180, 190],
gamma=0.5)
Trainer(
model=model,
version='tuned',
seed=0,
dataloader=DataLoader(
dataset=ClassificationDataset(train_dir),
batch_size=8,
num_workers=6,
shuffle=True,
drop_last=True
),
transform=transform,
optimizer=optimizer,
lr_scheduler=lr_scheduler,
loss=WeightedSum(
(DiceLoss(class_weights=[0.1, 0.1, 0.6, 0.3, 0.2]), 1),
(SegmentationCCE(), 8)
),
score=xview2.classification_score,
clip_grad_norm=0.999,
epochs=2,
validation_params=ValidationInTrainingParams(
dataloader=DataLoader(
dataset=ClassificationDataset(test_dir),
batch_size=2,
num_workers=6,
shuffle=False,
drop_last=False
),
transform=None,
interval=2,
)
)