-
Notifications
You must be signed in to change notification settings - Fork 57
/
train.py
229 lines (199 loc) · 8.88 KB
/
train.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
import open3d as o3d
import argparse
import random
import numpy as np
import torch
import torch.optim as optim
import sys
from dataset import *
from model import *
from utils import *
import os
import json
import time, datetime
import visdom
from time import time
sys.path.append("./emd/")
import emd_module as emd
parser = argparse.ArgumentParser()
parser.add_argument('--batchSize', type=int, default=8, help='input batch size')
parser.add_argument('--workers', type=int, help='number of data loading workers', default=12)
parser.add_argument('--nepoch', type=int, default=25, help='number of epochs to train for')
parser.add_argument('--model', type=str, default = '', help='optional reload model path')
parser.add_argument('--num_points', type=int, default = 8192, help='number of points')
parser.add_argument('--n_primitives', type=int, default = 16, help='number of surface elements')
parser.add_argument('--env', type=str, default ="MSN_TRAIN" , help='visdom environment')
opt = parser.parse_args()
print (opt)
class FullModel(nn.Module):
def __init__(self, model):
super(FullModel, self).__init__()
self.model = model
self.EMD = emd.emdModule()
def forward(self, inputs, gt, eps, iters):
output1, output2, expansion_penalty = self.model(inputs)
gt = gt[:, :, :3]
dist, _ = self.EMD(output1, gt, eps, iters)
emd1 = torch.sqrt(dist).mean(1)
dist, _ = self.EMD(output2, gt, eps, iters)
emd2 = torch.sqrt(dist).mean(1)
return output1, output2, emd1, emd2, expansion_penalty
vis = visdom.Visdom(port = 8097, env=opt.env) # set your port
now = datetime.datetime.now()
save_path = now.isoformat()
if not os.path.exists('./log/'):
os.mkdir('./log/')
dir_name = os.path.join('log', save_path)
if not os.path.exists(dir_name):
os.mkdir(dir_name)
logname = os.path.join(dir_name, 'log.txt')
os.system('cp ./train.py %s' % dir_name)
os.system('cp ./dataset.py %s' % dir_name)
os.system('cp ./model.py %s' % dir_name)
opt.manualSeed = random.randint(1, 10000)
print("Random Seed: ", opt.manualSeed)
random.seed(opt.manualSeed)
torch.manual_seed(opt.manualSeed)
best_val_loss = 10
dataset = ShapeNet(train=True, npoints=opt.num_points)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batchSize,
shuffle=True, num_workers=int(opt.workers))
dataset_test = ShapeNet(train=False, npoints=opt.num_points)
dataloader_test = torch.utils.data.DataLoader(dataset_test, batch_size=opt.batchSize,
shuffle=False, num_workers=int(opt.workers))
len_dataset = len(dataset)
print("Train Set Size: ", len_dataset)
network = MSN(num_points = opt.num_points, n_primitives = opt.n_primitives)
network = torch.nn.DataParallel(FullModel(network))
network.cuda()
network.module.model.apply(weights_init) #initialization of the weight
if opt.model != '':
network.module.model.load_state_dict(torch.load(opt.model))
print("Previous weight loaded ")
lrate = 0.001 #learning rate
optimizer = optim.Adam(network.module.model.parameters(), lr = lrate)
train_loss = AverageValueMeter()
val_loss = AverageValueMeter()
with open(logname, 'a') as f: #open and append
f.write(str(network.module.model) + '\n')
train_curve = []
val_curve = []
labels_generated_points = torch.Tensor(range(1, (opt.n_primitives+1)*(opt.num_points//opt.n_primitives)+1)).view(opt.num_points//opt.n_primitives,(opt.n_primitives+1)).transpose(0,1)
labels_generated_points = (labels_generated_points)%(opt.n_primitives+1)
labels_generated_points = labels_generated_points.contiguous().view(-1)
for epoch in range(opt.nepoch):
#TRAIN MODE
train_loss.reset()
network.module.model.train()
# learning rate schedule
if epoch==20:
optimizer = optim.Adam(network.module.model.parameters(), lr = lrate/10.0)
if epoch==40:
optimizer = optim.Adam(network.module.model.parameters(), lr = lrate/100.0)
for i, data in enumerate(dataloader, 0):
optimizer.zero_grad()
id, input, gt = data
input = input.float().cuda()
gt = gt.float().cuda()
input = input.transpose(2,1).contiguous()
output1, output2, emd1, emd2, expansion_penalty = network(input, gt.contiguous(), 0.005, 50)
loss_net = emd1.mean() + emd2.mean() + expansion_penalty.mean() * 0.1
loss_net.backward()
train_loss.update(emd2.mean().item())
optimizer.step()
if i % 10 == 0:
idx = random.randint(0, input.size()[0] - 1)
vis.scatter(X = gt.contiguous()[idx].data.cpu()[:, :3],
win = 'TRAIN_GT',
opts = dict(
title = id[idx],
markersize = 2,
),
)
vis.scatter(X = input.transpose(2,1).contiguous()[idx].data.cpu(),
win = 'TRAIN_INPUT',
opts = dict(
title = id[idx],
markersize = 2,
),
)
vis.scatter(X = output1[idx].data.cpu(),
Y = labels_generated_points[0:output1.size(1)],
win = 'TRAIN_COARSE',
opts = dict(
title= id[idx],
markersize=2,
),
)
vis.scatter(X = output2[idx].data.cpu(),
win = 'TRAIN_OUTPUT',
opts = dict(
title= id[idx],
markersize=2,
),
)
print(opt.env + ' train [%d: %d/%d] emd1: %f emd2: %f expansion_penalty: %f' %(epoch, i, len_dataset/opt.batchSize, emd1.mean().item(), emd2.mean().item(), expansion_penalty.mean().item()))
train_curve.append(train_loss.avg)
# VALIDATION
if epoch % 5 == 0:
val_loss.reset()
network.module.model.eval()
with torch.no_grad():
for i, data in enumerate(dataloader_test, 0):
id, input, gt = data
input = input.float().cuda()
gt = gt.float().cuda()
input = input.transpose(2,1).contiguous()
output1, output2, emd1, emd2, expansion_penalty = network(input, gt.contiguous(), 0.004, 3000)
val_loss.update(emd2.mean().item())
idx = random.randint(0, input.size()[0] - 1)
vis.scatter(X = gt.contiguous()[idx].data.cpu()[:, :3],
win = 'VAL_GT',
opts = dict(
title = id[idx],
markersize = 2,
),
)
vis.scatter(X = input.transpose(2,1).contiguous()[idx].data.cpu(),
win = 'VAL_INPUT',
opts = dict(
title = id[idx],
markersize = 2,
),
)
vis.scatter(X = output1[idx].data.cpu(),
Y = labels_generated_points[0:output1.size(1)],
win = 'VAL_COARSE',
opts = dict(
title= id[idx],
markersize=2,
),
)
vis.scatter(X = output2[idx].data.cpu(),
win = 'VAL_OUTPUT',
opts = dict(
title= id[idx],
markersize=2,
),
)
print(opt.env + ' val [%d: %d/%d] emd1: %f emd2: %f expansion_penalty: %f' %(epoch, i, len_dataset/opt.batchSize, emd1.mean().item(), emd2.mean().item(), expansion_penalty.mean().item()))
val_curve.append(val_loss.avg)
vis.line(X=np.column_stack((np.arange(len(train_curve)),np.arange(len(val_curve)))),
Y=np.column_stack((np.array(train_curve),np.array(val_curve))),
win='loss',
opts=dict(title="emd", legend=["train_curve" + opt.env, "val_curve" + opt.env], markersize=2, ), )
vis.line(X=np.column_stack((np.arange(len(train_curve)),np.arange(len(val_curve)))),
Y=np.log(np.column_stack((np.array(train_curve),np.array(val_curve)))),
win='log',
opts=dict(title="log_emd", legend=["train_curve"+ opt.env, "val_curve"+ opt.env], markersize=2, ), )
log_table = {
"train_loss" : train_loss.avg,
"val_loss" : val_loss.avg,
"epoch" : epoch,
"lr" : lrate,
"bestval" : best_val_loss,
}
with open(logname, 'a') as f:
f.write('json_stats: ' + json.dumps(log_table) + '\n')
print('saving net...')
torch.save(network.module.model.state_dict(), '%s/network.pth' % (dir_name))