This repository has been archived by the owner on Feb 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
reconstruction.py
223 lines (192 loc) · 7.61 KB
/
reconstruction.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Author: An Tao
@Contact: [email protected]
@File: reconstruction.py
@Time: 2020/1/2 10:26 AM
"""
import os
import sys
import time
import shutil
import torch
import torch.optim as optim
import numpy as np
from tensorboardX import SummaryWriter
from model import ReconstructionNet
from dataset import Dataset
from utils import Logger
class Reconstruction(object):
def __init__(self, args):
self.dataset_name = args.dataset
if args.epochs != None:
self.epochs = args.epochs
elif args.encoder == 'foldnet':
self.epochs = 278
elif args.encoder == 'dgcnn_cls':
self.epochs = 250
elif args.encoder == 'dgcnn_seg':
self.epochs = 290
self.batch_size = args.batch_size
self.snapshot_interval = args.snapshot_interval
self.no_cuda = args.no_cuda
self.model_path = args.model_path
# create exp directory
file = [f for f in args.model_path.split('/')]
if args.exp_name != None:
self.experiment_id = "Reconstruct_" + args.exp_name
elif file[-2] == 'models':
self.experiment_id = file[-3]
else:
self.experiment_id = "Reconstruct" + time.strftime('%m%d%H%M%S')
snapshot_root = 'snapshot/%s' % self.experiment_id
tensorboard_root = 'tensorboard/%s' % self.experiment_id
self.save_dir = os.path.join(snapshot_root, 'models/')
self.tboard_dir = tensorboard_root
# check arguments
if self.model_path == '':
if not os.path.exists(self.save_dir):
os.makedirs(self.save_dir)
else:
choose = input("Remove " + self.save_dir + " ? (y/n)")
if choose == "y":
shutil.rmtree(self.save_dir)
os.makedirs(self.save_dir)
else:
sys.exit(0)
if not os.path.exists(self.tboard_dir):
os.makedirs(self.tboard_dir)
else:
shutil.rmtree(self.tboard_dir)
os.makedirs(self.tboard_dir)
sys.stdout = Logger(os.path.join(snapshot_root, 'log.txt'))
self.writer = SummaryWriter(log_dir=self.tboard_dir)
# print args
print(str(args))
# get gpu id
gids = ''.join(args.gpu.split())
self.gpu_ids = [int(gid) for gid in gids.split(',')]
self.first_gpu = self.gpu_ids[0]
# generate dataset
self.train_dataset = Dataset(
root=args.dataset_root,
dataset_name=args.dataset,
split='all',
num_points=args.num_points,
random_translate=args.use_translate,
random_rotate=True,
random_jitter=args.use_jitter
)
self.train_loader = torch.utils.data.DataLoader(
self.train_dataset,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.workers
)
print("Training set size:", self.train_loader.dataset.__len__())
# initialize model
self.model = ReconstructionNet(args)
if self.model_path != '':
self._load_pretrain(args.model_path)
# load model to gpu
if not self.no_cuda:
if len(self.gpu_ids) != 1: # multiple gpus
self.model = torch.nn.DataParallel(self.model.cuda(self.first_gpu), self.gpu_ids)
else:
self.model = self.model.cuda(self.gpu_ids[0])
# initialize optimizer
self.parameter = self.model.parameters()
self.optimizer = optim.Adam(self.parameter, lr=0.0001*16/args.batch_size, betas=(0.9, 0.999), weight_decay=1e-6)
def run(self):
self.train_hist = {
'loss': [],
'per_epoch_time': [],
'total_time': []
}
best_loss = 1000000000
print('Training start!!')
start_time = time.time()
self.model.train()
if self.model_path != '':
start_epoch = self.model_path[-7:-4]
if start_epoch[0] == '_':
start_epoch = start_epoch[1:]
start_epoch = int(start_epoch)
else:
start_epoch = 0
for epoch in range(start_epoch, self.epochs):
loss = self.train_epoch(epoch)
# save snapeshot
if (epoch + 1) % self.snapshot_interval == 0:
self._snapshot(epoch + 1)
if loss < best_loss:
best_loss = loss
self._snapshot('best')
# save tensorboard
if self.writer:
self.writer.add_scalar('Train Loss', self.train_hist['loss'][-1], epoch)
self.writer.add_scalar('Learning Rate', self._get_lr(), epoch)
# finish all epoch
self._snapshot(epoch + 1)
if loss < best_loss:
best_loss = loss
self._snapshot('best')
self.train_hist['total_time'].append(time.time() - start_time)
print("Avg one epoch time: %.2f, total %d epochs time: %.2f" % (np.mean(self.train_hist['per_epoch_time']),
self.epochs, self.train_hist['total_time'][0]))
print("Training finish!... save training results")
def train_epoch(self, epoch):
epoch_start_time = time.time()
loss_buf = []
num_batch = int(len(self.train_loader.dataset) / self.batch_size)
for iter, (pts, _) in enumerate(self.train_loader):
if pts.size(0) == 1:
continue
if not self.no_cuda:
pts = pts.cuda(self.first_gpu)
# forward
self.optimizer.zero_grad()
output, _ = self.model(pts)
# loss
if len(self.gpu_ids) != 1: # multiple gpus
loss = self.model.module.get_loss(pts, output)
else:
loss = self.model.get_loss(pts, output)
# backward
loss.backward()
self.optimizer.step()
loss_buf.append(loss.detach().cpu().numpy())
# finish one epoch
epoch_time = time.time() - epoch_start_time
self.train_hist['per_epoch_time'].append(epoch_time)
self.train_hist['loss'].append(np.mean(loss_buf))
print(f'Epoch {epoch+1}: Loss {np.mean(loss_buf)}, time {epoch_time:.4f}s')
return np.mean(loss_buf)
def _snapshot(self, epoch):
state_dict = self.model.state_dict()
from collections import OrderedDict
new_state_dict = OrderedDict()
for key, val in state_dict.items():
if key[:6] == 'module':
name = key[7:] # remove 'module.'
else:
name = key
new_state_dict[name] = val
save_dir = os.path.join(self.save_dir, self.dataset_name)
torch.save(new_state_dict, save_dir + "_" + str(epoch) + '.pkl')
print(f"Save model to {save_dir}_{str(epoch)}.pkl")
def _load_pretrain(self, pretrain):
state_dict = torch.load(pretrain, map_location='cpu')
from collections import OrderedDict
new_state_dict = OrderedDict()
for key, val in state_dict.items():
if key[:6] == 'module':
name = key[7:] # remove 'module.'
else:
name = key
new_state_dict[name] = val
self.model.load_state_dict(new_state_dict)
print(f"Load model from {pretrain}")
def _get_lr(self, group=0):
return self.optimizer.param_groups[group]['lr']