-
Notifications
You must be signed in to change notification settings - Fork 0
/
train1.py
283 lines (233 loc) · 10.7 KB
/
train1.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import argparse
from datetime import datetime
import json
import numpy as np
from pathlib import Path
import time
from tqdm import tqdm
import warnings
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.tensorboard import SummaryWriter
from models.RandlaNet_mb import *
from utils.tools import Config as cfg
from utils.metrics import accuracy, intersection_over_union
from data.data import data_loaders
PANDA_TRAIN = r'/home/csgrad/mbhosale/Lidar_Segmentation/pandaset-devkit/data/PandaSet/train/'
PANDA_VALID = r'/home/csgrad/mbhosale/Lidar_Segmentation/pandaset-devkit/data/PandaSet/valid/'
data_name = 's3dis'
# data_name = 'pandaset'
def evaluate(model, loader, criterion, device):
model.eval()
losses = []
accuracies = []
ious = []
with torch.no_grad():
for points, labels in tqdm(loader, desc='Validation', leave=False):
points = points.to(device)
labels = labels.to(device)
if data_name == 'pandaset':
labels = labels.squeeze(-1)
scores = model(points)
loss = criterion(scores, labels)
losses.append(loss.cpu().item())
accuracies.append(accuracy(scores, labels))
ious.append(intersection_over_union(scores, labels))
return np.mean(losses), np.nanmean(np.array(accuracies), axis=0), np.nanmean(np.array(ious), axis=0)
def train(args):
if data_name == 's3dis':
train_path = args.dataset / args.train_dir
val_path = args.dataset / args.val_dir
logs_dir = args.logs_dir / args.name
logs_dir.mkdir(exist_ok=True, parents=True)
# determine number of classes
try:
with open(args.dataset / 'classes.json') as f:
labels = json.load(f)
num_classes = len(labels.keys())
except FileNotFoundError:
num_classes = int(input("Number of distinct classes in the dataset: "))
train_loader, val_loader = data_loaders(
args.dataset,
args.dataset_sampling,
batch_size=args.batch_size,
num_workers=args.num_workers,
pin_memory=True
)
d_in = next(iter(train_loader))[0].size(-1)
model = RandLANet(k=args.neighbors,
d_in=d_in,
num_classes=num_classes,
decimation=args.decimation,
device=args.gpu
)
print('Computing weights...', end='\t')
samples_per_class = np.array(cfg.class_weights)
n_samples = torch.tensor(cfg.class_weights, dtype=torch.float, device=args.gpu)
ratio_samples = n_samples / n_samples.sum()
weights = 1 / (ratio_samples + 0.02)
print('Done.')
print('Weights:', weights)
criterion = nn.CrossEntropyLoss(weight=weights)
optimizer = torch.optim.Adam(model.parameters(), lr=args.adam_lr)
scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, args.scheduler_gamma)
first_epoch = 1
if args.load:
path = max(list((args.logs_dir / args.load).glob('*.pth')))
print(f'Loading {path}...')
checkpoint = torch.load(path)
first_epoch = checkpoint['epoch']+1
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
scheduler.load_state_dict(checkpoint['scheduler_state_dict'])
with SummaryWriter(logs_dir) as writer:
for epoch in range(first_epoch, args.epochs+1):
print(f'=== EPOCH {epoch:d}/{args.epochs:d} ===')
t0 = time.time()
# Train
model.train()
# metrics
losses = []
accuracies = []
ious = []
# iterate over dataset
i = 0
le = len(train_loader)
for points, labels in tqdm(train_loader, desc='Training', leave=False):
points = points.to(args.gpu)
labels = labels.to(args.gpu)
if data_name == 'pandaset':
labels = labels.squeeze(-1)
optimizer.zero_grad()
scores = model(points)
logp = torch.distributions.utils.probs_to_logits(scores, is_binary=False)
loss = criterion(logp, labels)
# logpy = torch.gather(logp, 1, labels)
# loss = -(logpy).mean()
loss.backward()
optimizer.step()
losses.append(loss.cpu().item())
# print(f"Iteration: {i}/{le} Loss: {loss.item()}")
i +=1
accuracies.append(accuracy(scores, labels))
ious.append(intersection_over_union(scores, labels))
scheduler.step()
accs = np.nanmean(np.array(accuracies), axis=0)
ious = np.nanmean(np.array(ious), axis=0)
val_loss, val_accs, val_ious = evaluate(
model,
val_loader,
criterion,
args.gpu
)
loss_dict = {
'Training loss': np.mean(losses),
'Validation loss': val_loss
}
acc_dicts = [
{
'Training accuracy': acc,
'Validation accuracy': val_acc
} for acc, val_acc in zip(accs, val_accs)
]
iou_dicts = [
{
'Training accuracy': iou,
'Validation accuracy': val_iou
} for iou, val_iou in zip(ious, val_ious)
]
t1 = time.time()
d = t1 - t0
# Display results
for k, v in loss_dict.items():
print(f'{k}: {v:.7f}', end='\t')
print()
print('Accuracy ', *[f'{i:>5d}' for i in range(num_classes)], ' OA', sep=' | ')
print('Training: ', *[f'{acc:.3f}' if not np.isnan(acc) else ' nan' for acc in accs], sep=' | ')
print('Validation: ', *[f'{acc:.3f}' if not np.isnan(acc) else ' nan' for acc in val_accs], sep=' | ')
print('IoU ', *[f'{i:>5d}' for i in range(num_classes)], ' mIoU', sep=' | ')
print('Training: ', *[f'{iou:.3f}' if not np.isnan(iou) else ' nan' for iou in ious], sep=' | ')
print('Validation: ', *[f'{iou:.3f}' if not np.isnan(iou) else ' nan' for iou in val_ious], sep=' | ')
print('Time elapsed:', '{:.0f} s'.format(d) if d < 60 else '{:.0f} min {:02.0f} s'.format(*divmod(d, 60)))
# send results to tensorboard
writer.add_scalars('Loss', loss_dict, epoch)
for i in range(num_classes):
writer.add_scalars(f'Per-class accuracy/{i+1:02d}', acc_dicts[i], epoch)
writer.add_scalars(f'Per-class IoU/{i+1:02d}', iou_dicts[i], epoch)
writer.add_scalars('Per-class accuracy/Overall', acc_dicts[-1], epoch)
writer.add_scalars('Per-class IoU/Mean IoU', iou_dicts[-1], epoch)
if epoch % args.save_freq == 0:
torch.save(
dict(
epoch=epoch,
model_state_dict=model.state_dict(),
optimizer_state_dict=optimizer.state_dict(),
scheduler_state_dict=scheduler.state_dict()
),
args.logs_dir / args.name / f'checkpoint_{epoch:02d}.pth'
)
if __name__ == '__main__':
"""Parse program arguments"""
parser = argparse.ArgumentParser(
prog='RandLA-Net',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
base = parser.add_argument_group('Base options')
expr = parser.add_argument_group('Experiment parameters')
param = parser.add_argument_group('Hyperparameters')
dirs = parser.add_argument_group('Storage directories')
misc = parser.add_argument_group('Miscellaneous')
base.add_argument('--dataset', type=Path, help='location of the dataset',
default='/home/csgrad/mbhosale/RandLA-Net-pytorch/datasets/s3dis/subsampled')
expr.add_argument('--epochs', type=int, help='number of epochs',
default=20)
expr.add_argument('--load', type=str, help='model to load',
default='')
param.add_argument('--adam_lr', type=float, help='learning rate of the optimizer',
default=1e-2)
param.add_argument('--batch_size', type=int, help='batch size',
default=1)
param.add_argument('--decimation', type=int, help='ratio the point cloud is divided by at each layer',
default=4)
param.add_argument('--dataset_sampling', type=str, help='how dataset is sampled',
default='active_learning', choices=['active_learning', 'naive'])
param.add_argument('--neighbors', type=int, help='number of neighbors considered by k-NN',
default=16)
param.add_argument('--scheduler_gamma', type=float, help='gamma of the learning rate scheduler',
default=0.95)
dirs.add_argument('--test_dir', type=str, help='location of the test set in the dataset dir',
default='test')
dirs.add_argument('--train_dir', type=str, help='location of the training set in the dataset dir',
default='train')
dirs.add_argument('--val_dir', type=str, help='location of the validation set in the dataset dir',
default='val')
dirs.add_argument('--logs_dir', type=Path, help='path to tensorboard logs',
default='runs')
misc.add_argument('--gpu', type=int, help='which GPU to use (-1 for CPU)',
default=0)
misc.add_argument('--name', type=str, help='name of the experiment',
default=None)
misc.add_argument('--num_workers', type=int, help='number of threads for loading data',
default=0)
misc.add_argument('--save_freq', type=int, help='frequency of saving checkpoints',
default=10)
args = parser.parse_args()
if args.gpu >= 0:
if torch.cuda.is_available():
args.gpu = torch.device(f'cuda:{args.gpu:d}')
else:
warnings.warn('CUDA is not available on your machine. Running the algorithm on CPU.')
args.gpu = torch.device('cpu')
else:
args.gpu = torch.device('cpu')
if args.name is None:
if args.load:
args.name = args.load
else:
args.name = datetime.now().strftime('%Y-%m-%d_%H:%M')
t0 = time.time()
train(args)
t1 = time.time()
d = t1 - t0
print('Done. Time elapsed:', '{:.0f} s.'.format(d) if d < 60 else '{:.0f} min {:.0f} s.'.format(*divmod(d, 60)))