2

x is the image, yis the label, and metadata are dates, times etc.

for x, y_true, metadata in train_loader:
  print(x.shape)

The shape returns:

torch.Size([16, 3, 448, 448])

How do I go about displaying x as an image? Do I use plt?

2
  • 1
    Hi there! you can use matplotlib.pyplot as you say, but for that you have to consider that you have 16x3xWxH tensor, you will have to iterate over first dimention and then transpose (using torch transpose you have to transpose 2 times to get WxHx3 or only one to get HxWx3 ) you alternatively can convert the tensor to numpy to transpose automatically all dimensions Commented Nov 11, 2021 at 18:23
  • 1
    also I remomend you to use plt.subplot(4,4,1) and then call a for with a for idx,im in enumerate(x): Commented Nov 11, 2021 at 18:28

1 Answer 1

1

Your x is not a single image, but rather a batch of 16 different images, all of size 448x448 pixels.

You can use torchvision.utils.make_grid to convert x into a grid of 4x4 images, and then plot it:

import torchvision

with torch.no_grad():  # no need for gradients here
  grid = torchvision.utils.make_grid(x, nrow=4)  # you might consider normalize=True 
  # convert the grid into a numpy array suitable for plt
  grid_np = grid.cpu().numpy().transpose(1, 2, 0)  # channel dim should be last
  plt.matshow(grid_np)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.