Segment with stardist model#

from skimage.io import imread, imsave
from tnia.plotting.projections import show_xyz_slice, show_xyz_max
from stardist.models import Config3D, StarDist3D
from skimage.transform import resize
import os
from tnia.plotting.plt_helper import random_label_cmap
import sys
sys.path.append('../')
import decon_helper as dh

# define random label colormap, this is used to show the labels
colormap=random_label_cmap()
tnia available
stackview available

Load an input image#

In this cell we load an input image. Note that the image is bigger than the patch size we trained on. The CSBDeep framework will take care of applying the network in chunks.

input_names = [r'../../data/deep learning testing/inputs/nuclei.tif',
              r'../../data/deep learning testing/inputs/spheres2.tif',
              r'../../data/deep learning testing/inputs/spheres3.tif']

inputs=[]

for input_name in input_names:
    input = imread(input_name)
    nmin=input.min()
    nmax=input.max()
    input = (input.astype('float32')-nmin) / (nmax - nmin)
    inputs.append(input)
    dh.show_xyz_slice(input, 'tnia')
../_images/9b699b94900ca2f1987b5fb4e6fb5ceb8a4da774cc9790be1d756607e2989bc9.png ../_images/b1da3c1fa6c4a52375140063ab4680999e8356fe93ef09bc4fd0ab583997f595.png ../_images/28537a9a2a6e8cf291379ff680489a047759aeab9f71fd6e151fedb74e75cc66.png

Load the model#

Load the model that we trained in the previous notebook

# load the model using the name of the model and the path to the model
model = StarDist3D(None, 'stardist_model1', basedir=r'../../models')
Loading network weights from 'weights_best.h5'.
Couldn't load thresholds from 'thresholds.json', using default values. (Call 'optimize_thresholds' to change that.)
Using default values: prob_thresh=0.5, nms_thresh=0.4.
from tnia.deeplearning.dl_helper import apply_stardist
labels=[]

for input in inputs:

    labels_, details = apply_stardist(input, model, prob_thresh=0.3, nms_thresh=0.4, down_sample=1, pmin=0, pmax=100)
    labels.append(labels_)
for labels_, input in zip(labels, inputs):
    show_xyz_max(input)
    show_xyz_max(labels_, colormap=colormap)
../_images/04ef806b84c12547d3a010e7343ad85bbe9b71220f36f40c39f716501e776269.png ../_images/d7119a1b6577c1b137e6befacf709fa4dd8e7b97ebd875ff2f7f8b6413c6be15.png ../_images/6fd68d364067a340cf68cf2740b1bb46825f0717aebb004f2a9bf90da2d0f4ff.png ../_images/87eabbc0d28550e874672c082ef64e5ebb8b8b76f131b7dbe3b5418788b2e929.png ../_images/d28b881921b7aeab1a0255dcd6e9afe479d8b734e65506da7806afe236d71749.png ../_images/eefee160f486a73824d8e78223c4389f0bd20bf3e6009ce3bfd70306743c6bf0.png
import napari
for labels_, input in zip(labels, inputs):
    viewer = napari.Viewer()
    viewer.add_image(input, name='input')
    viewer.add_labels(labels_, name='stardist')
    viewer.show()
napari.manifest -> 'napari-hello' could not be imported: Cannot find module 'napari_plugins' declared in entrypoint: 'napari_plugins:napari.yaml'