Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 183 additions & 0 deletions src/tapenade/notebooks/segmentation_notebook_cellpose-sam.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "c163bc9a",
"metadata": {},
"source": [
"# Segmentation Notebook using CellPose-SAM"
]
},
{
"cell_type": "markdown",
"id": "c0ef5bb6",
"metadata": {},
"source": [
"The algorithm CellPose-SAM is described in the paper:\n",
"\n",
"Cellpose-SAM: superhuman generalization for cellular segmentation\n",
"\n",
"Marius Pachitariu, Michael Rariden, Carsen Stringer\n",
"\n",
"bioRxiv 2025.04.28.651001; doi: https://doi.org/10.1101/2025.04.28.651001"
]
},
{
"cell_type": "markdown",
"id": "c127cbbd",
"metadata": {},
"source": [
"### <font color='red'> After clicking on a code cell, press \"Shift+Enter\" to run the code, or click on the \"Run\" button in the toolbar above.<br>\n",
"\n",
"### Replace \"...\" signs with the appropriate path to your data.\n",
"</font>"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cf42aedc",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from cellpose import models, core, io, plot\n",
"from tapenade import get_path_to_demo_folder\n",
"import napari\n",
"import tifffile\n",
"io.logger_setup() # run this to get printing of progress\n",
"if core.use_gpu()==False:\n",
" raise ImportError(\"No GPU access, change your runtime\")"
]
},
{
"cell_type": "markdown",
"id": "badcf579",
"metadata": {},
"source": [
"Enter the path to the image you want to segment, or use the demo data."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d4a2b57f",
"metadata": {},
"outputs": [],
"source": [
"path_to_data = get_path_to_demo_folder()\n",
"data_normalized = tifffile.imread(path_to_data / \"image_isotropized_enhanced.tif\")"
]
},
{
"cell_type": "markdown",
"id": "83dbd61f",
"metadata": {},
"source": [
"## Quick note about pre-processing\n",
"\n",
"To prepare your data, we recommend normalizing the image values using `local_contrast_enhancement`, in the case where your image is very deep, as it can improve performance in deeper planes by enhancing the contrast using local statistics. To do so, use the processing notebook."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "39918b77",
"metadata": {},
"outputs": [],
"source": [
"# the demo data is already processed\n",
"\n",
"# data_normalized = local_contrast_enhancement(\n",
"# data,\n",
"# mask=mask,\n",
"# box_size=10,\n",
"# perc_low=1, perc_high=99\n",
"# )\n"
]
},
{
"cell_type": "markdown",
"id": "d032d50c",
"metadata": {},
"source": [
"## Run the prediction using CellPose-SAM"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "548be45c",
"metadata": {},
"outputs": [],
"source": [
"model = models.CellposeModel(gpu=True)\n",
"\n",
"labels, flows, _ = model.eval(data_normalized, z_axis=0, channel_axis=None,\n",
" batch_size=32,\n",
" do_3D=True, flow3D_smooth=1)\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "3e7ab04f",
"metadata": {},
"source": [
"## Check results (napari required)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9d168135",
"metadata": {},
"outputs": [],
"source": [
"viewer=napari.Viewer()\n",
"viewer.add_image(data_normalized, name='image')\n",
"viewer.add_labels(labels, name='labels')\n",
"napari.run()"
]
},
{
"cell_type": "markdown",
"id": "e4eb3303",
"metadata": {},
"source": [
"## Save results"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "231ec9e2",
"metadata": {},
"outputs": [],
"source": [
"tifffile.imwrite(..., labels) # replace ... with the path where you want to save the labels as a tif file"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "cellpose",
"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.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "napari-mine",
"display_name": "env-tapenade",
"language": "python",
"name": "python3"
},
Expand All @@ -319,7 +319,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.16"
"version": "3.10.14"
}
},
"nbformat": 4,
Expand Down
Loading