mirror of
https://github.com/mrdbourke/pytorch-deep-learning.git
synced 2026-11-27 06:07:09 +00:00
add annotations to 09
This commit is contained in:
1 parent
6483708936
commit
ee67375404
1 file changed
+356
-103
+356
-103
@@ -17,8 +17,11 @@
|
||||
"\n",
|
||||
"**Status:** Work in progress.\n",
|
||||
"\n",
|
||||
"Goal: Take FoodVision Mini model and make it publically accessible (deploy it to a usable app/interface)\n",
|
||||
"Goal: Take FoodVision Mini model and make it publically accessible (deploy it to a usable app/interface), want it to be:\n",
|
||||
"* Fast (a slow app is a boring app)\n",
|
||||
"* Good (95%+ accuracy for 3 classes but this would be harder to achieve with more classes)\n",
|
||||
"\n",
|
||||
"--\n",
|
||||
"* TK - What is model deployment?\n",
|
||||
" * TK - PyTorch workflow + model deployment... (another step)\n",
|
||||
"* TK - Why deploy a machine learning model?\n",
|
||||
@@ -27,7 +30,11 @@
|
||||
"* TK - On edge vs on cloud\n",
|
||||
"* TK - What to do when your model is deployed? E.g. monitoring? \n",
|
||||
" * TK - Perhaps talk about this when the model is actually deployed\n",
|
||||
" * TK - What could go wrong when a model is deployed?"
|
||||
" * TK - What could go wrong when a model is deployed?\n",
|
||||
"* TK - Creating a demo (what are the inputs and outputs)...\n",
|
||||
" * TK - Describing a machine learning problem... one of the best ways is in inputs and outputs, \"what are your ideal inputs and ideal outputs?\"\n",
|
||||
"\n",
|
||||
"## TK - What we're going to cover "
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -181,9 +188,19 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 1. Getting data\n",
|
||||
"## TK - 1. Getting data\n",
|
||||
"\n",
|
||||
"Get 20% data to train models on same playing field."
|
||||
"We left off in [08. PyTorch Paper Replicating](https://www.learnpytorch.io/08_pytorch_paper_replicating/#106-save-feature-extractor-vit-model-and-check-file-size) comparing our own ViT (Vision Transformer) feature extractor model to the EfficientNetB2 (EffNetB2) feature extractor model we created in [07. PyTorch Experiment Tracking](https://www.learnpytorch.io/07_pytorch_experiment_tracking/#9-load-in-the-best-model-and-make-predictions-with-it).\n",
|
||||
"\n",
|
||||
"And we found that there was a slight difference in the comparison.\n",
|
||||
"\n",
|
||||
"The EffNetB2 model was trained on 20% of the pizza, steak and sushi data from Food101 where as the ViT model was trained on 10%.\n",
|
||||
"\n",
|
||||
"Since our goal is to deploy the best model for our FoodVision Mini problem, let's start by downloading the [20% pizza, steak and sushi dataset](https://github.com/mrdbourke/pytorch-deep-learning/blob/main/data/pizza_steak_sushi_20_percent.zip) and train an EffNetB2 feature extractor and ViT feature extractor on it and then compare the two models.\n",
|
||||
"\n",
|
||||
"This way we'll be comparing apples to apples.\n",
|
||||
"\n",
|
||||
"> **Note:** The dataset we're downloading is a sample of the entire [Food101 dataset](https://pytorch.org/vision/main/generated/torchvision.datasets.Food101.html#food101) (101 food classes with 1,000 images each). More specifically, 20% refers to 20% of images from the pizza, steak and sushi classes selected at random. You can see how this dataset was created in [`extras/04_custom_data_creation.ipynb`](https://github.com/mrdbourke/pytorch-deep-learning/blob/main/extras/04_custom_data_creation.ipynb)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -217,6 +234,15 @@
|
||||
"data_20_percent_path"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Wonderful!\n",
|
||||
"\n",
|
||||
"Now we've got a dataset, let's creat training and test paths."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
@@ -234,42 +260,187 @@
|
||||
"source": [
|
||||
"## TK - FoodVision Mini modelling experiment outline\n",
|
||||
"\n",
|
||||
"Going to train two different feature extractor models:\n",
|
||||
"The ideal deployed model FoodVision Mini performs well and fast.\n",
|
||||
"\n",
|
||||
"1. EffNetB2\n",
|
||||
"2. ViT-B/16"
|
||||
"Ideally, we'd like a model that performs as close to real-time as possible.\n",
|
||||
"\n",
|
||||
"Real-time in this case being ~30FPS (frames per second) because that's about how fast the human eye can see (there is debate on this but let's just use ~30FPS as our benchmark).\n",
|
||||
"\n",
|
||||
"And for classifying three different classes (pizza, steak and sushi), we'd like a model that performs at 95%+ accuracy.\n",
|
||||
"\n",
|
||||
"Of course, higher accuracy would be nice but this might sacrifice speed.\n",
|
||||
"\n",
|
||||
"So our goals are:\n",
|
||||
"\n",
|
||||
"1. A model that can classify an image at ~30FPS (0.03 seconds per image).\n",
|
||||
"2. A model that performs at 95%+ accuracy.\n",
|
||||
"\n",
|
||||
"To try and achieve these results, let's bring in our best performing models from the previous sections: \n",
|
||||
"\n",
|
||||
"1. EffNetB2 feature extractor - originally created in [07. PyTorch Experiment Tracking section 7.5](https://www.learnpytorch.io/07_pytorch_experiment_tracking/#75-create-feature-extractor-models) using [`torchvision.models.efficientnet_b2`](https://pytorch.org/vision/stable/models/generated/torchvision.models.efficientnet_b2.html#efficientnet-b2) with adjusted `classifier` layers.\n",
|
||||
"2. ViT-B/16 feature extractor - originally created in [08. PyTorch Paper Replicating section 10](https://www.learnpytorch.io/08_pytorch_paper_replicating/#10-using-a-pretrained-vit-from-torchvisionmodels-on-the-same-dataset) using [`torchvision.models.vit_b_16`](https://pytorch.org/vision/stable/models/generated/torchvision.models.vit_b_16.html#vit-b-16) with adjusted `head` layers. \n",
|
||||
"\n",
|
||||
"TK image - experiment outline (2 models, same dataset, same training regime)\n",
|
||||
"\n",
|
||||
"> **Note:** A \"feature extractor model\" often starts with a model that has been pretrained on a dataset similar to your own problem. The pretrained model's base layers are often left frozen (the pretrained patterns/weights stay the same) whilst some of the top (or classifier/classification head) layers get customized to your own problem by training on your own data. We covered the concept of a feature extractor model in [06. PyTorch Transfer Learning section 3.4](https://www.learnpytorch.io/06_pytorch_transfer_learning/#34-freezing-the-base-model-and-changing-the-output-layer-to-suit-our-needs)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## TK - Creating an EffNetB2 feature extractor"
|
||||
"## TK - Creating an EffNetB2 feature extractor\n",
|
||||
"\n",
|
||||
"We first created an EffNetB2 feature extractor model in [07. PyTorch Experiment Tracking section 7.5](https://www.learnpytorch.io/07_pytorch_experiment_tracking/#75-create-feature-extractor-models).\n",
|
||||
"\n",
|
||||
"And we saw by the end of that section it performed very well.\n",
|
||||
"\n",
|
||||
"So let's now recreate it here so we can compare its results to a ViT feature extractor trained on the same data.\n",
|
||||
"\n",
|
||||
"To do so we can:\n",
|
||||
"1. Setup the pretrained weights as `torchvision.models.EfficientNet_B2_Weights.DEFAULT`, where \"`DEFAULT`\" means \"best currently available\" (or could use `weights=\"DEFAULT\"`). \n",
|
||||
"2. Get the pretrained model image transforms from the weights with the `transforms()` method.\n",
|
||||
"3. Create a pretrained model instance by passing the weights to an instance of `torchvision.models.efficientnet_b2`.\n",
|
||||
"4. Freeze the base layers in the model.\n",
|
||||
"5. Update the classifier head to suit our own data."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 71,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def create_effnetb2_model(num_classes:int=3, seed:int=42):\n",
|
||||
"# 1. Setup pretrained EffNetB2 weights\n",
|
||||
"effnetb2_weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT\n",
|
||||
"\n",
|
||||
"# 2. Get EffNetB2 transforms\n",
|
||||
"effnetb2_transforms = effnetb2_weights.transforms()\n",
|
||||
"\n",
|
||||
"# 3. Setup pretrained model\n",
|
||||
"effnetb2 = torchvision.models.efficientnet_b2(weights=effnetb2_weights) # could also use weights=\"DEFAULT\"\n",
|
||||
"\n",
|
||||
"# 4. Freeze the base layers in the model (this will freeze all layers to begin with)\n",
|
||||
"for param in model.parameters():\n",
|
||||
" param.requires_grad = False"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Now to change the classifier head, let's first inspect it using the `classifier` attribute of our model."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 74,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"Sequential(\n",
|
||||
" (0): Dropout(p=0.3, inplace=True)\n",
|
||||
" (1): Linear(in_features=1408, out_features=1000, bias=True)\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
"execution_count": 74,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Check out EffNetB2 classifier head\n",
|
||||
"effnetb2.classifier"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Excellent! To change the classifier head to suit our own problem, let's replace the `out_features` variable with the same number of classes we have (in our case, `out_features=3`, one for pizza, steak, sushi).\n",
|
||||
"\n",
|
||||
"> **Note:** This process of changing the output layers/classifier head will be dependent on the problem you're working on. For example, if you wanted a different *number* of outputs or a different *kind* of ouput, you would have to change the output layers accordingly. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 75,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 5. Update the classifier head\n",
|
||||
"effnetb2.classifier = nn.Sequential(\n",
|
||||
" nn.Dropout(p=0.3, inplace=True), # keep dropout layer same\n",
|
||||
" nn.Linear(in_features=1408, # keep in_features same \n",
|
||||
" out_features=3)) # change out_features to suit our number of classes"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Beautiful!\n",
|
||||
"\n",
|
||||
"Looks like our EffNetB2 feature extractor is ready to go, however, since there's quite a few steps involved here, how about we turn the code above into a function we can re-use later?\n",
|
||||
"\n",
|
||||
"We'll call it `create_effnetb2_model()` and it'll take a customizable number of classes and a random seed parameter for reproducibility.\n",
|
||||
"\n",
|
||||
"Ideally, it will return an EffNetB2 feature extractor along with its assosciated transforms."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 76,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def create_effnetb2_model(num_classes:int=3, \n",
|
||||
" seed:int=42):\n",
|
||||
" \"\"\"Creates and EfficientNetB2 feature extractor model and transforms.\n",
|
||||
"\n",
|
||||
" Args:\n",
|
||||
" num_classes (int, optional): number of classes in the classifier head. \n",
|
||||
" Defaults to 3.\n",
|
||||
" seed (int, optional): random seed value. Defaults to 42.\n",
|
||||
"\n",
|
||||
" Returns:\n",
|
||||
" model (torch.nn.Module): EffNetB2 feature extractor model. \n",
|
||||
" transforms (torchvision.transforms): EffNetB2 image transforms.\n",
|
||||
" \"\"\"\n",
|
||||
" # 1, 2, 3. Create EffNetB2 pretrained weights, transforms and model.\n",
|
||||
" weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT\n",
|
||||
" transforms = weights.transforms()\n",
|
||||
" model = torchvision.models.efficientnet_b2(weights=weights)\n",
|
||||
"\n",
|
||||
" # Freeze base model\n",
|
||||
" # 4. Freeze all layers in base model\n",
|
||||
" for param in model.parameters():\n",
|
||||
" param.requires_grad = False\n",
|
||||
"\n",
|
||||
" # Change classifier head\n",
|
||||
" # 5. Change classifier head with random seed for reproducibility\n",
|
||||
" torch.manual_seed(seed)\n",
|
||||
" model.classifier = nn.Sequential(\n",
|
||||
" nn.Dropout(p=0.3, inplace=True),\n",
|
||||
" nn.Linear(in_features=1408, out_features=num_classes),\n",
|
||||
" )\n",
|
||||
" return model, transforms\n",
|
||||
"\n",
|
||||
" return model, transforms"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Woohoo! That's a nice looking function, let's try it out."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 77,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"effnetb2, effnetb2_transforms = create_effnetb2_model(num_classes=3,\n",
|
||||
" seed=42)"
|
||||
]
|
||||
@@ -278,32 +449,110 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"```\n",
|
||||
"(classifier): Sequential(\n",
|
||||
" (0): Dropout(p=0.3, inplace=True)\n",
|
||||
" (1): Linear(in_features=1408, out_features=1000, bias=True)\n",
|
||||
" )\n",
|
||||
"```"
|
||||
"No errors, nice, now to really try it out, let's get a summary with `torchinfo.summary()`. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": 78,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"============================================================================================================================================\n",
|
||||
"Layer (type (var_name)) Input Shape Output Shape Param # Trainable\n",
|
||||
"============================================================================================================================================\n",
|
||||
"EfficientNet (EfficientNet) [1, 3, 224, 224] [1, 3] -- Partial\n",
|
||||
"├─Sequential (features) [1, 3, 224, 224] [1, 1408, 7, 7] -- False\n",
|
||||
"│ └─Conv2dNormActivation (0) [1, 3, 224, 224] [1, 32, 112, 112] -- False\n",
|
||||
"│ │ └─Conv2d (0) [1, 3, 224, 224] [1, 32, 112, 112] (864) False\n",
|
||||
"│ │ └─BatchNorm2d (1) [1, 32, 112, 112] [1, 32, 112, 112] (64) False\n",
|
||||
"│ │ └─SiLU (2) [1, 32, 112, 112] [1, 32, 112, 112] -- --\n",
|
||||
"│ └─Sequential (1) [1, 32, 112, 112] [1, 16, 112, 112] -- False\n",
|
||||
"│ │ └─MBConv (0) [1, 32, 112, 112] [1, 16, 112, 112] (1,448) False\n",
|
||||
"│ │ └─MBConv (1) [1, 16, 112, 112] [1, 16, 112, 112] (612) False\n",
|
||||
"│ └─Sequential (2) [1, 16, 112, 112] [1, 24, 56, 56] -- False\n",
|
||||
"│ │ └─MBConv (0) [1, 16, 112, 112] [1, 24, 56, 56] (6,004) False\n",
|
||||
"│ │ └─MBConv (1) [1, 24, 56, 56] [1, 24, 56, 56] (10,710) False\n",
|
||||
"│ │ └─MBConv (2) [1, 24, 56, 56] [1, 24, 56, 56] (10,710) False\n",
|
||||
"│ └─Sequential (3) [1, 24, 56, 56] [1, 48, 28, 28] -- False\n",
|
||||
"│ │ └─MBConv (0) [1, 24, 56, 56] [1, 48, 28, 28] (16,518) False\n",
|
||||
"│ │ └─MBConv (1) [1, 48, 28, 28] [1, 48, 28, 28] (43,308) False\n",
|
||||
"│ │ └─MBConv (2) [1, 48, 28, 28] [1, 48, 28, 28] (43,308) False\n",
|
||||
"│ └─Sequential (4) [1, 48, 28, 28] [1, 88, 14, 14] -- False\n",
|
||||
"│ │ └─MBConv (0) [1, 48, 28, 28] [1, 88, 14, 14] (50,300) False\n",
|
||||
"│ │ └─MBConv (1) [1, 88, 14, 14] [1, 88, 14, 14] (123,750) False\n",
|
||||
"│ │ └─MBConv (2) [1, 88, 14, 14] [1, 88, 14, 14] (123,750) False\n",
|
||||
"│ │ └─MBConv (3) [1, 88, 14, 14] [1, 88, 14, 14] (123,750) False\n",
|
||||
"│ └─Sequential (5) [1, 88, 14, 14] [1, 120, 14, 14] -- False\n",
|
||||
"│ │ └─MBConv (0) [1, 88, 14, 14] [1, 120, 14, 14] (149,158) False\n",
|
||||
"│ │ └─MBConv (1) [1, 120, 14, 14] [1, 120, 14, 14] (237,870) False\n",
|
||||
"│ │ └─MBConv (2) [1, 120, 14, 14] [1, 120, 14, 14] (237,870) False\n",
|
||||
"│ │ └─MBConv (3) [1, 120, 14, 14] [1, 120, 14, 14] (237,870) False\n",
|
||||
"│ └─Sequential (6) [1, 120, 14, 14] [1, 208, 7, 7] -- False\n",
|
||||
"│ │ └─MBConv (0) [1, 120, 14, 14] [1, 208, 7, 7] (301,406) False\n",
|
||||
"│ │ └─MBConv (1) [1, 208, 7, 7] [1, 208, 7, 7] (686,868) False\n",
|
||||
"│ │ └─MBConv (2) [1, 208, 7, 7] [1, 208, 7, 7] (686,868) False\n",
|
||||
"│ │ └─MBConv (3) [1, 208, 7, 7] [1, 208, 7, 7] (686,868) False\n",
|
||||
"│ │ └─MBConv (4) [1, 208, 7, 7] [1, 208, 7, 7] (686,868) False\n",
|
||||
"│ └─Sequential (7) [1, 208, 7, 7] [1, 352, 7, 7] -- False\n",
|
||||
"│ │ └─MBConv (0) [1, 208, 7, 7] [1, 352, 7, 7] (846,900) False\n",
|
||||
"│ │ └─MBConv (1) [1, 352, 7, 7] [1, 352, 7, 7] (1,888,920) False\n",
|
||||
"│ └─Conv2dNormActivation (8) [1, 352, 7, 7] [1, 1408, 7, 7] -- False\n",
|
||||
"│ │ └─Conv2d (0) [1, 352, 7, 7] [1, 1408, 7, 7] (495,616) False\n",
|
||||
"│ │ └─BatchNorm2d (1) [1, 1408, 7, 7] [1, 1408, 7, 7] (2,816) False\n",
|
||||
"│ │ └─SiLU (2) [1, 1408, 7, 7] [1, 1408, 7, 7] -- --\n",
|
||||
"├─AdaptiveAvgPool2d (avgpool) [1, 1408, 7, 7] [1, 1408, 1, 1] -- --\n",
|
||||
"├─Sequential (classifier) [1, 1408] [1, 3] -- True\n",
|
||||
"│ └─Dropout (0) [1, 1408] [1, 1408] -- --\n",
|
||||
"│ └─Linear (1) [1, 1408] [1, 3] 4,227 True\n",
|
||||
"============================================================================================================================================\n",
|
||||
"Total params: 7,705,221\n",
|
||||
"Trainable params: 4,227\n",
|
||||
"Non-trainable params: 7,700,994\n",
|
||||
"Total mult-adds (M): 657.64\n",
|
||||
"============================================================================================================================================\n",
|
||||
"Input size (MB): 0.60\n",
|
||||
"Forward/backward pass size (MB): 156.80\n",
|
||||
"Params size (MB): 30.82\n",
|
||||
"Estimated Total Size (MB): 188.22\n",
|
||||
"============================================================================================================================================"
|
||||
]
|
||||
},
|
||||
"execution_count": 78,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# summary(effnetb2, \n",
|
||||
"# input_size=(1, 3, 224, 224),\n",
|
||||
"# col_names=[\"input_size\", \"output_size\", \"num_params\", \"trainable\"],\n",
|
||||
"# col_width=20,\n",
|
||||
"# row_settings=[\"var_names\"])"
|
||||
"from torchinfo import summary\n",
|
||||
"summary(effnetb2, \n",
|
||||
" input_size=(1, 3, 224, 224),\n",
|
||||
" col_names=[\"input_size\", \"output_size\", \"num_params\", \"trainable\"],\n",
|
||||
" col_width=20,\n",
|
||||
" row_settings=[\"var_names\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### TK - Create DataLoaders for EffNetB2 "
|
||||
"Base layers frozen, top layers trainable and customized!\n",
|
||||
"\n",
|
||||
"TK image - turn model summary into image"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### TK - Create DataLoaders for EffNetB2 \n",
|
||||
"\n",
|
||||
"Our EffNetB2 feature extractor is ready, time to create some `DataLoader`s.\n",
|
||||
"\n",
|
||||
"UPTOHERE: detail the DataLoader creation...\n",
|
||||
"* TK Where have we done this before? (plenty of places...)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -316,7 +565,7 @@
|
||||
"train_dataloader_effnetb2, test_dataloader_effnetb2, class_names = data_setup.create_dataloaders(train_dir=train_dir,\n",
|
||||
" test_dir=test_dir,\n",
|
||||
" transform=effnetb2_transforms,\n",
|
||||
" batch_size=32)\n"
|
||||
" batch_size=32)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -334,7 +583,7 @@
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "67dc332241e14e9ab32c455c6db34589",
|
||||
"model_id": "3d4684293dc5410da7dd1862fddbe981",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
@@ -578,7 +827,7 @@
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "740c2aa06e7a4ab58fa685e36655b3f9",
|
||||
"model_id": "f851e14cc88d4c4fbe09136ee6bc1a8f",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
@@ -810,7 +1059,7 @@
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "96876288b60b44d599536a9d3d058261",
|
||||
"model_id": "657243417ae446e2af80737638b5c318",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
@@ -828,19 +1077,19 @@
|
||||
" 'class_name': 'steak',\n",
|
||||
" 'pred_prob': 0.9293267726898193,\n",
|
||||
" 'pred_class': 'steak',\n",
|
||||
" 'time_for_pred': 0.0611,\n",
|
||||
" 'time_for_pred': 0.0552,\n",
|
||||
" 'correct': True},\n",
|
||||
" {'image_path': PosixPath('data/pizza_steak_sushi_20_percent/test/steak/3100563.jpg'),\n",
|
||||
" 'class_name': 'steak',\n",
|
||||
" 'pred_prob': 0.9533632397651672,\n",
|
||||
" 'pred_class': 'steak',\n",
|
||||
" 'time_for_pred': 0.0376,\n",
|
||||
" 'time_for_pred': 0.0351,\n",
|
||||
" 'correct': True},\n",
|
||||
" {'image_path': PosixPath('data/pizza_steak_sushi_20_percent/test/steak/2752603.jpg'),\n",
|
||||
" 'class_name': 'steak',\n",
|
||||
" 'pred_prob': 0.7531499266624451,\n",
|
||||
" 'pred_class': 'steak',\n",
|
||||
" 'time_for_pred': 0.0367,\n",
|
||||
" 'time_for_pred': 0.0343,\n",
|
||||
" 'correct': True}]"
|
||||
]
|
||||
},
|
||||
@@ -874,7 +1123,7 @@
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "1f8a038bc23149f7ab7603782e270867",
|
||||
"model_id": "e9555bfb67f6461a89656ab8b9b95c1f",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
@@ -892,19 +1141,19 @@
|
||||
" 'class_name': 'steak',\n",
|
||||
" 'pred_prob': 0.9932767748832703,\n",
|
||||
" 'pred_class': 'steak',\n",
|
||||
" 'time_for_pred': 0.1636,\n",
|
||||
" 'time_for_pred': 0.1866,\n",
|
||||
" 'correct': True},\n",
|
||||
" {'image_path': PosixPath('data/pizza_steak_sushi_20_percent/test/steak/3100563.jpg'),\n",
|
||||
" 'class_name': 'steak',\n",
|
||||
" 'pred_prob': 0.9893474578857422,\n",
|
||||
" 'pred_class': 'steak',\n",
|
||||
" 'time_for_pred': 0.0685,\n",
|
||||
" 'time_for_pred': 0.0638,\n",
|
||||
" 'correct': True},\n",
|
||||
" {'image_path': PosixPath('data/pizza_steak_sushi_20_percent/test/steak/2752603.jpg'),\n",
|
||||
" 'class_name': 'steak',\n",
|
||||
" 'pred_prob': 0.997147262096405,\n",
|
||||
" 'pred_class': 'steak',\n",
|
||||
" 'time_for_pred': 0.0639,\n",
|
||||
" 'time_for_pred': 0.0626,\n",
|
||||
" 'correct': True}]"
|
||||
]
|
||||
},
|
||||
@@ -938,7 +1187,7 @@
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0.031052666666666683"
|
||||
"0.028969333333333333"
|
||||
]
|
||||
},
|
||||
"execution_count": 24,
|
||||
@@ -965,7 +1214,7 @@
|
||||
"{'test_loss': 0.281286683678627,\n",
|
||||
" 'test_acc': 0.96875,\n",
|
||||
" 'model_size (MB)': 29,\n",
|
||||
" 'time_per_pred_cpu': 0.031052666666666683}"
|
||||
" 'time_per_pred_cpu': 0.028969333333333333}"
|
||||
]
|
||||
},
|
||||
"execution_count": 25,
|
||||
@@ -986,7 +1235,7 @@
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0.06613866666666665"
|
||||
"0.0646173333333333"
|
||||
]
|
||||
},
|
||||
"execution_count": 26,
|
||||
@@ -1013,7 +1262,7 @@
|
||||
"{'test_loss': 0.06418213145807386,\n",
|
||||
" 'test_acc': 0.984659090909091,\n",
|
||||
" 'model_size (MB)': 327,\n",
|
||||
" 'time_per_pred_cpu': 0.06613866666666665}"
|
||||
" 'time_per_pred_cpu': 0.0646173333333333}"
|
||||
]
|
||||
},
|
||||
"execution_count": 27,
|
||||
@@ -1071,14 +1320,14 @@
|
||||
" <td>0.281287</td>\n",
|
||||
" <td>0.968750</td>\n",
|
||||
" <td>29</td>\n",
|
||||
" <td>0.031053</td>\n",
|
||||
" <td>0.028969</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>vit</th>\n",
|
||||
" <td>0.064182</td>\n",
|
||||
" <td>0.984659</td>\n",
|
||||
" <td>327</td>\n",
|
||||
" <td>0.066139</td>\n",
|
||||
" <td>0.064617</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
@@ -1087,8 +1336,8 @@
|
||||
"text/plain": [
|
||||
" test_loss test_acc model_size (MB) time_per_pred_cpu\n",
|
||||
"model \n",
|
||||
"effnetb2 0.281287 0.968750 29 0.031053\n",
|
||||
"vit 0.064182 0.984659 327 0.066139"
|
||||
"effnetb2 0.281287 0.968750 29 0.028969\n",
|
||||
"vit 0.064182 0.984659 327 0.064617"
|
||||
]
|
||||
},
|
||||
"execution_count": 28,
|
||||
@@ -1114,7 +1363,7 @@
|
||||
"test_loss 0.228173\n",
|
||||
"test_acc 1.016422\n",
|
||||
"model_size (MB) 11.275862\n",
|
||||
"time_per_pred_cpu 2.129887\n",
|
||||
"time_per_pred_cpu 2.230543\n",
|
||||
"dtype: float64"
|
||||
]
|
||||
},
|
||||
@@ -1185,16 +1434,16 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"data/pizza_steak_sushi_20_percent/test/sushi/1944743.jpg\n"
|
||||
"data/pizza_steak_sushi_20_percent/test/steak/864661.jpg\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"({'pizza': 0.05178415775299072,\n",
|
||||
" 'steak': 0.03133663162589073,\n",
|
||||
" 'sushi': 0.9168791174888611},\n",
|
||||
" 0.03212)"
|
||||
"({'pizza': 0.30049753189086914,\n",
|
||||
" 'steak': 0.40433385968208313,\n",
|
||||
" 'sushi': 0.29516857862472534},\n",
|
||||
" 0.04928)"
|
||||
]
|
||||
},
|
||||
"execution_count": 32,
|
||||
@@ -1240,9 +1489,9 @@
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[['data/pizza_steak_sushi_20_percent/test/sushi/2378406.jpg'],\n",
|
||||
" ['data/pizza_steak_sushi_20_percent/test/sushi/3886015.jpg'],\n",
|
||||
" ['data/pizza_steak_sushi_20_percent/test/sushi/1944743.jpg']]"
|
||||
"[['data/pizza_steak_sushi_20_percent/test/steak/1482489.jpg'],\n",
|
||||
" ['data/pizza_steak_sushi_20_percent/test/sushi/46797.jpg'],\n",
|
||||
" ['data/pizza_steak_sushi_20_percent/test/sushi/1383396.jpg']]"
|
||||
]
|
||||
},
|
||||
"execution_count": 34,
|
||||
@@ -1284,7 +1533,7 @@
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(<gradio.routes.App at 0x7f9aa6a68c10>, 'http://127.0.0.1:7860/', None)"
|
||||
"(<gradio.routes.App at 0x7fb6d2b20430>, 'http://127.0.0.1:7860/', None)"
|
||||
]
|
||||
},
|
||||
"execution_count": 35,
|
||||
@@ -1544,7 +1793,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 88,
|
||||
"execution_count": 44,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -1608,7 +1857,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 44,
|
||||
"execution_count": 45,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -1622,7 +1871,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 45,
|
||||
"execution_count": 46,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -1637,7 +1886,7 @@
|
||||
")"
|
||||
]
|
||||
},
|
||||
"execution_count": 45,
|
||||
"execution_count": 46,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -1646,9 +1895,18 @@
|
||||
"effnetb2_transforms"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### TK - Getting data for FoodVision Big\n",
|
||||
"\n",
|
||||
"> TK **Note:** If you're using Google Colab, the cell below will take ~5-10 minutes to fully run and download the Food101 images from PyTorch. This is because there is over 100,000 images being downloaded. If you restart your Google Colab runtime and come back to this cell, the images will have to redownload. Alternatively, if you're running this notebook locally, the images will be cached and stroed in the `root/` directory specified below. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 46,
|
||||
"execution_count": 47,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -1666,7 +1924,7 @@
|
||||
" 'breakfast_burrito']"
|
||||
]
|
||||
},
|
||||
"execution_count": 46,
|
||||
"execution_count": 47,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -1698,7 +1956,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 47,
|
||||
"execution_count": 48,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -1707,7 +1965,7 @@
|
||||
"(15150, 5050)"
|
||||
]
|
||||
},
|
||||
"execution_count": 47,
|
||||
"execution_count": 48,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -1735,7 +1993,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 48,
|
||||
"execution_count": 49,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -1765,13 +2023,13 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 49,
|
||||
"execution_count": 50,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "9f936307ce35432187e6289e229a4746",
|
||||
"model_id": "1603dc6e2dc642dc9637bf693cec4a53",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
@@ -1824,7 +2082,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 50,
|
||||
"execution_count": 51,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -1853,7 +2111,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 51,
|
||||
"execution_count": 52,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -1883,7 +2141,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 52,
|
||||
"execution_count": 53,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -1892,7 +2150,7 @@
|
||||
"<All keys matched successfully>"
|
||||
]
|
||||
},
|
||||
"execution_count": 52,
|
||||
"execution_count": 53,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -1912,7 +2170,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 53,
|
||||
"execution_count": 54,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -1942,7 +2200,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 54,
|
||||
"execution_count": 55,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -1962,23 +2220,23 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 55,
|
||||
"execution_count": 56,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"--2022-08-11 14:56:30-- https://raw.githubusercontent.com/mrdbourke/pytorch-deep-learning/main/images/04-pizza-dad.jpeg\n",
|
||||
"Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.109.133, 185.199.110.133, 185.199.111.133, ...\n",
|
||||
"Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.109.133|:443... connected.\n",
|
||||
"--2022-08-12 11:22:47-- https://raw.githubusercontent.com/mrdbourke/pytorch-deep-learning/main/images/04-pizza-dad.jpeg\n",
|
||||
"Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.110.133, 185.199.108.133, 185.199.109.133, ...\n",
|
||||
"Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.110.133|:443... connected.\n",
|
||||
"HTTP request sent, awaiting response... 200 OK\n",
|
||||
"Length: 2874848 (2.7M) [image/jpeg]\n",
|
||||
"Saving to: '04-pizza-dad.jpeg’\n",
|
||||
"\n",
|
||||
"04-pizza-dad.jpeg 100%[===================>] 2.74M 7.73MB/s in 0.4s \n",
|
||||
"04-pizza-dad.jpeg 100%[===================>] 2.74M 7.17MB/s in 0.4s \n",
|
||||
"\n",
|
||||
"2022-08-11 14:56:32 (7.73 MB/s) - '04-pizza-dad.jpeg’ saved [2874848/2874848]\n",
|
||||
"2022-08-12 11:22:48 (7.17 MB/s) - '04-pizza-dad.jpeg’ saved [2874848/2874848]\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
@@ -1990,7 +2248,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 56,
|
||||
"execution_count": 57,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -2006,7 +2264,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 57,
|
||||
"execution_count": 58,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -2015,7 +2273,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 58,
|
||||
"execution_count": 59,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -2025,7 +2283,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 59,
|
||||
"execution_count": 60,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -2034,7 +2292,7 @@
|
||||
"['apple_pie', 'baby_back_ribs', 'baklava', 'beef_carpaccio', 'beef_tartare']"
|
||||
]
|
||||
},
|
||||
"execution_count": 59,
|
||||
"execution_count": 60,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -2048,7 +2306,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 60,
|
||||
"execution_count": 61,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -2092,7 +2350,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 61,
|
||||
"execution_count": 62,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -2166,7 +2424,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 62,
|
||||
"execution_count": 63,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -2193,21 +2451,21 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 89,
|
||||
"execution_count": 64,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" adding: 09_pretrained_effnetb2_feature_extractor_food101_20_percent.pth (deflated 8%)\n",
|
||||
" adding: app.py (deflated 49%)\n",
|
||||
" adding: class_names.txt (deflated 48%)\n",
|
||||
" adding: examples/ (stored 0%)\n",
|
||||
" adding: examples/04-pizza-dad.jpeg (deflated 0%)\n",
|
||||
" adding: flagged/ (stored 0%)\n",
|
||||
" adding: model.py (deflated 46%)\n",
|
||||
" adding: requirements.txt (deflated 4%)\n",
|
||||
"updating: 09_pretrained_effnetb2_feature_extractor_food101_20_percent.pth (deflated 8%)\n",
|
||||
"updating: app.py (deflated 49%)\n",
|
||||
"updating: class_names.txt (deflated 48%)\n",
|
||||
"updating: examples/ (stored 0%)\n",
|
||||
"updating: examples/04-pizza-dad.jpeg (deflated 0%)\n",
|
||||
"updating: flagged/ (stored 0%)\n",
|
||||
"updating: model.py (deflated 46%)\n",
|
||||
"updating: requirements.txt (deflated 4%)\n",
|
||||
"Not running in Google Colab, can't use google.colab.files.download()\n"
|
||||
]
|
||||
}
|
||||
@@ -2235,11 +2493,6 @@
|
||||
"See the deployed app - https://huggingface.co/spaces/mrdbourke/foodvision_big/"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
@@ -2271,7 +2524,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 63,
|
||||
"execution_count": 65,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user