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')
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)
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'