-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
182 lines (162 loc) · 8.18 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import os
import gc
import open3d as o3d
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
from data_finetune import ModelNet40
from data_stanford import Stanford
from sun3d_read import Sun3d
from model import MultiCON
from util import transform_point_cloud, npmat2euler, unsupervisedloss
import numpy as np
from torch.utils.data import DataLoader
from tensorboardX import SummaryWriter
from tqdm import tqdm
from framework import train, test
# Part of the code is referred from: https://github.com/floodsung/LearningToCompare_FSL
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 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='MultiCON', metavar='N',
choices=['MultiCON'],
help='Model to use, [dcp]')
parser.add_argument('--num_points', type=int, default=1024, metavar='N',
help='Num of points to use')
# parameter
parser.add_argument('--emb_nn', type=str, default='dgcnn', metavar='N',
choices=['pointnet', 'dgcnn'],
help='Embedding nn to use, [pointnet, dgcnn]')
parser.add_argument('--pointer', type=str, default='transformer', metavar='N',
choices=['identity', 'transformer'],
help='Attention-based pointer generator to use, [identity, transformer]')
parser.add_argument('--head', type=str, default='svd', metavar='N',
choices=['mlp', 'svd', ],
help='Head to use, [mlp, svd]')
parser.add_argument('--emb_dims', type=int, default=512, metavar='N',
help='Dimension of embeddings')
parser.add_argument('--n_blocks', type=int, default=1, metavar='N',
help='Num of blocks of encoder&decoder')
parser.add_argument('--n_heads', type=int, default=4, metavar='N',
help='Num of heads in multiheadedattention')
parser.add_argument('--ff_dims', type=int, default=1024, metavar='N',
help='Num of dimensions of fc in transformer')
parser.add_argument('--dropout', type=float, default=0.0, metavar='N',
help='Dropout ratio in transformer')
# train & test
parser.add_argument('--batch_size', type=int, default=28, metavar='batch_size',
help='Size of batch)')
parser.add_argument('--test_batch_size', type=int, default=12, 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)')
# operation
parser.add_argument('--eval', action='store_true', default=True,
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='Wheter to test on unseen category')
parser.add_argument('--dataset', type=str, default='sun3d', choices=['modelnet40', 'stanford', 'sun3d'], metavar='N',
help='dataset to use')
parser.add_argument('--subset', type=str, default='bunny', choices=['bunny', 'drill', 'dragon', 'happy'], metavar='N',
help='subset in stanford to use')
parser.add_argument('--factor', type=float, default=4, metavar='N',
help='Divided factor for rotations')
parser.add_argument('--model_path', type=str, default='', metavar='N',
help='Pretrained model path')
args = parser.parse_args()
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))
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)
if args.dataset == 'stanford':
train_loader = DataLoader(
Stanford(partition='train', select_=args.subset, num_points=args.num_points, factor=args.factor),
batch_size=args.batch_size, shuffle=True, drop_last=True)
test_loader = DataLoader(
Stanford(partition='train', select_=args.subset, num_points=args.num_points, factor=args.factor),
batch_size=args.test_batch_size, shuffle=False, drop_last=False)
if args.dataset == 'sun3d':
test_loader = DataLoader(
Sun3d(partition='test'),
batch_size=args.test_batch_size, shuffle=False, drop_last=False)
# else:
# raise Exception("not implemented")
if args.model == 'MultiCON':
net = MultiCON(args).cuda()
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:
with torch.no_grad():
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()