mirror of
https://github.com/mrdbourke/pytorch-deep-learning.git
synced 2026-11-27 06:07:09 +00:00
add extras and docs
This commit is contained in:
1 parent
d68d69f164
commit
3dc9999878
10 files changed
+15767
-58
No files matched your search
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,785 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import torch\n",
|
||||
"import torchvision\n",
|
||||
"from torch import nn\n",
|
||||
"from torchvision import transforms, datasets\n",
|
||||
"from torchinfo import summary\n",
|
||||
"\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"\n",
|
||||
"from going_modular import data_setup, engine"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'cuda'"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
|
||||
"device"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"data/10_whole_foods dir exists, skipping download\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# TODO: Functionize this so it can be used in other notebooks \n",
|
||||
"import os\n",
|
||||
"if not os.path.exists(\"data/10_whole_foods\"):\n",
|
||||
" !wget https://storage.googleapis.com/food-vision-image-playground/10_whole_foods.zip\n",
|
||||
" !unzip -q 10_whole_foods.zip -d data\n",
|
||||
"else:\n",
|
||||
" print(\"data/10_whole_foods dir exists, skipping download\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(<torch.utils.data.dataloader.DataLoader at 0x7fee094e6dc0>,\n",
|
||||
" <torch.utils.data.dataloader.DataLoader at 0x7fee094e67c0>,\n",
|
||||
" ['apple',\n",
|
||||
" 'banana',\n",
|
||||
" 'beef',\n",
|
||||
" 'blueberries',\n",
|
||||
" 'carrots',\n",
|
||||
" 'chicken_wings',\n",
|
||||
" 'egg',\n",
|
||||
" 'honey',\n",
|
||||
" 'mushrooms',\n",
|
||||
" 'strawberries'])"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Setup Dirs\n",
|
||||
"train_dir = \"data/10_whole_foods/train\"\n",
|
||||
"test_dir = \"data/10_whole_foods/test\"\n",
|
||||
"\n",
|
||||
"from torchvision.datasets import ImageFolder\n",
|
||||
"\n",
|
||||
"normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],\n",
|
||||
" std=[0.229, 0.224, 0.225])\n",
|
||||
"\n",
|
||||
"simple_transform = transforms.Compose([\n",
|
||||
" transforms.Resize((224, 224)),\n",
|
||||
" transforms.ToTensor(),\n",
|
||||
" normalize\n",
|
||||
"])\n",
|
||||
"\n",
|
||||
"train_dataloader, test_dataloader, class_names = data_setup.create_dataloaders(train_dir=train_dir,\n",
|
||||
" test_dir=test_dir,\n",
|
||||
" transform=simple_transform,\n",
|
||||
" batch_size=32\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"train_dataloader, test_dataloader, class_names\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model = torchvision.models.efficientnet_b0(pretrained=True).to(device)\n",
|
||||
"\n",
|
||||
"# Print a summary\n",
|
||||
"# summary(model, input_shape=(1, 3, 224, 224))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"EfficientNet(\n",
|
||||
" (features): Sequential(\n",
|
||||
" (0): ConvNormActivation(\n",
|
||||
" (0): Conv2d(3, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (1): Sequential(\n",
|
||||
" (0): MBConv(\n",
|
||||
" (block): Sequential(\n",
|
||||
" (0): ConvNormActivation(\n",
|
||||
" (0): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=32, bias=False)\n",
|
||||
" (1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (1): SqueezeExcitation(\n",
|
||||
" (avgpool): AdaptiveAvgPool2d(output_size=1)\n",
|
||||
" (fc1): Conv2d(32, 8, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (fc2): Conv2d(8, 32, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (activation): SiLU(inplace=True)\n",
|
||||
" (scale_activation): Sigmoid()\n",
|
||||
" )\n",
|
||||
" (2): ConvNormActivation(\n",
|
||||
" (0): Conv2d(32, 16, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (stochastic_depth): StochasticDepth(p=0.0, mode=row)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (2): Sequential(\n",
|
||||
" (0): MBConv(\n",
|
||||
" (block): Sequential(\n",
|
||||
" (0): ConvNormActivation(\n",
|
||||
" (0): Conv2d(16, 96, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (1): ConvNormActivation(\n",
|
||||
" (0): Conv2d(96, 96, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), groups=96, bias=False)\n",
|
||||
" (1): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (2): SqueezeExcitation(\n",
|
||||
" (avgpool): AdaptiveAvgPool2d(output_size=1)\n",
|
||||
" (fc1): Conv2d(96, 4, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (fc2): Conv2d(4, 96, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (activation): SiLU(inplace=True)\n",
|
||||
" (scale_activation): Sigmoid()\n",
|
||||
" )\n",
|
||||
" (3): ConvNormActivation(\n",
|
||||
" (0): Conv2d(96, 24, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(24, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (stochastic_depth): StochasticDepth(p=0.0125, mode=row)\n",
|
||||
" )\n",
|
||||
" (1): MBConv(\n",
|
||||
" (block): Sequential(\n",
|
||||
" (0): ConvNormActivation(\n",
|
||||
" (0): Conv2d(24, 144, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(144, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (1): ConvNormActivation(\n",
|
||||
" (0): Conv2d(144, 144, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=144, bias=False)\n",
|
||||
" (1): BatchNorm2d(144, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (2): SqueezeExcitation(\n",
|
||||
" (avgpool): AdaptiveAvgPool2d(output_size=1)\n",
|
||||
" (fc1): Conv2d(144, 6, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (fc2): Conv2d(6, 144, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (activation): SiLU(inplace=True)\n",
|
||||
" (scale_activation): Sigmoid()\n",
|
||||
" )\n",
|
||||
" (3): ConvNormActivation(\n",
|
||||
" (0): Conv2d(144, 24, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(24, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (stochastic_depth): StochasticDepth(p=0.025, mode=row)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (3): Sequential(\n",
|
||||
" (0): MBConv(\n",
|
||||
" (block): Sequential(\n",
|
||||
" (0): ConvNormActivation(\n",
|
||||
" (0): Conv2d(24, 144, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(144, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (1): ConvNormActivation(\n",
|
||||
" (0): Conv2d(144, 144, kernel_size=(5, 5), stride=(2, 2), padding=(2, 2), groups=144, bias=False)\n",
|
||||
" (1): BatchNorm2d(144, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (2): SqueezeExcitation(\n",
|
||||
" (avgpool): AdaptiveAvgPool2d(output_size=1)\n",
|
||||
" (fc1): Conv2d(144, 6, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (fc2): Conv2d(6, 144, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (activation): SiLU(inplace=True)\n",
|
||||
" (scale_activation): Sigmoid()\n",
|
||||
" )\n",
|
||||
" (3): ConvNormActivation(\n",
|
||||
" (0): Conv2d(144, 40, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(40, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (stochastic_depth): StochasticDepth(p=0.037500000000000006, mode=row)\n",
|
||||
" )\n",
|
||||
" (1): MBConv(\n",
|
||||
" (block): Sequential(\n",
|
||||
" (0): ConvNormActivation(\n",
|
||||
" (0): Conv2d(40, 240, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(240, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (1): ConvNormActivation(\n",
|
||||
" (0): Conv2d(240, 240, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), groups=240, bias=False)\n",
|
||||
" (1): BatchNorm2d(240, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (2): SqueezeExcitation(\n",
|
||||
" (avgpool): AdaptiveAvgPool2d(output_size=1)\n",
|
||||
" (fc1): Conv2d(240, 10, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (fc2): Conv2d(10, 240, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (activation): SiLU(inplace=True)\n",
|
||||
" (scale_activation): Sigmoid()\n",
|
||||
" )\n",
|
||||
" (3): ConvNormActivation(\n",
|
||||
" (0): Conv2d(240, 40, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(40, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (stochastic_depth): StochasticDepth(p=0.05, mode=row)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (4): Sequential(\n",
|
||||
" (0): MBConv(\n",
|
||||
" (block): Sequential(\n",
|
||||
" (0): ConvNormActivation(\n",
|
||||
" (0): Conv2d(40, 240, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(240, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (1): ConvNormActivation(\n",
|
||||
" (0): Conv2d(240, 240, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), groups=240, bias=False)\n",
|
||||
" (1): BatchNorm2d(240, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (2): SqueezeExcitation(\n",
|
||||
" (avgpool): AdaptiveAvgPool2d(output_size=1)\n",
|
||||
" (fc1): Conv2d(240, 10, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (fc2): Conv2d(10, 240, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (activation): SiLU(inplace=True)\n",
|
||||
" (scale_activation): Sigmoid()\n",
|
||||
" )\n",
|
||||
" (3): ConvNormActivation(\n",
|
||||
" (0): Conv2d(240, 80, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(80, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (stochastic_depth): StochasticDepth(p=0.0625, mode=row)\n",
|
||||
" )\n",
|
||||
" (1): MBConv(\n",
|
||||
" (block): Sequential(\n",
|
||||
" (0): ConvNormActivation(\n",
|
||||
" (0): Conv2d(80, 480, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (1): ConvNormActivation(\n",
|
||||
" (0): Conv2d(480, 480, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=480, bias=False)\n",
|
||||
" (1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (2): SqueezeExcitation(\n",
|
||||
" (avgpool): AdaptiveAvgPool2d(output_size=1)\n",
|
||||
" (fc1): Conv2d(480, 20, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (fc2): Conv2d(20, 480, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (activation): SiLU(inplace=True)\n",
|
||||
" (scale_activation): Sigmoid()\n",
|
||||
" )\n",
|
||||
" (3): ConvNormActivation(\n",
|
||||
" (0): Conv2d(480, 80, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(80, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (stochastic_depth): StochasticDepth(p=0.07500000000000001, mode=row)\n",
|
||||
" )\n",
|
||||
" (2): MBConv(\n",
|
||||
" (block): Sequential(\n",
|
||||
" (0): ConvNormActivation(\n",
|
||||
" (0): Conv2d(80, 480, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (1): ConvNormActivation(\n",
|
||||
" (0): Conv2d(480, 480, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=480, bias=False)\n",
|
||||
" (1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (2): SqueezeExcitation(\n",
|
||||
" (avgpool): AdaptiveAvgPool2d(output_size=1)\n",
|
||||
" (fc1): Conv2d(480, 20, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (fc2): Conv2d(20, 480, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (activation): SiLU(inplace=True)\n",
|
||||
" (scale_activation): Sigmoid()\n",
|
||||
" )\n",
|
||||
" (3): ConvNormActivation(\n",
|
||||
" (0): Conv2d(480, 80, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(80, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (stochastic_depth): StochasticDepth(p=0.08750000000000001, mode=row)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (5): Sequential(\n",
|
||||
" (0): MBConv(\n",
|
||||
" (block): Sequential(\n",
|
||||
" (0): ConvNormActivation(\n",
|
||||
" (0): Conv2d(80, 480, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (1): ConvNormActivation(\n",
|
||||
" (0): Conv2d(480, 480, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), groups=480, bias=False)\n",
|
||||
" (1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (2): SqueezeExcitation(\n",
|
||||
" (avgpool): AdaptiveAvgPool2d(output_size=1)\n",
|
||||
" (fc1): Conv2d(480, 20, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (fc2): Conv2d(20, 480, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (activation): SiLU(inplace=True)\n",
|
||||
" (scale_activation): Sigmoid()\n",
|
||||
" )\n",
|
||||
" (3): ConvNormActivation(\n",
|
||||
" (0): Conv2d(480, 112, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(112, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (stochastic_depth): StochasticDepth(p=0.1, mode=row)\n",
|
||||
" )\n",
|
||||
" (1): MBConv(\n",
|
||||
" (block): Sequential(\n",
|
||||
" (0): ConvNormActivation(\n",
|
||||
" (0): Conv2d(112, 672, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (1): ConvNormActivation(\n",
|
||||
" (0): Conv2d(672, 672, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), groups=672, bias=False)\n",
|
||||
" (1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (2): SqueezeExcitation(\n",
|
||||
" (avgpool): AdaptiveAvgPool2d(output_size=1)\n",
|
||||
" (fc1): Conv2d(672, 28, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (fc2): Conv2d(28, 672, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (activation): SiLU(inplace=True)\n",
|
||||
" (scale_activation): Sigmoid()\n",
|
||||
" )\n",
|
||||
" (3): ConvNormActivation(\n",
|
||||
" (0): Conv2d(672, 112, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(112, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (stochastic_depth): StochasticDepth(p=0.1125, mode=row)\n",
|
||||
" )\n",
|
||||
" (2): MBConv(\n",
|
||||
" (block): Sequential(\n",
|
||||
" (0): ConvNormActivation(\n",
|
||||
" (0): Conv2d(112, 672, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (1): ConvNormActivation(\n",
|
||||
" (0): Conv2d(672, 672, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), groups=672, bias=False)\n",
|
||||
" (1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (2): SqueezeExcitation(\n",
|
||||
" (avgpool): AdaptiveAvgPool2d(output_size=1)\n",
|
||||
" (fc1): Conv2d(672, 28, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (fc2): Conv2d(28, 672, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (activation): SiLU(inplace=True)\n",
|
||||
" (scale_activation): Sigmoid()\n",
|
||||
" )\n",
|
||||
" (3): ConvNormActivation(\n",
|
||||
" (0): Conv2d(672, 112, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(112, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (stochastic_depth): StochasticDepth(p=0.125, mode=row)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (6): Sequential(\n",
|
||||
" (0): MBConv(\n",
|
||||
" (block): Sequential(\n",
|
||||
" (0): ConvNormActivation(\n",
|
||||
" (0): Conv2d(112, 672, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (1): ConvNormActivation(\n",
|
||||
" (0): Conv2d(672, 672, kernel_size=(5, 5), stride=(2, 2), padding=(2, 2), groups=672, bias=False)\n",
|
||||
" (1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (2): SqueezeExcitation(\n",
|
||||
" (avgpool): AdaptiveAvgPool2d(output_size=1)\n",
|
||||
" (fc1): Conv2d(672, 28, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (fc2): Conv2d(28, 672, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (activation): SiLU(inplace=True)\n",
|
||||
" (scale_activation): Sigmoid()\n",
|
||||
" )\n",
|
||||
" (3): ConvNormActivation(\n",
|
||||
" (0): Conv2d(672, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (stochastic_depth): StochasticDepth(p=0.1375, mode=row)\n",
|
||||
" )\n",
|
||||
" (1): MBConv(\n",
|
||||
" (block): Sequential(\n",
|
||||
" (0): ConvNormActivation(\n",
|
||||
" (0): Conv2d(192, 1152, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (1): ConvNormActivation(\n",
|
||||
" (0): Conv2d(1152, 1152, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), groups=1152, bias=False)\n",
|
||||
" (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (2): SqueezeExcitation(\n",
|
||||
" (avgpool): AdaptiveAvgPool2d(output_size=1)\n",
|
||||
" (fc1): Conv2d(1152, 48, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (fc2): Conv2d(48, 1152, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (activation): SiLU(inplace=True)\n",
|
||||
" (scale_activation): Sigmoid()\n",
|
||||
" )\n",
|
||||
" (3): ConvNormActivation(\n",
|
||||
" (0): Conv2d(1152, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (stochastic_depth): StochasticDepth(p=0.15000000000000002, mode=row)\n",
|
||||
" )\n",
|
||||
" (2): MBConv(\n",
|
||||
" (block): Sequential(\n",
|
||||
" (0): ConvNormActivation(\n",
|
||||
" (0): Conv2d(192, 1152, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (1): ConvNormActivation(\n",
|
||||
" (0): Conv2d(1152, 1152, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), groups=1152, bias=False)\n",
|
||||
" (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (2): SqueezeExcitation(\n",
|
||||
" (avgpool): AdaptiveAvgPool2d(output_size=1)\n",
|
||||
" (fc1): Conv2d(1152, 48, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (fc2): Conv2d(48, 1152, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (activation): SiLU(inplace=True)\n",
|
||||
" (scale_activation): Sigmoid()\n",
|
||||
" )\n",
|
||||
" (3): ConvNormActivation(\n",
|
||||
" (0): Conv2d(1152, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (stochastic_depth): StochasticDepth(p=0.1625, mode=row)\n",
|
||||
" )\n",
|
||||
" (3): MBConv(\n",
|
||||
" (block): Sequential(\n",
|
||||
" (0): ConvNormActivation(\n",
|
||||
" (0): Conv2d(192, 1152, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (1): ConvNormActivation(\n",
|
||||
" (0): Conv2d(1152, 1152, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), groups=1152, bias=False)\n",
|
||||
" (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (2): SqueezeExcitation(\n",
|
||||
" (avgpool): AdaptiveAvgPool2d(output_size=1)\n",
|
||||
" (fc1): Conv2d(1152, 48, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (fc2): Conv2d(48, 1152, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (activation): SiLU(inplace=True)\n",
|
||||
" (scale_activation): Sigmoid()\n",
|
||||
" )\n",
|
||||
" (3): ConvNormActivation(\n",
|
||||
" (0): Conv2d(1152, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (stochastic_depth): StochasticDepth(p=0.17500000000000002, mode=row)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (7): Sequential(\n",
|
||||
" (0): MBConv(\n",
|
||||
" (block): Sequential(\n",
|
||||
" (0): ConvNormActivation(\n",
|
||||
" (0): Conv2d(192, 1152, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (1): ConvNormActivation(\n",
|
||||
" (0): Conv2d(1152, 1152, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=1152, bias=False)\n",
|
||||
" (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" (2): SqueezeExcitation(\n",
|
||||
" (avgpool): AdaptiveAvgPool2d(output_size=1)\n",
|
||||
" (fc1): Conv2d(1152, 48, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (fc2): Conv2d(48, 1152, kernel_size=(1, 1), stride=(1, 1))\n",
|
||||
" (activation): SiLU(inplace=True)\n",
|
||||
" (scale_activation): Sigmoid()\n",
|
||||
" )\n",
|
||||
" (3): ConvNormActivation(\n",
|
||||
" (0): Conv2d(1152, 320, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(320, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (stochastic_depth): StochasticDepth(p=0.1875, mode=row)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (8): ConvNormActivation(\n",
|
||||
" (0): Conv2d(320, 1280, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
|
||||
" (1): BatchNorm2d(1280, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
||||
" (2): SiLU(inplace=True)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" (avgpool): AdaptiveAvgPool2d(output_size=1)\n",
|
||||
" (classifier): Sequential(\n",
|
||||
" (0): Dropout(p=0.2, inplace=True)\n",
|
||||
" (1): Linear(in_features=1280, out_features=1000, bias=True)\n",
|
||||
" )\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Update the classifier\n",
|
||||
"model.classifier = torch.nn.Linear(1280, 10).to(device)\n",
|
||||
"\n",
|
||||
"# Freeze all base layers (the feature extractor in EfficientNet)\n",
|
||||
"for param in model.features.parameters():\n",
|
||||
" param.requires_grad = False\n",
|
||||
"\n",
|
||||
"# Print a summary\n",
|
||||
"# model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Train model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Define loss and optimizer\n",
|
||||
"loss_fn = nn.CrossEntropyLoss()\n",
|
||||
"optimizer = torch.optim.Adam(model.parameters(), lr=0.001)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" 20%|██ | 1/5 [00:02<00:09, 2.48s/it]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch: 1 | train_loss: 1.9464 | train_acc: 0.4379 | test_loss: 1.3973 | test_acc: 0.7552\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" 40%|████ | 2/5 [00:04<00:06, 2.27s/it]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch: 2 | train_loss: 1.2162 | train_acc: 0.7912 | test_loss: 0.9931 | test_acc: 0.8125\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" 60%|██████ | 3/5 [00:06<00:04, 2.21s/it]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch: 3 | train_loss: 0.8517 | train_acc: 0.8540 | test_loss: 0.7805 | test_acc: 0.8906\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" 80%|████████ | 4/5 [00:08<00:02, 2.22s/it]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch: 4 | train_loss: 0.6407 | train_acc: 0.8846 | test_loss: 0.6374 | test_acc: 0.8542\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"100%|██████████| 5/5 [00:11<00:00, 2.22s/it]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch: 5 | train_loss: 0.5433 | train_acc: 0.8984 | test_loss: 0.6578 | test_acc: 0.8333\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Train model\n",
|
||||
"results = engine.train(model=model,\n",
|
||||
" train_dataloader=train_dataloader,\n",
|
||||
" test_dataloader=test_dataloader,\n",
|
||||
" optimizer=optimizer,\n",
|
||||
" loss_fn=loss_fn,\n",
|
||||
" epochs=5)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "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 truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x504 with 2 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from helper_functions import plot_loss_curves\n",
|
||||
"\n",
|
||||
"plot_loss_curves(results)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## TODO Extensions + Exercises\n",
|
||||
"* Try a different model from PyTorch pretrained models\n",
|
||||
"* Train for longer, what happens?\n",
|
||||
"* Could you add in some data augmentation? What happens then?"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"interpreter": {
|
||||
"hash": "3fbe1355223f7b2ffc113ba3ade6a2b520cadace5d5ec3e828c83ce02eb221bf"
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3.9.7 64-bit (conda)",
|
||||
"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.9.7"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,691 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# 08: PyTorch Profiling\n",
|
||||
"\n",
|
||||
"This notebook is an experiment to try out the PyTorch profiler.\n",
|
||||
"\n",
|
||||
"See here for more:\n",
|
||||
"* https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/\n",
|
||||
"* https://pytorch.org/docs/stable/profiler.html"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import torch\n",
|
||||
"import torchvision\n",
|
||||
"from torch import nn\n",
|
||||
"from torchvision import transforms, datasets\n",
|
||||
"from torchinfo import summary\n",
|
||||
"\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"\n",
|
||||
"from going_modular import data_setup, engine"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Setup device"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'cuda'"
|
||||
]
|
||||
},
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
|
||||
"device"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Get and load data"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"data/10_whole_foods dir exists, skipping download\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"import requests\n",
|
||||
"from zipfile import ZipFile\n",
|
||||
"\n",
|
||||
"def get_food_image_data():\n",
|
||||
" if not os.path.exists(\"data/10_whole_foods\"):\n",
|
||||
" os.makedirs(\"data/\", exist_ok=True)\n",
|
||||
" # Download data\n",
|
||||
" data_url = \"https://storage.googleapis.com/food-vision-image-playground/10_whole_foods.zip\"\n",
|
||||
" print(f\"Downloading data from {data_url}...\")\n",
|
||||
" requests.get(data_url)\n",
|
||||
" # Unzip data\n",
|
||||
" targ_dir = \"data/10_whole_foods\"\n",
|
||||
" print(f\"Extracting data to {targ_dir}...\")\n",
|
||||
" with ZipFile(\"10_whole_foods.zip\") as zip_ref:\n",
|
||||
" zip_ref.extractall(targ_dir)\n",
|
||||
" else:\n",
|
||||
" print(\"data/10_whole_foods dir exists, skipping download\")\n",
|
||||
"\n",
|
||||
"get_food_image_data()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 38,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(<torch.utils.data.dataloader.DataLoader at 0x7f052ab26e20>,\n",
|
||||
" <torch.utils.data.dataloader.DataLoader at 0x7f05299eed00>,\n",
|
||||
" ['apple',\n",
|
||||
" 'banana',\n",
|
||||
" 'beef',\n",
|
||||
" 'blueberries',\n",
|
||||
" 'carrots',\n",
|
||||
" 'chicken_wings',\n",
|
||||
" 'egg',\n",
|
||||
" 'honey',\n",
|
||||
" 'mushrooms',\n",
|
||||
" 'strawberries'])"
|
||||
]
|
||||
},
|
||||
"execution_count": 38,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Setup dirs\n",
|
||||
"train_dir = \"data/10_whole_foods/train\"\n",
|
||||
"test_dir = \"data/10_whole_foods/test\"\n",
|
||||
"\n",
|
||||
"# Setup ImageNet normalization levels (turns all images into similar distribution as ImageNet)\n",
|
||||
"normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],\n",
|
||||
" std=[0.229, 0.224, 0.225])\n",
|
||||
"\n",
|
||||
"# Create starter transform\n",
|
||||
"simple_transform = transforms.Compose([\n",
|
||||
" transforms.Resize((224, 224)),\n",
|
||||
" transforms.ToTensor(),\n",
|
||||
" normalize\n",
|
||||
"]) \n",
|
||||
"\n",
|
||||
"# Create data loaders\n",
|
||||
"train_dataloader, test_dataloader, class_names = data_setup.create_dataloaders(\n",
|
||||
" train_dir=train_dir,\n",
|
||||
" test_dir=test_dir,\n",
|
||||
" transform=simple_transform,\n",
|
||||
" batch_size=32,\n",
|
||||
" num_workers=8\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"train_dataloader, test_dataloader, class_names"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Load model "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 66,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model = torchvision.models.efficientnet_b0(pretrained=True).to(device)\n",
|
||||
"# model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 67,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Update the classifier\n",
|
||||
"model.classifier = torch.nn.Sequential(\n",
|
||||
" nn.Dropout(p=0.2),\n",
|
||||
" nn.Linear(1280, len(class_names)).to(device))\n",
|
||||
"\n",
|
||||
"# Freeze all base layers \n",
|
||||
"for param in model.features.parameters():\n",
|
||||
" param.requires_grad = False"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Train model and track results"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 68,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Define loss and optimizer\n",
|
||||
"loss_fn = nn.CrossEntropyLoss()\n",
|
||||
"optimizer = torch.optim.Adam(model.parameters(), lr=0.001)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Adjust training function to track results with `SummaryWriter`"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 69,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'EfficietNetB0'"
|
||||
]
|
||||
},
|
||||
"execution_count": 69,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"model.name = \"EfficietNetB0\"\n",
|
||||
"model.name"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 70,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from torch.utils.tensorboard import SummaryWriter\n",
|
||||
"from going_modular.engine import train_step, test_step\n",
|
||||
"from tqdm import tqdm\n",
|
||||
"writer = SummaryWriter()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Update the `train_step()` function to include the PyTorch profiler."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 71,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def train_step(model, dataloader, loss_fn, optimizer):\n",
|
||||
" model.train()\n",
|
||||
" train_loss, train_acc = 0, 0\n",
|
||||
" ## NEW: Add PyTorch profiler\n",
|
||||
"\n",
|
||||
" dir_to_save_logs = os.path.join(\"logs\", datetime.now().strftime(\"%Y-%m-%d-%H-%M\"))\n",
|
||||
" with torch.profiler.profile(\n",
|
||||
" on_trace_ready=torch.profiler.tensorboard_trace_handler(dir_name=dir_to_save_logs),\n",
|
||||
" # with_stack=True # this adds a lot of overhead to training (tracing all the stack)\n",
|
||||
" ):\n",
|
||||
" for batch, (X, y) in enumerate(dataloader):\n",
|
||||
" # Send data to GPU\n",
|
||||
" X, y = X.to(device, non_blocking=True), y.to(device, non_blocking=True)\n",
|
||||
" \n",
|
||||
" # Turn on mixed precision if available\n",
|
||||
" with torch.autocast(device_type=device, enabled=True):\n",
|
||||
" # 1. Forward pass\n",
|
||||
" y_pred = model(X)\n",
|
||||
"\n",
|
||||
" # 2. Calculate loss\n",
|
||||
" loss = loss_fn(y_pred, y)\n",
|
||||
"\n",
|
||||
" # 3. Optimizer zero grad\n",
|
||||
" optimizer.zero_grad()\n",
|
||||
"\n",
|
||||
" # 4. Loss backward\n",
|
||||
" loss.backward()\n",
|
||||
"\n",
|
||||
" # 5. Optimizer step\n",
|
||||
" optimizer.step()\n",
|
||||
"\n",
|
||||
" # 6. Calculate metrics\n",
|
||||
" train_loss += loss.item()\n",
|
||||
" y_pred_class = torch.softmax(y_pred, dim=1).argmax(dim=1)\n",
|
||||
" # print(f\"y: \\n{y}\\ny_pred_class:{y_pred_class}\")\n",
|
||||
" # print(f\"y argmax: {y_pred.argmax(dim=1)}\")\n",
|
||||
" # print(f\"Equal: {(y_pred_class == y)}\")\n",
|
||||
" train_acc += (y_pred_class == y).sum().item() / len(y_pred)\n",
|
||||
" # print(f\"batch: {batch} train_acc: {train_acc}\")\n",
|
||||
"\n",
|
||||
" # Adjust returned metrics\n",
|
||||
" return train_loss / len(dataloader), train_acc / len(dataloader)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"TK - Now to use the writer, we've got to adjust the `train()` function..."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 72,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def train(\n",
|
||||
" model,\n",
|
||||
" train_dataloader,\n",
|
||||
" test_dataloader,\n",
|
||||
" optimizer,\n",
|
||||
" loss_fn=nn.CrossEntropyLoss(),\n",
|
||||
" epochs=5,\n",
|
||||
"):\n",
|
||||
"\n",
|
||||
" results = {\"train_loss\": [], \"train_acc\": [], \"test_loss\": [], \"test_acc\": []}\n",
|
||||
"\n",
|
||||
" for epoch in tqdm(range(epochs)):\n",
|
||||
" train_loss, train_acc = train_step(\n",
|
||||
" model=model,\n",
|
||||
" dataloader=train_dataloader,\n",
|
||||
" loss_fn=loss_fn,\n",
|
||||
" optimizer=optimizer,\n",
|
||||
" )\n",
|
||||
" test_loss, test_acc = test_step(\n",
|
||||
" model=model, dataloader=test_dataloader, loss_fn=loss_fn\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" # Print out what's happening\n",
|
||||
" print(\n",
|
||||
" f\"Epoch: {epoch+1} | \"\n",
|
||||
" f\"train_loss: {train_loss:.4f} | \"\n",
|
||||
" f\"train_acc: {train_acc:.4f} | \"\n",
|
||||
" f\"test_loss: {test_loss:.4f} | \"\n",
|
||||
" f\"test_acc: {test_acc:.4f}\"\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" # Update results\n",
|
||||
" results[\"train_loss\"].append(train_loss)\n",
|
||||
" results[\"train_acc\"].append(train_acc)\n",
|
||||
" results[\"test_loss\"].append(test_loss)\n",
|
||||
" results[\"test_acc\"].append(test_acc)\n",
|
||||
"\n",
|
||||
" # Add results to SummaryWriter\n",
|
||||
" writer.add_scalars(main_tag=\"Loss\", \n",
|
||||
" tag_scalar_dict={\"train_loss\": train_loss,\n",
|
||||
" \"test_loss\": test_loss},\n",
|
||||
" global_step=epoch)\n",
|
||||
" writer.add_scalars(main_tag=\"Accuracy\", \n",
|
||||
" tag_scalar_dict={\"train_acc\": train_acc,\n",
|
||||
" \"test_acc\": test_acc}, \n",
|
||||
" global_step=epoch)\n",
|
||||
" \n",
|
||||
" # Close the writer\n",
|
||||
" writer.close()\n",
|
||||
"\n",
|
||||
" return results"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 73,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" 20%|██ | 1/5 [00:05<00:21, 5.27s/it]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch: 1 | train_loss: 1.9644 | train_acc: 0.4386 | test_loss: 1.5205 | test_acc: 0.7865\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" 40%|████ | 2/5 [00:09<00:14, 4.94s/it]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch: 2 | train_loss: 1.2589 | train_acc: 0.7878 | test_loss: 1.1589 | test_acc: 0.7604\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" 60%|██████ | 3/5 [00:14<00:09, 4.72s/it]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch: 3 | train_loss: 0.8642 | train_acc: 0.8776 | test_loss: 0.9347 | test_acc: 0.7917\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" 80%|████████ | 4/5 [00:18<00:04, 4.56s/it]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch: 4 | train_loss: 0.6827 | train_acc: 0.8856 | test_loss: 0.6637 | test_acc: 0.8750\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"100%|██████████| 5/5 [00:23<00:00, 4.65s/it]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch: 5 | train_loss: 0.5688 | train_acc: 0.9069 | test_loss: 0.6175 | test_acc: 0.8854\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Train model\n",
|
||||
"# Note: Not using engine.train() since the original script isn't updated\n",
|
||||
"results = train(model=model,\n",
|
||||
" train_dataloader=train_dataloader,\n",
|
||||
" test_dataloader=test_dataloader,\n",
|
||||
" optimizer=optimizer,\n",
|
||||
" loss_fn=loss_fn,\n",
|
||||
" epochs=5)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Looks like mixed precision doesn't offer much benefit for smaller feature extraction models..."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# # Without mixed precision\n",
|
||||
"# 20%|██ | 1/5 [00:03<00:14, 3.71s/it]Epoch: 1 | train_loss: 0.5229 | train_acc: 0.9054 | test_loss: 0.5776 | test_acc: 0.8542\n",
|
||||
"# 40%|████ | 2/5 [00:07<00:11, 3.74s/it]Epoch: 2 | train_loss: 0.4699 | train_acc: 0.9001 | test_loss: 0.5160 | test_acc: 0.8802\n",
|
||||
"# 60%|██████ | 3/5 [00:10<00:07, 3.63s/it]Epoch: 3 | train_loss: 0.3913 | train_acc: 0.9196 | test_loss: 0.4888 | test_acc: 0.8906\n",
|
||||
"# 80%|████████ | 4/5 [00:14<00:03, 3.61s/it]Epoch: 4 | train_loss: 0.3724 | train_acc: 0.9371 | test_loss: 0.4931 | test_acc: 0.8698\n",
|
||||
"# 100%|██████████| 5/5 [00:18<00:00, 3.61s/it]Epoch: 5 | train_loss: 0.3315 | train_acc: 0.9381 | test_loss: 0.4405 | test_acc: 0.8750\n",
|
||||
"\n",
|
||||
"# # With mixed precision\n",
|
||||
"# 20%|██ | 1/5 [00:04<00:17, 4.40s/it]Epoch: 1 | train_loss: 0.3027 | train_acc: 0.9554 | test_loss: 0.4386 | test_acc: 0.8802\n",
|
||||
"# 40%|████ | 2/5 [00:08<00:13, 4.49s/it]Epoch: 2 | train_loss: 0.2826 | train_acc: 0.9539 | test_loss: 0.4080 | test_acc: 0.8802\n",
|
||||
"# 60%|██████ | 3/5 [00:13<00:08, 4.48s/it]Epoch: 3 | train_loss: 0.2450 | train_acc: 0.9609 | test_loss: 0.4130 | test_acc: 0.8750\n",
|
||||
"# 80%|████████ | 4/5 [00:18<00:04, 4.53s/it]Epoch: 4 | train_loss: 0.2450 | train_acc: 0.9594 | test_loss: 0.4158 | test_acc: 0.8802\n",
|
||||
"# 100%|██████████| 5/5 [00:22<00:00, 4.49s/it]Epoch: 5 | train_loss: 0.2307 | train_acc: 0.9639 | test_loss: 0.4124 | test_acc: 0.8906"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Try mixed precision with larger model\n",
|
||||
"\n",
|
||||
"Now we'll try turn on mixed precision with a larger model (e.g. EffifientNetB0 with all layers tuneable)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 74,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Unfreeze all base layers \n",
|
||||
"for param in model.features.parameters():\n",
|
||||
" param.requires_grad = True\n",
|
||||
"\n",
|
||||
"# for param in model.features.parameters():\n",
|
||||
"# print(param.requires_grad)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 75,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" 20%|██ | 1/5 [00:13<00:53, 13.27s/it]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch: 1 | train_loss: 0.4934 | train_acc: 0.8586 | test_loss: 0.6467 | test_acc: 0.7969\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" 40%|████ | 2/5 [00:27<00:42, 14.09s/it]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch: 2 | train_loss: 0.1750 | train_acc: 0.9628 | test_loss: 1.1806 | test_acc: 0.8385\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" 60%|██████ | 3/5 [00:42<00:28, 14.28s/it]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch: 3 | train_loss: 0.1362 | train_acc: 0.9619 | test_loss: 0.5831 | test_acc: 0.8802\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" 80%|████████ | 4/5 [00:57<00:14, 14.47s/it]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch: 4 | train_loss: 0.1743 | train_acc: 0.9462 | test_loss: 0.5702 | test_acc: 0.8854\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"100%|██████████| 5/5 [01:11<00:00, 14.38s/it]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch: 5 | train_loss: 0.2437 | train_acc: 0.9352 | test_loss: 0.7096 | test_acc: 0.8125\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Train model\n",
|
||||
"# Note: Not using engine.train() since the original script isn't updated\n",
|
||||
"results = train(model=model,\n",
|
||||
" train_dataloader=train_dataloader,\n",
|
||||
" test_dataloader=test_dataloader,\n",
|
||||
" optimizer=optimizer,\n",
|
||||
" loss_fn=loss_fn,\n",
|
||||
" epochs=5)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# # Without mixed precision...\n",
|
||||
"# 20%|██ | 1/5 [00:11<00:46, 11.61s/it]Epoch: 1 | train_loss: 0.4507 | train_acc: 0.8648 | test_loss: 1.0603 | test_acc: 0.7604\n",
|
||||
"# 40%|████ | 2/5 [00:24<00:36, 12.21s/it]Epoch: 2 | train_loss: 0.1659 | train_acc: 0.9464 | test_loss: 0.6398 | test_acc: 0.8490\n",
|
||||
"# 60%|██████ | 3/5 [00:36<00:24, 12.38s/it]Epoch: 3 | train_loss: 0.1261 | train_acc: 0.9698 | test_loss: 0.7149 | test_acc: 0.8542\n",
|
||||
"# 80%|████████ | 4/5 [00:49<00:12, 12.53s/it]Epoch: 4 | train_loss: 0.1250 | train_acc: 0.9609 | test_loss: 0.7441 | test_acc: 0.7917\n",
|
||||
"# 100%|██████████| 5/5 [01:02<00:00, 12.42s/it]Epoch: 5 | train_loss: 0.1282 | train_acc: 0.9564 | test_loss: 0.8701 | test_acc: 0.8385\n",
|
||||
"\n",
|
||||
"# # With mixed precision...\n",
|
||||
"# 20%|██ | 1/5 [00:13<00:53, 13.27s/it]Epoch: 1 | train_loss: 0.4934 | train_acc: 0.8586 | test_loss: 0.6467 | test_acc: 0.7969\n",
|
||||
"# 40%|████ | 2/5 [00:27<00:42, 14.09s/it]Epoch: 2 | train_loss: 0.1750 | train_acc: 0.9628 | test_loss: 1.1806 | test_acc: 0.8385\n",
|
||||
"# 60%|██████ | 3/5 [00:42<00:28, 14.28s/it]Epoch: 3 | train_loss: 0.1362 | train_acc: 0.9619 | test_loss: 0.5831 | test_acc: 0.8802\n",
|
||||
"# 80%|████████ | 4/5 [00:57<00:14, 14.47s/it]Epoch: 4 | train_loss: 0.1743 | train_acc: 0.9462 | test_loss: 0.5702 | test_acc: 0.8854\n",
|
||||
"# 100%|██████████| 5/5 [01:11<00:00, 14.38s/it]Epoch: 5 | train_loss: 0.2437 | train_acc: 0.9352 | test_loss: 0.7096 | test_acc: 0.8125"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Checking the PyTorch profiler, it seems that mixed precision utilises some Tensor Cores, however, these aren't large numbers.\n",
|
||||
"\n",
|
||||
"E.g. it uses 9-12% Tensor Cores. Perhaps the slow down when using mixed precision is because the tensors have to get altered and converted when there isn't very many of them. For example only 9-12% of tensors get converted so the speed up gains aren't realised on these tensors because they get cancelled out by the conversion time."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Extensions\n",
|
||||
"* Does changing the data input size to EfficientNetB4 change its results? E.g. input image size of (380, 380) instead of (224, 224)?"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
" "
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"interpreter": {
|
||||
"hash": "3fbe1355223f7b2ffc113ba3ade6a2b520cadace5d5ec3e828c83ce02eb221bf"
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3.9.7 64-bit (conda)",
|
||||
"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.9.7"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,58 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"True"
|
||||
]
|
||||
},
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import torch\n",
|
||||
"\n",
|
||||
"# Should print True if CUDA-enabled GPU is available, if not will print False\n",
|
||||
"torch.cuda.is_available()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"interpreter": {
|
||||
"hash": "3fbe1355223f7b2ffc113ba3ade6a2b520cadace5d5ec3e828c83ce02eb221bf"
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3.9.7 64-bit (conda)",
|
||||
"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.9.7"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Reference in new issue
Block a user