MulaiMulai sekarang secara gratis

Image classification with ResNet

You have created the model from the pre-trained ResNet18. Now, it is time to test it on an example image.

You are going to apply preprocessing transforms to an image and classify it. You will need to use the softmax() layer followed by the argmax(), since ResNet18 has been trained on a multi-class dataset.

You have selected the following image to use for prediction testing: A cup of espresso

The preprocessing transform is saved as preprocess. The PIL image is uploaded as img.

Latihan ini adalah bagian dari kursus

Deep Learning for Images with PyTorch

Lihat Kursus

Petunjuk latihan

  • Apply the preprocessing transforms to the image and reshape it using .unsqueeze(0) to add the batch dimension.
  • Pass the image through the model, reshape the output using .squeeze(0) to remove the batch dimension, and add a softmax() layer.
  • Apply argmax() to select the highest-probability class.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# Apply preprocessing transforms
batch = ____.____

# Apply model with softmax layer
prediction = ____.____.____

# Apply argmax
class_id = prediction.____.item()
score = prediction[class_id].item()
category_name = weights.meta["categories"][class_id]
print(category_name)
Edit dan Jalankan Kode