-
Notifications
You must be signed in to change notification settings - Fork 0
/
vissualize.py
225 lines (186 loc) · 8.9 KB
/
vissualize.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import _init_paths
from dataset.dataset import FullDataset
from config import Configuration
import os
import sys
import argparse
import random
import torch
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.utils.data
import torchvision.transforms as transforms
from torch.autograd import Variable
import utils
from utils import *
import data_utils as d_utils
import open3d as o3d
# from Encoder import Decoder
from Encoder1024 import Decoder
from D_net import D_net
# import shapenet_part_loader
from mayavi import mlab
from completion.inquary import *
sys.path.insert(1, '/home/nsuresh/Documents/ShapeAware-Pipeline/dataset')
torch.backends.cudnn.enabled = False
parser = argparse.ArgumentParser()
# parser.add_argument('--dataset', default='ShapeNet', help='ModelNet10|ModelNet40|ShapeNet')
parser.add_argument('--dataroot', default='dataset/train', help='path to dataset')
parser.add_argument('--workers', type=int,default=2, help='number of data loading workers')
parser.add_argument('--batchSize', type=int, default=1, help='input batch size')
parser.add_argument('--pnum', type=int, default=2048, help='the point number of a sample')
parser.add_argument('--crop_point_num',type=int,default=1024,help='0 means do not use else use with this weight')
parser.add_argument('--nc', type=int, default=3)
parser.add_argument('--niter', type=int, default=300, help='number of epochs to train for')
parser.add_argument('--weight_decay', type=float, default=0.001)
parser.add_argument('--learning_rate', default=0.0002, type=float, help='learning rate in training')
parser.add_argument('--beta1', type=float, default=0.9, help='beta1 for adam. default=0.9')
parser.add_argument('--cuda', type = bool, default = False, help='enables cuda')
parser.add_argument('--ngpu', type=int, default=2, help='number of GPUs to use')
parser.add_argument('--netG', default='Trained_Model_1/gen_net_Table_Attention48.pth', help="")
parser.add_argument('--netD', default='Trained_Model_1/dis_net_Table_Attention48.pth', help="")
parser.add_argument('--infile',type = str, default = 'test_files/crop12.csv')
parser.add_argument('--infile_real',type = str, default = 'test_files/real11.csv')
parser.add_argument('--manualSeed', type=int, help='manual seed')
parser.add_argument('--drop',type=float,default=0.2)
parser.add_argument('--num_scales',type=int,default=3,help='number of scales')
# Set the first parameter of '--point_scales_list' equal to (point_number + 512).
parser.add_argument('--point_scales_list',type=list,default=[2048,1024],help='number of points in each scales')
parser.add_argument('--each_scales_size',type=int,default=1,help='each scales size')
parser.add_argument('--wtl2',type=float,default=0.9,help='0 means do not use else use with this weight')
parser.add_argument('--cropmethod', default = 'random_center', help = 'random|center|random_center')
parser.add_argument('--cloud_size',type=int,default=1024,help='0 means do not use else use with this weight')
opt = parser.parse_args()
print(opt)
def distance_squre1(p1,p2):
return (p1[0]-p2[0])**2+(p1[1]-p2[1])**2+(p1[2]-p2[2])**2
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
test_set = FullDataset(mode='val')
test_loader = torch.utils.data.DataLoader(test_set, batch_size=opt.batchSize, shuffle=False, num_workers=int(opt.workers))
gen_net = Decoder(opt.point_scales_list[0], opt.crop_point_num)
dis_net = D_net(opt.crop_point_num)
# dis_net = D_net(4, opt.crop_point_num)
USE_CUDA = True
criterion_PointLoss = PointLoss().to(device)
def weights_init_normal(m):
classname = m.__class__.__name__
if classname.find("Conv2d") != -1:
torch.nn.init.normal_(m.weight.data, 0.0, 0.02)
elif classname.find("Conv1d") != -1:
torch.nn.init.normal_(m.weight.data, 0.0, 0.02)
elif classname.find("BatchNorm2d") != -1:
torch.nn.init.normal_(m.weight.data, 1.0, 0.02)
torch.nn.init.constant_(m.bias.data, 0.0)
elif classname.find("BatchNorm1d") != -1:
torch.nn.init.normal_(m.weight.data, 1.0, 0.02)
torch.nn.init.constant_(m.bias.data, 0.0)
# initialize generator and discriminator weights and device
if USE_CUDA:
print("Using", torch.cuda.device_count(), "GPUs")
gen_net = torch.nn.DataParallel(gen_net)
gen_net.to(device)
gen_net.apply(weights_init_normal)
dis_net = torch.nn.DataParallel(dis_net)
dis_net.to(device)
dis_net.apply(weights_init_normal)
if opt.netG != '':
gen_net.load_state_dict(torch.load(opt.netG, map_location=lambda storage, location: storage)['state_dict'])
resume_epoch = torch.load(opt.netG)['epoch']
print('G loaded')
if opt.netD != '':
dis_net.load_state_dict(torch.load(opt.netD, map_location=lambda storage, location: storage)['state_dict'])
resume_epoch = torch.load(opt.netD)['epoch']
# print('D loaded')
# print(resume_epoch)
for i, data in enumerate(test_loader):
real_center, target = data
batch_size = real_center.size()[0]
if batch_size < opt.batchSize: continue
real_center = real_center.float()
input_cropped1 = torch.FloatTensor(batch_size, opt.pnum, 3)
input_cropped1 = input_cropped1.data.copy_(real_center)
real_center = torch.unsqueeze(real_center, 1)
input_cropped1 = torch.unsqueeze(input_cropped1, 1)
real_center = real_center.to(device)
target = target.to(device)
real_center = torch.squeeze(real_center, 1)
input_cropped1 = input_cropped1.to(device)
input_cropped1 = torch.squeeze(input_cropped1, 1)
input_cropped1 = Variable(input_cropped1, requires_grad=False)
gen_net.eval()
fake_fine1, fake_fine, conv11, conv12 = gen_net(input_cropped1)
# CD_loss = criterion_PointLoss(torch.squeeze(fake_fine, 1), torch.squeeze(real_center, 1))
# small_partial, small_complete, small_prior = preprocess(partial, model_points, eval_config.partial_pcd_num, eval_config.complete_pcd_num, eval_config.prior_num)
# if len(small_partial.size()) < 3:
# small_partial = torch.unsqueeze(small_partial, 0)
# small_complete = torch.unsqueeze(small_complete, 0)
# batch_size = small_partial.size()[0]
# small_partial = small_partial.float()
# input_cropped1 = torch.FloatTensor(batch_size, eval_config.partial_pcd_num, 3)
# input_cropped1 = input_cropped1.data.copy_(small_partial)
# small_partial = torch.unsqueeze(small_partial, 1)
# input_cropped1 = torch.unsqueeze(input_cropped1, 1)
# small_partial = small_partial.to(device)
# small_complete = small_complete.to(device)
# small_partial = torch.squeeze(small_partial, 1)
# input_cropped1 = input_cropped1.to(device)
# input_cropped1 = torch.squeeze(input_cropped1, 1)
# input_cropped1 = Variable(input_cropped1, requires_grad=False)
# gen_net.eval()
# fake_fine1, fake_fine, conv11, conv12 = gen_net(input_cropped1)
# # _, fake_fine, conv11, conv12, conv21, conv22 = gen_net(input_cropped1, small_prior)
# fake_fine = target.cpu()
np_fake = fake_fine[0].cpu().detach().numpy()
# np_fake = np_fake*10
np_complete = target.cpu()
np_complete = np_complete[0].cpu().detach().numpy()
input_cropped1 = input_cropped1.cpu()
np_crop = real_center[0].cpu().numpy()
# print(np_fake.shape, np_crop.shape, real_center.shape)
# display = np.vstack((np_fake, np_fake1, np_crop))
x = np_crop[:, 0] # x position of point
y = np_crop[:, 1] # y position of point
z = np_crop[:, 2] # z position of point
xf = np_fake[:, 0] # x position of point
yf = np_fake[:, 1] # y position of point
zf = np_fake[:, 2] # z position of point
# d = np.sqrt(x ** 2 + y ** 2) # Map Distance from sensor
vals = 'height'
if vals == "height":
col = z
else:
col = d
cloud = np_crop
cloud_pcd = o3d.geometry.PointCloud()
cloud_pcd.points = o3d.utility.Vector3dVector(cloud)
o3d.io.write_point_cloud("/home/nsuresh/Documents/ShapeAware-Pipeline/dataset/vis_new/partial_{}.ply".format(i), cloud_pcd)
cloud2 = np_fake
cloud_pcd2 = o3d.geometry.PointCloud()
cloud_pcd2.points = o3d.utility.Vector3dVector(cloud2)
o3d.io.write_point_cloud("/home/nsuresh/Documents/ShapeAware-Pipeline/dataset/vis_new/modelgen_{}.ply".format(i), cloud_pcd2)
cloud3 = np_complete
cloud_pcd3 = o3d.geometry.PointCloud()
cloud_pcd3.points = o3d.utility.Vector3dVector(cloud3)
o3d.io.write_point_cloud("/home/nsuresh/Documents/ShapeAware-Pipeline/dataset/vis_new/complete_{}.ply".format(i), cloud_pcd3)
# fig = plt.figure(figsize=(640, 500))
# ax = fig.add_subplot(projection='3d')
# ax.scatter(x, y, z)
# ax.scatter(xf, yf, zf)
# plt.scatter3(x, y, z, C=(0.9, 0.87, 0.54))
# plt.scatter3(xf, yf, zf, C=(0.54, 0.82, 0.94))
# fig = mlab.figure(bgcolor=(1, 1, 1), size=(640, 500))
# mlab.points3d(x, y, z,
# color=(0.9, 0.87, 0.54),
# mode="sphere",
# scale_factor = 0.01,
# figure=fig,
# )
# mlab.points3d(xf, yf, zf,
# color=(0.54, 0.82, 0.94), #baby blue: 0.54, 0.82, 0.94
# mode="sphere",
# scale_factor = 0.01,
# figure=fig,
# )
# mlab.show()