From 7765ddde590a1baa391d0f7ca1cae0a03376fbdb Mon Sep 17 00:00:00 2001 From: Daniel Bourke Date: Thu, 2 Jun 2022 15:21:08 +1000 Subject: [PATCH] update experiment tracking notebook --- 07_pytorch_experiment_tracking.ipynb | 770 ++++++++++++++++++++------- 1 file changed, 570 insertions(+), 200 deletions(-) diff --git a/07_pytorch_experiment_tracking.ipynb b/07_pytorch_experiment_tracking.ipynb index 7555793..ed69b8b 100644 --- a/07_pytorch_experiment_tracking.ipynb +++ b/07_pytorch_experiment_tracking.ipynb @@ -1,5 +1,12 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\"Open" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -32,7 +39,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -72,7 +79,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -81,7 +88,7 @@ "'cuda'" ] }, - "execution_count": 3, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -100,7 +107,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -116,7 +123,7 @@ "PosixPath('data/pizza_steak_sushi')" ] }, - "execution_count": 4, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -189,18 +196,18 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "(,\n", - " ,\n", + "(,\n", + " ,\n", " ['pizza', 'steak', 'sushi'])" ] }, - "execution_count": 5, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -241,7 +248,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -251,7 +258,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -277,7 +284,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -297,7 +304,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -306,7 +313,7 @@ "'EfficietNetB0'" ] }, - "execution_count": 9, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -318,7 +325,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -338,7 +345,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -431,13 +438,13 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "6bd065e7a06d4f2fb61968d5d76754c6", + "model_id": "c7acb79d1dd2456785ddb69b477682ec", "version_major": 2, "version_minor": 0 }, @@ -473,33 +480,89 @@ ] }, { - "cell_type": "code", - "execution_count": 16, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# TK - view the above in TensorBoard..." + "## TK 5. View the above in TensorBoard...\n", + "\n", + "TK - What is TensorBoard?\n", + "\n", + "TK You can view the training results above in TensorBoard in a number of ways:\n", + "\n", + "| Code environment | How to view TensorBoard | Resource |\n", + "| ----- | ----- | ----- |\n", + "| VS Code (notebooks or Python scripts) | Press `SHIFT + CMD + P` to open the Command Palette and search for the command \"Python: Launch TensorBoard\". | [VS Code Guide on TensorBoard and PyTorch](https://code.visualstudio.com/docs/datascience/pytorch-support#_tensorboard-integration) |\n", + "| Jupyter and Colab Notebooks | Make sure [TensorBoard is installed](https://pypi.org/project/tensorboard/), load it with `%load_ext tensorboard` and then view your results with `%tensorboard --logdir DIR_WITH_LOGS`. | [`torch.utils.tensorboard`](https://pytorch.org/docs/stable/tensorboard.html) and [Get started with TensorBoard](https://www.tensorflow.org/tensorboard/get_started) |\n", + "\n", + "You can also upload your experiments to [tensorboard.dev](https://tensorboard.dev/) to share them publicly with others." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Reusing TensorBoard on port 6006 (pid 1168517), started 2 days, 3:10:48 ago. (Use '!kill 1168517' to kill it.)" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "\n", + " \n", + " \n", + " " + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# Example code to run in Google Colab\n", + "%load_ext tensorboard\n", + "%tensorboard --logdir runs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## TK Create a helper function to build `SummaryWriter()` instances" + "## TK 6. Create a helper function to build `SummaryWriter()` instances" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'2022-05-31'" + "'2022-06-02'" ] }, - "execution_count": 13, + "execution_count": 35, "metadata": {}, "output_type": "execute_result" } @@ -511,13 +574,12 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "def create_writer(experiment_name, model_name, extra=None):\n", - " # assert experiment_name, \"experiment_name can't be None\"\n", - " # assert model_name, \"model_name can't be None\"\n", + " # TK - docstring\n", "\n", " # Get timestamp of current date (all experiments on certain day live in same folder)\n", " timestamp = datetime.now().strftime(\"%Y-%m-%d\")\n", @@ -532,7 +594,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 37, "metadata": {}, "outputs": [], "source": [ @@ -614,13 +676,13 @@ " if writer:\n", " # Add results to SummaryWriter\n", " writer.add_scalars(main_tag=\"Loss\", \n", - " tag_scalar_dict={\"train_loss\": train_loss,\n", + " tag_scalar_dict={\"train_loss\": train_loss,\n", " \"test_loss\": test_loss},\n", - " global_step=epoch)\n", + " global_step=epoch)\n", " writer.add_scalars(main_tag=\"Accuracy\", \n", - " tag_scalar_dict={\"train_acc\": train_acc,\n", + " tag_scalar_dict={\"train_acc\": train_acc,\n", " \"test_acc\": test_acc}, \n", - " global_step=epoch)\n", + " global_step=epoch)\n", "\n", " # Close the writer\n", " writer.close()\n", @@ -638,19 +700,16 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## TK - Experiment creation\n", + "## TK - 7. Setting up a series of modelling experiments\n", "\n", - "TK - exerpiments to track:\n", + "TK - turn this into a table...\n", + "\n", + "TK - experiments to track:\n", "- model_0 = 10% data, effnet_b0, 5 epochs\n", "- model_1 = 10% data, effnet_b0, 10 epochs (double training time)\n", "- model_2 = 20% data, effnet_b0, 5 epochs (double data)\n", "- model_3 = 10% data, effnet_b2, 5 epochs (double model parameters)\n", - "\n", - "Want:\n", - "* A list or dict of different models and experiments to run\n", - "* Each experiment should run only changing the model or augmentation type \n", - " * e.g. Experiment 1: model = EffNetB0, aug = None \n", - " * Experiment 2: model = EffNetB4, aug = None \n", + "- 2 x epochs, 2 x models, 2 x datasets = 6 models total\n", "\n", "See a list of Torch pretrained models here: https://pytorch.org/vision/stable/models.html, all pretrained models are expected to have data inputs that are normalized with the following:\n", "```python\n", @@ -661,7 +720,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 38, "metadata": {}, "outputs": [], "source": [ @@ -691,7 +750,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 39, "metadata": {}, "outputs": [ { @@ -713,7 +772,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 40, "metadata": {}, "outputs": [], "source": [ @@ -727,7 +786,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 41, "metadata": {}, "outputs": [ { @@ -736,7 +795,7 @@ "(8, 15, 3, 3)" ] }, - "execution_count": 21, + "execution_count": 41, "metadata": {}, "output_type": "execute_result" } @@ -771,91 +830,23 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 42, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "============================================================================================================================================\n", - "Layer (type (var_name)) Input Shape Output Shape Param # Trainable\n", - "============================================================================================================================================\n", - "EfficientNet -- -- -- True\n", - "├─Sequential (features) [32, 3, 224, 224] [32, 1408, 7, 7] -- True\n", - "│ └─ConvNormActivation (0) [32, 3, 224, 224] [32, 32, 112, 112] -- True\n", - "│ │ └─Conv2d (0) [32, 3, 224, 224] [32, 32, 112, 112] 864 True\n", - "│ │ └─BatchNorm2d (1) [32, 32, 112, 112] [32, 32, 112, 112] 64 True\n", - "│ │ └─SiLU (2) [32, 32, 112, 112] [32, 32, 112, 112] -- --\n", - "│ └─Sequential (1) [32, 32, 112, 112] [32, 16, 112, 112] -- True\n", - "│ │ └─MBConv (0) [32, 32, 112, 112] [32, 16, 112, 112] 1,448 True\n", - "│ │ └─MBConv (1) [32, 16, 112, 112] [32, 16, 112, 112] 612 True\n", - "│ └─Sequential (2) [32, 16, 112, 112] [32, 24, 56, 56] -- True\n", - "│ │ └─MBConv (0) [32, 16, 112, 112] [32, 24, 56, 56] 6,004 True\n", - "│ │ └─MBConv (1) [32, 24, 56, 56] [32, 24, 56, 56] 10,710 True\n", - "│ │ └─MBConv (2) [32, 24, 56, 56] [32, 24, 56, 56] 10,710 True\n", - "│ └─Sequential (3) [32, 24, 56, 56] [32, 48, 28, 28] -- True\n", - "│ │ └─MBConv (0) [32, 24, 56, 56] [32, 48, 28, 28] 16,518 True\n", - "│ │ └─MBConv (1) [32, 48, 28, 28] [32, 48, 28, 28] 43,308 True\n", - "│ │ └─MBConv (2) [32, 48, 28, 28] [32, 48, 28, 28] 43,308 True\n", - "│ └─Sequential (4) [32, 48, 28, 28] [32, 88, 14, 14] -- True\n", - "│ │ └─MBConv (0) [32, 48, 28, 28] [32, 88, 14, 14] 50,300 True\n", - "│ │ └─MBConv (1) [32, 88, 14, 14] [32, 88, 14, 14] 123,750 True\n", - "│ │ └─MBConv (2) [32, 88, 14, 14] [32, 88, 14, 14] 123,750 True\n", - "│ │ └─MBConv (3) [32, 88, 14, 14] [32, 88, 14, 14] 123,750 True\n", - "│ └─Sequential (5) [32, 88, 14, 14] [32, 120, 14, 14] -- True\n", - "│ │ └─MBConv (0) [32, 88, 14, 14] [32, 120, 14, 14] 149,158 True\n", - "│ │ └─MBConv (1) [32, 120, 14, 14] [32, 120, 14, 14] 237,870 True\n", - "│ │ └─MBConv (2) [32, 120, 14, 14] [32, 120, 14, 14] 237,870 True\n", - "│ │ └─MBConv (3) [32, 120, 14, 14] [32, 120, 14, 14] 237,870 True\n", - "│ └─Sequential (6) [32, 120, 14, 14] [32, 208, 7, 7] -- True\n", - "│ │ └─MBConv (0) [32, 120, 14, 14] [32, 208, 7, 7] 301,406 True\n", - "│ │ └─MBConv (1) [32, 208, 7, 7] [32, 208, 7, 7] 686,868 True\n", - "│ │ └─MBConv (2) [32, 208, 7, 7] [32, 208, 7, 7] 686,868 True\n", - "│ │ └─MBConv (3) [32, 208, 7, 7] [32, 208, 7, 7] 686,868 True\n", - "│ │ └─MBConv (4) [32, 208, 7, 7] [32, 208, 7, 7] 686,868 True\n", - "│ └─Sequential (7) [32, 208, 7, 7] [32, 352, 7, 7] -- True\n", - "│ │ └─MBConv (0) [32, 208, 7, 7] [32, 352, 7, 7] 846,900 True\n", - "│ │ └─MBConv (1) [32, 352, 7, 7] [32, 352, 7, 7] 1,888,920 True\n", - "│ └─ConvNormActivation (8) [32, 352, 7, 7] [32, 1408, 7, 7] -- True\n", - "│ │ └─Conv2d (0) [32, 352, 7, 7] [32, 1408, 7, 7] 495,616 True\n", - "│ │ └─BatchNorm2d (1) [32, 1408, 7, 7] [32, 1408, 7, 7] 2,816 True\n", - "│ │ └─SiLU (2) [32, 1408, 7, 7] [32, 1408, 7, 7] -- --\n", - "├─AdaptiveAvgPool2d (avgpool) [32, 1408, 7, 7] [32, 1408, 1, 1] -- --\n", - "├─Sequential (classifier) [32, 1408] [32, 1000] -- True\n", - "│ └─Dropout (0) [32, 1408] [32, 1408] -- --\n", - "│ └─Linear (1) [32, 1408] [32, 1000] 1,409,000 True\n", - "============================================================================================================================================\n", - "Total params: 9,109,994\n", - "Trainable params: 9,109,994\n", - "Non-trainable params: 0\n", - "Total mult-adds (G): 21.09\n", - "============================================================================================================================================\n", - "Input size (MB): 19.27\n", - "Forward/backward pass size (MB): 5017.79\n", - "Params size (MB): 36.44\n", - "Estimated Total Size (MB): 5073.49\n", - "============================================================================================================================================" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "effnetb2 = torchvision.models.efficientnet_b2(pretrained=True).to(device)\n", - "summary(model=effnetb2, \n", - " input_size=(32, 3, 224, 224), # make sure this is \"input_size\", not \"input_shape\"\n", - " # col_names=[\"input_size\"], # uncomment for smaller output\n", - " col_names=[\"input_size\", \"output_size\", \"num_params\", \"trainable\"],\n", - " col_width=20,\n", - " row_settings=[\"var_names\"]\n", - ") " + "# summary(model=effnetb2, \n", + "# input_size=(32, 3, 224, 224), # make sure this is \"input_size\", not \"input_shape\"\n", + "# # col_names=[\"input_size\"], # uncomment for smaller output\n", + "# col_names=[\"input_size\", \"output_size\", \"num_params\", \"trainable\"],\n", + "# col_width=20,\n", + "# row_settings=[\"var_names\"]\n", + "# ) " ] }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 43, "metadata": {}, "outputs": [ { @@ -864,18 +855,19 @@ "1408" ] }, - "execution_count": 23, + "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ + "# Get the number of in_features of the EfficientNetB2 classifier layer\n", "len(effnetb2.classifier.state_dict()[\"1.weight\"][0])" ] }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 44, "metadata": {}, "outputs": [], "source": [ @@ -892,28 +884,42 @@ "\n", "# EfficientNetB0\n", "def create_effnetb0():\n", - " set_seeds()\n", + " # Get the base mdoel\n", " model = torchvision.models.efficientnet_b0(pretrained=True).to(device)\n", + "\n", + " # Free the base model layers\n", + " for param in model.features.parameters():\n", + " param.requires_grad = False\n", + "\n", + " # Change the classifier head\n", + " set_seeds()\n", " model.classifier = nn.Sequential(\n", " nn.Dropout(p=0.2),\n", " nn.Linear(in_features=1280, out_features=OUT_FEATURES)\n", " ).to(device)\n", - " for param in model.features.parameters():\n", - " param.requires_grad = False\n", + "\n", + " # Give the model a name\n", " model.name = \"effnetb0\"\n", " print(f\"[INFO] Creating new {model.name} model.\")\n", " return model\n", "\n", "# EfficientNetB2\n", "def create_effnetb2():\n", - " set_seeds()\n", + " # Get the base model\n", " model = torchvision.models.efficientnet_b2(pretrained=True).to(device)\n", + "\n", + " # Change the classifier head\n", + " set_seeds()\n", " model.classifier = nn.Sequential(\n", " nn.Dropout(p=0.3),\n", " nn.Linear(in_features=1408, out_features=OUT_FEATURES)\n", " ).to(device)\n", + "\n", + " # Free the base model layers\n", " for param in model.features.parameters():\n", " param.requires_grad = False\n", + "\n", + " # Give the model a name\n", " model.name = \"effnetb2\"\n", " print(f\"[INFO] Creating new {model.name} model.\")\n", " return model" @@ -928,10 +934,13 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 45, "metadata": {}, "outputs": [], "source": [ + "# Create epochs list\n", + "num_epochs = [5, 10]\n", + "\n", "# Create models list (need to create a new model for each experiment)\n", "models = [\"effnetb0\", \"effnetb2\"]\n", "\n", @@ -942,7 +951,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 51, "metadata": {}, "outputs": [ { @@ -952,14 +961,15 @@ "[INFO] Creating new effnetb0 model.\n", "[INFO] Model: effnetb0\n", "[INFO] DataLoader: data_10_percent\n", + "[INFO] Training for 5 epochs\n", "----------\n", - "Created SummaryWriter and saving to runs/2022-05-31/data_10_percent/effnetb0...\n" + "Created SummaryWriter and saving to runs/2022-06-02/data_10_percent/effnetb0/_5_epochs...\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "8c5255b2f23b4ad8b3092036fcf02345", + "model_id": "8c07e32372d243ab9c8cbbab97efaa1d", "version_major": 2, "version_minor": 0 }, @@ -974,24 +984,27 @@ "name": "stdout", "output_type": "stream", "text": [ - "Epoch: 1 | train_loss: 1.0302 | train_acc: 0.4375 | test_loss: 0.8725 | test_acc: 0.6629\n", - "Epoch: 2 | train_loss: 0.9131 | train_acc: 0.6484 | test_loss: 0.6730 | test_acc: 0.9062\n", - "Epoch: 3 | train_loss: 0.7303 | train_acc: 0.8516 | test_loss: 0.6539 | test_acc: 0.8655\n", - "Epoch: 4 | train_loss: 0.6947 | train_acc: 0.7578 | test_loss: 0.6534 | test_acc: 0.8456\n", - "Epoch: 5 | train_loss: 0.6930 | train_acc: 0.7695 | test_loss: 0.6130 | test_acc: 0.8665\n", + "Epoch: 1 | train_loss: 1.0520 | train_acc: 0.4961 | test_loss: 0.9212 | test_acc: 0.4782\n", + "Epoch: 2 | train_loss: 0.8743 | train_acc: 0.6875 | test_loss: 0.8136 | test_acc: 0.6203\n", + "Epoch: 3 | train_loss: 0.8097 | train_acc: 0.6523 | test_loss: 0.7196 | test_acc: 0.8258\n", + "Epoch: 4 | train_loss: 0.7096 | train_acc: 0.7578 | test_loss: 0.5920 | test_acc: 0.8864\n", + "Epoch: 5 | train_loss: 0.5999 | train_acc: 0.9062 | test_loss: 0.5689 | test_acc: 0.8864\n", + "[INFO] Saving model to 07_effnetb0_data_10_percent_5_epochs.pth\n", + "[INFO] Saving model to: models/07_effnetb0_data_10_percent_5_epochs.pth\n", "----------\n", "\n", "[INFO] Creating new effnetb2 model.\n", "[INFO] Model: effnetb2\n", "[INFO] DataLoader: data_10_percent\n", + "[INFO] Training for 5 epochs\n", "----------\n", - "Created SummaryWriter and saving to runs/2022-05-31/data_10_percent/effnetb2...\n" + "Created SummaryWriter and saving to runs/2022-06-02/data_10_percent/effnetb2/_5_epochs...\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "f65eb3df1cb040589682aa54750f3d07", + "model_id": "da29eefda13f4105b3cc2fde3e1e1e56", "version_major": 2, "version_minor": 0 }, @@ -1006,24 +1019,107 @@ "name": "stdout", "output_type": "stream", "text": [ - "Epoch: 1 | train_loss: 0.9841 | train_acc: 0.5391 | test_loss: 0.9598 | test_acc: 0.6610\n", - "Epoch: 2 | train_loss: 0.8143 | train_acc: 0.7109 | test_loss: 0.8642 | test_acc: 0.7330\n", - "Epoch: 3 | train_loss: 0.7728 | train_acc: 0.6719 | test_loss: 0.8046 | test_acc: 0.8163\n", - "Epoch: 4 | train_loss: 0.6622 | train_acc: 0.7773 | test_loss: 0.6918 | test_acc: 0.8665\n", - "Epoch: 5 | train_loss: 0.5634 | train_acc: 0.9258 | test_loss: 0.6887 | test_acc: 0.8570\n", + "Epoch: 1 | train_loss: 1.0914 | train_acc: 0.3867 | test_loss: 0.9558 | test_acc: 0.6610\n", + "Epoch: 2 | train_loss: 0.9239 | train_acc: 0.6445 | test_loss: 0.8713 | test_acc: 0.8144\n", + "Epoch: 3 | train_loss: 0.8080 | train_acc: 0.7578 | test_loss: 0.7511 | test_acc: 0.9072\n", + "Epoch: 4 | train_loss: 0.7182 | train_acc: 0.8867 | test_loss: 0.7161 | test_acc: 0.9081\n", + "Epoch: 5 | train_loss: 0.6847 | train_acc: 0.7656 | test_loss: 0.7087 | test_acc: 0.8873\n", + "[INFO] Saving model to 07_effnetb2_data_10_percent_5_epochs.pth\n", + "[INFO] Saving model to: models/07_effnetb2_data_10_percent_5_epochs.pth\n", + "----------\n", + "\n", + "[INFO] Creating new effnetb0 model.\n", + "[INFO] Model: effnetb0\n", + "[INFO] DataLoader: data_10_percent\n", + "[INFO] Training for 10 epochs\n", + "----------\n", + "Created SummaryWriter and saving to runs/2022-06-02/data_10_percent/effnetb0/_10_epochs...\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "242b96e6f5e1436c97f8454d4fc49b66", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + " 0%| | 0/10 [00:00\n", + " \n", "