forked from vinits5/PointNetLK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_classifier.py
234 lines (189 loc) · 8.28 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
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
import argparse
import os
import sys
import logging
import numpy
import numpy as np
import data
import torch
import torch.utils.data
import torchvision
from torch.utils.data import DataLoader
from tensorboardX import SummaryWriter
from tqdm import tqdm
from model import PointNet_classifier, PointNet_features
def _init_(args):
if not os.path.exists('checkpoints'):
os.makedirs('checkpoints')
if not os.path.exists('checkpoints/' + args.exp_name):
os.makedirs('checkpoints/' + args.exp_name)
if not os.path.exists('checkpoints/' + args.exp_name + '/' + 'models'):
os.makedirs('checkpoints/' + args.exp_name + '/' + 'models')
os.system('cp main.py checkpoints' + '/' + args.exp_name + '/' + 'main.py.backup')
os.system('cp model.py checkpoints' + '/' + args.exp_name + '/' + 'model.py.backup')
class IOStream:
def __init__(self, path):
self.f = open(path, 'a')
def cprint(self, text):
print(text)
self.f.write(text + '\n')
self.f.flush()
def close(self):
self.f.close()
def test_one_epoch(device, model, test_loader):
model.eval()
test_loss = 0.0
pred = 0.0
count = 0
for i, data in enumerate(tqdm(test_loader)):
points, target = data
points = points.to(device)
target = target.to(device)
output = model(points)
loss_val = model.loss(output, target)
test_loss += loss_val.item()
count += output.size(0)
_, pred1 = output.max(dim=1)
ag = (pred1 == target)
am = ag.sum()
pred += am.item()
test_loss = float(test_loss)/count
accuracy = float(pred)/count
return test_loss, accuracy
def test(args, model, test_loader, textio):
test_loss, test_accuracy = test_one_epoch(args.device, model, test_loader)
textio.cprint('Validation Loss: %f & Validation Accuracy: %f'%(test_loss, test_accuracy))
def train_one_epoch(device, model, train_loader, optimizer):
model.train()
train_loss = 0.0
pred = 0.0
count = 0
for i, data in enumerate(tqdm(train_loader)):
points, target = data
points = points.to(device)
target = target.to(device)
output = model(points)
loss_val = model.loss(output, target)
# print(loss_val.item())
# forward + backward + optimize
optimizer.zero_grad()
loss_val.backward()
optimizer.step()
train_loss += loss_val.item()
count += output.size(0)
_, pred1 = output.max(dim=1)
ag = (pred1 == target)
am = ag.sum()
pred += am.item()
train_loss = float(train_loss)/count
accuracy = float(pred)/count
return train_loss, accuracy
def train(args, model, train_loader, test_loader, boardio, textio, checkpoint):
learnable_params = filter(lambda p: p.requires_grad, model.parameters())
if args.optimizer == 'Adam':
optimizer = torch.optim.Adam(learnable_params)
else:
optimizer = torch.optim.SGD(learnable_params, lr=0.1)
if checkpoint is not None:
min_loss = checkpoint['min_loss']
optimizer.load_state_dict(checkpoint['optimizer'])
best_test_loss = np.inf
for epoch in range(args.start_epoch, args.epochs):
train_loss, train_accuracy = train_one_epoch(args.device, model, train_loader, optimizer)
test_loss, test_accuracy = test_one_epoch(args.device, model, test_loader)
if test_loss<best_test_loss:
best_test_loss = test_loss
snap = {'epoch': epoch + 1,
'model': model.state_dict(),
'min_loss': best_test_loss,
'optimizer' : optimizer.state_dict(),}
torch.save(snap, 'checkpoints/%s/models/best_model_snap.t7' % (args.exp_name))
torch.save(model.state_dict(), 'checkpoints/%s/models/best_model.t7' % (args.exp_name))
torch.save(model.ptnet.state_dict(), 'checkpoints/%s/models/best_ptnet_model.t7' % (args.exp_name))
torch.save(snap, 'checkpoints/%s/models/model_snap.t7' % (args.exp_name))
torch.save(model.state_dict(), 'checkpoints/%s/models/model.t7' % (args.exp_name))
torch.save(model.ptnet.state_dict(), 'checkpoints/%s/models/ptnet_model.t7' % (args.exp_name))
boardio.add_scalar('Train Loss', train_loss, epoch+1)
boardio.add_scalar('Test Loss', test_loss, epoch+1)
boardio.add_scalar('Best Test Loss', best_test_loss, epoch+1)
boardio.add_scalar('Train Accuracy', train_accuracy, epoch+1)
boardio.add_scalar('Test Accuracy', test_accuracy, epoch+1)
textio.cprint('EPOCH:: %d, Traininig Loss: %f, Testing Loss: %f, Best Loss: %f'%(epoch+1, train_loss, test_loss, best_test_loss))
textio.cprint('EPOCH:: %d, Traininig Accuracy: %f, Testing Accuracy: %f'%(epoch+1, train_accuracy, test_accuracy))
def options():
parser = argparse.ArgumentParser(description='Point Cloud Registration')
parser.add_argument('--exp_name', type=str, default='exp_classifier_debug', metavar='N',
help='Name of the experiment')
parser.add_argument('--dataset_path', type=str, default='ModelNet40',
metavar='PATH', help='path to the input dataset') # like '/path/to/ModelNet40'
parser.add_argument('-c', '--categoryfile', type=str, default='./sampledata/modelnet40_half1.txt',
metavar='PATH', help='path to the categories to be trained') # eg. './sampledata/modelnet40_half1.txt'
parser.add_argument('--eval', type=bool, default=False, help='Train or Evaluate the network.')
# settings for input data
parser.add_argument('--dataset_type', default='modelnet', choices=['modelnet', 'shapenet2'],
metavar='DATASET', help='dataset type (default: modelnet)')
parser.add_argument('--num_points', default=1024, type=int,
metavar='N', help='points in point-cloud (default: 1024)')
# settings for PointNet
parser.add_argument('--pointnet', default='tune', type=str, choices=['fixed', 'tune'],
help='train pointnet (default: tune)')
parser.add_argument('--emb_dims', default=1024, type=int,
metavar='K', help='dim. of the feature vector (default: 1024)')
parser.add_argument('--symfn', default='max', choices=['max', 'avg'],
help='symmetric function (default: max)')
# settings for on training
parser.add_argument('--seed', type=int, default=1234)
parser.add_argument('-j', '--workers', default=4, type=int,
metavar='N', help='number of data loading workers (default: 4)')
parser.add_argument('-b', '--batch_size', default=32, type=int,
metavar='N', help='mini-batch size (default: 32)')
parser.add_argument('--epochs', default=200, type=int,
metavar='N', help='number of total epochs to run')
parser.add_argument('--start_epoch', default=0, type=int,
metavar='N', help='manual epoch number (useful on restarts)')
parser.add_argument('--optimizer', default='Adam', choices=['Adam', 'SGD'],
metavar='METHOD', help='name of an optimizer (default: Adam)')
parser.add_argument('--resume', default='', type=str,
metavar='PATH', help='path to latest checkpoint (default: null (no-use))')
parser.add_argument('--pretrained', default='', type=str,
metavar='PATH', help='path to pretrained model file (default: null (no-use))')
parser.add_argument('--device', default='cuda:0', type=str,
metavar='DEVICE', help='use CUDA if available')
args = parser.parse_args()
return args
def main():
args = options()
args.dataset_path = os.path.join(os.getcwd(), os.pardir, os.pardir, 'ModelNet40', 'ModelNet40')
torch.backends.cudnn.deterministic = True
torch.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
np.random.seed(args.seed)
boardio = SummaryWriter(log_dir='checkpoints/' + args.exp_name)
_init_(args)
textio = IOStream('checkpoints/' + args.exp_name + '/run.log')
textio.cprint(str(args))
trainset, testset = data.get_classification_datasets(args)
train_loader = DataLoader(trainset, batch_size=args.batch_size, shuffle=True, drop_last=True, num_workers=args.workers)
test_loader = DataLoader(testset, batch_size=args.batch_size, shuffle=False, drop_last=False, num_workers=args.workers)
if not torch.cuda.is_available():
args.device = 'cpu'
args.device = torch.device(args.device)
# Create PointNet Model.
ptnet = PointNet_features(emb_dims=args.emb_dims, symfn=args.symfn)
model = PointNet_classifier(ptnet=ptnet)
checkpoint = None
if args.resume:
assert os.path.isfile(args.resume)
checkpoint = torch.load(args.resume)
args.start_epoch = checkpoint['epoch']
model.load_state_dict(checkpoint['model'])
if args.pretrained:
assert os.path.isfile(args.pretrained)
model.load_state_dict(torch.load(args.pretrained, map_location='cpu'))
model.to(args.device)
if args.eval:
test(args, model, test_loader, textio)
else:
train(args, model, train_loader, test_loader, boardio, textio, checkpoint)
if __name__ == '__main__':
main()