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.

This exercise is part of the course
Deep Learning for Images with PyTorch
Exercise instructions
- Convert the
bboxinto tensors usingtorch.tensor(). - Reshape
bbox_tensorby adding a batch dimension usingunsqueeze(0). - Create a transform to resize
imageto(224)and transform to an unscaled image tensor. - Apply
transformtoimage.
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)