-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathutils.py
162 lines (147 loc) · 6.17 KB
/
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
# *_*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 pandas as pd
import torch.nn.functional as F
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):
savepath = path + '/checkpoint-%f-%04d.pth' % (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(model, loader):
metrics = defaultdict(lambda:list())
hist_acc = []
for batch_id, (x, y) in tqdm(enumerate(loader), total=len(loader),smoothing=0.9):
x = x.float().cuda()
y = y.long().cuda()
x = x.permute(0,2,1)
y_pred,_ = model(x)
_, y_pred = torch.max(y_pred, -1)
pred_choice = y_pred.data.max(1)[1]
correct = pred_choice.eq(y.data).cpu().sum()
metrics['accuracy'].append(correct.data)
hist_acc.append(np.mean(metrics['accuracy']))
metrics['accuracy'] = np.mean(metrics['accuracy'])
return metrics, hist_acc
def compute_iou(pred,target,iou_tabel=None):
ious = []
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 np.unique(batch_target):
intersection = np.sum((batch_target == cat) & (batch_choice == cat))
union = float(np.sum((batch_target == cat) | (batch_choice == cat)))
iou = intersection/union
ious.append(iou)
iou_tabel[cat,0] += iou
iou_tabel[cat,1] += 1
return np.mean(ious), iou_tabel
def test_seg(model, loader, catdict, num_classes = 13):
''' catdict = {0:Airplane, 1:Airplane, ...49:Table} '''
iou_tabel = np.zeros((len(catdict),3))
metrics = defaultdict(lambda:list())
hist_acc = []
for batch_id, (points, target) in tqdm(enumerate(loader), total=len(loader), smoothing=0.9):
batchsize, num_point, _ = points.size()
points, target = Variable(points.float()), Variable(target.long())
points = points.transpose(2, 1)
points, target = points.cuda(), target.cuda()
pred = model(points[:,:3,:],points[:,3:,:])
mean_iou, iou_tabel = compute_iou(pred,target,iou_tabel)
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))
metrics['iou'].append(mean_iou)
iou_tabel[:,2] = iou_tabel[:,0] /(iou_tabel[:,1]+0.01)
hist_acc += metrics['accuracy']
metrics['accuracy'] = np.mean(metrics['accuracy'])
iou_tabel = pd.DataFrame(iou_tabel,columns=['iou','count','mean_iou'])
iou_tabel['Category_IOU'] = [cat_value for cat_value in catdict.values()]
cat_iou = iou_tabel.groupby('Category_IOU')['mean_iou'].mean()
return metrics, hist_acc, 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()