forked from DeyvidKochanov-TomTom/kprnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_kitti.py
152 lines (129 loc) · 5.18 KB
/
train_kitti.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
import argparse
from pathlib import Path
import torch
import torch.distributed as dist
import torch.nn as nn
from apex import parallel
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm
from datasets import semantic_kitti
from models import deeplab
import utils
parser = argparse.ArgumentParser("Train on semantic kitti")
parser.add_argument("--semantic-kitti-dir", required=True, type=Path)
parser.add_argument("--model-dir", required=True, type=Path)
parser.add_argument("--checkpoint-dir", required=True, type=Path)
args = parser.parse_args()
def run_val(model, val_loader, n_iter, writer):
print("Runnign validation")
model.eval()
loss_fn = nn.CrossEntropyLoss(ignore_index=255)
eval_metric = utils.evaluation.Eval(19, 255)
with torch.no_grad():
average_loss = 0
for step, items in tqdm(enumerate(val_loader)):
images = items["image"].cuda(0, non_blocking=True)
labels = items["labels"].long().cuda(0, non_blocking=True)
py = items["py"].float().cuda(0, non_blocking=True)
px = items["px"].float().cuda(0, non_blocking=True)
pxyz = items["points_xyz"].float().cuda(0, non_blocking=True)
knns = items["knns"].long().cuda(0, non_blocking=True)
predictions = model(images, px, py, pxyz, knns)
loss = loss_fn(predictions, labels)
average_loss += loss.item()
_, predictions_argmax = torch.max(predictions, 1)
eval_metric.update(predictions_argmax.cpu().numpy(), labels.cpu().numpy())
average_loss /= step
miou, ious = eval_metric.getIoU()
print(f"Iteration {n_iter} Average Val Loss: {average_loss}, mIou {miou}")
print(f"Per class Ious {ious}")
writer.add_scalar("val/val", average_loss, n_iter)
writer.add_scalar("val/mIoU", miou, n_iter)
def train(rank):
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.enabled = True
dist.init_process_group(
backend="nccl", init_method="tcp://localhost:34567", world_size=8, rank=rank
)
dist.barrier()
model = deeplab.resnext101_aspp_kp(19)
torch.cuda.set_device(rank)
if rank == 0:
writer = SummaryWriter(log_dir=args.checkpoint_dir, flush_secs=20)
model = parallel.convert_syncbn_model(model)
model.cuda(rank)
model.load_state_dict(
torch.load(
args.model_dir / "resnext_cityscapes_2p.pth", map_location=f"cuda:{rank}"
),
strict=False,
)
dist.barrier()
if rank == 0:
print(model.parameters)
model = parallel.DistributedDataParallel(model)
train_dataset = semantic_kitti.SemanticKitti(
args.semantic_kitti_dir / "dataset/sequences", "train",
)
train_sampler = utils.dist_utils.TrainingSampler(train_dataset)
train_loader = torch.utils.data.DataLoader(
train_dataset,
batch_size=3,
num_workers=8,
drop_last=True,
shuffle=False,
pin_memory=True,
sampler=train_sampler,
)
val_loader = torch.utils.data.DataLoader(
dataset=semantic_kitti.SemanticKitti(
args.semantic_kitti_dir / "dataset/sequences", "val",
),
batch_size=1,
shuffle=False,
num_workers=4,
drop_last=False,
)
loss_fn = utils.ohem.OhemCrossEntropy(ignore_index=255, thresh=0.9, min_kept=10000)
optimizer = torch.optim.SGD(
model.parameters(), lr=0.00001, momentum=0.9, weight_decay=1e-4
)
scheduler = utils.cosine_schedule.CosineAnnealingWarmUpRestarts(
optimizer, T_0=96000, T_mult=10, eta_max=0.01875, T_up=1000, gamma=0.5
)
n_iter = 0
for epoch in range(120):
print("starting epoch ", epoch)
model.train()
for step, items in enumerate(train_loader):
images = items["image"].cuda(rank, non_blocking=True)
labels = items["labels"].long().cuda(rank, non_blocking=True)
py = items["py"].float().cuda(rank, non_blocking=True)
px = items["px"].float().cuda(rank, non_blocking=True)
pxyz = items["points_xyz"].float().cuda(rank, non_blocking=True)
knns = items["knns"].long().cuda(rank, non_blocking=True)
predictions = model(images, px, py, pxyz, knns)
loss = loss_fn(predictions, labels)
optimizer.zero_grad()
loss.backward()
nn.utils.clip_grad_norm_(model.parameters(), 3.0)
optimizer.step()
if rank == 0:
print(
f"Epoch: {epoch} Iteration: {step} / {len(train_loader)} Loss: {loss.item()}"
)
writer.add_scalar("loss/train", loss.item(), n_iter)
writer.add_scalar("lr", optimizer.param_groups[0]["lr"], n_iter)
n_iter += 1
scheduler.step()
if rank == 0:
if (epoch + 1) % 5 == 0:
run_val(model, val_loader, n_iter, writer)
torch.save(
model.module.state_dict(), args.checkpoint_dir / f"epoch{epoch}.pth"
)
def main() -> None:
ngpus = torch.cuda.device_count()
torch.multiprocessing.spawn(train, nprocs=ngpus)
if __name__ == "__main__":
main()