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.

Questo esercizio fa parte del corso
Deep Learning for Images with PyTorch
Istruzioni dell'esercizio
- 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.
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
# 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)