Commit 3c1affe1 authored by walker's avatar walker
Browse files

started notebook for own data

parent 666576ba
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "Q7c7V4yEqDc_"
},
"source": [
"## Run cellpose on own data\n",
"* This is a modification of the 2 demo notebooks to run cellpose on my own data.\n",
"* Can run on both CPU and GPU:\n",
" * For CPU activate *cellposecpu* environment\n",
" * For GPU activate *cellpose* environment\n",
"* Notebooks was written and tested on/for CBG cluster.\n",
"\n",
"## TODO\n",
"* 3D better integration\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "h_iAN7cAthma"
},
"source": [
"### Import libraries and check GPU\n",
"First line checks the Cuda version. It should be 9.0.176 or report 'not found' (if running on CPU)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!nvcc --version"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "hG3LSmJmLylT",
"outputId": "1fa3d4e7-5c03-4699-ac33-879a319343e5"
},
"outputs": [],
"source": [
"import numpy as np\n",
"import time, os, sys\n",
"import skimage.io\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib as mpl\n",
"import glob\n",
"%matplotlib inline\n",
"mpl.rcParams['figure.dpi'] = 300\n",
"\n",
"import mxnet as mx\n",
"from cellpose import utils, models, io, plot\n",
"\n",
"use_GPU = models.use_gpu()\n",
"print('GPU activated? %d'%use_GPU)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "d1Ta76yatmjH"
},
"source": [
"### Load images"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"imgfile_pattern=\"../data_examples/*png\" # imgnames, with wildcard *\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"imgfiles=sorted(glob.glob(imgfile_pattern))\n",
"imgs = [skimage.io.imread(f) for f in imgfiles]\n",
"numimgs = len(imgs)\n",
"print(\"Number of images: \",numimgs)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 631
},
"colab_type": "code",
"id": "ldNwr_zxMVha",
"outputId": "af1b046a-c0d4-484c-dc0e-b700b4d7a441"
},
"outputs": [],
"source": [
"# plot 2 random example images\n",
"idxs=np.random.randint(0,numimgs,(2))\n",
"plt.figure(figsize=(8,4))\n",
"for i in range(2):\n",
" plt.subplot(1,2,i+1)\n",
" plt.imshow(imgs[idxs[i]])\n",
" plt.axis(\"equal\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "3JRxBPmatrK7"
},
"source": [
"### run cellpose"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# model: \n",
"# \"cyto\" or \"nuclei\"\n",
"model_type=\"cyto\" \n",
"\n",
"# average 4 nets\n",
"net_avg=True # default=True\n",
"\n",
"# cell diameter: \n",
"# None or size in px. \n",
"# If None: calculated per image. If 3D data: must be specified in px.\n",
"diameter=None \n",
"\n",
"# channels: - I restrict to same-channels-in-all-images\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",
"channels=[0,0]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 119
},
"colab_type": "code",
"id": "XcYskYudMajM",
"outputId": "2468460d-5003-4c91-9065-7db2086b9445"
},
"outputs": [],
"source": [
"model = models.Cellpose(gpu=use_GPU, model_type=model_type, net_avg=net_avg)\n",
"\n",
"masks, flows, styles, diams = model.eval(imgs, diameter=diameter, \n",
" flow_threshold=None, channels=channels)\n",
"# can also process single images"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Display results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"colab_type": "code",
"id": "8P5voZOVM-H9",
"outputId": "fc182667-80de-4b25-c503-625c61013a3d"
},
"outputs": [],
"source": [
"num_toplot=3 # numimgs\n",
"\n",
"num_toplot=min(num_toplot,numimgs)\n",
"for idx in range(num_toplot):\n",
" print(idx)\n",
" maski = masks[idx]\n",
" flowi = flows[idx][0]\n",
"\n",
" fig = plt.figure(figsize=(12,5))\n",
" plot.show_segmentation(fig, imgs[idx], maski, flowi, channels=channels)\n",
" plt.tight_layout()\n",
" plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "qj5AIZ825o7P"
},
"source": [
"### Save results "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"savedir=\"../output\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if not os.path.exists(savedir):\n",
" os.makedirs(savedir)\n",
"\n",
"outnames=[os.path.join(savedir,os.path.basename(f).rsplit(\".\")[0]) for f in imgfiles]\n",
"outnames"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# save as image: tif/png\n",
"io.save_masks(imgs,masks,flows,file_names=outnames,png=True)\n",
"\n",
"# save as numpy so that can be loaded in gui\n",
"io.masks_flows_to_seg(imgs, masks, flows, diams, outnames, channels)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "acPatqlKt7EI"
},
"source": [
"## run cellpose 3D\n",
"TODO: from here not updated yet"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# for faster testing: don't average the 4 nets\n",
"model = models.Cellpose(gpu=use_GPU, model_type='cyto',net_avg=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 255
},
"colab_type": "code",
"id": "92RStllFN2Ek",
"outputId": "aa7f69fc-5f84-4175-f64d-21ea7e2b9dd0"
},
"outputs": [],
"source": [
"# test 3D stack\n",
"\n",
"# in this example I'm using a random matrix, put your own data here\n",
"data = np.random.randn(120,512,512).astype(np.float32)\n",
"\n",
"# with 3D you have to set the diameter manually (no auto detect)\n",
"masks, flows, styles, diams = model.eval(data, channels=[0,0], diameter=30, do_3D=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"collapsed_sections": [],
"include_colab_link": true,
"name": "run_cellpose_GPU.ipynb",
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "cellposecpu",
"language": "python",
"name": "cellposecpu"
},
"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.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment