-
Notifications
You must be signed in to change notification settings - Fork 29
/
train_clss.py
151 lines (134 loc) · 7.03 KB
/
train_clss.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
import argparse
import numpy as np
import os
import time
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from models.pointnet2_cls import pointnet2_cls_ssg, pointnet2_cls_msg, cls_loss
from data.ModelNet40 import ModelNet40
def train_one_epoch(train_loader, model, loss_func, optimizer, device):
losses, total_seen, total_correct = [], 0, 0
for data, labels in train_loader:
optimizer.zero_grad() # Important
labels = labels.to(device)
xyz, points = data[:, :, :3], data[:, :, 3:]
pred = model(xyz.to(device), points.to(device))
loss = loss_func(pred, labels)
loss.backward()
optimizer.step()
pred = torch.max(pred, dim=-1)[1]
total_correct += torch.sum(pred == labels)
total_seen += xyz.shape[0]
losses.append(loss.item())
return np.mean(losses), total_correct, total_seen, total_correct / float(total_seen)
def test_one_epoch(test_loader, model, loss_func, device):
losses, total_seen, total_correct = [], 0, 0
for data, labels in test_loader:
labels = labels.to(device)
xyz, points = data[:, :, :3], data[:, :, 3:]
with torch.no_grad():
pred = model(xyz.to(device), points.to(device))
loss = loss_func(pred, labels)
pred = torch.max(pred, dim=-1)[1]
total_correct += torch.sum(pred == labels)
total_seen += xyz.shape[0]
losses.append(loss.item())
return np.mean(losses), total_correct, total_seen, total_correct / float(total_seen)
def train(train_loader, test_loader, model, loss_func, optimizer, scheduler, device, ngpus, nepoches, log_interval, log_dir, checkpoint_interval):
if not os.path.exists(log_dir):
os.makedirs(log_dir)
checkpoint_dir = os.path.join(log_dir, 'checkpoints')
if not os.path.exists(checkpoint_dir):
os.makedirs(checkpoint_dir)
tensorboard_dir = os.path.join(log_dir, 'tensorboard')
if not os.path.exists(tensorboard_dir):
os.makedirs(tensorboard_dir)
writer = SummaryWriter(tensorboard_dir)
for epoch in range(nepoches):
if epoch % checkpoint_interval == 0:
print('='*40)
if ngpus > 1:
torch.save(model.module.state_dict(), os.path.join(checkpoint_dir, "pointnet2_cls_%d.pth" % epoch))
else:
torch.save(model.state_dict(), os.path.join(checkpoint_dir, "pointnet2_cls_%d.pth" % epoch))
model.eval()
lr = optimizer.state_dict()['param_groups'][0]['lr']
loss, total_correct, total_seen, acc = test_one_epoch(test_loader, model, loss_func, device)
print('Test Epoch: {} / {}, lr: {:.6f}, Loss: {:.2f}, Corr: {}, Total: {}, Acc: {:.4f}'.format(epoch, nepoches, lr, loss, total_correct, total_seen, acc))
writer.add_scalar('test loss', loss, epoch)
writer.add_scalar('test acc', acc, epoch)
model.train()
loss, total_correct, total_seen, acc = train_one_epoch(train_loader, model, loss_func, optimizer, device)
writer.add_scalar('train loss', loss, epoch)
writer.add_scalar('train acc', acc, epoch)
if epoch % log_interval == 0:
lr = optimizer.state_dict()['param_groups'][0]['lr']
print('Train Epoch: {} / {}, lr: {:.6f}, Loss: {:.2f}, Corr: {}, Total: {}, Acc: {:.4f}'.format(epoch, nepoches, lr, loss, total_correct, total_seen, acc))
scheduler.step()
if __name__ == '__main__':
Models = {
'pointnet2_cls_ssg': pointnet2_cls_ssg,
'pointnet2_cls_msg': pointnet2_cls_msg
}
parser = argparse.ArgumentParser()
parser.add_argument('--data_root', type=str, required=True, help='Root to the dataset')
parser.add_argument('--batch_size', type=int, default=32, help='Batch size')
parser.add_argument('--npoints', type=int, default=1024, help='Number of the training points')
parser.add_argument('--nclasses', type=int, default=40, help='Number of classes')
parser.add_argument('--augment', type=bool, default=False, help='Augment the train data')
parser.add_argument('--dp', type=bool, default=False, help='Random input dropout during training')
parser.add_argument('--model', type=str, default='pointnet2_cls_ssg', help='Model name')
parser.add_argument('--gpus', type=str, default='0', help='Cuda ids')
parser.add_argument('--lr', type=float, default=0.001, help='Initial learing rate')
parser.add_argument('--decay_rate', type=float, default=1e-4, help='Initial learing rate')
parser.add_argument('--nepoches', type=int, default=251, help='Number of traing epoches')
parser.add_argument('--step_size', type=int, default=20, help='StepLR step size')
parser.add_argument('--gamma', type=float, default=0.7, help='StepLR gamma')
parser.add_argument('--log_interval', type=int, default=10, help='Print iterval')
parser.add_argument('--log_dir', type=str, required=True, help='Train/val loss and accuracy logs')
parser.add_argument('--checkpoint_interval', type=int, default=10, help='Checkpoint saved interval')
args = parser.parse_args()
print(args)
device_ids = list(map(int, args.gpus.strip().split(','))) if ',' in args.gpus else [int(args.gpus)]
ngpus = len(device_ids)
modelnet40_train = ModelNet40(data_root=args.data_root, split='train', npoints=args.npoints, augment=args.augment, dp=args.dp)
modelnet40_test = ModelNet40(data_root=args.data_root, split='test', npoints=args.npoints)
train_loader = DataLoader(dataset=modelnet40_train, batch_size=args.batch_size // ngpus, shuffle=True, num_workers=4)
test_loader = DataLoader(dataset=modelnet40_test, batch_size=args.batch_size // ngpus, shuffle=False, num_workers=4)
print('Train set: {}'.format(len(modelnet40_train)))
print('Test set: {}'.format(len(modelnet40_test)))
Model = Models[args.model]
model = Model(6, args.nclasses)
# Mutli-gpus
device = torch.device("cuda:{}".format(device_ids[0]) if torch.cuda.is_available() else "cpu")
if ngpus > 1 and torch.cuda.device_count() > 1:
model = nn.DataParallel(model, device_ids=device_ids)
model = model.to(device)
loss = cls_loss().to(device)
#optimizer = torch.optim.SGD(model.parameters(), lr=args.init_lr, momentum=args.momentum)
optimizer = torch.optim.Adam(
model.parameters(),
lr=args.lr,
betas=(0.9, 0.999),
eps=1e-08,
weight_decay=args.decay_rate
)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=args.step_size, gamma=0.7)
tic = time.time()
train(train_loader=train_loader,
test_loader=test_loader,
model=model,
loss_func=loss,
optimizer=optimizer,
scheduler=scheduler,
device=device,
ngpus=ngpus,
nepoches=args.nepoches,
log_interval=args.log_interval,
log_dir=args.log_dir,
checkpoint_interval=args.checkpoint_interval,
)
toc = time.time()
print('Training completed, {:.2f} minutes'.format((toc - tic) / 60))