-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
131 lines (88 loc) · 4.74 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
import configargparse
import pathlib
import logging
from typing import List
import yaml
import torch
from torch_geometric import data
from pytorch_lightning import Trainer, seed_everything
from pytorch_lightning.callbacks import ModelCheckpoint, LearningRateMonitor, EarlyStopping
from nn_model import SimpleClsLDGCN, BaseTransform, TrainTransform
from point_cloud_cls import ClassificationModelTrainer, dataset
import train_param
LOGGER = logging.getLogger()
def init(seed: int):
seed_everything(seed)
def load_yaml(path_to_file):
with open(path_to_file, "r", encoding="utf-8") as yml_config:
return yaml.safe_load(yml_config)
def get_model(class_labels: List[str]):
model_params = train_param.ModelParams()
model_params_dict = {"out_chan": len(class_labels), **model_params.__dict__}
model = SimpleClsLDGCN(**model_params_dict)
optim_params = train_param.OptimizerParams()
scheduler_params = train_param.SchedulerParams()
LOGGER.info("Train model on %s features to classify %s classes", model_params.in_chan, len(class_labels))
lighting_model = ClassificationModelTrainer(
model, optim_params, scheduler_params, class_labels)
return lighting_model
def get_train_loader(dataset, batch_size: int, num_workers: int):
return data.DataLoader(dataset, batch_size=batch_size,
pin_memory=True, num_workers=num_workers, shuffle=True)
def get_test_loader(dataset, batch_size: int, num_workers: int):
return data.DataLoader(dataset, batch_size=batch_size,
pin_memory=True, num_workers=num_workers)
def get_train_test_dataset(num_points: int, data_config):
base_transform = BaseTransform(num_points)
train_transform = TrainTransform(num_points, angle_degree=15, axis=2, rnd_shift=0.02)
data_root = data_config["data_root_dir"]
classes = data_config["classes"]
input_archive = data_config["input"]
train_dataset = dataset.SimpleShapes(input_archive, data_root, classes, transform=train_transform, is_train=True)
test_dataset = dataset.SimpleShapes(input_archive, data_root, classes, transform=base_transform, is_train=False)
return train_dataset, test_dataset
def train(args):
exp_dir = pathlib.Path(args.exp_dir)
exp_dir.mkdir(exist_ok=True, parents=True)
check_dir = exp_dir / "checkpoints"
check_dir.mkdir(exist_ok=True)
data_config = load_yaml(args.data_config)
data_params = train_param.DataParams()
train_dataset, test_dataset = get_train_test_dataset(data_params.num_points, data_config)
train_params = train_param.TrainParams()
train_loader = get_train_loader(train_dataset, train_params.batch_size, args.num_workers)
test_loader = get_test_loader(test_dataset, train_params.batch_size, args.num_workers)
model = get_model(train_dataset.label_encoder.classes_)
checkpoint_callback = ModelCheckpoint(monitor="Train/overall_accuracy",
save_top_k=-1,
period=5,
mode="max",
filename="{epoch}")
learning_rate_monitor = LearningRateMonitor(logging_interval="epoch")
early_stopping = EarlyStopping(monitor="Test/overall_accuracy", min_delta=1e-2,
mode="max", verbose=True, strict=False)
gpus = 1
if not torch.cuda.is_available():
LOGGER.warning("CUDA is not available. Fallback to CPU training. It may be very slow")
gpus = None
trainer = Trainer(gpus=gpus,
deterministic=train_params.deterministic,
benchmark=train_params.benchmark,
check_val_every_n_epoch=train_params.valid_every,
default_root_dir=str(exp_dir),
fast_dev_run=args.fast_dev_run,
max_epochs=train_params.epochs,
callbacks=[learning_rate_monitor, checkpoint_callback, early_stopping])
trainer.fit(model, train_dataloader=train_loader, val_dataloaders=test_loader)
def main(args):
init(train_param.SEED)
train(args)
if __name__ == "__main__":
parser = configargparse.ArgumentParser()
parser.add_argument("--config", is_config_file=True, required=True, help="Train config")
parser.add_argument("--exp_dir", required=True, type=str, help="A path to directory with checkpoints and logs")
parser.add_argument("--num_workers", default=2, type=int, help="A number of workers to load data")
parser.add_argument("--data_config", required=True, type=str, help="A path to data config")
parser.add_argument("--fast_dev_run", action="store_true", help="Enable fast dev run for testing purpose")
args = parser.parse_args()
main(args)