-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.py
43 lines (39 loc) · 1.43 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
import numpy as np
import torch
import torch.nn as nn
from torch.autograd import Variable
import math
import torch.nn.functional as F
import pdb
from sklearn.metrics import confusion_matrix
def Entropy(input_):
bs = input_.size(0)
epsilon = 1e-5
entropy = -input_ * torch.log(input_ + epsilon)
entropy = torch.sum(entropy, dim=1)
return entropy
def cal_acc(loader, netF, netB, netC, flag=False):
start_test = True
with torch.no_grad():
iter_test = iter(loader)
for i in range(len(loader)):
data = iter_test.next()
inputs = data[0]
labels = data[1]
inputs = inputs.cuda()
outputs = netC(netB(netF(inputs)))
if start_test:
all_output = outputs.float().cpu()
all_label = labels.float()
start_test = False
else:
all_output = torch.cat((all_output, outputs.float().cpu()), 0)
all_label = torch.cat((all_label, labels.float()), 0)
_, predict = torch.max(all_output, 1)
accuracy = torch.sum(torch.squeeze(predict).float() == all_label).item() / float(all_label.size()[0])
matrix = confusion_matrix(all_label, torch.squeeze(predict).float())
acc = matrix.diagonal()/matrix.sum(axis=1) * 100
aacc = acc.mean()
aa = [str(np.round(i, 2)) for i in acc]
acc = ' '.join(aa)
return aacc, acc