{ "cells": [ { "cell_type": "markdown", "id": "7edf7168", "metadata": {}, "source": [ "# TD2: Deep learning" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 2, "id": "6e18f2fd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CUDA is not available. Training on CPU ...\n" ] } ], "source": [ "import torch\n", "from torchvision import transforms\n", "from torchvision import models\n", "\n", "\n", "# check if CUDA is available\n", "train_on_gpu = torch.cuda.is_available()\n", "\n", "if not train_on_gpu:\n", " print(\"CUDA is not available. Training on CPU ...\")\n", "else:\n", " print(\"CUDA is available! Training on GPU ...\")" ] }, { "cell_type": "markdown", "id": "201470f9", "metadata": {}, "source": [ "## Exercise 3: working with pre-trained models.\n", "\n", "PyTorch offers several pre-trained models https://pytorch.org/vision/0.8/models.html \n", "We will use ResNet50 trained on ImageNet dataset (https://www.image-net.org/index.php). Use the following code with the files `imagenet-simple-labels.json` that contains the imagenet labels and the image dog.png that we will use as test.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "b4d13080", "metadata": {}, "outputs": [], "source": [ "import json\n", "from PIL import Image\n", "\n", "# Choose an image to pass through the model\n", "test_image = \"dog.png\"\n", "\n", "# Configure matplotlib for pretty inline plots\n", "#%matplotlib inline\n", "#%config InlineBackend.figure_format = 'retina'\n", "\n", "# Prepare the labels\n", "with open(\"imagenet-simple-labels.json\") as f:\n", " labels = json.load(f)\n", "\n", "# First prepare the transformations: resize the image to what the model was trained on and convert it to a tensor\n", "data_transform = transforms.Compose(\n", " [\n", " transforms.Resize((224, 224)),\n", " transforms.ToTensor(),\n", " transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),\n", " ]\n", ")\n", "\n", "# Load the image\n", "image = Image.open(test_image)\n", "plt.imshow(image), plt.xticks([]), plt.yticks([])\n", "\n", "# Now apply the transformation, expand the batch dimension, and send the image to the GPU\n", "# image = data_transform(image).unsqueeze(0).cuda()\n", "image = data_transform(image).unsqueeze(0)\n", "\n", "# Download the model if it's not there already. It will take a bit on the first run, after that it's fast\n", "model = models.resnet50(pretrained=True)\n", "\n", "# Send the model to the GPU\n", "# model.cuda()\n", "# Set layers such as dropout and batchnorm in evaluation mode\n", "model.eval()\n", "\n", "# Get the 1000-dimensional model output\n", "out = model(image)\n", "# Find the predicted class\n", "print(\"Predicted class is: {}\".format(labels[out.argmax()]))" ] }, { "cell_type": "markdown", "id": "184cfceb", "metadata": {}, "source": [ "Experiments:\n", "\n", "Study the code and the results obtained. Possibly add other images downloaded from the internet.\n", "\n", "What is the size of the model? Quantize it and then check if the model is still able to correctly classify the other images.\n", "\n", "Experiment with other pre-trained CNN models.\n", "\n", " \n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.8.5 ('base')", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" }, "vscode": { "interpreter": { "hash": "9e3efbebb05da2d4a1968abe9a0645745f54b63feb7a85a514e4da0495be97eb" } } }, "nbformat": 4, "nbformat_minor": 5 }