-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
144 lines (115 loc) · 5.1 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
# *_*coding:utf-8 *_*
import os
import shutil
import numpy as np
import torch
from torch.autograd import Variable
from tqdm import tqdm
from collections import defaultdict
import pandas as pd
from dataloader import generate_plyfile, plydataset
from loss import IoULoss, DiceLoss
def compute_cat_iou(pred,target,iou_tabel): # pred [B,N,C] target [B,N]
iou_list = []
target = target.cpu().data.numpy()
for j in range(pred.size(0)):
batch_pred = pred[j] # batch_pred [N,C]
batch_target = target[j] # batch_target [N]
batch_choice = batch_pred.data.max(1)[1].cpu().data.numpy() # index of max value batch_choice [N]
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 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 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 compute_mACC(pred, label_face):
s = 0.
for i in range(33):
p = torch.where(pred==i, 1., 0.)
l = torch.where(label_face==i, 1., 0.)
acc = (p * l).sum() / (l.sum() + 1)
s += acc
return s / 17
def test_semseg(model, loader, num_classes = 8, gpu=True, generate_ply=False):
'''
Input
:param model:
:param loader:
:param num_classes:
:param pointnet2:
Output
metrics: metrics['accuracy']-> overall accuracy
metrics['iou']-> mean Iou
hist_acc: history of accuracy
cat_iou: IoU for o category
'''
iou_tabel = np.zeros((num_classes,3))
metrics = defaultdict(lambda:list())
dice_loss = DiceLoss()
hist_acc = []
macc = 0
mdice = 0
shutil.rmtree('./pred_global')
os.mkdir('./pred_global')
for batch_id, (index, points, label_face, label_face_onehot, name, raw_points_face, idx_face) in tqdm(enumerate(loader), total=len(loader), smoothing=0.9):
batchsize, num_point, _ = points.size()
points_face = raw_points_face[0].numpy()
index_face = index[0].numpy()
coordinate = points.transpose(2,1)
normal = points[:, :, 12:]
centre = points[:, :, 9:12]
label_face = label_face[:, :, 0]
coordinate, label_face, centre, idx_face = Variable(coordinate.float()), Variable(label_face.long()), Variable(centre.float()), Variable(idx_face.float())
coordinate, label_face, centre, idx_face = coordinate.cuda(), label_face.cuda(), centre.cuda(), idx_face.cuda()
with torch.no_grad():
# pred, _ = model(coordinate, idx_face)
pred = model(coordinate, idx_face)
# pred = model(coordinate)
mdice += dice_loss(pred.max(dim=-1)[0], label_face)
iou_tabel, iou_list = compute_cat_iou(pred,label_face,iou_tabel)
pred = pred.contiguous().view(-1, num_classes)
label_face = label_face.view(-1, 1)[:, 0]
pred_choice = pred.data.max(1)[1]
macc += compute_mACC(pred_choice, label_face).cpu().data.numpy()
correct = pred_choice.eq(label_face.data).cpu().sum()
metrics['accuracy'].append(correct.item()/ (batchsize * num_point))
label_face = pred_choice.cpu().reshape(pred_choice.shape[0], 1)
if generate_ply:
#label_face=label_optimization(index_face, label_face)
generate_plyfile(index_face, points_face, label_face, path=("pred_global/%s") % name)
iou_tabel[:,2] = iou_tabel[:,0] /iou_tabel[:,1]
# iou = np.where(iou_tabel<=1.)
hist_acc += metrics['accuracy']
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'] = ["label%d"%(i) for i in range(num_classes)]
cat_iou = iou_tabel.groupby('Category_IOU')['mean_iou'].mean()
mIoU = np.mean(cat_iou)
# macc = macc / 29.
# mdice = mdice / 29.
return metrics, mIoU, cat_iou, macc, mdice