forked from meder411/PointNet-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_classifier.py
166 lines (123 loc) · 4 KB
/
train_classifier.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
import torch
import torch.nn as nn
import torch.autograd as grad
import torch.nn.functional as F
from torch.utils.data import DataLoader
import numpy as np
import time
import os.path as osp
import os
from dataloader import ModelNet40
from models.pointnet_classifier import PointNetClassifier
def main():
num_points = 2000
dims = 3
batch_size = 32
num_epochs = 60
lr = 0.001
printout = 20
reg_weight = 0.001
dataset_root_path = 'data/ModelNet40/'
snapshot = 10
snapshot_dir = 'snapshots'
try:
os.mkdir(snapshot_dir)
except:
pass
# Instantiate a dataset loader
model_net = ModelNet40(dataset_root_path)
data_loader = DataLoader(model_net, batch_size=batch_size,
shuffle=True, num_workers=12)
gt_key = model_net.get_gt_key()
# Instantiate the network
classifier = PointNetClassifier(num_points, dims).train().cuda().double()
loss = nn.CrossEntropyLoss()
regularization = nn.MSELoss()
optimizer = torch.optim.Adam(classifier.parameters(), lr=lr)
scheduler = torch.optim.lr_scheduler.StepLR(
optimizer, step_size=20, gamma=0.5)
# Identity matrix for enforcing orthogonality of second transform
identity = grad.Variable(torch.eye(64).double().cuda(),
requires_grad=False)
# Some timers and a counter
forward_time = 0.
backprop_time = 0.
network_time = 0.
batch_counter = 0
# Whether to save a snapshot
save = False
print 'Starting training...\n'
# Run through all epochs
for ep in xrange(num_epochs):
if ep % snapshot == 0 and ep != 0:
save = True
# Update the optimizer according to the learning rate schedule
scheduler.step()
for i, sample in enumerate(data_loader):
# Parse loaded data
points = grad.Variable(sample[0]).cuda()
target = grad.Variable(sample[1]).cuda()
# Record starting time
start_time = time.time()
# Zero out the gradients
optimizer.zero_grad()
# Forward pass
pred, T2 = classifier(points)
# Compute forward pass time
forward_finish = time.time()
forward_time += forward_finish - start_time
# Compute cross entropy loss
pred_error = loss(pred, target)
# Also enforce orthogonality in the embedded transform
reg_error = regularization(
torch.bmm(T2, T2.permute(0,2,1)),
identity.expand(T2.shape[0], -1, -1))
# Total error is the weighted sum of the prediction error and the
# regularization error
total_error = pred_error + reg_weight * reg_error
# Backpropagate
total_error.backward()
# Update the weights
optimizer.step()
# Compute backprop time
backprop_finish = time.time()
backprop_time += backprop_finish - forward_finish
# Compute network time
network_finish = time.time()
network_time += network_finish - start_time
# Increment batch counter
batch_counter += 1
#------------------------------------------------------------------
# Print feedback
#------------------------------------------------------------------
if (i+1) % printout == 0:
# Print progress
print 'Epoch {}/{}'.format(ep+1, num_epochs)
print 'Batches {}-{}/{} (BS = {})'.format(i-printout+1, i,
len(model_net) / batch_size, batch_size)
print 'PointClouds Seen: {}'.format(
ep * len(model_net) + (i+1) * batch_size)
# Print network speed
print '{:16}[ {:12}{:12} ]'.format('Total Time', 'Forward', 'Backprop')
print ' {:<14.3f}[ {:<10.3f} {:<10.3f} ]' \
.format(network_time, forward_time, backprop_time)
# Print current error
print '{:16}[ {:12}{:12} ]'.format('Total Error',
'Pred Error', 'Reg Error')
print ' {:<14.4f}[ {:<10.4f} {:<10.4f} ]'.format(
total_error.data[0], pred_error.data[0], reg_error.data[0])
print '\n'
# Reset timers
forward_time = 0.
backprop_time = 0.
network_time = 0.
if save:
print 'Saving model snapshot...'
save_model(classifier, snapshot_dir, ep)
save = False
def save_model(model, snapshot_dir, ep):
save_path = osp.join(snapshot_dir, 'snapshot{}.params' \
.format(ep))
torch.save(model.state_dict(), save_path)
if __name__ == '__main__':
main()