diff --git "a/notebooks/trained_model_prediction.ipynb" "b/notebooks/trained_model_prediction.ipynb" new file mode 100644--- /dev/null +++ "b/notebooks/trained_model_prediction.ipynb" @@ -0,0 +1,859 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "6386b06e", + "metadata": {}, + "source": [ + "## Prediction using trained model" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "id": "91ca82a8-e04c-4e5a-a953-030b45aedfaf", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "from cellpose import models\n", + "from skimage import io as skio\n", + "import numpy as np\n", + "from tqdm import tqdm # progress bar (optional)\n", + "import re\n", + "\n", + "from pathlib import Path\n", + "\n", + "image_ext = \".png\"\n", + "\n", + "flow_threshold = 0.9\n", + "cellprob_threshold = -6\n", + "min_size = 1" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "id": "e05ddb32", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Segmenting images: 0%| | 0/64 [00:00" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "\n", + "# Create a binary mask (optional: if combined_mask contains labels like 1,2,3...)\n", + "binary_mask = (combined_mask > 0).astype(np.uint8)\n", + "\n", + "plt.figure(figsize=(10, 10))\n", + "plt.imshow(binary_mask, cmap='gray') # all non-zero values will be gray\n", + "plt.title('Combined Mask - Single Color')\n", + "plt.axis('off')\n", + "\n", + "# Save the figure as PNG\n", + "plt.savefig('combined_mask_grey_testing_model.png', bbox_inches='tight', dpi=300)\n", + "\n", + "# Show the plot\n", + "plt.show()\n" + ] + }, + { + "cell_type": "markdown", + "id": "320f62be", + "metadata": {}, + "source": [ + "### validate original and stiched mask size" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5870d0b5", + "metadata": {}, + "outputs": [], + "source": [ + "from PIL import Image\n", + "import numpy as np\n", + "\n", + "image = Image.open(\"jayden_img.ome.png\").convert(\"RGB\")\n", + "image_np = np.array(image)\n", + "mask = np.load(\"combined_full_mask_testing_model.npy\")" + ] + }, + { + "cell_type": "markdown", + "id": "2502978f", + "metadata": {}, + "source": [ + "## overlay mask and the oroginal image" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "49e0938d", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "from PIL import Image\n", + "from skimage.segmentation import find_boundaries\n", + "\n", + "# Define colors\n", + "overlay_color = np.array([238, 144, 144]) # Light green\n", + "boundary_color = np.array([100, 100, 255]) # Navy blue\n", + "alpha = 0.5 # Transparency for overlay\n", + "\n", + "# Ensure mask is binary\n", + "mask = (mask > 0).astype(np.uint8)\n", + "\n", + "# Create a copy for overlay\n", + "overlay = image_np.copy()\n", + "\n", + "# Apply overlay color where mask is 1\n", + "overlay[mask == 1] = ((1 - alpha) * image_np[mask == 1] + alpha * overlay_color).astype(np.uint8)\n", + "\n", + "# --- Add navy blue boundaries ---\n", + "from skimage.segmentation import find_boundaries\n", + "\n", + "# Find boundaries in the mask\n", + "boundaries = find_boundaries(mask, mode='outer')\n", + "\n", + "# Draw boundary color\n", + "overlay[boundaries] = boundary_color\n", + "\n", + "# Show plot\n", + "plt.figure(figsize=(10, 10))\n", + "plt.imshow(overlay)\n", + "plt.axis(\"off\")\n", + "plt.title(\"Image with Mask Overlay and Navy Blue Boundary\")\n", + "plt.show()\n", + "\n", + "# Save the image\n", + "output = Image.fromarray(overlay)\n", + "output.save(\"0_image_with_mask_overlay_with_white_boundary_model.png\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ce4a45e3-d008-45ba-91e6-21c247d0b54b", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3d24b30f-258e-4def-9c92-2028764243d4", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.17" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}