-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
54 lines (43 loc) · 1.58 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
from torchvision import transforms, datasets
import torch
import torchvision
import torch.nn as nn
from torch.utils.data import dataset, dataloader
import matplotlib.pyplot as plt
import numpy as np
import torch.nn.functional as F
from Recog_modelv1 import Net
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = Net().to(device)
print(model)
criterion = nn.CrossEntropyLoss().to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.0001)
data_transform = transforms.Compose([
transforms.ToTensor()
])
model.load_state_dict(torch.load('\dev\Alzheimers\Saved_Model\RecogModelv4.pt'))
test_data = torchvision.datasets.ImageFolder("\dev\Alzheimers\seperated-data\resized_val", transform=data_transform)
test_loader = torch.utils.data.DataLoader(test_data, batch_size=1, shuffle = False, num_workers=4)
correct = 0
total = 0
pred=[]
label=[]
with torch.no_grad():
for inputs, labels in test_loader:
inputs=inputs.to(device)
labels = labels.to(device)
outputs = model(inputs[0][0].reshape(1,1,inputs.shape[2],inputs.shape[3]))
loss = criterion(outputs, labels)
print(loss.item())
_, predicted = torch.max(outputs.data, 1)
pred.append(predicted)
label.append(labels)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the test images: %d %%' % (100 * correct / total))
print(predicted,labels)
print('Accuracy of the network on the test images: %d %%' % (
100 * correct / total))
# print(total)
# print(pred)
# print(label)