-
plt.imshow(target_image.squeeze().permute(1, 2, 0)) # make sure it's the right size for matplotlib why don't we turn this "f"Pred: {target_image_pred_label} ..." into CPU as well (?) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Example Code:Here's an improved version of your snippet with explanations: import matplotlib.pyplot as plt
# Assume `target_image` is a tensor of shape (C, H, W)
plt.imshow(target_image.squeeze().permute(1, 2, 0)) # Make sure the image is in the right format for Matplotlib
# Convert `target_image_pred_label` to CPU if it's a tensor
if isinstance(target_image_pred_label, torch.Tensor):
target_image_pred_label = target_image_pred_label.cpu().item()
# Format the title based on whether class names are available
if class_names:
title = f"Pred: {class_names[target_image_pred_label]} | 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('off') # Turn off axis
plt.show() Summary:
In summary, the key is to understand whether the data is a tensor or a scalar and whether it needs to be moved to the CPU for plotting or other operations. |
Beta Was this translation helpful? Give feedback.
Example Code:
Here's an improved version of your snippet with explanations: