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": [
+ "
"
+ ]
+ },
{
"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, ?it/s]"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "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",
+ "Epoch: 6 | train_loss: 0.5619 | train_acc: 0.8945 | test_loss: 0.5964 | test_acc: 0.8864\n",
+ "Epoch: 7 | train_loss: 0.5578 | train_acc: 0.7891 | test_loss: 0.5590 | test_acc: 0.8864\n",
+ "Epoch: 8 | train_loss: 0.4700 | train_acc: 0.9492 | test_loss: 0.5199 | test_acc: 0.8759\n",
+ "Epoch: 9 | train_loss: 0.5729 | train_acc: 0.7695 | test_loss: 0.5120 | test_acc: 0.8873\n",
+ "Epoch: 10 | train_loss: 0.4782 | train_acc: 0.8242 | test_loss: 0.4650 | test_acc: 0.9072\n",
+ "[INFO] Saving model to 07_effnetb0_data_10_percent_10_epochs.pth\n",
+ "[INFO] Saving model to: models/07_effnetb0_data_10_percent_10_epochs.pth\n",
+ "----------\n",
+ "\n",
+ "[INFO] Creating new effnetb2 model.\n",
+ "[INFO] Model: effnetb2\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/effnetb2/_10_epochs...\n"
+ ]
+ },
+ {
+ "data": {
+ "application/vnd.jupyter.widget-view+json": {
+ "model_id": "5c30dac86f8b40f7bef3c83bcc581453",
+ "version_major": 2,
+ "version_minor": 0
+ },
+ "text/plain": [
+ " 0%| | 0/10 [00:00, ?it/s]"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "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",
+ "Epoch: 6 | train_loss: 0.6115 | train_acc: 0.7891 | test_loss: 0.6332 | test_acc: 0.9280\n",
+ "Epoch: 7 | train_loss: 0.6124 | train_acc: 0.8008 | test_loss: 0.6401 | test_acc: 0.8769\n",
+ "Epoch: 8 | train_loss: 0.5196 | train_acc: 0.9336 | test_loss: 0.6201 | test_acc: 0.8977\n",
+ "Epoch: 9 | train_loss: 0.5421 | train_acc: 0.8086 | test_loss: 0.6231 | test_acc: 0.8769\n",
+ "Epoch: 10 | train_loss: 0.4919 | train_acc: 0.8086 | test_loss: 0.5879 | test_acc: 0.8873\n",
+ "[INFO] Saving model to 07_effnetb2_data_10_percent_10_epochs.pth\n",
+ "[INFO] Saving model to: models/07_effnetb2_data_10_percent_10_epochs.pth\n",
"----------\n",
"\n",
"[INFO] Creating new effnetb0 model.\n",
"[INFO] Model: effnetb0\n",
"[INFO] DataLoader: data_20_percent\n",
+ "[INFO] Training for 5 epochs\n",
"----------\n",
- "Created SummaryWriter and saving to runs/2022-05-31/data_20_percent/effnetb0...\n"
+ "Created SummaryWriter and saving to runs/2022-06-02/data_20_percent/effnetb0/_5_epochs...\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
- "model_id": "d78f5e5e26b5414eb92eb2f8e02419bc",
+ "model_id": "beb5a98ddda24d2cbd524541791e27b4",
"version_major": 2,
"version_minor": 0
},
@@ -1038,24 +1134,27 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "Epoch: 1 | train_loss: 0.9477 | train_acc: 0.5938 | test_loss: 0.7064 | test_acc: 0.8864\n",
- "Epoch: 2 | train_loss: 0.7019 | train_acc: 0.8208 | test_loss: 0.5432 | test_acc: 0.8968\n",
- "Epoch: 3 | train_loss: 0.5844 | train_acc: 0.8521 | test_loss: 0.4946 | test_acc: 0.8873\n",
- "Epoch: 4 | train_loss: 0.4977 | train_acc: 0.8479 | test_loss: 0.3916 | test_acc: 0.9176\n",
- "Epoch: 5 | train_loss: 0.4637 | train_acc: 0.8292 | test_loss: 0.3895 | test_acc: 0.9384\n",
+ "Epoch: 1 | train_loss: 0.9588 | train_acc: 0.6125 | test_loss: 0.6551 | test_acc: 0.8655\n",
+ "Epoch: 2 | train_loss: 0.6894 | train_acc: 0.8521 | test_loss: 0.5800 | test_acc: 0.8873\n",
+ "Epoch: 3 | train_loss: 0.5805 | train_acc: 0.8604 | test_loss: 0.4576 | test_acc: 0.9176\n",
+ "Epoch: 4 | train_loss: 0.4937 | train_acc: 0.8646 | test_loss: 0.4454 | test_acc: 0.9176\n",
+ "Epoch: 5 | train_loss: 0.4886 | train_acc: 0.8500 | test_loss: 0.3913 | test_acc: 0.9176\n",
+ "[INFO] Saving model to 07_effnetb0_data_20_percent_5_epochs.pth\n",
+ "[INFO] Saving model to: models/07_effnetb0_data_20_percent_5_epochs.pth\n",
"----------\n",
"\n",
"[INFO] Creating new effnetb2 model.\n",
"[INFO] Model: effnetb2\n",
"[INFO] DataLoader: data_20_percent\n",
+ "[INFO] Training for 5 epochs\n",
"----------\n",
- "Created SummaryWriter and saving to runs/2022-05-31/data_20_percent/effnetb2...\n"
+ "Created SummaryWriter and saving to runs/2022-06-02/data_20_percent/effnetb2/_5_epochs...\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
- "model_id": "2ca5695d756b45e4828d030c07f52a96",
+ "model_id": "be92607064144c84a8e45607cd1dcff9",
"version_major": 2,
"version_minor": 0
},
@@ -1070,52 +1169,152 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "Epoch: 1 | train_loss: 0.9870 | train_acc: 0.4896 | test_loss: 0.8395 | test_acc: 0.8466\n",
- "Epoch: 2 | train_loss: 0.7037 | train_acc: 0.8396 | test_loss: 0.6912 | test_acc: 0.8674\n",
- "Epoch: 3 | train_loss: 0.5997 | train_acc: 0.8562 | test_loss: 0.5892 | test_acc: 0.9186\n",
- "Epoch: 4 | train_loss: 0.4890 | train_acc: 0.8875 | test_loss: 0.5617 | test_acc: 0.8873\n",
- "Epoch: 5 | train_loss: 0.4489 | train_acc: 0.9125 | test_loss: 0.4787 | test_acc: 0.8977\n",
+ "Epoch: 1 | train_loss: 0.9819 | train_acc: 0.5604 | test_loss: 0.7775 | test_acc: 0.8049\n",
+ "Epoch: 2 | train_loss: 0.7298 | train_acc: 0.8021 | test_loss: 0.6665 | test_acc: 0.8873\n",
+ "Epoch: 3 | train_loss: 0.6011 | train_acc: 0.8458 | test_loss: 0.5633 | test_acc: 0.9280\n",
+ "Epoch: 4 | train_loss: 0.5429 | train_acc: 0.8354 | test_loss: 0.5685 | test_acc: 0.8977\n",
+ "Epoch: 5 | train_loss: 0.4410 | train_acc: 0.8708 | test_loss: 0.4470 | test_acc: 0.9384\n",
+ "[INFO] Saving model to 07_effnetb2_data_20_percent_5_epochs.pth\n",
+ "[INFO] Saving model to: models/07_effnetb2_data_20_percent_5_epochs.pth\n",
"----------\n",
- "\n"
+ "\n",
+ "[INFO] Creating new effnetb0 model.\n",
+ "[INFO] Model: effnetb0\n",
+ "[INFO] DataLoader: data_20_percent\n",
+ "[INFO] Training for 10 epochs\n",
+ "----------\n",
+ "Created SummaryWriter and saving to runs/2022-06-02/data_20_percent/effnetb0/_10_epochs...\n"
+ ]
+ },
+ {
+ "data": {
+ "application/vnd.jupyter.widget-view+json": {
+ "model_id": "7a5c4e7a584e4664901c7edb94a40262",
+ "version_major": 2,
+ "version_minor": 0
+ },
+ "text/plain": [
+ " 0%| | 0/10 [00:00, ?it/s]"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Epoch: 1 | train_loss: 0.9588 | train_acc: 0.6125 | test_loss: 0.6551 | test_acc: 0.8655\n",
+ "Epoch: 2 | train_loss: 0.6894 | train_acc: 0.8521 | test_loss: 0.5800 | test_acc: 0.8873\n",
+ "Epoch: 3 | train_loss: 0.5805 | train_acc: 0.8604 | test_loss: 0.4576 | test_acc: 0.9176\n",
+ "Epoch: 4 | train_loss: 0.4937 | train_acc: 0.8646 | test_loss: 0.4454 | test_acc: 0.9176\n",
+ "Epoch: 5 | train_loss: 0.4886 | train_acc: 0.8500 | test_loss: 0.3913 | test_acc: 0.9176\n",
+ "Epoch: 6 | train_loss: 0.3708 | train_acc: 0.8833 | test_loss: 0.3565 | test_acc: 0.9072\n",
+ "Epoch: 7 | train_loss: 0.3558 | train_acc: 0.9208 | test_loss: 0.3182 | test_acc: 0.9072\n",
+ "Epoch: 8 | train_loss: 0.3739 | train_acc: 0.8938 | test_loss: 0.3346 | test_acc: 0.8977\n",
+ "Epoch: 9 | train_loss: 0.2975 | train_acc: 0.9375 | test_loss: 0.3088 | test_acc: 0.9280\n",
+ "Epoch: 10 | train_loss: 0.3625 | train_acc: 0.8479 | test_loss: 0.2771 | test_acc: 0.9072\n",
+ "[INFO] Saving model to 07_effnetb0_data_20_percent_10_epochs.pth\n",
+ "[INFO] Saving model to: models/07_effnetb0_data_20_percent_10_epochs.pth\n",
+ "----------\n",
+ "\n",
+ "[INFO] Creating new effnetb2 model.\n",
+ "[INFO] Model: effnetb2\n",
+ "[INFO] DataLoader: data_20_percent\n",
+ "[INFO] Training for 10 epochs\n",
+ "----------\n",
+ "Created SummaryWriter and saving to runs/2022-06-02/data_20_percent/effnetb2/_10_epochs...\n"
+ ]
+ },
+ {
+ "data": {
+ "application/vnd.jupyter.widget-view+json": {
+ "model_id": "3e904514585b49339ff7c4434f44ac50",
+ "version_major": 2,
+ "version_minor": 0
+ },
+ "text/plain": [
+ " 0%| | 0/10 [00:00, ?it/s]"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Epoch: 1 | train_loss: 0.9819 | train_acc: 0.5604 | test_loss: 0.7775 | test_acc: 0.8049\n",
+ "Epoch: 2 | train_loss: 0.7298 | train_acc: 0.8021 | test_loss: 0.6665 | test_acc: 0.8873\n",
+ "Epoch: 3 | train_loss: 0.6011 | train_acc: 0.8458 | test_loss: 0.5633 | test_acc: 0.9280\n",
+ "Epoch: 4 | train_loss: 0.5429 | train_acc: 0.8354 | test_loss: 0.5685 | test_acc: 0.8977\n",
+ "Epoch: 5 | train_loss: 0.4410 | train_acc: 0.8708 | test_loss: 0.4470 | test_acc: 0.9384\n",
+ "Epoch: 6 | train_loss: 0.3876 | train_acc: 0.9125 | test_loss: 0.4564 | test_acc: 0.8977\n",
+ "Epoch: 7 | train_loss: 0.3481 | train_acc: 0.9271 | test_loss: 0.4227 | test_acc: 0.9384\n",
+ "Epoch: 8 | train_loss: 0.3861 | train_acc: 0.8792 | test_loss: 0.4350 | test_acc: 0.9280\n",
+ "Epoch: 9 | train_loss: 0.3315 | train_acc: 0.8979 | test_loss: 0.4254 | test_acc: 0.9081\n",
+ "Epoch: 10 | train_loss: 0.3382 | train_acc: 0.8979 | test_loss: 0.3909 | test_acc: 0.9384\n",
+ "[INFO] Saving model to 07_effnetb2_data_20_percent_10_epochs.pth\n",
+ "[INFO] Saving model to: models/07_effnetb2_data_20_percent_10_epochs.pth\n",
+ "----------\n",
+ "\n",
+ "CPU times: user 36 s, sys: 31.4 s, total: 1min 7s\n",
+ "Wall time: 1min 19s\n"
]
}
],
"source": [
+ "%%time\n",
+ "from going_modular.going_modular.utils import save_model\n",
+ "\n",
+ "# Set the random seeds\n",
"set_seeds()\n",
"\n",
- "# TK - make sure model is training from scratch each time, e.g. don't retrain the same model, a new one has to be created \n",
- "# TK - Loop through the dataloaders first to train a new model on each dataloader \n",
+ "# Loop through each DataLoader\n",
"for dataloader_name, train_dataloader in train_dataloaders.items():\n",
- " for model_name in models:\n",
- " # Select the model\n",
- " if model_name == \"effnetb0\":\n",
- " model = create_effnetb0()\n",
- " else:\n",
- " model = create_effnetb2()\n",
- " \n",
- " # Create information print outs\n",
- " print(f\"[INFO] Model: {model_name}\")\n",
- " print(f\"[INFO] DataLoader: {dataloader_name}\")\n",
- " print(f\"----------\") \n",
- " \n",
- " # Create a new loss and optimizer for every model\n",
- " loss_fn = nn.CrossEntropyLoss()\n",
- " optimizer = torch.optim.Adam(params=model.parameters(), lr=0.001)\n",
"\n",
- " # Train target model with target dataloaders and track experiments\n",
- " train(model=model,\n",
- " train_dataloader=train_dataloader,\n",
- " test_dataloader=test_dataloader, \n",
- " optimizer=optimizer,\n",
- " loss_fn=loss_fn,\n",
- " epochs=5,\n",
- " device=device,\n",
- " writer=create_writer(experiment_name=dataloader_name,\n",
- " model_name=model_name\n",
- " ))\n",
- " \n",
- " # TK - add functionality to save the best model / save all models and then get the best one?\n",
- " print(\"----------\\n\")"
+ " # Loop through each number of epochs\n",
+ " for epochs in num_epochs: \n",
+ "\n",
+ " # Loop through each model name and create a new model based on the name\n",
+ " for model_name in models:\n",
+ "\n",
+ " # Select the model\n",
+ " if model_name == \"effnetb0\":\n",
+ " model = create_effnetb0()\n",
+ " else:\n",
+ " model = create_effnetb2()\n",
+ " \n",
+ " # Create information print outs\n",
+ " print(f\"[INFO] Model: {model_name}\")\n",
+ " print(f\"[INFO] DataLoader: {dataloader_name}\")\n",
+ " print(f\"[INFO] Training for {epochs} epochs\")\n",
+ " print(f\"----------\") \n",
+ " \n",
+ " # Create a new loss and optimizer for every model\n",
+ " loss_fn = nn.CrossEntropyLoss()\n",
+ " optimizer = torch.optim.Adam(params=model.parameters(), lr=0.001)\n",
+ "\n",
+ " # Train target model with target dataloaders and track experiments\n",
+ " train(model=model,\n",
+ " train_dataloader=train_dataloader,\n",
+ " test_dataloader=test_dataloader, \n",
+ " optimizer=optimizer,\n",
+ " loss_fn=loss_fn,\n",
+ " epochs=epochs,\n",
+ " device=device,\n",
+ " writer=create_writer(experiment_name=dataloader_name,\n",
+ " model_name=model_name,\n",
+ " extra=f\"{epochs}_epochs\"\n",
+ " ))\n",
+ " \n",
+ " # Save the model to file so we can get back the best model\n",
+ " save_filepath = f\"07_{model_name}_{dataloader_name}_{epochs}_epochs.pth\"\n",
+ " print(f\"[INFO] Saving model to {save_filepath}\")\n",
+ " save_model(model=model,\n",
+ " target_dir=\"models\",\n",
+ " model_name=save_filepath)\n",
+ " print(\"----------\\n\")"
]
},
{
@@ -1134,19 +1333,36 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 49,
"metadata": {},
"outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The tensorboard extension is already loaded. To reload it, use:\n",
+ " %reload_ext tensorboard\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "Reusing TensorBoard on port 6006 (pid 1168517), started 2 days, 3:34:54 ago. (Use '!kill 1168517' to kill it.)"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ },
{
"data": {
"text/html": [
"\n",
- "