|
112 | 112 | { |
113 | 113 | "cell_type": "code", |
114 | 114 | "execution_count": null, |
115 | | - "metadata": { |
116 | | - "cellView": "form", |
117 | | - "execution": {} |
118 | | - }, |
| 115 | + "metadata": {}, |
119 | 116 | "outputs": [], |
120 | 117 | "source": [ |
121 | | - "# @title Install dependencies\n", |
122 | | - "!pip install facenet-pytorch torchaudio==2.2.2 torchvision==0.17.2 torch==2.2.2 numpy==1.26.4 --quiet\n", |
123 | | - "!pip install Pillow>=10.2.0 --quiet" |
| 118 | + "# # @title Uncomment to install dependencies\n", |
| 119 | + "# # @markdown Install `facenet` - A model used to do facial recognition.\n", |
| 120 | + "# # @markdown This is an old package and requires we downgrade many others.\n", |
| 121 | + "# # @markdown You may be asked to restart your session the first time you run this.\n", |
| 122 | + "# !pip install facenet-pytorch torchvision==0.17.2 torchaudio==2.2.2 torch==2.2.2 numpy==1.26.4 --quiet\n", |
| 123 | + "# !pip install Pillow==10.2.0 --quiet" |
124 | 124 | ] |
125 | 125 | }, |
126 | 126 | { |
|
534 | 534 | { |
535 | 535 | "cell_type": "code", |
536 | 536 | "execution_count": null, |
537 | | - "metadata": { |
538 | | - "cellView": "form", |
539 | | - "execution": {} |
540 | | - }, |
| 537 | + "metadata": {}, |
541 | 538 | "outputs": [], |
542 | 539 | "source": [ |
543 | 540 | "# @title Import Alexnet\n", |
544 | | - "# @markdown This cell gives you the `alexnet` model as well as the `input_image` and `input_batch` variables used below\n", |
545 | | - "import requests, urllib\n", |
546 | | - "\n", |
547 | | - "# Original link: https://s3.amazonaws.com/pytorch/models/alexnet-owt-4df8aa71.pth\n", |
548 | | - "state_dict = torch.hub.load_state_dict_from_url(\"https://osf.io/9dzeu/download\")\n", |
| 541 | + "import requests, urllib, io, os\n", |
| 542 | + "\n", |
| 543 | + "# Download and save to disk first\n", |
| 544 | + "weights_path = \"alexnet-owt-4df8aa71.pth\"\n", |
| 545 | + "if not os.path.exists(weights_path):\n", |
| 546 | + " print(\"Downloading weights...\")\n", |
| 547 | + " response = requests.get(\"https://osf.io/9dzeu/download\", allow_redirects=True)\n", |
| 548 | + " response.raise_for_status()\n", |
| 549 | + " with open(weights_path, \"wb\") as f:\n", |
| 550 | + " f.write(response.content)\n", |
| 551 | + " print(f\"Saved {len(response.content) / 1e6:.1f} MB\")\n", |
| 552 | + "\n", |
| 553 | + "# Verify it's a valid torch file before loading\n", |
| 554 | + "print(f\"File size on disk: {os.path.getsize(weights_path) / 1e6:.1f} MB\")\n", |
| 555 | + "state_dict = torch.load(weights_path, map_location=\"cpu\", weights_only=False)\n", |
549 | 556 | "\n", |
550 | 557 | "alexnet = AlexNet()\n", |
551 | 558 | "alexnet.load_state_dict(state_dict=state_dict)\n", |
| 559 | + "print(\"Model loaded successfully!\")\n", |
552 | 560 | "\n", |
553 | 561 | "url, filename = (\"https://raw.githubusercontent.com/NeuromatchAcademy/course-content-dl/main/tutorials/W2D2_Convnets/static/dog.jpg\", \"dog.jpg\")\n", |
554 | 562 | "try: urllib.URLopener().retrieve(url, filename)\n", |
|
563 | 571 | " std=[0.229, 0.224, 0.225]),\n", |
564 | 572 | " ])\n", |
565 | 573 | "input_tensor = preprocess(input_image)\n", |
566 | | - "input_batch = input_tensor.unsqueeze(0) # Create a mini-batch as expected by the model\n", |
| 574 | + "input_batch = input_tensor.unsqueeze(0)\n", |
567 | 575 | "\n", |
568 | | - "# Move the input and model to GPU for speed if available\n", |
569 | 576 | "if torch.cuda.is_available():\n", |
570 | | - " input_batch = input_batch.cuda()\n", |
571 | | - " alexnet.cuda()" |
| 577 | + " input_batch = input_batch.cuda()\n", |
| 578 | + " alexnet.cuda()" |
572 | 579 | ] |
573 | 580 | }, |
574 | 581 | { |
|
3017 | 3024 | "---\n", |
3018 | 3025 | "# Summary\n", |
3019 | 3026 | "\n", |
3020 | | - "In this tutorial, you have learned about the modern Convnets (CNNs), their architecture, and operating principles. Also, you are now familiar with the notion of *Transfer Learning*, and you have learned when to apply it. If you have time left, you will learn more about the speed vs. accuracy trade-off. In the next tutorial, we will see the modern convnets in a facial recognition task." |
| 3027 | + "So far, you have learned about the modern Convnets (CNNs), their architecture, and operating principles. If you have time left, you can continue to learn more about the speed vs. accuracy trade-off, as well as the modern convnets in a facial recognition task." |
3021 | 3028 | ] |
3022 | 3029 | }, |
3023 | 3030 | { |
|
4477 | 4484 | "name": "python", |
4478 | 4485 | "nbconvert_exporter": "python", |
4479 | 4486 | "pygments_lexer": "ipython3", |
4480 | | - "version": "3.10.20" |
| 4487 | + "version": "3.9.25" |
4481 | 4488 | } |
4482 | 4489 | }, |
4483 | 4490 | "nbformat": 4, |
|
0 commit comments