-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.py
55 lines (39 loc) · 1.49 KB
/
demo.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
from dataloader import RetinopathyLoader
from train import load_model
from torch.utils.data import DataLoader
from tqdm import tqdm
import torch.nn as nn
import torch
def demo(checkpoints):
device="cuda:0"
# prepare data
test_set=RetinopathyLoader("./Data/data","test",256)
test_loader=DataLoader(test_set,batch_size=32,num_workers=4)
test_size=len(test_set)
for _model in checkpoints:
print(_model)
model_path=checkpoints[_model]
model=load_model(model_path)
model.to(device)
model.eval()
loss_func=nn.CrossEntropyLoss()
test_loss=0.0
test_acc=0.0
for idx,(inputs,targets) in enumerate(tqdm(test_loader)):
inputs = inputs.to(device, dtype=torch.float)
targets = targets.to(device, dtype=torch.long)
outputs = model(inputs)
test_loss += loss_func(outputs, targets).item()*inputs.shape[0]
_, predicted = torch.max(outputs.data, 1)
test_acc += (predicted == targets).sum().item()
test_loss /= test_size
test_acc /= test_size
print(f"test_loss: {test_loss:.4}\ttest_acc: {test_acc:.4}")
if __name__=='__main__':
checkpoints={
'resnet50_pretrained':'./checkpoints/best/resnet50_pretrained.pt',
'resnet50':'./checkpoints/best/resnet50.pt',
'resnet18_pretrained':'./checkpoints/best/resnet18_pretrained.pt',
'resnet18':'./checkpoints/best/resnet18.pt',
}
demo(checkpoints)