Get startedGet started for free

Image tensors

A coffee company has an object detection project where they need to annotate objects of interest, in this case, espresso shots. You have created a list with the bounding box coordinates for an espresso shot image. Now, you need to convert the image and the coordinates into tensors.

torch and torchvision have been imported. torchvision.transforms is imported as transforms. The image has been loaded as image using Image.open() from PIL library. The bounding box coordinates are stored in the variable bbox.

espresso

This exercise is part of the course

Deep Learning for Images with PyTorch

View Course

Exercise instructions

  • Convert the bbox into tensors using torch.tensor().
  • Reshape bbox_tensor by adding a batch dimension using unsqueeze(0).
  • Create a transform to resize image to (224) and transform to an unscaled image tensor.
  • Apply transform to image.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Convert bbox into tensors
bbox_tensor = ____

# Add a new batch dimension
bbox_tensor = bbox_tensor.____

# Resize image and transform tensor
transform = transforms.Compose([
  transforms.____,
  transforms.____
])

# Apply transform to image
image_tensor = ____
print(image_tensor)
Edit and Run Code