-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheval.py
96 lines (74 loc) · 3.06 KB
/
eval.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import argparse
import os
import torch.utils.data as data
import torch
from utils.classifier_dataset import PneumoniaDataset, ImageTransform
from utils.metrics import classifier_evaluate
from utils.plot_utils import plot_confusion_matrix
from utils.common_utils import create_folder
from models.create_model import create_model
def predict(model, data_loader):
"""
Tests the model on the given data_loader and returns the predicted and actual labels.
Args:
- model: the model to be tested
- data_loader: the data loader to be used for testing
Returns:
- pred_lst: a list of predicted labels
- label_lst: a list of actual labels
"""
model.eval()
pred_lst = []
label_lst = []
with torch.no_grad():
for inputs, targets in data_loader:
inputs, targets = inputs.cuda(), targets.cuda()
outputs = model(inputs)
_, pred = torch.max(outputs, 1)
pred_lst.extend(pred.cpu().numpy())
label_lst.extend(targets.cpu().numpy())
return pred_lst, label_lst
def test(ckpt_path, device, save_dir='figures/confusion_matrix/'):
create_folder(save_dir)
# build model
model = create_model('resnet18-sam', pretrained=False)
if model is None:
assert "model can not be None!"
# load checkpoints
state_dict = torch.load(ckpt_path, map_location=device)
model.load_state_dict(state_dict)
model.eval()
# Get pred and labels for evaluation
pred, labels = predict(model, test_loader)
# get metrics
accuracy, precision, recall, f1, cm = classifier_evaluate(pred, labels)
print(f"The accuracy: {100. * accuracy:.2f} %")
print(f"The precision: {100. * precision:.2f} %")
print(f"The recall: {100. * recall:.2f} %")
print(f"f1: {100. * f1:.2f} %")
print(f"Confusion matrix: {cm}")
# save cm
plot_confusion_matrix(cm, save_dir, title='ChestXRay-CM')
if __name__ == '__main__':
# Define cmd arguments
parser = argparse.ArgumentParser()
parser.add_argument('--ckpt-path', type=str, required=True, default='pretrained/resnet18-sam.pth',
help='checkpoints path for inference')
parser.add_argument('--batch-size', type=int, default=8,
help='batch size for training (default: 32)')
parser.add_argument('--workers', type=int, default=0,
help='number of data loading workers (default: 4)')
parser.add_argument('--device', type=str, default='cuda',
help='inference device. [cuda | cpu]')
args = parser.parse_args()
# Check input arguments
if not os.path.exists(args.ckpt_path):
print(f'Cannot find the checkpoints: {args.ckpt_path}')
exit()
# Load test dataset
test_dataset = PneumoniaDataset('./data/real', './data/fake', mode='test',
transform=ImageTransform())
test_loader = torch.utils.data.DataLoader(
test_dataset, batch_size=args.batch_size, shuffle=False, num_workers=args.workers)
# Run to test
test(args.ckpt_path, args.device)