From 6397d9b062d9cfc2b80bde5296f31b281ebfd7ee Mon Sep 17 00:00:00 2001 From: Daniel Bourke Date: Tue, 23 Aug 2022 10:22:45 +1000 Subject: [PATCH] add images to 09 --- 09_pytorch_model_deployment.ipynb | 212 ++++++------------------------ 1 file changed, 43 insertions(+), 169 deletions(-) diff --git a/09_pytorch_model_deployment.ipynb b/09_pytorch_model_deployment.ipynb index 9627535..34519c9 100644 --- a/09_pytorch_model_deployment.ipynb +++ b/09_pytorch_model_deployment.ipynb @@ -25,7 +25,7 @@ "\n", "In other words, **we're going to deploy our FoodVision Mini model to the internet as a usable app!**\n", "\n", - "TK image - showcase what we're doing, gif of FoodVision Mini being used on a mobile device" + "TK image - showcase what we're doing, gif of FoodVision Mini being used on a mobile device, can get a gif of taking a photo of sushi" ] }, { @@ -50,7 +50,12 @@ "\n", "Meaning a Tesla car's computer vision system will interact with the car's route planning program and then the route planning program will get inputs and feedback from the driver.\n", "\n", - "TK image - someone interacting with an ML model (e.g. Tesla car/FoodVision mini app), something interacting with an ML model (e.g. a program)" + "\"two \n", + "\n", + "*Machine learning model deployment involves making your model available to someone or something else. For example, someone might use your model as part of a food recognition app (such as FoodVision Mini or [Nutrify](https://nutrify.app)). And something else might be another model or program using your model such as a banking system using a machine learning model to detect if a transaction is fraud or not.* \n", + " \n", + " \n", + " " ] }, { @@ -87,7 +92,7 @@ "\n", "Thus this highlights the importance of model deployment: it helps you figure out errors in your model that aren't obvious during training.\n", "\n", - "TK image - workflow + model deployment" + "TK image - PyTorch workflow flowchart + model deployment" ] }, { @@ -204,8 +209,6 @@ "\n", "But for our training pipeline, it's okay for it to happen in a batch (offline) fashion, which is what we've been doing throughout the previous chapters.\n", "\n", - "\n", - "\n", "TK image - predicting in real-time (credit card fraud/FoodVision Mini) vs predicting batch mode (photos sorting app)" ] }, @@ -482,7 +485,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## TK - 2. FoodVision Mini modelling experiment outline\n", + "## TK - 2. FoodVision Mini model deployment experiment outline\n", "\n", "The ideal deployed model FoodVision Mini performs well and fast.\n", "\n", @@ -690,93 +693,27 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 80, "metadata": {}, - "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": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "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\"])" + "\n", + "# # Print EffNetB2 model summary (uncomment for full output) \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": [ - "Base layers frozen, top layers trainable and customized!\n", + "\"effnetb2\n", "\n", - "TK image - turn model summary into image" + "Base layers frozen, top layers trainable and customized!" ] }, { @@ -1244,18 +1181,20 @@ ], "source": [ "from torchinfo import summary\n", - "summary(vit, \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\"])" + "\n", + "# # Print ViT feature extractor model summary (uncomment for full output)\n", + "# summary(vit, \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 image - summary output\n", + "\"vit\n", "\n", "Just like our EffNetB2 feature extractor model, our ViT model's base layers are frozen and the output layer is customized to our needs! \n", "\n", @@ -3691,8 +3630,9 @@ "1. Unzipping the file.\n", "2. Opening terminal or a command line prompt.\n", "3. Changing into the `foodvision_mini` directory (`cd foodvision_mini`).\n", - "4. Installing the requirements (`pip install requirements.txt`).\n", - "5. Run the app (`python app.py`).\n", + "4. TK create an environment with `python3 -m venv env`... activate it... use it...\n", + "5. Installing the requirements (`pip install requirements.txt`).\n", + "6. Run the app (`python app.py`).\n", "\n", "This should result in a Gradio demo just like the one we built above running locally on your machine.\n", "\n", @@ -3848,93 +3788,26 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 81, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "============================================================================================================================================\n", - "Layer (type (var_name)) Input Shape Output Shape Param # Trainable\n", - "============================================================================================================================================\n", - "EfficientNet (EfficientNet) [1, 3, 224, 224] [1, 101] -- 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, 101] -- True\n", - "│ └─Dropout (0) [1, 1408] [1, 1408] -- --\n", - "│ └─Linear (1) [1, 1408] [1, 101] 142,309 True\n", - "============================================================================================================================================\n", - "Total params: 7,843,303\n", - "Trainable params: 142,309\n", - "Non-trainable params: 7,700,994\n", - "Total mult-adds (M): 657.78\n", - "============================================================================================================================================\n", - "Input size (MB): 0.60\n", - "Forward/backward pass size (MB): 156.80\n", - "Params size (MB): 31.37\n", - "Estimated Total Size (MB): 188.77\n", - "============================================================================================================================================" - ] - }, - "execution_count": 62, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "# Get a summary of EffNetB2 feature extractor for Food101\n", "from torchinfo import summary\n", - "summary(effnetb2_food101, \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\"])" + "\n", + "# # Get a summary of EffNetB2 feature extractor for Food101 with 101 output classes (uncomment for full output)\n", + "# summary(effnetb2_food101, \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 image - turn summary into image\n", - "\n", + "\"effnetb2\n", + " \n", "Nice!\n", "\n", "See how just like our EffNetB2 model for FoodVision Mini the base layers are frozen (these are pretrained on ImageNet) and the outer layers (the `classifier` layers) are trainble with an ouput shape of `[batch_size, 101]` (`101` for 101 classes in Food101). \n", @@ -5092,6 +4965,7 @@ "## TK - Main takeaways\n", "\n", "UPTOHERE:\n", + "* Ask: \"Can I deploy my model sooner rather than later?\" - bring in the fourth dataset split, the real world \n", "* Go back through notebook and fix up TK's\n", "* Fill out main takeaways" ]