-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
63 lines (54 loc) · 2.35 KB
/
Copy pathutils.py
File metadata and controls
63 lines (54 loc) · 2.35 KB
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
import torch
import torch.nn as nn
import torch.nn.functional as F
def get_visual_interpretability_classification(nets_v_interpret, non_interpretable_image_label, input_images, sound_class_labels, device):
"""Returns
[0]: List of predicted class for each image of the batch. Ex: [0, 0, 1, 0, ..., 0]. Dimension equal to batch size.
[1]: List of sum of interpretable images for each class. Ex: [4, 0, 3, 6, 5]. Dimension equal to the number of sound classes.
"""
_, num_channels, height, width = input_images.size()
one_image_batch = torch.zeros((1, num_channels, height, width), device=device) # @UndefinedVariable
#transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
predictions = []
predictions_by_class = [0]*len(nets_v_interpret)
for i, img in enumerate(input_images):
one_image_batch[0] = img
output = nets_v_interpret[sound_class_labels[i]](one_image_batch)
num_classes = output.size()[1]
res = 0
if num_classes==1:
# Correct implementation. Variable target not initialized. Why use a tensor???
#predictions = torch.zeros(target.size(), device=device) # @UndefinedVariable
for i,o in enumerate(output):
if o > 0:
res = 1
else:
res = non_interpretable_image_label
elif num_classes==2:
res = output.max(1)[1].item()
predictions.append(res) # get the index of the max log-probability
if res > 0:
predictions_by_class[sound_class_labels[i]] += 1
return predictions, predictions_by_class
def get_activated(x, act_type, n_slope=0.2, alph=1.0):
if act_type == 'sigmoid':
x = torch.sigmoid(x) # @UndefinedVariable
elif act_type == 'relu':
x = F.relu(x)
elif act_type == 'l_relu':
l_relu = nn.LeakyReLU(negative_slope=n_slope)
x = l_relu(x)
elif act_type == 'softplus':
x = F.softplus(x)
elif act_type == 'elu':
elu = nn.ELU(alpha=alph)
x = elu(x)
elif act_type == 'celu':
celu = nn.CELU(alpha=alph)
x = celu(x)
elif act_type == 'selu':
selu = nn.SELU(alpha=alph)
x = selu(x)
elif act_type == 'tanh':
x = torch.tanh(x) # @UndefinedVariable
return x