-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtrain.py
More file actions
executable file
·259 lines (247 loc) · 8.69 KB
/
train.py
File metadata and controls
executable file
·259 lines (247 loc) · 8.69 KB
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
import logging
import os
import resource
from argparse import REMAINDER, ArgumentParser
from datetime import datetime
from typing import Any, Dict
import torch.nn
from pytorch_lightning import LightningModule, Trainer, seed_everything
from pytorch_lightning.callbacks import (
LearningRateMonitor,
ModelCheckpoint,
TQDMProgressBar,
)
from pytorch_lightning.loggers import WandbLogger
from torch.utils.data import DataLoader
from yacs.config import CfgNode
import cups
from cups.augmentation import (
CopyPasteAugmentation,
PhotometricAugmentations,
ResolutionJitter,
get_pseudo_label_augmentations,
)
from cups.data import (
CITYSCAPES_CLASSNAMES,
CITYSCAPES_CLASSNAMES_19,
CITYSCAPES_STUFF_CLASSES,
CITYSCAPES_STUFF_CLASSES_19,
CITYSCAPES_THING_CLASSES,
CITYSCAPES_THING_CLASSES_19,
CityscapesPanopticValidation,
KITTIPanopticValidation,
PseudoLabelDataset,
StepDataset,
collate_function_validation,
)
from cups.utils import RTPTCallback
rlimit = resource.getrlimit(resource.RLIMIT_NOFILE)
resource.setrlimit(resource.RLIMIT_NOFILE, (16384, rlimit[1]))
logging.basicConfig(format="%(message)s")
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
torch.set_float32_matmul_precision("medium")
def configure() -> CfgNode:
"""Function loads default config, experiment config, and parses command line arguments.
Returns:
config (CfgNode): Config object.
"""
# Manage command line arguments
parser = ArgumentParser()
parser.add_argument(
"--cuda_visible_devices",
default=None,
type=str,
help="Sets the visible cuda devices.",
)
parser.add_argument(
"config",
help="Modify config options using the command-line",
default=None,
nargs=REMAINDER,
)
parser.add_argument(
"--disable_wandb",
default=False,
action="store_true",
help="Binary flag. If set run will not be tracked with Weights and Biases.",
)
parser.add_argument("--experiment_config_file", default=None, type=str, help="Path to experiment config file.")
# Get arguments
args = parser.parse_args()
# Arguments to dict
args_dict: Dict[str, Any] = vars(args)
# Set cuda devices
if (cuda_visible_devices := args_dict.pop("cuda_visible_devices")) is not None:
os.environ["CUDA_VISIBLE_DEVICES"] = cuda_visible_devices
# Disable Weights and Biases
if args_dict.pop("disable_wandb"):
os.environ["WANDB_MODE"] = "disabled"
# Get path to experiment config file
experiment_config_file = args_dict.pop("experiment_config_file")
# Load config
config: CfgNode = cups.get_default_config(
experiment_config_file=experiment_config_file, command_line_arguments=args.config
)
return config
def main() -> None:
# Get config
config: CfgNode = configure()
# Print config
log.info(config)
# Set seed
seed_everything(config.SYSTEM.SEED)
# Make datasets
training_dataset = PseudoLabelDataset(
root=config.DATA.ROOT,
root_pseudo=config.DATA.ROOT_PSEUDO,
return_detectron2_format=True,
ground_truth_scale=config.DATA.SCALE,
crop_resolution=config.DATA.CROP_RESOLUTION,
thing_stuff_threshold=config.DATA.THING_STUFF_THRESHOLD,
ignore_unknown_thing_regions=config.DATA.IGNORE_UNKNOWN_THING_REGIONS,
augmentations=get_pseudo_label_augmentations(config.DATA.CROP_RESOLUTION),
dataset=config.DATA.DATASET,
only_use_non_empty_samples=True,
)
# Init validation set
if config.DATA.DATASET == "cityscapes":
validation_dataset = CityscapesPanopticValidation(
root=config.DATA.ROOT_VAL,
crop_resolution=(512, 1024), # For efficiency, we validate on half of the original resolution
num_classes=27,
resize_scale=0.5,
)
# Get set of classes in dataset
thing_classes = CITYSCAPES_THING_CLASSES
stuff_classes = CITYSCAPES_STUFF_CLASSES
class_names = CITYSCAPES_CLASSNAMES
else:
validation_dataset = KITTIPanopticValidation( # type: ignore
root=config.DATA.ROOT_VAL,
crop_resolution=(368, 1240),
num_classes=19,
resize_scale=1.0,
)
# Get set of classes in dataset
thing_classes = CITYSCAPES_THING_CLASSES_19
stuff_classes = CITYSCAPES_STUFF_CLASSES_19
class_names = CITYSCAPES_CLASSNAMES_19
# Print dataset length
log.info(f"{len(training_dataset)} training samples and {len(validation_dataset)} validation samples detected.")
# Make data loaders
training_data_loader = DataLoader(
dataset=StepDataset(
training_dataset, steps=config.TRAINING.STEPS * config.SYSTEM.NUM_GPUS * config.TRAINING.BATCH_SIZE
),
batch_size=config.TRAINING.BATCH_SIZE,
shuffle=True,
num_workers=config.SYSTEM.NUM_WORKERS,
collate_fn=lambda x: x,
drop_last=True,
pin_memory=False,
persistent_workers=False,
prefetch_factor=6,
)
validation_data_loader = DataLoader(
dataset=validation_dataset,
batch_size=4,
shuffle=False,
num_workers=config.SYSTEM.NUM_WORKERS,
collate_fn=collate_function_validation,
drop_last=False,
pin_memory=False,
)
# Init model
model: LightningModule = cups.build_model_pseudo(
config=config,
thing_classes=thing_classes,
stuff_classes=stuff_classes,
thing_pseudo_classes=training_dataset.things_classes,
stuff_pseudo_classes=training_dataset.stuff_classes,
class_weights=(
tuple(
(
1.0 / (torch.tensor(training_dataset.class_distribution) * len(training_dataset.class_distribution))
).tolist()
)
if config.TRAINING.CLASS_WEIGHTING
else None
),
copy_paste_augmentation=(
CopyPasteAugmentation(
thing_class=len(training_dataset.stuff_classes),
max_num_pasted_objects=config.AUGMENTATION.MAX_NUM_PASTED_OBJECTS,
)
if config.AUGMENTATION.COPY_PASTE
else None
),
photometric_augmentation=PhotometricAugmentations(),
resolution_jitter_augmentation=ResolutionJitter(
scales=None,
resolutions=config.AUGMENTATION.RESOLUTIONS,
),
class_names=class_names,
)
# Print model
log.info(model)
# Init experiments folder since W&B otherwise warns and uses temp
os.makedirs(
os.path.join(os.getcwd() if config.SYSTEM.LOG_PATH is None else config.SYSTEM.LOG_PATH, "experiments"),
exist_ok=True,
)
if config.SYSTEM.RUN_NAME is not None:
run_name = config.SYSTEM.RUN_NAME
else:
run_name = "pseudo_" + datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
experiment_path = os.path.join(
os.getcwd() if config.SYSTEM.LOG_PATH is None else config.SYSTEM.LOG_PATH,
"experiments",
run_name,
)
os.makedirs(experiment_path, exist_ok=True)
# Init logger
logger = WandbLogger(
name=run_name,
log_model=False,
save_dir=experiment_path,
project="Unsupervised Panoptic Segmentation",
entity="oliver_and_christoph",
)
# Init trainer
trainer: Trainer = Trainer(
default_root_dir=experiment_path,
accelerator=config.SYSTEM.ACCELERATOR,
devices=config.SYSTEM.NUM_GPUS,
num_nodes=config.SYSTEM.NUM_NODES,
strategy=config.SYSTEM.DISTRIBUTED_BACKEND,
precision=config.TRAINING.PRECISION,
max_steps=config.TRAINING.STEPS,
min_steps=config.TRAINING.STEPS,
callbacks=[
RTPTCallback(name_initials="CR&OH", experiment_name="UPS"),
TQDMProgressBar(refresh_rate=1),
ModelCheckpoint(
filename="ups_checkpoint_{step:06d}",
every_n_train_steps=config.TRAINING.VAL_EVERY_N_STEPS,
save_last=True,
save_top_k=-1,
),
LearningRateMonitor(logging_interval="step", log_momentum=True),
],
logger=logger,
log_every_n_steps=config.TRAINING.LOG_EVERT_N_STEPS,
gradient_clip_algorithm=config.TRAINING.GRADIENT_CLIP_ALGORITHM,
gradient_clip_val=config.TRAINING.GRADIENT_CLIP_VAL,
check_val_every_n_epoch=None,
val_check_interval=config.TRAINING.VAL_EVERY_N_STEPS,
num_sanity_val_steps=0,
)
# Perform training
trainer.fit(
model=model,
train_dataloaders=training_data_loader,
val_dataloaders=validation_data_loader,
)
if __name__ == "__main__":
main()