Running semantic segmentation
Good job designing the U-Net! You will find an already pre-trained model very similar to the one you have just built available to you. This model has been trained on a large set of images and contains a few small additions to the architecture, such as the batch norm layers.
You can instantiate the model as UNet(), which will provide the model with the pre-trained weights. You task is to use it to produce segmentation masks for the following image of a car.

Image from PIL has already been imported for you.
Questo esercizio fa parte del corso
Deep Learning for Images with PyTorch
Istruzioni dell'esercizio
- Instantiate
UNet()in a variable calledmodel. - Load the image at
car.jpgto a variable calledimage. - Produce segmentation masks by passing the image to the model and
squeeze(0)-ing the output.
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
# Load model
model = ____
model.eval()
# Load and transform image
image = ____
transform = transforms.Compose([transforms.ToTensor()])
image_tensor = transform(image).unsqueeze(0)
# Predict segmentation mask
with torch.no_grad():
prediction = ____
# Display mask
plt.imshow(prediction[1, :, :])
plt.show()