class Segmentation():
    def __init__(self,
                 img_shape=None,
                 pixel_size=None,
                 r_min=None,
                 frame_data=None,
                 edge_dist=15,
                 channel_width=0,
                 **kwargs):
        # rmin in µm
        # channel_width in pixel ?
        # pixel_size in m / not in mµ!!
        self.unet = UNet().create_model((img_shape[0], img_shape[1], 1),
                                        1,
                                        d=8)
        network_path = "/home/user/Desktop/2020_Deformation_Cytometer/models_local/background_weighting_with_custom_function/Unet_andy_Unet_class_weight_like_cw0_01_20201110-041226.h5"
        self.unet.load_weights(network_path)

        self.pixel_size = pixel_size
        self.r_min = r_min
        self.frame_data = frame_data if frame_data is not frame_data else {}
        self.edge_dist = edge_dist
        self.config = {}
        self.config["channel_width_px"] = channel_width
        self.config["pixel_size_m"] = pixel_size

    def segmentation(self, img):
        if len(img.shape) == 3:
            img = img[:, :, 0]
        img = (img - np.mean(img)) / np.std(img).astype(np.float32)
        prob_map = self.unet.predict(img[None, :, :, None])[0, :, :, 0]
        prediction_mask = prob_map > 0.5
        cells = mask_to_cells(prediction_mask, img, self.config, self.r_min,
                              self.frame_data, self.edge_dist)
        return prediction_mask, cells, prob_map
class Segmentation():
    def __init__(self,
                 img_shape=None,
                 pixel_size=None,
                 r_min=None,
                 frame_data=None,
                 edge_dist=15,
                 channel_width=0,
                 **kwargs):
        # rmin in µm
        # channel_width in pixel ?
        # pixel_size in m / not in mµ!!
        self.unet = UNet().create_model((img_shape[0], img_shape[1], 1),
                                        1,
                                        d=8)
        network_path = "/home/user/Downloads/andyUnet_andy_bens_network_long_n300_20201027-082115_checkpoint.h5"
        self.unet.load_weights(network_path)

        self.pixel_size = pixel_size
        self.r_min = r_min
        self.frame_data = frame_data if frame_data is not frame_data else {}
        self.edge_dist = edge_dist
        self.config = {}
        self.config["channel_width_px"] = channel_width
        self.config["pixel_size_m"] = pixel_size

    def segmentation(self, img):
        if len(img.shape) == 3:
            img = img[:, :, 0]
        img = (img - np.mean(img)) / np.std(img).astype(np.float32)
        prob_map = self.unet.predict(img[None, :, :, None])[0, :, :, 0]
        prediction_mask = prob_map > 0.5
        cells = mask_to_cells(prediction_mask, img, self.config, self.r_min,
                              self.frame_data, self.edge_dist)
        return prediction_mask, cells, prob_map
class Segmentation():
    def __init__(self,
                 img_shape=None,
                 pixel_size=None,
                 r_min=None,
                 frame_data=None,
                 edge_dist=15,
                 channel_width=0,
                 **kwargs):
        # rmin in µm
        # channel_width in pixel ?
        # pixel_size in m / not in mµ!!
        self.unet = UNet().create_model((img_shape[0], img_shape[1], 1),
                                        1,
                                        d=8)
        network_path = "/home/user/Software/Deformation_Cytometer/deformationcytometer/detection/includes/v0.3/Unet_transfer_immune_cells.h5"
        self.unet.load_weights(network_path)

        self.pixel_size = pixel_size
        self.r_min = r_min
        self.frame_data = frame_data if frame_data is not frame_data else {}
        self.edge_dist = edge_dist
        self.config = {}
        self.config["channel_width_px"] = channel_width
        self.config["pixel_size_m"] = pixel_size

    def segmentation(self, img):
        if len(img.shape) == 3:
            img = img[:, :, 0]
        img = (img - np.mean(img)) / np.std(img).astype(np.float32)
        prob_map = self.unet.predict(img[None, :, :, None])[0, :, :, 0]
        prediction_mask = prob_map > 0.5
        cells = mask_to_cells(prediction_mask, img, self.config, self.r_min,
                              self.frame_data, self.edge_dist)
        return prediction_mask, cells, prob_map
class Segmentation():
    def __init__(self,
                 img_shape=None,
                 pixel_size=None,
                 r_min=None,
                 frame_data=None,
                 edge_dist=15,
                 channel_width=0,
                 **kwargs):
        # rmin in µm
        # channel_width in pixel ?
        # pixel_size in m / not in mµ!!
        self.unet = UNet().create_model((img_shape[0], img_shape[1], 1),
                                        1,
                                        d=8)
        network_path = "/home/user/Desktop/2020_Deformation_Cytometer/local_training/Unet_test_reproducability_1_20201203-104240.h5"
        self.unet.load_weights(network_path)

        self.pixel_size = pixel_size
        self.r_min = r_min
        self.frame_data = frame_data if frame_data is not frame_data else {}
        self.edge_dist = edge_dist
        self.config = {}
        self.config["channel_width_px"] = channel_width
        self.config["pixel_size_m"] = pixel_size

    def segmentation(self, img):
        if len(img.shape) == 3:
            img = img[:, :, 0]
        img = (img - np.mean(img)) / np.std(img).astype(np.float32)
        prob_map = self.unet.predict(img[None, :, :, None])[0, :, :, 0]
        prediction_mask = prob_map > 0.5
        cells, prediction_mask = mask_to_cells_edge(prediction_mask,
                                                    img,
                                                    self.config,
                                                    self.r_min,
                                                    self.frame_data,
                                                    self.edge_dist,
                                                    return_mask=True)
        return prediction_mask, cells, prob_map
Beispiel #5
0
class Segmentation():
    def __init__(self,
                 network_path=None,
                 img_shape=None,
                 pixel_size=None,
                 r_min=None,
                 frame_data=None,
                 edge_dist=15,
                 channel_width=0,
                 edge_only=False,
                 return_mask=True,
                 d=8,
                 **kwargs):
        # rmin in µm
        # channel_width in pixel ?
        # pixel_size in m / not in mµ!!
        self.unet = UNet().create_model((img_shape[0], img_shape[1], 1),
                                        1,
                                        d=d)
        #network_path ="/home/user/Desktop/2020_Deformation_Cytometer/models_local/weights_andy_transfer_learning1/andyUnet_andy_transfer_long_n200__20201006-155443.h5"
        self.unet.load_weights(network_path)

        self.pixel_size = pixel_size
        self.r_min = r_min
        self.frame_data = frame_data if frame_data is not frame_data else {}
        self.edge_dist = edge_dist
        self.config = {}
        self.config["channel_width_px"] = channel_width
        self.config["pixel_size_m"] = pixel_size
        self.edge_only = edge_only
        self.return_mask = return_mask

    def search_cells(self, prediction_mask, img):

        if self.edge_only:
            if self.return_mask:
                cells, prediction_mask = mask_to_cells_edge(
                    prediction_mask,
                    img,
                    self.config,
                    self.r_min,
                    self.frame_data,
                    self.edge_dist,
                    return_mask=self.return_mask)
            else:
                cells = mask_to_cells_edge(prediction_mask,
                                           img,
                                           self.config,
                                           self.r_min,
                                           self.frame_data,
                                           self.edge_dist,
                                           return_mask=self.return_mask)
        else:
            cells = mask_to_cells(prediction_mask, img, self.config,
                                  self.r_min, self.frame_data, self.edge_dist)

        return prediction_mask, cells

    def segmentation(self, img):
        # image batch
        if len(img.shape) == 4:
            img = preprocess_batch(img)
            prediction_mask = self.unet.predict(img) > 0.5
            cells = []
            for i in range(prediction_mask.shape[0]):
                _, cells_ = self.search_cells(prediction_mask[i, :, :, 0],
                                              img[i, :, :, 0])
                cells.extend(cells_)
            prediction_mask = None
        # single image
        elif len(img.shape) == 2:

            img = (img - np.mean(img)) / np.std(img).astype(np.float32)
            prediction_mask = self.unet.predict(img[None, :, :, None])[0, :, :,
                                                                       0] > 0.5
            prediction_mask, cells = self.search_cells(prediction_mask, img)
        else:
            raise Exception("incorrect image shape: img.shape == " +
                            str(img.shape))
        return prediction_mask, cells