-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
96 lines (68 loc) · 3.18 KB
/
Copy pathpredict.py
File metadata and controls
96 lines (68 loc) · 3.18 KB
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
"""
Author: Jacob Pitsenberger
Date: 12-21-23
Module: predict.py
This module provides functions for making predictions using trained neural network models. It includes a prediction
function and a main function for demonstrating predictions with models trained with and without a validation set.
Functions:
- predict(model: Net, images: torch.Tensor, labels: torch.Tensor) -> None:
Makes predictions using a trained neural network model and visualizes the results.
- main() -> None:
Main function to demonstrate predictions with models trained with and without a validation set.
"""
import torch
from net import Net
import numpy as np
import matplotlib.pyplot as plt
from load_and_visualize_data import get_loaders_no_validation, get_batch
def predict(model: Net, images: torch.Tensor, labels: torch.Tensor) -> None:
"""Make predictions using a trained neural network model and visualize the results.
Args:
- model (Net): Trained neural network model.
- images (torch.Tensor): Batch of images for prediction.
- labels (torch.Tensor): True labels corresponding to the images.
Returns:
None
"""
# get sample outputs
output = model(images)
# convert output probabilities to predicted class
_, preds = torch.max(output, 1)
# prep images for display
images = images.numpy()
# plot the images in the batch, along with predicted and true labels
fig = plt.figure(figsize=(25, 4))
# Iterate over the first 20 indices in the batch for visualization
for idx in np.arange(20):
# Create a subplot for each image in a 2x10 grid
ax = fig.add_subplot(2, int(20 / 2), idx + 1, xticks=[], yticks=[])
# Display the image in grayscale using imshow
ax.imshow(np.squeeze(images[idx]), cmap='gray')
# Set the title for the subplot with the predicted and true labels
title_text = "{} ({})".format(str(preds[idx].item()), str(labels[idx].item()))
# Set the title color based on prediction correctness (green if correct, red if incorrect)
title_color = "green" if preds[idx] == labels[idx] else "red"
# Set the title of the subplot with the formatted text and color
ax.set_title(title_text, color=title_color)
# Display the entire plot
plt.show()
def main() -> None:
"""Main function to demonstrate predictions with models trained with and without a validation set.
Returns:
None
"""
# Instantiate models and load pre-trained weights
model_nv = Net()
model_nv.load_state_dict(torch.load('output/model_no_validation.pt'))
model_v = Net()
model_v.load_state_dict(torch.load('output/model_with_validation.pt'))
# get our test_loader, either method call to get this works (validation or no validation - same test set regardless)
train_loader, test_loader = get_loaders_no_validation()
# get a batch of images and their labels from the test data.
images, labels = get_batch(test_loader)
# Predict with the model that wasn't trained with a validation set.
predict(model_nv, images, labels)
# Predict with the model that was trained with a validation set.
predict(model_v, images, labels)
if __name__ == "__main__":
main()