{
"cells": [
{
"cell_type": "markdown",
"id": "3f64620a-5c2d-4f93-8c92-a54dbdd672a0",
"metadata": {},
"source": [
"# Quick start with micro-sam\n",
"\n",
"This notebook shows the very basics necessary to segment an image using [micro-sam](https://github.com/computational-cell-analytics/micro-sam). \n",
"\n",
"## Installation\n",
"\n",
"You can install micro-sam in a conda environment like this. If you never worked with conda-environments before, consider reading [this blog post](https://biapol.github.io/blog/mara_lampert/getting_started_with_miniforge_and_python/readme.html) first.\n",
"```\n",
"mamba install -y -q -c conda-forge micro_sam\n",
"```\n",
"\n",
"For result visualization we use [stackview]() which can be installed using pip.\n",
"```\n",
"pip install stackview\n",
"```\n",
"\n",
"First we import the required libraries."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "a00fdeac-2f97-4da5-8c32-46b51eb3d684",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from micro_sam.automatic_segmentation import get_predictor_and_segmenter, automatic_instance_segmentation\n",
"from skimage.data import cells3d\n",
"import stackview"
]
},
{
"cell_type": "markdown",
"id": "246e025f-e366-4795-9046-8aa0bfd91a25",
"metadata": {},
"source": [
"We load an example 2D image from the [scikit-image](https://scikit-image.org/docs/stable/api/skimage.data.html) library."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a9bb2ea5-d6dd-4329-a9bc-45d21a3e8635",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"\n",
" \n",
" | \n",
"\n",
"\n",
"\n",
"shape | (256, 256) | \n",
"dtype | uint16 | \n",
"size | 128.0 kB | \n",
"min | 277 | max | 44092 | \n",
" \n",
" \n",
" | \n",
"
\n",
"
"
],
"text/plain": [
"StackViewNDArray([[4496, 5212, 6863, ..., 2917, 2680, 2642],\n",
" [4533, 5146, 7555, ..., 2843, 2857, 2748],\n",
" [4640, 6082, 8452, ..., 3372, 3039, 3128],\n",
" ...,\n",
" [1339, 1403, 1359, ..., 4458, 4314, 4795],\n",
" [1473, 1560, 1622, ..., 3967, 4531, 4204],\n",
" [1380, 1368, 1649, ..., 3091, 3558, 3682]], dtype=uint16)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"image = cells3d()[30,0]\n",
"\n",
"stackview.insight(image)"
]
},
{
"cell_type": "markdown",
"id": "2b915aa3-ca50-4339-bf6a-866d35a87f2c",
"metadata": {},
"source": [
"Loading a pre-trained micro-sam model and applying it to an image just takes two lines of python code:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6542f7b7-d66b-46f7-93c8-e05d415cbada",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Compute Image Embeddings 2D: 100%|███████████████████████████████████████████████████████| 1/1 [00:00<00:00, 1.50it/s]\n",
"Initialize instance segmentation with decoder: 100%|█████████████████████████████████████| 1/1 [00:00<00:00, 5.14it/s]\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
" \n",
" | \n",
"\n",
"\n",
"\n",
"shape | (256, 256) | \n",
"dtype | int32 | \n",
"size | 256.0 kB | \n",
"min | 1 | max | 39 | \n",
" \n",
"\n",
" | \n",
"
\n",
"
"
],
"text/plain": [
"StackViewNDArray([[ 6, 6, 6, ..., 5, 5, 5],\n",
" [ 6, 6, 6, ..., 5, 5, 5],\n",
" [ 6, 6, 6, ..., 5, 5, 5],\n",
" ...,\n",
" [39, 39, 39, ..., 37, 37, 37],\n",
" [39, 39, 39, ..., 37, 37, 37],\n",
" [39, 39, 39, ..., 37, 37, 37]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Load model\n",
"predictor, segmenter = get_predictor_and_segmenter(model_type=\"vit_b_lm\")\n",
"\n",
"# Apply model\n",
"label_image = automatic_instance_segmentation(predictor=predictor, segmenter=segmenter, input_path=image)\n",
"\n",
"# Visualize result\n",
"stackview.insight(label_image)"
]
},
{
"cell_type": "markdown",
"id": "de7e1b9d-2231-4dd5-8eb5-7223c0f9dcb3",
"metadata": {
"tags": []
},
"source": [
"We can also quickly show the result using an animated curtain."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "22f0ad18-08fa-4644-bc18-849ac5ab4e2c",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/html": [
"
"
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"stackview.animate_curtain(image, label_image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b38378b3-d86f-4442-9bd0-f1c425ef8fbf",
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}