-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsome_notes_on_testing_model.py
81 lines (58 loc) · 2.27 KB
/
some_notes_on_testing_model.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 19 17:13:41 2022
@author: wendytsai
"""
#from https://github.com/musikalkemist/pytorchforaudio/tree/main/03%20Making%20predictions
import torch
from train import model, download_mnist_datasets
class_mapping = [
"0",
"1",
"2",
]
def predict(model, input, target, class_mapping):
model.eval()
with torch.no_grad():
predictions = model(input)
# Tensor (1, 10) -> [ [0.1, 0.01, ..., 0.6] ]
predicted_index = predictions[0].argmax(0)
predicted = class_mapping[predicted_index]
expected = class_mapping[target]
return predicted, expected
if __name__ == "__main__":
# load back the model
model = model()
state_dict = torch.load("model.pth")
model.load_state_dict(state_dict)
# load MNIST validation dataset
_, validation_data = download_mnist_datasets()
# get a sample from the validation dataset for inference
input, target = validation_data[0][0], validation_data[0][1]
# make an inference
predicted, expected = predict(model, input, target,
class_mapping)
print(f"Predicted: '{predicted}', expected: '{expected}'")
# from https://learn.microsoft.com/en-us/windows/ai/windows-ml/tutorials/pytorch-train-model
import matplotlib.pyplot as plt
import numpy as np
# Function to show the images
def imageshow(img):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()
# Function to test the model with a batch of images and show the labels predictions
def testBatch():
# get batch of images from the test DataLoader
images, labels = next(iter(test_loader))
# show all images as one image grid
imageshow(torchvision.utils.make_grid(images))
# Let's see what if the model identifiers the labels of those example
outputs = model(images)
# We got the probability for every 10 labels. The highest (max) probability should be correct label
_, predicted = torch.max(outputs, 1)
# Let's show the predicted labels on the screen to compare with the real ones
print('Predicted: ', ' '.join('%5s' % classes[predicted[j]]
for j in range(batch_size)))