forked from torch-points3d/torch-points3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
255 lines (202 loc) · 7.89 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import os
import torch
import hydra
import time
import logging
from omegaconf import OmegaConf
# Import building function for model and dataset
from torch_points3d.datasets.dataset_factory import instantiate_dataset
from torch_points3d.models.model_factory import instantiate_model
# Import BaseModel / BaseDataset for type checking
from torch_points3d.models.base_model import BaseModel
from torch_points3d.datasets.base_dataset import BaseDataset
# Import from metrics
from torch_points3d.metrics.base_tracker import BaseTracker
from torch_points3d.metrics.colored_tqdm import Coloredtqdm as Ctq
from torch_points3d.metrics.model_checkpoint import ModelCheckpoint
# Utils import
from torch_points3d.utils.colors import COLORS
from torch_points3d.utils.config import launch_wandb
from torch_points3d.visualization import Visualizer
log = logging.getLogger(__name__)
def train_epoch(
epoch: int,
model: BaseModel,
dataset,
device: str,
tracker: BaseTracker,
checkpoint: ModelCheckpoint,
visualizer: Visualizer,
debugging,
):
early_break = getattr(debugging, "early_break", False)
profiling = getattr(debugging, "profiling", False)
model.train()
tracker.reset("train")
visualizer.reset(epoch, "train")
train_loader = dataset.train_dataloader
iter_data_time = time.time()
with Ctq(train_loader) as tq_train_loader:
for i, data in enumerate(tq_train_loader):
t_data = time.time() - iter_data_time
iter_start_time = time.time()
model.set_input(data, device)
model.optimize_parameters(epoch, dataset.batch_size)
if i % 10 == 0:
tracker.track(model)
tq_train_loader.set_postfix(
**tracker.get_metrics(),
data_loading=float(t_data),
iteration=float(time.time() - iter_start_time),
color=COLORS.TRAIN_COLOR
)
if visualizer.is_active:
visualizer.save_visuals(model.get_current_visuals())
iter_data_time = time.time()
if early_break:
break
if profiling:
if i > getattr(debugging, "num_batches", 50):
return 0
tracker.finalise()
metrics = tracker.publish(epoch)
checkpoint.save_best_models_under_current_metrics(model, metrics, tracker.metric_func)
log.info("Learning rate = %f" % model.learning_rate)
def eval_epoch(
epoch: int,
model: BaseModel,
dataset,
device,
tracker: BaseTracker,
checkpoint: ModelCheckpoint,
visualizer: Visualizer,
debugging,
):
early_break = getattr(debugging, "early_break", False)
model.eval()
tracker.reset("val")
visualizer.reset(epoch, "val")
loader = dataset.val_dataloader
with Ctq(loader) as tq_val_loader:
for data in tq_val_loader:
with torch.no_grad():
model.set_input(data, device)
model.forward()
tracker.track(model)
tq_val_loader.set_postfix(**tracker.get_metrics(), color=COLORS.VAL_COLOR)
if visualizer.is_active:
visualizer.save_visuals(model.get_current_visuals())
if early_break:
break
tracker.finalise()
metrics = tracker.publish(epoch)
tracker.print_summary()
checkpoint.save_best_models_under_current_metrics(model, metrics, tracker.metric_func)
def test_epoch(
epoch: int,
model: BaseModel,
dataset,
device,
tracker: BaseTracker,
checkpoint: ModelCheckpoint,
visualizer: Visualizer,
debugging,
):
early_break = getattr(debugging, "early_break", False)
model.eval()
loaders = dataset.test_dataloaders
for loader in loaders:
stage_name = loader.dataset.name
tracker.reset(stage_name)
visualizer.reset(epoch, stage_name)
with Ctq(loader) as tq_test_loader:
for data in tq_test_loader:
with torch.no_grad():
model.set_input(data, device)
model.forward()
tracker.track(model)
tq_test_loader.set_postfix(**tracker.get_metrics(), color=COLORS.TEST_COLOR)
if visualizer.is_active:
visualizer.save_visuals(model.get_current_visuals())
if early_break:
break
tracker.finalise()
metrics = tracker.publish(epoch)
tracker.print_summary()
checkpoint.save_best_models_under_current_metrics(model, metrics, tracker.metric_func)
def run(
cfg, model, dataset: BaseDataset, device, tracker: BaseTracker, checkpoint: ModelCheckpoint, visualizer: Visualizer
):
profiling = getattr(cfg.debugging, "profiling", False)
for epoch in range(checkpoint.start_epoch, cfg.training.epochs):
log.info("EPOCH %i / %i", epoch, cfg.training.epochs)
train_epoch(epoch, model, dataset, device, tracker, checkpoint, visualizer, cfg.debugging)
if profiling:
return 0
if dataset.has_val_loader:
eval_epoch(epoch, model, dataset, device, tracker, checkpoint, visualizer, cfg.debugging)
if dataset.has_test_loaders:
test_epoch(epoch, model, dataset, device, tracker, checkpoint, visualizer, cfg.debugging)
# Single test evaluation in resume case
if checkpoint.start_epoch > cfg.training.epochs:
if dataset.has_test_loaders:
test_epoch(epoch, model, dataset, device, tracker, checkpoint, visualizer, cfg.debugging)
@hydra.main(config_path="conf/config.yaml")
def main(cfg):
OmegaConf.set_struct(cfg, False) # This allows getattr and hasattr methods to function correctly
if cfg.pretty_print:
print(cfg.pretty())
# Get device
device = torch.device("cuda" if (torch.cuda.is_available() and cfg.training.cuda) else "cpu")
log.info("DEVICE : {}".format(device))
# Enable CUDNN BACKEND
torch.backends.cudnn.enabled = cfg.training.enable_cudnn
# Profiling
profiling = getattr(cfg.debugging, "profiling", False)
if profiling:
# Set the num_workers as torch.utils.bottleneck doesn't work well with it
cfg.training.num_workers = 0
# Start Wandb if public
launch_wandb(cfg, cfg.wandb.public and cfg.wandb.log)
# Checkpoint
checkpoint = ModelCheckpoint(
cfg.training.checkpoint_dir,
cfg.model_name,
cfg.training.weight_name,
run_config=cfg,
resume=bool(cfg.training.checkpoint_dir),
)
# Create model and datasets
if not checkpoint.is_empty:
dataset = instantiate_dataset(checkpoint.data_config)
model = checkpoint.create_model(dataset, weight_name=cfg.training.weight_name)
else:
dataset = instantiate_dataset(cfg.data)
model = instantiate_model(cfg, dataset)
model.instantiate_optimizers(cfg)
log.info(model)
model.log_optimizers()
log.info("Model size = %i", sum(param.numel() for param in model.parameters() if param.requires_grad))
# Set dataloaders
dataset.create_dataloaders(
model,
cfg.training.batch_size,
cfg.training.shuffle,
cfg.training.num_workers,
cfg.training.precompute_multi_scale,
)
log.info(dataset)
# Choose selection stage
selection_stage = getattr(cfg, "selection_stage", "")
checkpoint.selection_stage = dataset.resolve_saving_stage(selection_stage)
tracker: BaseTracker = dataset.get_tracker(cfg.wandb.log, cfg.tensorboard.log)
launch_wandb(cfg, not cfg.wandb.public and cfg.wandb.log)
# Run training / evaluation
model = model.to(device)
visualizer = Visualizer(cfg.visualization, dataset.num_batches, dataset.batch_size, os.getcwd())
run(cfg, model, dataset, device, tracker, checkpoint, visualizer)
# https://github.com/facebookresearch/hydra/issues/440
hydra._internal.hydra.GlobalHydra.get_state().clear()
return 0
if __name__ == "__main__":
main()