Skip to content
Merged
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
316 changes: 316 additions & 0 deletions notebooks/run_cellpose3.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,316 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "Nc9k-7j1-CUF"
},
"source": [
"<a href=\"https://colab.research.google.com/github/MouseLand/cellpose/blob/main/notebooks/run_cellpose3.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Install and run cellpose3 for denoising and segmentation\n",
"## ⚠️ **Warning:** this notebook will install cellpose3 which is not forwards compatible with cellpose4 (CPSAM). Be careful with your environments and the `pip` command below. ⚠️\n",
"\n",
"## The dedicated denoising components were removed from cellpose4 by training on noisy images, and cellpose4 only has a segmentation network. "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "U_WCmrG5-CUL"
},
"source": [
"# Running cellpose3 in colab with a GPU\n",
"\n",
"<font size = 4>Cellpose3 now allows you to restore and segment noisy/blurry/low res images!\n",
"\n",
"For more details on Cellpose3 check out the [paper](https://www.biorxiv.org/content/10.1101/2024.02.10.579780v1).\n",
"\n",
"Mount your google drive to access all your image files. This also ensures that the segmentations are saved to your google drive."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "HrakTaa9-CUQ"
},
"source": [
"## Installation\n",
"\n",
"Install cellpose -- by default the torch GPU version is installed in COLAB notebook."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "efSQoWFw-CUU",
"outputId": "472a7900-7821-4bc6-d3b3-00a463476721"
},
"outputs": [],
"source": [
"!pip install \"opencv-python-headless>=4.9.0.80\"\n",
"!pip install cellpose==3.1.1.2"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "j7uUatzC-CUY"
},
"source": [
"Check CUDA version and that GPU is working in cellpose and import other libraries."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "a8muq8KG-CUa",
"outputId": "75fabdc8-a976-476d-9f79-d9fc6213eccb"
},
"outputs": [],
"source": [
"!nvcc --version\n",
"!nvidia-smi\n",
"\n",
"import os, shutil\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from cellpose import core, utils, io, models, metrics\n",
"from glob import glob\n",
"\n",
"use_GPU = core.use_gpu()\n",
"yn = ['NO', 'YES']\n",
"print(f'>>> GPU activated? {yn[use_GPU]}')"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SzD7QlBP-CUd"
},
"source": [
"## Images\n",
"\n",
"Load in your own data or use ours (below)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 568
},
"id": "PYevQVQd-CUe",
"outputId": "895a5ed4-b2cc-482d-d741-32218eee76bc"
},
"outputs": [],
"source": [
"import numpy as np\n",
"import time, os, sys\n",
"from urllib.parse import urlparse\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib as mpl\n",
"%matplotlib inline\n",
"mpl.rcParams['figure.dpi'] = 200\n",
"from cellpose import utils, io\n",
"\n",
"# download noisy images from website\n",
"url = \"http://www.cellpose.org/static/data/test_poisson.npz\"\n",
"filename = \"test_poisson.npz\"\n",
"utils.download_url_to_file(url, filename)\n",
"dat = np.load(filename, allow_pickle=True)[\"arr_0\"].item()\n",
"\n",
"imgs = dat[\"test_noisy\"]\n",
"plt.figure(figsize=(8,3))\n",
"for i, iex in enumerate([2, 18, 20]):\n",
" img = imgs[iex].squeeze()\n",
" plt.subplot(1,3,1+i)\n",
" plt.imshow(img, cmap=\"gray\", vmin=0, vmax=1)\n",
" plt.axis('off')\n",
"plt.tight_layout()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "g1dO0Oia-CUk"
},
"source": [
"Mount your google drive here if you want to load your own images:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "1qyAEK7R-CUp"
},
"outputs": [],
"source": [
"\n",
"#@markdown ###Run this cell to connect your Google Drive to Colab\n",
"\n",
"#@markdown * Click on the URL.\n",
"\n",
"#@markdown * Sign in your Google Account.\n",
"\n",
"#@markdown * Copy the authorization code.\n",
"\n",
"#@markdown * Enter the authorization code.\n",
"\n",
"#@markdown * Click on \"Files\" site on the right. Refresh the site. Your Google Drive folder should now be available here as \"drive\".\n",
"\n",
"#mounts user's Google Drive to Google Colab.\n",
"\n",
"from google.colab import drive\n",
"drive.mount('/content/gdrive')\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "-KYaPm0H-CUs"
},
"source": [
"## run denoising and segmentation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "wm6YEVJN-CUu",
"outputId": "f9c222c8-013d-4cbe-ba07-aa0172f8532f"
},
"outputs": [],
"source": [
"# RUN CELLPOSE3\n",
"\n",
"from cellpose import denoise, io\n",
"\n",
"io.logger_setup() # run this to get printing of progress\n",
"\n",
"# DEFINE CELLPOSE MODEL\n",
"# model_type=\"cyto3\" or \"nuclei\", or other model\n",
"# restore_type: \"denoise_cyto3\", \"deblur_cyto3\", \"upsample_cyto3\", \"denoise_nuclei\", \"deblur_nuclei\", \"upsample_nuclei\"\n",
"model = denoise.CellposeDenoiseModel(gpu=True, model_type=\"cyto3\",\n",
" restore_type=\"denoise_cyto3\")\n",
"\n",
"# define CHANNELS to run segementation on\n",
"# grayscale=0, R=1, G=2, B=3\n",
"# channels = [cytoplasm, nucleus]\n",
"# if NUCLEUS channel does not exist, set the second channel to 0\n",
"# channels = [0,0]\n",
"# IF ALL YOUR IMAGES ARE THE SAME TYPE, you can give a list with 2 elements\n",
"# channels = [0,0] # IF YOU HAVE GRAYSCALE\n",
"# channels = [2,3] # IF YOU HAVE G=cytoplasm and B=nucleus\n",
"# channels = [2,1] # IF YOU HAVE G=cytoplasm and R=nucleus\n",
"# OR if you have different types of channels in each image\n",
"# channels = [[2,3], [0,0], [0,0]]\n",
"\n",
"# if you have a nuclear channel, you can use the nuclei restore model on the nuclear channel with\n",
"# model = denoise.CellposeDenoiseModel(..., chan2_restore=True)\n",
"\n",
"# NEED TO SPECIFY DIAMETER OF OBJECTS\n",
"# in this case we have them from the ground-truth masks\n",
"diams = dat[\"diam_test\"]\n",
"\n",
"masks, flows, styles, imgs_dn = model.eval(imgs, diameter=diams, channels=[0,0])\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tH33nBAE-CUy"
},
"source": [
"plot results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"id": "8bAJc0qt-CU0",
"outputId": "906b3476-c272-4cd8-a9cb-a1f46eacce5c"
},
"outputs": [],
"source": [
"plt.figure(figsize=(8,12))\n",
"for i, iex in enumerate([2, 18, 20]):\n",
" img = imgs[iex].squeeze()\n",
" plt.subplot(3,3,1+i)\n",
" plt.imshow(img, cmap=\"gray\", vmin=0, vmax=1)\n",
" plt.axis('off')\n",
" plt.title(\"noisy\")\n",
"\n",
" img_dn = imgs_dn[iex].squeeze()\n",
" plt.subplot(3,3,4+i)\n",
" plt.imshow(img_dn, cmap=\"gray\", vmin=0, vmax=1)\n",
" plt.axis('off')\n",
" plt.title(\"denoised\")\n",
"\n",
" plt.subplot(3,3,7+i)\n",
" plt.imshow(img_dn, cmap=\"gray\", vmin=0, vmax=1)\n",
" outlines = utils.outlines_list(masks[iex])\n",
" for o in outlines:\n",
" plt.plot(o[:,0], o[:,1], color=[1,1,0])\n",
" plt.axis('off')\n",
" plt.title(\"segmentation\")\n",
"\n",
"plt.tight_layout()\n",
"plt.show()"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"gpuType": "T4",
"provenance": []
},
"kernelspec": {
"display_name": "cp4",
"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.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}