mirror of
https://github.com/mrdbourke/pytorch-deep-learning.git
synced 2026-11-27 06:07:09 +00:00
add pred and plot image function
This commit is contained in:
1 parent
3900991c70
commit
d5383fcbda
1 file changed
+97
-23
+97
-23
@@ -9,6 +9,7 @@ import numpy as np
|
||||
|
||||
from torch import nn
|
||||
|
||||
|
||||
def plot_decision_boundary(model: torch.nn.Module, X: torch.Tensor, y: torch.Tensor):
|
||||
"""Plots decision boundaries of model predicting on X in comparison to y.
|
||||
|
||||
@@ -21,8 +22,7 @@ def plot_decision_boundary(model: torch.nn.Module, X: torch.Tensor, y: torch.Ten
|
||||
# Setup prediction boundaries and grid
|
||||
x_min, x_max = X[:, 0].min() - 0.1, X[:, 0].max() + 0.1
|
||||
y_min, y_max = X[:, 1].min() - 0.1, X[:, 1].max() + 0.1
|
||||
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 101),
|
||||
np.linspace(y_min, y_max, 101))
|
||||
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 101), np.linspace(y_min, y_max, 101))
|
||||
|
||||
# Make features
|
||||
X_to_pred_on = torch.from_numpy(np.column_stack((xx.ravel(), yy.ravel()))).float()
|
||||
@@ -34,10 +34,10 @@ def plot_decision_boundary(model: torch.nn.Module, X: torch.Tensor, y: torch.Ten
|
||||
|
||||
# Test for multi-class or binary and adjust logits to prediction labels
|
||||
if len(torch.unique(y)) > 2:
|
||||
y_pred = torch.softmax(y_logits, dim=1).argmax(dim=1) # mutli-class
|
||||
else:
|
||||
y_pred = torch.round(torch.sigmoid(y_logits)) # binary
|
||||
|
||||
y_pred = torch.softmax(y_logits, dim=1).argmax(dim=1) # mutli-class
|
||||
else:
|
||||
y_pred = torch.round(torch.sigmoid(y_logits)) # binary
|
||||
|
||||
# Reshape preds and plot
|
||||
y_pred = y_pred.reshape(xx.shape).detach().numpy()
|
||||
plt.contourf(xx, yy, y_pred, cmap=plt.cm.RdYlBu, alpha=0.7)
|
||||
@@ -45,29 +45,29 @@ def plot_decision_boundary(model: torch.nn.Module, X: torch.Tensor, y: torch.Ten
|
||||
plt.xlim(xx.min(), xx.max())
|
||||
plt.ylim(yy.min(), yy.max())
|
||||
|
||||
|
||||
# Plot linear data or training and test and predictions (optional)
|
||||
def plot_predictions(train_data,
|
||||
train_labels,
|
||||
test_data,
|
||||
test_labels,
|
||||
predictions=None):
|
||||
"""
|
||||
def plot_predictions(
|
||||
train_data, train_labels, test_data, test_labels, predictions=None
|
||||
):
|
||||
"""
|
||||
Plots linear training data and test data and compares predictions.
|
||||
"""
|
||||
plt.figure(figsize=(10, 7))
|
||||
plt.figure(figsize=(10, 7))
|
||||
|
||||
# Plot training data in blue
|
||||
plt.scatter(train_data, train_labels, c="b", s=4, label="Training data")
|
||||
|
||||
# Plot test data in green
|
||||
plt.scatter(test_data, test_labels, c="g", s=4, label="Testing data")
|
||||
# Plot training data in blue
|
||||
plt.scatter(train_data, train_labels, c="b", s=4, label="Training data")
|
||||
|
||||
if predictions is not None:
|
||||
# Plot the predictions in red (predictions were made on the test data)
|
||||
plt.scatter(test_data, predictions, c="r", s=4, label="Predictions")
|
||||
# Plot test data in green
|
||||
plt.scatter(test_data, test_labels, c="g", s=4, label="Testing data")
|
||||
|
||||
if predictions is not None:
|
||||
# Plot the predictions in red (predictions were made on the test data)
|
||||
plt.scatter(test_data, predictions, c="r", s=4, label="Predictions")
|
||||
|
||||
# Show the legend
|
||||
plt.legend(prop={"size": 14})
|
||||
|
||||
# Show the legend
|
||||
plt.legend(prop={"size": 14});
|
||||
|
||||
# Calculate accuracy (a classification metric)
|
||||
def accuracy_fn(y_true, y_pred):
|
||||
@@ -137,3 +137,77 @@ def plot_loss_curves(results):
|
||||
plt.title("Accuracy")
|
||||
plt.xlabel("Epochs")
|
||||
plt.legend()
|
||||
|
||||
|
||||
# Pred and plot image function from notebook 04
|
||||
# See creation: https://www.learnpytorch.io/04_pytorch_custom_datasets/#113-putting-custom-image-prediction-together-building-a-function
|
||||
from typing import List
|
||||
import torchvision
|
||||
|
||||
|
||||
def pred_and_plot_image(
|
||||
model: torch.nn.Module,
|
||||
image_path: str,
|
||||
class_names: List[str] = None,
|
||||
transform=None,
|
||||
device: torch.device = "cuda" if torch.cuda.is_avilable() else "cpu",
|
||||
):
|
||||
"""Makes a prediction on a target image with a trained model and plots the image.
|
||||
|
||||
Args:
|
||||
model (torch.nn.Module): trained PyTorch image classification model.
|
||||
image_path (str): filepath to target image.
|
||||
class_names (List[str], optional): different class names for target image. Defaults to None.
|
||||
transform (_type_, optional): transform of target image. Defaults to None.
|
||||
device (torch.device, optional): target device to compute on. Defaults to "cuda" if torch.cuda.is_avilable() else "cpu".
|
||||
|
||||
Returns:
|
||||
Matplotlib plot of target image and model prediction as title.
|
||||
|
||||
Example usage:
|
||||
pred_and_plot_image(model=model,
|
||||
image="some_image.jpeg",
|
||||
class_names=["class_1", "class_2", "class_3"],
|
||||
transform=torchvision.transforms.ToTensor(),
|
||||
device=device)
|
||||
"""
|
||||
|
||||
# 1. Load in image and convert the tensor values to float32
|
||||
target_image = torchvision.io.read_image(str(image_path)).type(torch.float32)
|
||||
|
||||
# 2. Divide the image pixel values by 255 to get them between [0, 1]
|
||||
target_image = target_image / 255.0
|
||||
|
||||
# 3. Transform if necessary
|
||||
if transform:
|
||||
target_image = transform(target_image)
|
||||
|
||||
# 4. Make sure the model is on the target device
|
||||
model.to(device)
|
||||
|
||||
# 5. Turn on model evaluation mode and inference mode
|
||||
model.eval()
|
||||
with torch.inference_mode():
|
||||
# Add an extra dimension to the image
|
||||
target_image = target_image.unsqueeze(dim=0)
|
||||
|
||||
# Make a prediction on image with an extra dimension and send it to the target device
|
||||
target_image_pred = model(target_image.to(device))
|
||||
|
||||
# 6. Convert logits -> prediction probabilities (using torch.softmax() for multi-class classification)
|
||||
target_image_pred_probs = torch.softmax(target_image_pred, dim=1)
|
||||
|
||||
# 7. Convert prediction probabilities -> prediction labels
|
||||
target_image_pred_label = torch.argmax(target_image_pred_probs, dim=1)
|
||||
|
||||
# 8. Plot the image alongside the prediction and prediction probability
|
||||
plt.imshow(
|
||||
target_image.squeeze().permute(1, 2, 0)
|
||||
) # make sure it's the right size for matplotlib
|
||||
if class_names:
|
||||
title = f"Pred: {class_names[target_image_pred_label.cpu()]} | Prob: {target_image_pred_probs.max().cpu():.3f}"
|
||||
else:
|
||||
title = f"Pred: {target_image_pred_label} | Prob: {target_image_pred_probs.max().cpu():.3f}"
|
||||
plt.title(title)
|
||||
plt.axis(False)
|
||||
|
||||
Reference in new issue
Block a user