Aan de slagGa gratis aan de slag

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

Deze oefening maakt deel uit van de cursus

Deep Learning for Images with PyTorch

Cursus bekijken

Oefeninstructies

  • 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.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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)
Code bewerken en uitvoeren