stardist / stardist

StarDist - Object Detection with Star-convex Shapes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem with output segmentation with 2D_versatile_he

niccolo99mandelli opened this issue · comments

Hi, I'm using the StarDist library for histological image segmentation in WSI format using the pre-trained model 2D_versatile_he. The model construction and segmentation process are running correctly, however, when saving the image, the output labels appear completely black, and the segmented nuclei are not present. The error occurs for both small-sized and large-sized slides, using the two provided prediction methods. The input slides I provide have a structure very similar to those in the example and refer to portions of tissues. I have also tried using different formats such as .svs and .tif, but the problem persists.
Below is the code I am using:
Note: I am using the library in Julia and invoking it with Julia's PyCall.

using PyCall

py"""
import numpy as np
import matplotlib.pyplot as plt

from tifffile import imread, imsave
from csbdeep.utils import Path, normalize
from csbdeep.utils.tf import keras_import
keras = keras_import()

from stardist import export_imagej_rois, random_label_cmap
from stardist.models import StarDist2D

np.random.seed(0)
cmap = random_label_cmap()

from zipfile import ZIP_DEFLATED


img = imread('C:/Users/nicom/Desktop/Extra/segmentation/TCGA-OR-A5J1-01A-01-TS1.CFE08710-54B8-45B0-86AE-500D6E36D8A5_004.svs')
model = StarDist2D.from_pretrained('2D_versatile_he')
from csbdeep.data import Normalizer, normalize_mi_ma

class MyNormalizer(Normalizer):
    def __init__(self, mi, ma):
            self.mi, self.ma = mi, ma
    def before(self, x, axes):
        return normalize_mi_ma(x, self.mi, self.ma, dtype=np.float32)
    def after(*args, **kwargs):
        assert False
    @property
    def do_after(self):
        return False

mi, ma = 0, 255
normalizer = MyNormalizer(mi, ma)

# labels, polys = model.predict_instances(img, normalizer=normalizer, n_tiles=(32,32,1))
labels, polys = model.predict_instances_big(img, axes='YXC', block_size=2048, min_overlap=128, context=128,
                                            normalizer=normalizer, n_tiles=(4,4,1))

imsave('labels.tif', labels, compress=ZIP_DEFLATED)
"""

What could be the possible cause of the problem?

Thanks in advance,
Niccolò Mandelli

Hi @niccolo99mandelli, sorry for the late reply!

Maybe it's just a brightness/constrast issue that the output label image appears black, but actually isn't empty.
See this form post for a similar situation.

Next time, please discuss issues like this (which likely aren't bugs) at the image.sc forum.