From b6dfc93c085c59a3a0539d43e79de995963ec2d0 Mon Sep 17 00:00:00 2001 From: Ben-Salmon Date: Fri, 23 Aug 2024 00:37:59 +0000 Subject: [PATCH] Commit from GitHub Actions (Build Notebooks) --- 01_CARE/exercise.ipynb | 59 +++++++++++++++++++++++++++--------- 02_Noise2Void/exercise.ipynb | 15 +++++++-- 03_COSDD/exercise.ipynb | 29 ++---------------- 04_DenoiSplit/exercise.ipynb | 2 +- 4 files changed, 62 insertions(+), 43 deletions(-) diff --git a/01_CARE/exercise.ipynb b/01_CARE/exercise.ipynb index a16f38b..5f5d67c 100644 --- a/01_CARE/exercise.ipynb +++ b/01_CARE/exercise.ipynb @@ -55,9 +55,6 @@ "metadata": {}, "outputs": [], "source": [ - "%load_ext tensorboard\n", - "\n", - "\n", "import tifffile\n", "import numpy as np\n", "from pathlib import Path\n", @@ -482,7 +479,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -736,12 +732,37 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Task 3: Tensorboard

\n", + "\n", + "We'll monitor the training of all models in 05_image_restoration using Tensorboard. \n", + "This is a program that plots the training and validation loss of networks as they train, and can also show input/output image pairs.\n", + "Follow these steps to launch Tensorboard.\n", + "\n", + "1) Open the extensions panel in VS Code. Look for this icon. \n", + "\n", + "![image](nb_data/extensions.png)\n", + "\n", + "2) Search Tensorboard and install and install the extension published by Microsoft.\n", + "3) Open the command palette (ctrl+shift+p), search for Python: Launch Tensorboard and hit enter.\n", + "4) When prompted, select either \"Use current working directory\" or \"Select another folder\" and enter the path to the `01_CARE/runs/` directory.\n", + "\n", + "Once it's open, continue to the next cell to start training, then open the Tensorboard tab to see the loss curves.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "%tensorboard --logdir runs" + "In tensorboard, click the SCALARS tab to see the training and validation loss curves. \n", + "At the end of each epoch, refresh Tensorboard using the button in the top right to see the latest loss.\n", + "\n", + "Click the IMAGES tab to see the noisy inputs, denoised outputs and clean targets.\n", + "These are updated at the end of each epoch too." ] }, { @@ -757,6 +778,13 @@ "\n", "# tensorboard\n", "tb_logger = SummaryWriter(\"runs/Unet\"+datetime.now().strftime('%d%H-%M%S'))\n", + "def log_image(image, tag, logger, step):\n", + " normalised_image = image.cpu().numpy()\n", + " normalised_image = normalised_image - np.percentile(normalised_image, 1)\n", + " normalised_image = normalised_image / np.percentile(normalised_image, 99)\n", + " normalised_image = np.clip(normalised_image, 0, 1)\n", + " logger.add_images(tag=tag, img_tensor=normalised_image, global_step=step)\n", + "\n", "\n", "train_losses = []\n", "val_losses = []\n", @@ -793,11 +821,9 @@ " tb_logger.add_scalar(tag=\"val_loss\", scalar_value=val_loss, global_step=step)\n", "\n", " # we always log the last validation images\n", - " tb_logger.add_images(tag=\"val_input\", img_tensor=batch.to(\"cpu\"), global_step=step)\n", - " tb_logger.add_images(tag=\"val_target\", img_tensor=target.to(\"cpu\"), global_step=step)\n", - " tb_logger.add_images(\n", - " tag=\"val_prediction\", img_tensor=output.to(\"cpu\"), global_step=step\n", - " )\n", + " log_image(batch, tag=\"val_input\", logger=tb_logger, step=step)\n", + " log_image(target, tag=\"val_target\", logger=tb_logger, step=step)\n", + " log_image(output, tag=\"val_prediction\", logger=tb_logger, step=step)\n", "\n", " print(f\"Validation loss: {val_loss.item()}\")\n", "\n", @@ -888,7 +914,7 @@ "tags": [] }, "source": [ - "

Task 3: Predict using the correct mean/std

\n", + "

Task 4: Predict using the correct mean/std

\n", "\n", "In Part 1 we normalized the inputs and the targets before feeding them into the model. This means that the model will output normalized clean images, but we'd like them to be on the same scale as the real clean images.\n", "\n", @@ -1001,6 +1027,11 @@ "This notebook has shown how matched pairs of noisy and clean images can train a UNet to denoise, but what if we don't have any clean images? In the next notebook, we'll try Noise2Void, a method for training a UNet to denoise with only noisy images.\n", "
" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] } ], "metadata": { diff --git a/02_Noise2Void/exercise.ipynb b/02_Noise2Void/exercise.ipynb index b4ad63a..990a397 100644 --- a/02_Noise2Void/exercise.ipynb +++ b/02_Noise2Void/exercise.ipynb @@ -364,8 +364,8 @@ " patch_size=[64, 64],\n", " batch_size=128,\n", " num_epochs=10,\n", - " roi_size=3,\n", - " masked_pixel_percentage=0.05,\n", + " roi_size=11,\n", + " masked_pixel_percentage=0.2,\n", " logger=\"tensorboard\"\n", ")" ] @@ -428,6 +428,8 @@ "\n", "Remember the configuration? Didn't we set `logger` to `tensorboard`? Then we can visualize the loss curve!\n", "\n", + "Open Tensorboard in VS Code (check Task 3 in 01_CARE) to monitor training. \n", + "Logs for this model are stored in the `02_N2V/logs/` folder.\n", "
\n", "\n", "

Question: N2V loss curve

\n", @@ -629,6 +631,15 @@ "ax[1].imshow(new_preds.squeeze(), cmap=\"gray\")" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "train_image[:128, :128].shape" + ] + }, { "attachments": {}, "cell_type": "markdown", diff --git a/03_COSDD/exercise.ipynb b/03_COSDD/exercise.ipynb index b1dc8d5..b8ef461 100644 --- a/03_COSDD/exercise.ipynb +++ b/03_COSDD/exercise.ipynb @@ -480,14 +480,9 @@ "\n", "### Task 1.4.\n", "\n", - "We're going to use Tensorboard to monitor training metrics. Run the two cells below to open tensorboard in the notebook. If it doesn't work, follow the steps below to open it in a browser.\n", - "\n", - "1. Open a terminal.\n", - "2. Enter `conda activate 05_image_restoration` to activate an environment with Tensorboard installed.\n", - "3. Enter `tensorboard --logdir 05_image_restoration/03_COSDD/checkpoints`\n", - "4. Finally, open a browser and enter localhost:6006 in the address bar.\n", - "\n", - "Once you're in tensorboard, you'll see the training logs of your model and the logs of a model that's already been trained for 3.5 hours.\n", + "Open Tensorboard (check Task 3 in 01_CARE) to monitor training.\n", + "This model is unlike the previous two because it has more than one loss curve.\n", + "The cell below describes how to interpret each one.\n", "
" ] }, @@ -511,24 +506,6 @@ "Note that the trainer is set to train for only 10 minutes in this example. Remove the line with `max_time` to train fully." ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "%load_ext tensorboard" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "%tensorboard --logdir checkpoints/" - ] - }, { "cell_type": "markdown", "metadata": {}, diff --git a/04_DenoiSplit/exercise.ipynb b/04_DenoiSplit/exercise.ipynb index a62de5a..e46c60d 100644 --- a/04_DenoiSplit/exercise.ipynb +++ b/04_DenoiSplit/exercise.ipynb @@ -796,7 +796,7 @@ }, { "cell_type": "markdown", - "id": "dbecd052", + "id": "52159fdb", "metadata": {}, "source": [ "

End of the exercise

\n",