-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcompat_utils.py
48 lines (40 loc) · 1.27 KB
/
compat_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
"""
Utility functions for 3D models training.
"""
import numpy as np
import torch
def compute_overall_iou(pred_np, target_np, num_classes):
"""
Compute overall IoU given prediction and target numpy arrays.
"""
shape_ious = []
for shape_idx in range(pred_np.shape[0]): # sample_idx
part_ious = []
for part in range(num_classes):
intersect = np.sum(
np.logical_and(pred_np[shape_idx] == part, target_np[shape_idx] == part)
)
union = np.sum(
np.logical_or(pred_np[shape_idx] == part, target_np[shape_idx] == part)
)
n_preds = np.sum(target_np[shape_idx] == part)
if n_preds != 0:
iou = intersect / float(union)
part_ious.append(iou)
shape_ious.append(np.mean(part_ious))
return shape_ious
def inplace_relu(m):
"""
Set all ReLU layers in a model to be inplace.
"""
classname = m.__class__.__name__
if classname.find("ReLU") != -1:
m.inplace = True
def to_categorical(y, num_classes):
"""
Convert a class vector (integers) to a one-hot vector.
"""
new_y = torch.eye(num_classes)[y.cpu().data.numpy(),]
if y.is_cuda:
return new_y.cuda()
return new_y