Ejemplo n.º 1
0
    def execute(self):
        padded_bcube = self.bcube.uncrop(self.pad, self.mip)
        for k, specs in enumerate(self.src_specs[::-1]):
            z = specs['src_z']
            mask_id = specs['mask_id']
            bcube = padded_bcube.reset_coords(zs=z, ze=z+1, in_place=False)
            imgs = {}
            for name in ['src_img', 'src_mask', 'src_field']:
                layer = self.src_layers[str(specs[name])]
                if name == 'src_mask':
                    layer.binarizer = helpers.Binarizer(['eq', mask_id])
                imgs[name] = layer.read(bcube=bcube, mip=self.mip)
            mask = residuals.res_warp_img(imgs['src_mask'].float(), 
                                          imgs['src_field'])
            mask = (mask > 0.4).bool()
            cropped_mask = helpers.crop(mask, self.pad)
            img = residuals.res_warp_img(imgs['src_img'].float(), 
                                         imgs['src_field'])
            cropped_img = helpers.crop(img, self.pad)
            if k == 0:
                dst_img = cropped_img
                dst_img[~cropped_mask] = 0
            else:
                dst_img[cropped_mask] = cropped_img[cropped_mask]

        self.dst_layer.write(dst_img, bcube=self.bcube, mip=self.mip)
Ejemplo n.º 2
0
 def __init__(self, binarization=None, num_channels=1, **kwargs):
     self.binarizer = helpers.Binarizer(binarization)
     if num_channels != 1:
         raise exceptions.ArgumentError(
             "Mask layer 'num_channels'",
             "Mask layer must have 1 channels. 'num_channels' provided: {}".
             format(num_channels))
     super().__init__(**kwargs)
Ejemplo n.º 3
0
    def execute(self):
        device = "cuda" if torch.cuda.is_available() else "cpu"
        padded_bcube = self.bcube.uncrop(self.pad, self.mip)
        for k, specs in enumerate(self.src_specs[::-1]):
            src_z = specs["src_z"]
            dst_z = self.bcube.z_range()[0]

            corgie_logger.info(f"Load fields for {padded_bcube}")
            # backwards compatible
            if not isinstance(specs["src_field"], list):
                specs["src_field"] = [specs["src_field"]]
            mask_layer = self.src_layers[str(specs["src_mask"])]

            field_ids = list(map(str, specs["src_field"]))
            corgie_logger.info(f"field ids={field_ids}")
            z_list = specs.get("src_field_z", [src_z] * len(field_ids))
            fields = FieldSet([self.src_layers[n] for n in field_ids])
            field = fields.read(
                bcube=padded_bcube, z_list=z_list, mip=self.mip, device=device
            )
            bcube = padded_bcube.reset_coords(zs=src_z, ze=src_z + 1, in_place=False)

            mask_trans = helpers.percentile_trans_adjuster(field)
            mask_trans = mask_trans.round_to_mip(self.mip, mask_layer.data_mip)
            corgie_logger.debug(f"mask_trans: {mask_trans}")

            mask_bcube = bcube.translate(
                x_offset=mask_trans.y, y_offset=mask_trans.x, mip=self.mip
            )

            corgie_logger.info(f"Load masks for {mask_bcube}")
            mask_id = specs["mask_id"]
            mask_layer.binarizer = helpers.Binarizer(["eq", mask_id])
            mask = mask_layer.read(bcube=mask_bcube, mip=self.mip, device=device)
            mask = residuals.res_warp_img(
                mask.float(), field - mask_trans.to_tensor(device=field.device)
            ).tensor()
            mask = (mask > 0.4).bool()
            cropped_mask = helpers.crop(mask, self.pad)

            relabel_id = torch.as_tensor(specs.get("relabel_id", k + 1), dtype=torch.uint8)
            if k == 0:
                dst_img = cropped_mask * relabel_id
                dst_img[~cropped_mask] = 0
            else:
                dst_img[cropped_mask] = cropped_mask[cropped_mask] * relabel_id

        self.dst_layer.write(dst_img.cpu(), bcube=self.bcube, mip=self.mip)
Ejemplo n.º 4
0
    def execute(self):
        device = "cuda" if torch.cuda.is_available() else "cpu"

        # Field padding
        padded_bcube = self.bcube.uncrop(self.pad, self.mip)
        for k, specs in enumerate(self.src_specs[::-1]):
            src_z = specs["src_z"]
            dst_z = self.bcube.z_range()[0]

            corgie_logger.info(f"Load fields for {padded_bcube}")
            # backwards compatible
            if not isinstance(specs["src_field"], list):
                specs["src_field"] = [specs["src_field"]]
            mask_layer = self.src_layers[str(specs["src_mask"])]

            field_ids = list(map(str, specs["src_field"]))
            corgie_logger.info(f"field ids={field_ids}")
            z_list = specs.get("src_field_z", [src_z] * len(field_ids))
            fields = FieldSet([self.src_layers[n] for n in field_ids])
            field = fields.read(
                bcube=padded_bcube, z_list=z_list, mip=self.mip, device=device
            )
            bcube = padded_bcube.reset_coords(zs=src_z, ze=src_z + 1, in_place=False)

            # Extend image/mask cutout to account for field spread
            render_pad = int((field.max_vector() - field.min_vector()).max().ceil().tensor().item())
            snap_factor = 2 ** (max(self.mip, mask_layer.data_mip) - self.mip)
            render_pad = math.ceil(render_pad / snap_factor) * snap_factor
            render_pad = min(render_pad, 4096)  # Safety

            render_bcube = bcube.uncrop(render_pad, self.mip)
            corgie_logger.debug(f"render_pad: {render_pad}")

            # Move image/mask cutout to account for field drift
            img_trans = helpers.percentile_trans_adjuster(field)
            mask_trans = img_trans.round_to_mip(self.mip, mask_layer.data_mip)
            corgie_logger.debug(f"img_trans: {img_trans} | mask_trans: {mask_trans}")

            img_bcube = render_bcube.translate(
                x_offset=img_trans.y, y_offset=img_trans.x, mip=self.mip
            )
            mask_bcube = render_bcube.translate(
                x_offset=mask_trans.y, y_offset=mask_trans.x, mip=self.mip
            )

            if render_pad > 0:
                field = torch.nn.functional.pad(field, [render_pad, render_pad, render_pad, render_pad], mode='replicate')

            corgie_logger.info(f"Load masks for {mask_bcube}")
            mask_id = specs["mask_id"]
            mask_layer.binarizer = helpers.Binarizer(["eq", mask_id])
            mask = mask_layer.read(bcube=mask_bcube, mip=self.mip, device=device)
            mask = residuals.res_warp_img(
                mask.float(), field - mask_trans.to_tensor(device=field.device)
            ).tensor()
            mask = (mask > 0.4).bool()
            cropped_mask = helpers.crop(mask, self.pad + render_pad)

            corgie_logger.info(f"Load image for {img_bcube}")
            if cropped_mask.sum() == 0:
                cropped_img = torch.zeros_like(cropped_mask, dtype=torch.float)
            else:
                img_layer = self.src_layers[str(specs["src_img"])]
                img = img_layer.read(bcube=img_bcube, mip=self.mip, device=device)
                img = residuals.res_warp_img(
                    img.float(), field - img_trans.to_tensor(device=field.device)
                )
                cropped_img = helpers.crop(img, self.pad + render_pad)

            # write to composite image
            if k == 0:
                dst_img = cropped_img
                dst_img[~cropped_mask] = 0
            else:
                dst_img[cropped_mask] = cropped_img[cropped_mask]

        self.dst_layer.write(dst_img.cpu(), bcube=self.bcube, mip=self.mip)