Ejemplo n.º 1
0
def plot(target_label,pred_label):
    plt.figure(figsize=(16,10))
    plt.subplot(211)
    plt.imshow(target_label.squeeze(),cmap=random_label_cmap())
    plt.axis('off')
    plt.title('Ground truth')
    plt.subplot(212)
    plt.axis('off')
    plt.imshow(pred_label,cmap=random_label_cmap())
    plt.title('Predicted Label.')
    plt.show()
Ejemplo n.º 2
0
def plot(volume, title=None, save=None, show=False):
    if volume.ndim == 4:
        for i in [1]:
            plt.figure()
            plt.imshow(volume[i, 1], vmin=0, vmax=a_max)
            plt.title(title)
            plt.colorbar()
            if save:
                plt.savefig(f'pics/{save}_dir_{title}')
    elif volume.ndim == 3:
        plt.figure()
        plt.imshow(volume[1])
        plt.title(title)
        plt.colorbar()
        if save:
            plt.savefig(f'pics/{save}_dir_{title}')
    else:
        plt.figure()
        plt.imshow(volume, cmap=random_label_cmap(n=1))
        plt.title(title)
        plt.colorbar()
        if save:
            plt.savefig(f'pics/{save}_dir_{title}')
    if show:
        plt.show()
Ejemplo n.º 3
0
 def __init__(self,
              from_pretrained='2D_versatile_fluo',
              normalize_func=None):
     if from_pretrained is None:  # Train your own model
         raise NotImplementedError
     self.model = StarDist2D.from_pretrained(from_pretrained)
     self.lbl_cmap = random_label_cmap()
     if normalize_func is None:
         self.normalize_func = self.default_normalize
Ejemplo n.º 4
0
def show_test_images(X, Y_truth, Y_pred, file_names, show_num=None):
    num_images = min(show_num, len(X)) if show_num is not None else len(X)
    lbl_cmap = random_label_cmap()

    for i in range(num_images):
        fig, axes = plt.subplots(1, 3, figsize=(20, 10))
        axes[0].imshow(X[i], clim=(0, 1), cmap='gray')
        axes[0].set_title(f'{file_names[i]}: raw image')
        axes[0].axis('off')
        axes[1].imshow(Y_truth[i], clim=(0, 1), cmap='gray')
        axes[1].set_title(f'{file_names[i]}: ground truth')
        axes[1].axis('off')
        axes[2].imshow(X[i], clim=(0, 1), cmap='gray')
        axes[2].imshow(Y_pred[i][0],
                       cmap=lbl_cmap,
                       interpolation="nearest",
                       alpha=0.5)
        num_objects = len(Y_pred[i][1]['coord'])
        axes[2].set_title(f'{file_names[i]}: predicted, {num_objects} objects')
        axes[2].axis('off')
Ejemplo n.º 5
0
def main(argv):
    base_path = "{}".format(os.getenv("HOME"))
    problem_cls = CLASS_OBJSEG

    with BiaflowsJob.from_cli(argv) as bj:
        bj.job.update(status=Job.RUNNING,
                      progress=0,
                      statusComment="Initialization...")

        # 1. Prepare data for workflow
        in_imgs, gt_imgs, in_path, gt_path, out_path, tmp_path = prepare_data(
            problem_cls, bj, is_2d=True, **bj.flags)
        list_imgs = [image.filepath for image in in_imgs]

        # 2. Run Stardist model on input images
        bj.job.update(progress=25, statusComment="Launching workflow...")

        #Loading pre-trained Stardist model
        np.random.seed(17)

        lbl_cmap = random_label_cmap()
        model_fluo = StarDist2D(None,
                                name='2D_versatile_fluo',
                                basedir='/models/')
        model_he = StarDist2D(None, name='2D_versatile_he', basedir='/models/')

        #Go over images
        for img_path in list_imgs:
            fluo = True
            img = imageio.imread(img_path)
            n_channel = 3 if img.ndim == 3 else 1

            if n_channel == 3:
                # Check if 3-channel grayscale image or actually an RGB image
                if np.array_equal(img[:, :, 0],
                                  img[:, :, 1]) and np.array_equal(
                                      img[:, :, 0], img[:, :, 2]):
                    img = skimage.color.rgb2gray(img)
                else:
                    fluo = False

            # normalize channels independently (0,1,2) normalize channels jointly (0,1)
            axis_norm = (0, 1)
            img = normalize(img,
                            bj.parameters.stardist_norm_perc_low,
                            bj.parameters.stardist_norm_perc_high,
                            axis=axis_norm)

            #Stardist model prediction with thresholds
            if fluo:
                labels, details = model_fluo.predict_instances(
                    img,
                    prob_thresh=bj.parameters.stardist_prob_t,
                    nms_thresh=bj.parameters.stardist_nms_t)
            else:
                labels, details = model_he.predict_instances(
                    img,
                    prob_thresh=bj.parameters.stardist_prob_t,
                    nms_thresh=bj.parameters.stardist_nms_t)

            # Convert labels to uint16 for BIAFLOWS
            labels = labels.astype(np.uint16)
            imageio.imwrite(os.path.join(out_path, os.path.basename(img_path)),
                            labels)

        # 3. Upload data to BIAFLOWS
        upload_data(problem_cls,
                    bj,
                    in_imgs,
                    out_path,
                    **bj.flags,
                    monitor_params={
                        "start": 60,
                        "end": 90,
                        "period": 0.1,
                        "prefix":
                        "Extracting and uploading polygons from masks"
                    })

        # 4. Compute and upload metrics
        bj.job.update(progress=90,
                      statusComment="Computing and uploading metrics...")
        upload_metrics(problem_cls, bj, in_imgs, gt_path, out_path, tmp_path,
                       **bj.flags)

        # 5. Pipeline finished
        bj.job.update(progress=100,
                      status=Job.TERMINATED,
                      status_comment="Finished.")
def main(argv):
    with CytomineJob.from_cli(argv) as conn:
        conn.job.update(status=Job.RUNNING,
                        progress=0,
                        statusComment="Initialization...")
        # base_path = "{}".format(os.getenv("HOME")) # Mandatory for Singularity
        base_path = "/home/mmu/Desktop"
        working_path = os.path.join(base_path, str(conn.job.id))

        #Loading pre-trained Stardist model
        np.random.seed(17)
        lbl_cmap = random_label_cmap()
        #Stardist H&E model downloaded from https://github.com/mpicbg-csbd/stardist/issues/46
        #Stardist H&E model downloaded from https://drive.switch.ch/index.php/s/LTYaIud7w6lCyuI
        model = StarDist2D(
            None, name='2D_versatile_HE', basedir='/models/'
        )  #use local model file in ~/models/2D_versatile_HE/

        #Select images to process
        images = ImageInstanceCollection().fetch_with_filter(
            "project", conn.parameters.cytomine_id_project)
        list_imgs = []
        if conn.parameters.cytomine_id_images == 'all':
            for image in images:
                list_imgs.append(int(image.id))
        else:
            list_imgs = [
                int(id_img)
                for id_img in conn.parameters.cytomine_id_images.split(',')
            ]

        #Go over images
        for id_image in conn.monitor(list_imgs,
                                     prefix="Running detection on image",
                                     period=0.1):
            #Dump ROI annotations in img from Cytomine server to local images
            #conn.job.update(status=Job.RUNNING, progress=0, statusComment="Fetching ROI annotations...")
            roi_annotations = AnnotationCollection()
            roi_annotations.project = conn.parameters.cytomine_id_project
            roi_annotations.term = conn.parameters.cytomine_id_roi_term
            roi_annotations.image = id_image  #conn.parameters.cytomine_id_image
            roi_annotations.showWKT = True
            roi_annotations.fetch()
            print(roi_annotations)
            #Go over ROI in this image
            #for roi in conn.monitor(roi_annotations, prefix="Running detection on ROI", period=0.1):
            for roi in roi_annotations:
                #Get Cytomine ROI coordinates for remapping to whole-slide
                #Cytomine cartesian coordinate system, (0,0) is bottom left corner
                print(
                    "----------------------------ROI------------------------------"
                )
                roi_geometry = wkt.loads(roi.location)
                print("ROI Geometry from Shapely: {}".format(roi_geometry))
                print("ROI Bounds")
                print(roi_geometry.bounds)
                minx = roi_geometry.bounds[0]
                miny = roi_geometry.bounds[3]
                #Dump ROI image into local PNG file
                roi_path = os.path.join(
                    working_path,
                    str(roi_annotations.project) + '/' +
                    str(roi_annotations.image) + '/' + str(roi.id))
                roi_png_filename = os.path.join(roi_path + '/' + str(roi.id) +
                                                '.png')
                print("roi_png_filename: %s" % roi_png_filename)
                roi.dump(dest_pattern=roi_png_filename, mask=True, alpha=True)
                #roi.dump(dest_pattern=os.path.join(roi_path,"{id}.png"), mask=True, alpha=True)

                #Stardist works with TIFF images without alpha channel, flattening PNG alpha mask to TIFF RGB
                im = Image.open(roi_png_filename)
                bg = Image.new("RGB", im.size, (255, 255, 255))
                bg.paste(im, mask=im.split()[3])
                roi_tif_filename = os.path.join(roi_path + '/' + str(roi.id) +
                                                '.tif')
                bg.save(roi_tif_filename, quality=100)
                X_files = sorted(glob(roi_path + '/' + str(roi.id) + '*.tif'))
                X = list(map(imread, X_files))
                n_channel = 3 if X[0].ndim == 3 else X[0].shape[-1]
                axis_norm = (
                    0, 1
                )  # normalize channels independently  (0,1,2) normalize channels jointly
                if n_channel > 1:
                    print("Normalizing image channels %s." %
                          ('jointly' if axis_norm is None or 2 in axis_norm
                           else 'independently'))

                #Going over ROI images in ROI directory (in our case: one ROI per directory)
                for x in range(0, len(X)):
                    print("------------------- Processing ROI file %d: %s" %
                          (x, roi_tif_filename))
                    img = normalize(X[x],
                                    conn.parameters.stardist_norm_perc_low,
                                    conn.parameters.stardist_norm_perc_high,
                                    axis=axis_norm)
                    #Stardist model prediction with thresholds
                    labels, details = model.predict_instances(
                        img,
                        prob_thresh=conn.parameters.stardist_prob_t,
                        nms_thresh=conn.parameters.stardist_nms_t)
                    print("Number of detected polygons: %d" %
                          len(details['coord']))
                    cytomine_annotations = AnnotationCollection()
                    #Go over detections in this ROI, convert and upload to Cytomine
                    for pos, polygroup in enumerate(details['coord'], start=1):
                        #Converting to Shapely annotation
                        points = list()
                        for i in range(len(polygroup[0])):
                            #Cytomine cartesian coordinate system, (0,0) is bottom left corner
                            #Mapping Stardist polygon detection coordinates to Cytomine ROI in whole slide image
                            p = Point(minx + polygroup[1][i],
                                      miny - polygroup[0][i])
                            points.append(p)

                        annotation = Polygon(points)
                        #Append to Annotation collection
                        cytomine_annotations.append(
                            Annotation(
                                location=annotation.wkt,
                                id_image=
                                id_image,  #conn.parameters.cytomine_id_image,
                                id_project=conn.parameters.cytomine_id_project,
                                id_terms=[
                                    conn.parameters.cytomine_id_cell_term
                                ]))
                        print(".", end='', flush=True)

                    #Send Annotation Collection (for this ROI) to Cytomine server in one http request
                    ca = cytomine_annotations.save()

        conn.job.update(status=Job.TERMINATED,
                        progress=100,
                        statusComment="Finished.")
Ejemplo n.º 7
0
            return 'x=%1.4f, y=%1.4f' % (x, y)
    return format_coord


#for i in range(labels_new.shape[0]):

#    fig, ax = plt.subplots(1, 1)
#    ax.imshow(labels_new[i], cmap='tab20b')
#    ax.format_coord = format_maker(labels_new[i])
#plt.show()




fig, axes = plt.subplots(1, 3, figsize=(20, 8))
axes[0].imshow(labels_new[plot_slice], cmap=random_label_cmap())
axes[0].set_title('reconstruction')
axes[0].format_coord = format_maker(labels_new[plot_slice])
axes[1].imshow(labels[plot_slice], cmap=random_label_cmap())
axes[1].set_title('input')


x = [offset[2] for offset in offsets_new]
y = [offset[1] for offset in offsets_new]
#c = ['b']*number_of_attractive_channels + ['r']*(len(offsets_new)-number_of_attractive_channels)


axes[2].scatter(x[:number_of_attractive_channels], y[:number_of_attractive_channels], c='blue', label='attractive')
axes[2].scatter(x[number_of_attractive_channels:], y[number_of_attractive_channels:], c='red', label='repulsive')
axes[2].legend()
axes[2].set_title('attractive and repulsive edges')
Ejemplo n.º 8
0
my_parser.add_argument('--use_gpu',type=bool,default=True,help='Use GPU or not')
my_parser.add_argument('--grid',type=list,default=(2,2),help='Subsampled grid for increased efficiency and larger field of view')
my_parser.add_argument('--limit_gpu_mem',type=float,default=0.8,help='Limit for the GPU memory usage')
my_parser.add_argument('--model_dir',type=str,default='models',help='Models directory')
my_parser.add_argument('--model_name',type=str,default='stardist',help='Name of the model')
my_parser.add_argument('--quick_demo',type=bool,default='False',help='Run quick training demo (True) or full training (False)')
my_parser.add_argument('--epochs',type=int,default=400,help='Number of training epochs')
my_parser.add_argument('--steps_per_epoch',type=int,default=100,help='Steps per training epoch')


args = my_parser.parse_args()


trainpath=args.path
np.random.seed(42)
lbl_cmap = random_label_cmap()


X = sorted(glob(trainpath+'/images/*.tif'))
Y = sorted(glob(trainpath+'/masks/*.tif'))
assert all(Path(x).name==Path(y).name for x,y in zip(X,Y))

X = list(map(imread,X))
Y = list(map(imread,Y))
n_channel = 1 if X[0].ndim == 2 else X[0].shape[-1]



axis_norm = (0,1)   # normalize channels independently
# axis_norm = (0,1,2) # normalize channels jointly
if n_channel > 1:
Ejemplo n.º 9
0
import sys
import numpy as np
import matplotlib.pyplot as plt

import skimage
import csbdeep.utils
import stardist

sys.path.append('../')
import utils.evaluation
import utils.predictions

lbl_cmap = stardist.random_label_cmap()


def _visualize_intersection(y_true, y_pred):
    '''
    '''
    y_true = y_true > 0
    y_pred = y_pred > 0

    img = np.zeros([y_true.shape[0], y_true.shape[1], 3])
    # Red – false negatives
    img[:, :, 0] = np.where(y_true == y_pred, 0, 1)
    # Green – true positives
    img[:, :, 1] = np.where(y_true == y_pred, y_pred, 0)
    # Blue – false positives
    img[:, :, 2] = np.where(y_true == y_pred, y_true != y_pred, 0)
    return img