-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
64 lines (57 loc) · 1.63 KB
/
test.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import os
def test(device,test_path,testloader):
total=0
i=0
model = torch.load(test_path)
max_acc=0
min_acc=1
with torch.no_grad():
for batch_num,data in enumerate(testloader):
inputs, target = data
inputs = inputs.to(device)
target = target.to(device)
output = model(inputs)
prediction = torch.max(output, 1)
test_correct = np.mean(prediction[1].cpu().numpy() == target.cpu().numpy())
total +=test_correct
max_acc=max(max_acc,test_correct)
min_acc=min(min_acc,test_correct)
i+=1
print("Min Accuracy:-"+str(min_acc))
print("Max Accuracy:-"+str(max_acc))
print("Mean Accuracy:-"+str(total/i))
def test_folder(device,test_path,testloader):
max_acc_model=0
model_name=""
total=0
i=0
paths=[os.path.join(test_path,i) for i in os.listdir(test_path)]
for path in paths:
model = torch.load(path)
max_acc=0
min_acc=1
with torch.no_grad():
for batch_num,data in enumerate(testloader):
inputs, target = data
inputs = inputs.to(device)
target = target.to(device)
output = model(inputs)
prediction = torch.max(output, 1)
test_correct = np.mean(prediction[1].cpu().numpy() == target.cpu().numpy())
total +=test_correct
max_acc=max(max_acc,test_correct)
min_acc=min(min_acc,test_correct)
i+=1
print(path)
print("Min Accuracy:-"+str(min_acc))
print("Max Accuracy:-"+str(max_acc))
print("Mean Accuracy:-"+str(total/i))
if max_acc_model<total/i:
max_acc_model=total/i
model_name=path
print("Best Model:-"+model_name)
print("Best Model Accuracy:-"+str(max_acc_model))