forked from microsoft/torchgeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
executable file
·238 lines (201 loc) · 8.97 KB
/
train.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
#!/usr/bin/env python3
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""torchgeo model training script."""
import os
from typing import Any, Dict, Tuple, Type, cast
import pytorch_lightning as pl
from omegaconf import DictConfig, OmegaConf
from pytorch_lightning import loggers as pl_loggers
from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint
from torchgeo.datamodules import (
BigEarthNetDataModule,
ChesapeakeCVPRDataModule,
COWCCountingDataModule,
DeepGlobeLandCoverDataModule,
ETCI2021DataModule,
EuroSATDataModule,
GID15DataModule,
InriaAerialImageLabelingDataModule,
LandCoverAIDataModule,
LoveDADataModule,
NAIPChesapeakeDataModule,
NASAMarineDebrisDataModule,
Potsdam2DDataModule,
RESISC45DataModule,
SEN12MSDataModule,
So2SatDataModule,
SpaceNet1DataModule,
TropicalCycloneDataModule,
UCMercedDataModule,
Vaihingen2DDataModule,
)
from torchgeo.trainers import (
BYOLTask,
ClassificationTask,
MultiLabelClassificationTask,
ObjectDetectionTask,
RegressionTask,
SemanticSegmentationTask,
)
TASK_TO_MODULES_MAPPING: Dict[
str, Tuple[Type[pl.LightningModule], Type[pl.LightningDataModule]]
] = {
"bigearthnet": (MultiLabelClassificationTask, BigEarthNetDataModule),
"byol": (BYOLTask, ChesapeakeCVPRDataModule),
"chesapeake_cvpr": (SemanticSegmentationTask, ChesapeakeCVPRDataModule),
"cowc_counting": (RegressionTask, COWCCountingDataModule),
"cyclone": (RegressionTask, TropicalCycloneDataModule),
"deepglobelandcover": (SemanticSegmentationTask, DeepGlobeLandCoverDataModule),
"eurosat": (ClassificationTask, EuroSATDataModule),
"etci2021": (SemanticSegmentationTask, ETCI2021DataModule),
"gid15": (SemanticSegmentationTask, GID15DataModule),
"inria": (SemanticSegmentationTask, InriaAerialImageLabelingDataModule),
"landcoverai": (SemanticSegmentationTask, LandCoverAIDataModule),
"loveda": (SemanticSegmentationTask, LoveDADataModule),
"naipchesapeake": (SemanticSegmentationTask, NAIPChesapeakeDataModule),
"nasa_marine_debris": (ObjectDetectionTask, NASAMarineDebrisDataModule),
"potsdam2d": (SemanticSegmentationTask, Potsdam2DDataModule),
"resisc45": (ClassificationTask, RESISC45DataModule),
"sen12ms": (SemanticSegmentationTask, SEN12MSDataModule),
"so2sat": (ClassificationTask, So2SatDataModule),
"spacenet1": (SemanticSegmentationTask, SpaceNet1DataModule),
"ucmerced": (ClassificationTask, UCMercedDataModule),
"vaihingen2d": (SemanticSegmentationTask, Vaihingen2DDataModule),
}
def set_up_omegaconf() -> DictConfig:
"""Loads program arguments from either YAML config files or command line arguments.
This method loads defaults/a schema from "conf/defaults.yaml" as well as potential
arguments from the command line. If one of the command line arguments is
"config_file", then we additionally read arguments from that YAML file. One of the
config file based arguments or command line arguments must specify task.name. The
task.name value is used to grab a task specific defaults from its respective
trainer. The final configuration is given as merge(task_defaults, defaults,
config file, command line). The merge() works from the first argument to the last,
replacing existing values with newer values. Additionally, if any values are
merged into task_defaults without matching types, then there will be a runtime
error.
Returns:
an OmegaConf DictConfig containing all the validated program arguments
Raises:
FileNotFoundError: when ``config_file`` does not exist
ValueError: when ``task.name`` is not a valid task
"""
conf = OmegaConf.load("conf/defaults.yaml")
command_line_conf = OmegaConf.from_cli()
if "config_file" in command_line_conf:
config_fn = command_line_conf.config_file
if not os.path.isfile(config_fn):
raise FileNotFoundError(f"config_file={config_fn} is not a valid file")
user_conf = OmegaConf.load(config_fn)
conf = OmegaConf.merge(conf, user_conf)
conf = OmegaConf.merge( # Merge in any arguments passed via the command line
conf, command_line_conf
)
# These OmegaConf structured configs enforce a schema at runtime, see:
# https://omegaconf.readthedocs.io/en/2.0_branch/structured_config.html#merging-with-other-configs
task_name = conf.experiment.task
task_config_fn = os.path.join("conf", f"{task_name}.yaml")
if task_name == "test":
task_conf = OmegaConf.create()
elif os.path.exists(task_config_fn):
task_conf = cast(DictConfig, OmegaConf.load(task_config_fn))
else:
raise ValueError(
f"experiment.task={task_name} is not recognized as a valid task"
)
conf = OmegaConf.merge(task_conf, conf)
conf = cast(DictConfig, conf) # convince mypy that everything is alright
return conf
def main(conf: DictConfig) -> None:
"""Main training loop."""
######################################
# Setup output directory
######################################
experiment_name = conf.experiment.name
task_name = conf.experiment.task
if os.path.isfile(conf.program.output_dir):
raise NotADirectoryError("`program.output_dir` must be a directory")
os.makedirs(conf.program.output_dir, exist_ok=True)
experiment_dir = os.path.join(conf.program.output_dir, experiment_name)
os.makedirs(experiment_dir, exist_ok=True)
if len(os.listdir(experiment_dir)) > 0:
if conf.program.overwrite:
print(
f"WARNING! The experiment directory, {experiment_dir}, already exists, "
+ "we might overwrite data in it!"
)
else:
raise FileExistsError(
f"The experiment directory, {experiment_dir}, already exists and isn't "
+ "empty. We don't want to overwrite any existing results, exiting..."
)
with open(os.path.join(experiment_dir, "experiment_config.yaml"), "w") as f:
OmegaConf.save(config=conf, f=f)
######################################
# Choose task to run based on arguments or configuration
######################################
# Convert the DictConfig into a dictionary so that we can pass as kwargs.
task_args = cast(Dict[str, Any], OmegaConf.to_object(conf.experiment.module))
datamodule_args = cast(
Dict[str, Any], OmegaConf.to_object(conf.experiment.datamodule)
)
datamodule: pl.LightningDataModule
task: pl.LightningModule
if task_name in TASK_TO_MODULES_MAPPING:
task_class, datamodule_class = TASK_TO_MODULES_MAPPING[task_name]
task = task_class(**task_args)
datamodule = datamodule_class(**datamodule_args)
else:
raise ValueError(
f"experiment.task={task_name} is not recognized as a valid task"
)
######################################
# Setup trainer
######################################
tb_logger = pl_loggers.TensorBoardLogger(conf.program.log_dir, name=experiment_name)
csv_logger = pl_loggers.CSVLogger(conf.program.log_dir, name=experiment_name)
if isinstance(task, ObjectDetectionTask):
monitor_metric = "val_map"
mode = "max"
else:
monitor_metric = "val_loss"
mode = "min"
checkpoint_callback = ModelCheckpoint(
monitor=monitor_metric,
filename="checkpoint-epoch{epoch:02d}-val_loss{val_loss:.2f}",
dirpath=experiment_dir,
save_top_k=1,
save_last=True,
)
early_stopping_callback = EarlyStopping(
monitor=monitor_metric, min_delta=0.00, patience=18, mode=mode
)
trainer_args = cast(Dict[str, Any], OmegaConf.to_object(conf.trainer))
trainer_args["callbacks"] = [checkpoint_callback, early_stopping_callback]
trainer_args["logger"] = [tb_logger, csv_logger]
trainer_args["default_root_dir"] = experiment_dir
trainer = pl.Trainer(**trainer_args)
if trainer_args.get("auto_lr_find"):
trainer.tune(model=task, datamodule=datamodule)
######################################
# Run experiment
######################################
trainer.fit(model=task, datamodule=datamodule)
trainer.test(ckpt_path="best", datamodule=datamodule)
if __name__ == "__main__":
# Taken from https://github.com/pangeo-data/cog-best-practices
_rasterio_best_practices = {
"GDAL_DISABLE_READDIR_ON_OPEN": "EMPTY_DIR",
"AWS_NO_SIGN_REQUEST": "YES",
"GDAL_MAX_RAW_BLOCK_CACHE_SIZE": "200000000",
"GDAL_SWATH_SIZE": "200000000",
"VSI_CURL_CACHE_SIZE": "200000000",
}
os.environ.update(_rasterio_best_practices)
conf = set_up_omegaconf()
# Set random seed for reproducibility
# https://pytorch-lightning.readthedocs.io/en/latest/api/pytorch_lightning.utilities.seed.html#pytorch_lightning.utilities.seed.seed_everything
pl.seed_everything(conf.program.seed)
# Main training procedure
main(conf)