forked from BloodAxe/xView3-The-First-Place-Solution
-
Notifications
You must be signed in to change notification settings - Fork 6
/
train_multilabel.py
296 lines (251 loc) · 12.8 KB
/
train_multilabel.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
import sys
import traceback
import warnings
import catalyst
import numpy as np
import pandas as pd
import torch
from xview3.early_stopping import EarlyStoppingCallback
def warn_with_traceback(message, category, filename, lineno, file=None, line=None):
log = file if hasattr(file, "write") else sys.stderr
traceback.print_stack(file=log)
log.write(warnings.formatwarning(message, category, filename, lineno, line))
warnings.showwarning = warn_with_traceback
from functools import partial
from typing import Optional, Tuple, List
from catalyst.callbacks import ControlFlowCallback
from catalyst.core import Callback
from omegaconf import DictConfig
from pytorch_toolbelt.utils.catalyst import (
ShowPolarBatchesCallback,
BestMetricCheckpointCallback,
HyperParametersCallback,
)
from torch import nn
from torch.utils.data import Dataset, Sampler
from xview3 import *
from xview3.centernet import *
class CenternetPipeline(Pipeline):
valid_df: Optional[pd.DataFrame] = None
def __init__(self, cfg):
super().__init__(cfg)
self.box_coder: Optional[MultilabelCircleNetCoder] = None
self.shore_root = None
def build_datasets(self, config) -> Tuple[Dataset, Dataset, Optional[Sampler], List[Callback]]:
dataset = MultilabelCircleNetDataModule(data_dir=config.dataset.data_dir)
train_df, valid_df, _, shore_root = dataset.train_val_split(
splitter=config.dataset.splitter,
fold=config.dataset.fold,
num_folds=config.dataset.num_folds,
)
self.valid_df = valid_df.copy() # Keep a copy of unmodified groundtruth
self.master_print("Dataset", config.dataset.slug)
self.master_print("\tignore_low_confidence_detections", config.dataset.ignore_low_confidence_detections)
self.master_print("\tignore_low_confidence_labels ", config.dataset.ignore_low_confidence_labels)
self.master_print("\tfilter_low_confidence_objects ", config.dataset.filter_low_confidence_objects)
self.master_print("Train dataset")
self.master_print("\tUnknown vessels", np.isnan(train_df.is_vessel.values.astype(float)).sum())
self.master_print("\tUnknown fishing", np.isnan(train_df.is_fishing.values.astype(float)).sum())
self.master_print("Valid dataset")
self.master_print("\tUnknown vessels", np.isnan(valid_df.is_vessel.values.astype(float)).sum())
self.master_print("\tUnknown fishing", np.isnan(valid_df.is_fishing.values.astype(float)).sum())
if config.dataset.filter_low_confidence_objects and config.dataset.ignore_low_confidence_labels:
raise ValueError(f"filter_low_confidence_objects and ignore_low_confidence_labels are mutually exclusive")
if config.dataset.filter_low_confidence_objects:
train_df = filter_low_confidence_objects(train_df)
valid_df = filter_low_confidence_objects(valid_df)
self.master_print("Excluding low-confidence objects from training & validation")
elif config.dataset.ignore_low_confidence_labels:
train_df = ignore_low_confidence_objects(train_df)
valid_df = ignore_low_confidence_objects(valid_df)
self.master_print("Ignoring low-confidence labels from training & validation")
self.shore_root = shore_root
import albumentations as A
augmentations = build_augmentations(config.augs.spatial)
normalization = build_normalization(config.normalization)
individual_augmentations = dict(
[(key, A.Compose(build_augmentations(value))) for key, value in config.augs.individual.items() if value is not None]
)
if config.sampler.sampler_type == "each_object":
train_ds, train_sampler = dataset.get_random_crop_each_object_dataset(
train_df,
train_image_size=config.train.train_image_size,
input_channels=config.dataset.channels,
box_coder=self.box_coder,
num_samples=config.sampler.num_samples,
individual_augmentations=individual_augmentations,
augmentations=augmentations,
normalization=normalization,
balance_crowd=config.sampler.balance_crowd,
balance_near_shore=config.sampler.balance_near_shore,
balance_vessel_type=config.sampler.balance_vessel_type,
balance_fishing_type=config.sampler.balance_fishing_type,
balance_per_scene=config.sampler.balance_per_scene,
balance_location=config.sampler.balance_location,
channels_last=config.torch.channels_last,
)
else:
train_ds, train_sampler = dataset.get_random_crop_training_dataset(
train_df,
train_image_size=config.train.train_image_size,
input_channels=config.dataset.channels,
box_coder=self.box_coder,
num_samples=config.sampler.num_samples,
crop_around_ship_p=config.sampler.crop_around_ship_p,
individual_augmentations=individual_augmentations,
augmentations=augmentations,
normalization=normalization,
balance_near_shore=config.sampler.balance_near_shore,
balance_crowd=config.sampler.balance_crowd,
balance_location=config.sampler.balance_location,
balance_folder=config.sampler.balance_folder,
channels_last=config.torch.channels_last,
shore_root=shore_root,
)
valid_ds = dataset.get_validation_dataset(
valid_df,
valid_crop_size=config.train.valid_image_size,
input_channels=config.dataset.channels,
box_coder=self.box_coder,
normalization=normalization,
channels_last=config.torch.channels_last,
)
self.master_print(
"Train",
"instances",
len(train_df),
"dataset",
len(train_ds),
"sampler",
len(train_sampler) if train_sampler else "N/A",
)
self.master_print("Valid", "instances", len(valid_df), "dataset", len(valid_ds))
return train_ds, valid_ds, train_sampler, []
def build_metrics(self, config, loaders, model):
callbacks = [
CircleNetRocAucMetricsCallback(prefix="extra_metrics"),
MultilabelCircleNetDecodeCallback(box_coder=self.box_coder),
]
show_batches = self.cfg["train"].get("show", False)
callbacks += [
# This is the vanilla method for computing score that matches the public LB atm.
# ControlFlowCallback(
# MultilabelCircleNetDetectionMetrics(
# groundtruth_df=filter_low_confidence_objects(self.valid_df),
# shore_root=self.shore_root,
# prefix="conf_",
# objectness_thresholds=[0.5],
# is_vessel_threshold=[0.5],
# is_fishing_threshold=[0.5],
# loc_performance_matching_method=compute_loc_performance,
# ),
# loaders="valid",
# ),
# This is the 'right' method for computing score.
ControlFlowCallback(
MultilabelCircleNetDetectionMetrics(
groundtruth_df=self.valid_df,
shore_root=self.shore_root,
prefix="",
objectness_thresholds=[0.25, 0.275, 0.3, 0.325, 0.350, 0.375, 0.4, 0.425, 0.45, 0.475, 0.5, 0.525, 0.55],
),
loaders="valid",
),
]
if self.is_master:
callbacks += [
# BestMetricCheckpointCallback(
# target_metric="metrics/conf_aggregate",
# target_metric_minimize=False,
# save_n_best=3,
# ),
BestMetricCheckpointCallback(
target_metric="metrics/aggregate",
target_metric_minimize=False,
save_n_best=3,
),
]
if config["train"]["early_stopping"] > 0:
callbacks.append(
EarlyStoppingCallback(
metrics=[
# "metrics/conf_aggregate",
"metrics/aggregate",
],
minimize=False,
min_delta=1e-6,
patience=config["train"]["early_stopping"],
)
)
if self.is_master:
if show_batches:
visualize_fn = visualize_predictions_multilabel_centernet
visualize_batch = partial(
visualize_fn,
box_coder=self.box_coder,
min_confidence_score=0.3,
# max_image_size=2048,
)
callbacks.append(ShowPolarBatchesCallback(visualize_batch, metric="loss", minimize=True))
callbacks.append(
HyperParametersCallback(
hparam_dict=dict(
# Model stuff
model=str(self.cfg.model.config.slug),
model_output_stride=model.box_coder.output_stride,
# Box Coder
box_coder_heatmap_encoding=str(self.cfg.model.config.box_coder.heatmap_encoding),
box_coder_labels_encoding=str(self.cfg.model.config.box_coder.labels_encoding),
box_coder_fixed_radius=str(self.cfg.model.config.box_coder.fixed_radius),
box_coder_labels_radius=str(self.cfg.model.config.box_coder.labels_radius),
# Optimizer
optimizer=str(self.cfg.optimizer.name),
optimizer_lr=float(self.cfg.optimizer.params.lr),
optimizer_eps=float(self.cfg.optimizer.params.eps) if "eps" in self.cfg.optimizer.params else "None",
optimizer_wd=float(self.cfg.optimizer.params.weight_decay),
optimizer_scheduler=str(self.cfg.scheduler.scheduler_name),
# Dataset
dataset=str(self.cfg.dataset.slug),
dataset_fold=int(self.cfg.dataset.fold) if self.cfg.dataset.fold is not None else "None",
dataset_filter_low_confidence_objects=bool(self.cfg.dataset.filter_low_confidence_objects),
dataset_ignore_low_confidence_labels=bool(self.cfg.dataset.ignore_low_confidence_labels),
dataset_ignore_low_confidence_detections=bool(self.cfg.dataset.ignore_low_confidence_detections),
dataset_augmentations=str(self.cfg.augs.slug),
dataset_train_image_size=f"{self.cfg.train.train_image_size}",
# Sampling
sampler=str(self.cfg.sampler.slug),
sampler_num_samples=int(self.cfg.sampler.num_samples),
sampler_type=str(self.cfg.sampler.sampler_type),
sampler_balance_crowd=bool(self.cfg.sampler.balance_crowd),
balance_near_shore=bool(self.cfg.sampler.balance_near_shore),
balance_vessel_type=bool(self.cfg.sampler.balance_vessel_type),
balance_fishing_type=bool(self.cfg.sampler.balance_fishing_type),
balance_location=bool(self.cfg.sampler.balance_location),
# Loss
loss=str(self.cfg.loss.slug),
)
),
)
return callbacks
def get_model(self, model_config: DictConfig) -> nn.Module:
model: nn.Module = get_detection_model(model_config)
self.box_coder = model.box_coder
self.master_print("BoxCoder")
self.master_print(self.box_coder)
return model
@hydra_dpp_friendly_main(config_path="configs", config_name="multilabel_circle_net")
def main(config: DictConfig) -> None:
torch.cuda.empty_cache()
catalyst.utils.set_global_seed(int(config.seed))
torch.set_anomaly_enabled(config.torch.detect_anomaly)
torch.backends.cudnn.deterministic = config.torch.deterministic
torch.backends.cudnn.benchmark = config.torch.benchmark
# The flag below controls whether to allow TF32 on matmul. This flag defaults to True.
torch.backends.cuda.matmul.allow_tf32 = config.torch.cuda_allow_tf32
# The flag below controls whether to allow TF32 on cuDNN. This flag defaults to True.
torch.backends.cudnn.allow_tf32 = config.torch.cudnn_allow_tf32
if config.dataset.data_dir is None:
raise ValueError("--data-dir must be set")
CenternetPipeline(config).train()
if __name__ == "__main__":
main()