-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.py
282 lines (238 loc) · 11.7 KB
/
main.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import os
import gc
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.optim.lr_scheduler import MultiStepLR, StepLR
from data import ModelNet40, SceneflowDataset
from model import FlowNet3D
import numpy as np
from torch.utils.data import DataLoader
# from tensorboardX import SummaryWriter
from tqdm import tqdm
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 _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')
os.system('cp data.py checkpoints' + '/' + args.exp_name + '/' + 'data.py.backup')
def weights_init(m):
classname=m.__class__.__name__
if classname.find('Conv2d') != -1:
nn.init.kaiming_normal_(m.weight.data)
if classname.find('Conv1d') != -1:
nn.init.kaiming_normal_(m.weight.data)
def scene_flow_EPE_np(pred, labels, mask):
error = np.sqrt(np.sum((pred - labels)**2, 2) + 1e-20)
gtflow_len = np.sqrt(np.sum(labels*labels, 2) + 1e-20) # B,N
acc1 = np.sum(np.logical_or((error <= 0.05)*mask, (error/gtflow_len <= 0.05)*mask), axis=1)
acc2 = np.sum(np.logical_or((error <= 0.1)*mask, (error/gtflow_len <= 0.1)*mask), axis=1)
mask_sum = np.sum(mask, 1)
acc1 = acc1[mask_sum > 0] / mask_sum[mask_sum > 0]
acc1 = np.mean(acc1)
acc2 = acc2[mask_sum > 0] / mask_sum[mask_sum > 0]
acc2 = np.mean(acc2)
EPE = np.sum(error * mask, 1)[mask_sum > 0] / mask_sum[mask_sum > 0]
EPE = np.mean(EPE)
return EPE, acc1, acc2
def test_one_epoch(args, net, test_loader):
net.eval()
total_loss = 0
total_epe = 0
total_acc3d = 0
total_acc3d_2 = 0
num_examples = 0
for i, data in tqdm(enumerate(test_loader), total = len(test_loader)):
pc1, pc2, color1, color2, flow, mask1 = data
pc1 = pc1.cuda().transpose(2,1).contiguous()
pc2 = pc2.cuda().transpose(2,1).contiguous()
color1 = color1.cuda().transpose(2,1).contiguous()
color2 = color2.cuda().transpose(2,1).contiguous()
flow = flow.cuda()
mask1 = mask1.cuda().float()
batch_size = pc1.size(0)
num_examples += batch_size
flow_pred = net(pc1, pc2, color1, color2).permute(0,2,1)
loss = torch.mean(mask1 * torch.sum((flow_pred - flow) * (flow_pred - flow), -1) / 2.0)
epe_3d, acc_3d, acc_3d_2 = scene_flow_EPE_np(flow_pred.detach().cpu().numpy(), flow.detach().cpu().numpy(), mask1.detach().cpu().numpy())
total_epe += epe_3d * batch_size
total_acc3d += acc_3d * batch_size
total_acc3d_2+=acc_3d_2*batch_size
# print('batch EPE 3D: %f\tACC 3D: %f\tACC 3D 2: %f' % (epe_3d, acc_3d, acc_3d_2))
total_loss += loss.item() * batch_size
return total_loss * 1.0 / num_examples, total_epe * 1.0 / num_examples, total_acc3d * 1.0 / num_examples, total_acc3d_2 * 1.0 / num_examples
def train_one_epoch(args, net, train_loader, opt):
net.train()
num_examples = 0
total_loss = 0
for i, data in tqdm(enumerate(train_loader), total = len(train_loader)):
pc1, pc2, color1, color2, flow, mask1 = data
pc1 = pc1.cuda().transpose(2,1).contiguous()
pc2 = pc2.cuda().transpose(2,1).contiguous()
color1 = color1.cuda().transpose(2,1).contiguous()
color2 = color2.cuda().transpose(2,1).contiguous()
flow = flow.cuda().transpose(2,1).contiguous()
mask1 = mask1.cuda().float()
batch_size = pc1.size(0)
opt.zero_grad()
num_examples += batch_size
flow_pred = net(pc1, pc2, color1, color2)
loss = torch.mean(mask1 * torch.sum((flow_pred - flow) ** 2, 1) / 2.0)
loss.backward()
opt.step()
total_loss += loss.item() * batch_size
# if (i+1) % 100 == 0:
# print("batch: %d, mean loss: %f" % (i, total_loss / 100 / batch_size))
# total_loss = 0
return total_loss * 1.0 / num_examples
def test(args, net, test_loader, boardio, textio):
test_loss, epe, acc, acc_2 = test_one_epoch(args, net, test_loader)
textio.cprint('==FINAL TEST==')
textio.cprint('mean test loss: %f\tEPE 3D: %f\tACC 3D: %f\tACC 3D 2: %f'%(test_loss, epe, acc, acc_2))
def train(args, net, train_loader, test_loader, boardio, textio):
if args.use_sgd:
print("Use SGD")
opt = optim.SGD(net.parameters(), lr=args.lr * 100, momentum=args.momentum, weight_decay=1e-4)
else:
print("Use Adam")
opt = optim.Adam(net.parameters(), lr=args.lr, weight_decay=1e-4)
# scheduler = MultiStepLR(opt, milestones=[75, 150, 200], gamma=0.1)
scheduler = StepLR(opt, 10, gamma = 0.7)
best_test_loss = np.inf
for epoch in range(args.epochs):
textio.cprint('==epoch: %d, learning rate: %f=='%(epoch, opt.param_groups[0]['lr']))
train_loss = train_one_epoch(args, net, train_loader, opt)
textio.cprint('mean train EPE loss: %f'%train_loss)
test_loss, epe, acc, acc_2 = test_one_epoch(args, net, test_loader)
textio.cprint('mean test loss: %f\tEPE 3D: %f\tACC 3D: %f\tACC 3D 2: %f'%(test_loss, epe, acc, acc_2))
if best_test_loss >= test_loss:
best_test_loss = test_loss
textio.cprint('best test loss till now: %f'%test_loss)
if torch.cuda.device_count() > 1:
torch.save(net.module.state_dict(), 'checkpoints/%s/models/model.best.t7' % args.exp_name)
else:
torch.save(net.state_dict(), 'checkpoints/%s/models/model.best.t7' % args.exp_name)
scheduler.step()
# if torch.cuda.device_count() > 1:
# torch.save(net.module.state_dict(), 'checkpoints/%s/models/model.%d.t7' % (args.exp_name, epoch))
# else:
# torch.save(net.state_dict(), 'checkpoints/%s/models/model.%d.t7' % (args.exp_name, epoch))
# gc.collect()
def main():
parser = argparse.ArgumentParser(description='Point Cloud Registration')
parser.add_argument('--exp_name', type=str, default='exp', metavar='N',
help='Name of the experiment')
parser.add_argument('--model', type=str, default='flownet', metavar='N',
choices=['flownet'],
help='Model to use, [flownet]')
parser.add_argument('--emb_dims', type=int, default=512, metavar='N',
help='Dimension of embeddings')
parser.add_argument('--num_points', type=int, default=2048,
help='Point Number [default: 2048]')
parser.add_argument('--dropout', type=float, default=0.5, metavar='N',
help='Dropout ratio in transformer')
parser.add_argument('--batch_size', type=int, default=64, metavar='batch_size',
help='Size of batch)')
parser.add_argument('--test_batch_size', type=int, default=32, metavar='batch_size',
help='Size of batch)')
parser.add_argument('--epochs', type=int, default=250, metavar='N',
help='number of episode to train ')
parser.add_argument('--use_sgd', action='store_true', default=False,
help='Use SGD')
parser.add_argument('--lr', type=float, default=0.001, metavar='LR',
help='learning rate (default: 0.001, 0.1 if using sgd)')
parser.add_argument('--momentum', type=float, default=0.9, metavar='M',
help='SGD momentum (default: 0.9)')
parser.add_argument('--no_cuda', action='store_true', default=False,
help='enables CUDA training')
parser.add_argument('--seed', type=int, default=1234, metavar='S',
help='random seed (default: 1)')
parser.add_argument('--eval', action='store_true', default=False,
help='evaluate the model')
parser.add_argument('--cycle', type=bool, default=False, metavar='N',
help='Whether to use cycle consistency')
parser.add_argument('--gaussian_noise', type=bool, default=False, metavar='N',
help='Wheter to add gaussian noise')
parser.add_argument('--unseen', type=bool, default=False, metavar='N',
help='Whether to test on unseen category')
parser.add_argument('--dataset', type=str, default='SceneflowDataset',
choices=['SceneflowDataset'], metavar='N',
help='dataset to use')
parser.add_argument('--dataset_path', type=str, default='../../datasets/data_processed_maxcut_35_20k_2k_8192', metavar='N',
help='dataset to use')
parser.add_argument('--model_path', type=str, default='', metavar='N',
help='Pretrained model path')
args = parser.parse_args()
# os.environ['CUDA_VISIBLE_DEVICES'] = '0,1'
# CUDA settings
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)
boardio = []
_init_(args)
textio = IOStream('checkpoints/' + args.exp_name + '/run.log')
textio.cprint(str(args))
if args.dataset == 'modelnet40':
train_loader = DataLoader(
ModelNet40(num_points=args.num_points, partition='train', gaussian_noise=args.gaussian_noise,
unseen=args.unseen, factor=args.factor),
batch_size=args.batch_size, shuffle=True, drop_last=True)
test_loader = DataLoader(
ModelNet40(num_points=args.num_points, partition='test', gaussian_noise=args.gaussian_noise,
unseen=args.unseen, factor=args.factor),
batch_size=args.test_batch_size, shuffle=False, drop_last=False)
elif args.dataset == 'SceneflowDataset':
train_loader = DataLoader(
SceneflowDataset(npoints=args.num_points, root = args.dataset_path, partition='train'),
batch_size=args.batch_size, shuffle=True, drop_last=True)
test_loader = DataLoader(
SceneflowDataset(npoints=args.num_points, root = args.dataset_path, partition='test'),
batch_size=args.test_batch_size, shuffle=False, drop_last=False)
else:
raise Exception("not implemented")
if args.model == 'flownet':
net = FlowNet3D(args).cuda()
net.apply(weights_init)
if args.eval:
if args.model_path is '':
model_path = 'checkpoints' + '/' + args.exp_name + '/models/model.best.t7'
else:
model_path = args.model_path
print(model_path)
if not os.path.exists(model_path):
print("can't find pretrained model")
return
net.load_state_dict(torch.load(model_path), strict=False)
if torch.cuda.device_count() > 1:
net = nn.DataParallel(net)
print("Let's use", torch.cuda.device_count(), "GPUs!")
else:
raise Exception('Not implemented')
if args.eval:
test(args, net, test_loader, boardio, textio)
else:
train(args, net, train_loader, test_loader, boardio, textio)
print('FINISH')
# boardio.close()
if __name__ == '__main__':
main()