-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_shape_ae.py
171 lines (136 loc) · 7.14 KB
/
train_shape_ae.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
import os
os.environ['CUDA_LAUNCH_BLOCKING'] = "1"
import torch
import torch.optim as optim
import numpy as np
import argparse
from configparser import ConfigParser
from model.pointnet_ae import PCAE
from dataset import ShapeNetDataSet
from fastprogress import master_bar, progress_bar
from chamfer_distance import ChamferDistance
from torch.utils.tensorboard import SummaryWriter
import datetime
import h5py
config_file_name = "config.ini"
current_time = datetime.datetime.now().strftime("%m-%d-%Y-%H:%M:%S")
summary_writer = SummaryWriter(log_dir='logs/shape-ae/' + current_time)
def train(dataset_dir, num_of_points, batch_size, epochs, learning_rate, output_dir):
train_dataset = ShapeNetDataSet(dataset_dir=dataset_dir, num_of_points=num_of_points)
train_dataloader = torch.utils.data.DataLoader(train_dataset,
batch_size=batch_size,
shuffle=True)
test_dataset = ShapeNetDataSet(dataset_dir, num_of_points=num_of_points, train=False)
test_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=True, )
model = PCAE(num_of_points)
print(model)
ch_distance = ChamferDistance()
if torch.cuda.is_available():
model.cuda()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
if not os.path.isdir(output_dir):
os.mkdir(output_dir)
with open(os.path.join(output_dir, 'training_log.csv'), 'w+') as fid:
fid.write('epoch,train_loss,test_loss\n')
mb = master_bar(range(epochs))
train_loss = []
test_loss = []
for epoch in mb:
epoch_train_loss = []
epoch_train_acc = []
batch_number = 0
for data in progress_bar(train_dataloader, parent=mb):
batch_number += 1
points, targets = data
if torch.cuda.is_available():
points, targets = points.cuda(), targets.cuda()
if points.shape[0] <= 1:
continue
optimizer.zero_grad()
model = model.train()
reconstructed = model(points)
rand = np.random.randint(0, 100)
if rand < 5:
pcfile = h5py.File('reconstructed/' + str(batch_number) +'_' + str(epoch) + '_train.h5', 'w')
pcfile.create_dataset('shape_' + str(batch_number) +'_' + str(epoch), data=points[0].detach().cpu().numpy())
# np.savetxt(os.path.join(output_dir, str(batch_number) + str(epoch) + '_train.pts'),
# points[0].detach().cpu().numpy(),
# delimiter=' ', fmt='%1.4e')
dist1, dist2 = ch_distance(points, reconstructed)
loss = (torch.mean(dist1)) + (torch.mean(dist2))
epoch_train_loss.append(loss.cpu().item())
epoch_train_loss.append(loss.item())
summary_writer.add_scalar('training loss',
loss.item(),
epoch * len(train_dataloader) + batch_number)
loss.backward()
optimizer.step()
mb.child.comment = 'train loss: %f, train accuracy: %f' % (np.mean(epoch_train_loss),
np.mean(epoch_train_acc))
epoch_test_loss = []
for batch_number, data in enumerate(test_dataloader):
points, targets = data
if torch.cuda.is_available():
points, targets = points.cuda(), targets.cuda()
model = model.eval()
reconstructed = model(points)
dist1, dist2 = ch_distance(points, reconstructed)
loss = (torch.mean(dist1)) + (torch.mean(dist2))
if loss > 0.5:
# np.savetxt(os.path.join(output_dir, str(batch_number) + str(epoch) + '_val_ground.pts'),
# points.detach().cpu().numpy(),
# delimiter=' ', fmt='%1.4e')
# np.savetxt(os.path.join(output_dir, str(batch_number) + str(epoch) + '_val_constructed.pts'),
# reconstructed.detach().cpu().numpy(),
# delimiter=' ', fmt='%1.4e')
pcfile = h5py.File('reconstructed/' + str(batch_number) + '_' + str(epoch) + '_val_gtruth.h5', 'w')
pcfile.create_dataset('shape_' + str(batch_number) + '_' + str(epoch),
data=points.detach().cpu().numpy())
pcfile = h5py.File('reconstructed/' + str(batch_number) + '_' + str(epoch) + '_val.h5', 'w')
pcfile.create_dataset('shape_' + str(batch_number) + '_' + str(epoch),
data=reconstructed.detach().cpu().numpy())
epoch_test_loss.append(loss.cpu().item())
epoch_test_loss.append(loss.item())
mb.write('Epoch %s: train loss: %s, val loss: %s'
% (epoch,
np.mean(epoch_train_loss),
np.mean(epoch_test_loss)))
summary_writer.add_scalar('validation loss',
loss.item(),
epoch * len(test_dataloader) + batch_number)
if test_loss and np.mean(epoch_test_loss) < np.min(test_loss):
torch.save(model.state_dict(), os.path.join(output_dir, 'shapenet_classification_model.pth'))
with open(os.path.join(output_dir, 'training_log.csv'), 'a') as fid:
fid.write('%s,%s,%s\n' % (epoch,
np.mean(epoch_train_loss),
np.mean(epoch_test_loss)))
train_loss.append(np.mean(epoch_train_loss))
test_loss.append(np.mean(epoch_test_loss))
if __name__ == '__main__':
print("main***********")
parser = argparse.ArgumentParser()
parser.add_argument('dataset_folder', type=str, help='path to the dataset folder')
parser.add_argument('output_dir', type=str, help='output folder')
parser.add_argument('--number_of_points', type=int, default=1024, help='number of points per cloud')
parser.add_argument('--batch_size', type=int, default=64, help='batch size')
parser.add_argument('--epochs', type=int, default=10, help='number of epochs')
parser.add_argument('--learning_rate', type=float, default=0.001, help='learning rate')
args = parser.parse_args()
#set values in conifg file
cfgfile = open(config_file_name, 'r+')
config = ConfigParser()
config.read(config_file_name)
config.set('train', 'dataset_folder', args.dataset_folder)
config.set('train', 'output_dir', args.output_dir)
config.set('train', 'number_of_points', str(args.number_of_points))
config.set('train', 'batch_size', str(args.batch_size))
config.set('train', 'epochs', str(args.epochs))
config.set('train', 'learning_rate', str(args.learning_rate))
config.write(cfgfile)
cfgfile.close()
train(dataset_dir=args.dataset_folder,
num_of_points=args.number_of_points,
batch_size=args.batch_size,
epochs=args.epochs,
learning_rate=args.learning_rate,
output_dir=args.output_dir)