-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfer.py
More file actions
45 lines (41 loc) · 1.38 KB
/
infer.py
File metadata and controls
45 lines (41 loc) · 1.38 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
from network import model
from dataset import MySet
from torch.utils.data import DataLoader
import os
import torch
import numpy as np
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
def test(net, loader):
net.eval()
predicts, labels = [], []
for batch_idx, (data, label) in enumerate(loader):
data = data.cuda()
y = net(data)
predict = y.data.cpu().numpy()
label = label.data.numpy()
predicts.extend(np.argmax(predict, axis=1))
labels.extend(label)
acc = accuracy_score(labels, predicts)
tn, fp, fn, tp = confusion_matrix(labels, predicts).ravel()
return acc, tn, fp, fn, tp
name = ''
txt_path = 'data/txt'
os.environ['CUDA_VISIBLE_DEVICES'] = '4'
# MODEL
output_path = "output_test/"+name
best_path = os.path.join(output_path, "best_weights")
best_weight = os.path.join(best_path, '15_0_2_0.9074.pth.gz')
net = model().cuda()
net.load_state_dict(torch.load(best_weight))
# DATA
test_set = MySet(txt_path, mode="test")
test_loader = DataLoader(test_set, batch_size=10, num_workers=0)
# TESTING
test_acc, tn, fp, fn, tp = test(net, test_loader)
precision = tp/(tp+fp)
sensitive = tp/(tp+fn)
specificity = tn/(tn+fp)
F1score = 2*tp/(2*tp+fp+fn)
print("The test acc:{}".format(test_acc))
print("The precision:{}|sensitive:{}|specificity:{}|F1score:{}".format(precision, sensitive, specificity, F1score))