forked from Jiang-Muyun/PointNet12
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcd_utils.py
269 lines (239 loc) · 10.5 KB
/
pcd_utils.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# *_*coding:utf-8 *_*
import os
import numpy as np
import torch
import matplotlib.pyplot as plt
from torch.autograd import Variable
from tqdm import tqdm
from collections import defaultdict
import datetime
import multiprocessing
import pandas as pd
import torch.nn.functional as F
import sys
import my_log as log
import time
def mkdir(fn):
os.makedirs(fn, exist_ok=True)
return fn
def select_avaliable(fn_list):
selected = None
for fn in fn_list:
if os.path.exists(fn):
selected = fn
break
if selected is None:
log.err(log.yellow("Could not find dataset from"), fn_list)
else:
return selected
def to_categorical(y, num_classes):
""" 1-hot encodes a tensor """
new_y = torch.eye(num_classes)[y.cpu().data.numpy(),]
if (y.is_cuda):
return new_y.cuda()
return new_y
def show_example(x, y, x_reconstruction, y_pred,save_dir, figname):
x = x.squeeze().cpu().data.numpy()
x = x.permute(0,2,1)
y = y.cpu().data.numpy()
x_reconstruction = x_reconstruction.squeeze().cpu().data.numpy()
_, y_pred = torch.max(y_pred, -1)
y_pred = y_pred.cpu().data.numpy()
fig, ax = plt.subplots(1, 2)
ax[0].imshow(x, cmap='Greys')
ax[0].set_title('Input: %d' % y)
ax[1].imshow(x_reconstruction, cmap='Greys')
ax[1].set_title('Output: %d' % y_pred)
plt.savefig(save_dir + figname + '.png')
def save_checkpoint(epoch, train_accuracy, test_accuracy, model, optimizer, path,modelnet='checkpoint'):
savepath = path + '/%s-%.5f-%04d.pth' % (modelnet,test_accuracy, epoch)
state = {
'epoch': epoch,
'train_accuracy': train_accuracy,
'test_accuracy': test_accuracy,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
}
torch.save(state, savepath)
def test_clf(model, loader):
mean_correct = []
for j, data in tqdm(enumerate(loader, 0), total=len(loader), smoothing=0.9):
points, target = data
target = target[:, 0]
points = points.transpose(2, 1)
points, target = points.cuda(), target.cuda()
classifier = model.eval()
pred, _ = classifier(points)
pred_choice = pred.data.max(1)[1]
correct = pred_choice.eq(target.long().data).cpu().sum()
mean_correct.append(correct.item()/float(points.size()[0]))
return np.mean(mean_correct)
def compute_cat_iou(pred, target, num_classes ,iou_tabel):
iou_list = []
target = target.cpu().data.numpy()
for j in range(pred.size(0)):
batch_pred = pred[j]
batch_target = target[j]
batch_choice = batch_pred.data.max(1)[1].cpu().data.numpy()
for cat in range(num_classes):
# intersection = np.sum((batch_target == cat) & (batch_choice == cat))
# union = float(np.sum((batch_target == cat) | (batch_choice == cat)))
# iou = intersection/union if not union ==0 else 1
I = np.sum(np.logical_and(batch_choice == cat, batch_target == cat))
U = np.sum(np.logical_or(batch_choice == cat, batch_target == cat))
if U == 0:
iou = 1 # If the union of groundtruth and prediction points is empty, then count part IoU as 1
else:
iou = I / float(U)
iou_tabel[cat,0] += iou
iou_tabel[cat,1] += 1
iou_list.append(iou)
return iou_tabel,iou_list
def calc_categorical_iou(pred, target, num_classes ,iou_tabel):
choice = pred.max(2)[1]
target.squeeze_(-1)
for cat in range(num_classes):
I = torch.sum((choice == cat) & (target == cat)).float()
U = torch.sum((choice == cat) | (target == cat)).float()
if U == 0:
iou = 1
else:
iou = (I / U).cpu().numpy()
iou_tabel[cat,0] += iou
iou_tabel[cat,1] += 1
return iou_tabel
def compute_overall_iou(pred, target, num_classes):
shape_ious = []
pred_np = pred.cpu().data.numpy()
target_np = target.cpu().data.numpy()
for shape_idx in range(pred.size(0)):
part_ious = []
for part in range(num_classes):
I = np.sum(np.logical_and(pred_np[shape_idx].max(1) == part, target_np[shape_idx] == part))
U = np.sum(np.logical_or(pred_np[shape_idx].max(1) == part, target_np[shape_idx] == part))
if U == 0:
iou = 1 #If the union of groundtruth and prediction points is empty, then count part IoU as 1
else:
iou = I / float(U)
part_ious.append(iou)
shape_ious.append(np.mean(part_ious))
return shape_ious
def test_partseg(model, loader, catdict, model_name, num_classes = 50):
''' catdict = {0:Airplane, 1:Airplane, ...49:Table} '''
iou_tabel = np.zeros((len(catdict),3))
iou_list = []
metrics = defaultdict(lambda:list())
hist_acc = []
# mean_correct = []
for points, label, target, norm_plt in tqdm(loader, total=len(loader), smoothing=0.9, dynamic_ncols=True):
batchsize, num_point,_= points.size()
points, label, target, norm_plt = Variable(points.float()),Variable(label.long()), Variable(target.long()),Variable(norm_plt.float())
points = points.transpose(2, 1)
norm_plt = norm_plt.transpose(2, 1)
points, label, target, norm_plt = points.cuda(), label.squeeze().cuda(), target.cuda(), norm_plt.cuda()
if model_name == 'pointnet':
labels_pred, seg_pred, _ = model(points,to_categorical(label,16))
else:
seg_pred = model(points, norm_plt, to_categorical(label, 16))
# labels_pred_choice = labels_pred.data.max(1)[1]
# labels_correct = labels_pred_choice.eq(label.long().data).cpu().sum()
# mean_correct.append(labels_correct.item() / float(points.size()[0]))
# print(pred.size())
iou_tabel, iou = compute_cat_iou(seg_pred,target,num_classes,iou_tabel)
iou_list+=iou
# shape_ious += compute_overall_iou(pred, target, num_classes)
seg_pred = seg_pred.contiguous().view(-1, num_classes)
target = target.view(-1, 1)[:, 0]
pred_choice = seg_pred.data.max(1)[1]
correct = pred_choice.eq(target.data).cpu().sum()
metrics['accuracy'].append(correct.item()/ (batchsize * num_point))
iou_tabel[:,2] = iou_tabel[:,0] /iou_tabel[:,1]
hist_acc += metrics['accuracy']
metrics['accuracy'] = np.mean(hist_acc)
metrics['inctance_avg_iou'] = np.mean(iou_list)
# metrics['label_accuracy'] = np.mean(mean_correct)
iou_tabel = pd.DataFrame(iou_tabel,columns=['iou','count','mean_iou'])
iou_tabel['Category_IOU'] = [catdict[i] for i in range(len(catdict)) ]
cat_iou = iou_tabel.groupby('Category_IOU')['mean_iou'].mean()
metrics['class_avg_iou'] = np.mean(cat_iou)
return metrics, hist_acc, cat_iou
def test_semseg(model, loader, catdict, model_name, num_classes):
iou_tabel = np.zeros((len(catdict),3))
metrics = defaultdict(lambda:list())
with torch.no_grad():
for points, target in tqdm(loader, total=len(loader), smoothing=0.9, dynamic_ncols=True):
batchsize, num_point, _ = points.size()
points, target = Variable(points.float()), Variable(target.long())
points = points.transpose(2, 1)
points, target = points.cuda(), target.cuda()
if model_name == 'pointnet':
pred, _ = model(points)
else:
pred = model(points)
# iou_tabel, iou_list = compute_cat_iou(pred,target,num_classes,iou_tabel)
iou_tabel = calc_categorical_iou(pred,target,num_classes,iou_tabel)
# shape_ious += compute_overall_iou(pred, target, num_classes)
pred = pred.contiguous().view(-1, num_classes)
target = target.view(-1, 1)[:, 0]
pred_choice = pred.data.max(1)[1]
correct = pred_choice.eq(target.data).cpu().sum()
metrics['accuracy'].append(correct.item()/ (batchsize * num_point))
iou_tabel[:,2] = iou_tabel[:,0] /iou_tabel[:,1]
metrics['accuracy'] = np.mean(metrics['accuracy'])
metrics['iou'] = np.mean(iou_tabel[:, 2])
iou_tabel = pd.DataFrame(iou_tabel,columns=['iou','count','mean_iou'])
iou_tabel['Category_IOU'] = [catdict[i] for i in range(len(catdict)) ]
cat_iou = iou_tabel.groupby('Category_IOU')['mean_iou'].mean()
return metrics, cat_iou
def compute_avg_curve(y, n_points_avg):
avg_kernel = np.ones((n_points_avg,)) / n_points_avg
rolling_mean = np.convolve(y, avg_kernel, mode='valid')
return rolling_mean
def plot_loss_curve(history,n_points_avg,n_points_plot,save_dir):
curve = np.asarray(history['loss'])[-n_points_plot:]
avg_curve = compute_avg_curve(curve, n_points_avg)
plt.plot(avg_curve, '-g')
curve = np.asarray(history['margin_loss'])[-n_points_plot:]
avg_curve = compute_avg_curve(curve, n_points_avg)
plt.plot(avg_curve, '-b')
curve = np.asarray(history['reconstruction_loss'])[-n_points_plot:]
avg_curve = compute_avg_curve(curve, n_points_avg)
plt.plot(avg_curve, '-r')
plt.legend(['Total Loss', 'Margin Loss', 'Reconstruction Loss'])
plt.savefig(save_dir + '/'+ str(datetime.datetime.now().strftime('%Y-%m-%d %H-%M')) + '_total_result.png')
plt.close()
def plot_acc_curve(total_train_acc,total_test_acc,save_dir):
plt.plot(total_train_acc, '-b',label = 'train_acc')
plt.plot(total_test_acc, '-r',label = 'test_acc')
plt.legend()
plt.ylabel('acc')
plt.xlabel('epoch')
plt.title('Accuracy of training and test')
plt.savefig(save_dir +'/'+ str(datetime.datetime.now().strftime('%Y-%m-%d %H-%M'))+'_total_acc.png')
plt.close()
def show_point_cloud(tuple,seg_label=[],title=None):
import matplotlib.pyplot as plt
if seg_label == []:
x = [x[0] for x in tuple]
y = [y[1] for y in tuple]
z = [z[2] for z in tuple]
ax = plt.subplot(111, projection='3d')
ax.scatter(x, y, z, c='b', cmap='spectral')
ax.set_zlabel('Z')
ax.set_ylabel('Y')
ax.set_xlabel('X')
else:
category = list(np.unique(seg_label))
color = ['b','r','g','y','w','b','p']
ax = plt.subplot(111, projection='3d')
for categ_index in range(len(category)):
tuple_seg = tuple[seg_label == category[categ_index]]
x = [x[0] for x in tuple_seg]
y = [y[1] for y in tuple_seg]
z = [z[2] for z in tuple_seg]
ax.scatter(x, y, z, c=color[categ_index], cmap='spectral')
ax.set_zlabel('Z')
ax.set_ylabel('Y')
ax.set_xlabel('X')
plt.title(title)
plt.show()