예제 #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)
예제 #2
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)
예제 #3
0
파일: render.py 프로젝트: scottwedge/corgie
    def execute(self):
        padded_bcube = self.bcube.uncrop(self.pad, self.mip)

        for f in self.additional_fields:
            self.src_stack.add_layer(f)

        src_translation, src_data_dict = self.src_stack.read_data_dict(
            padded_bcube, mip=self.mip, stack_name='src')
        agg_field = src_data_dict[f"src_agg_field"]

        if self.blackout_masks:
            mask_layers = self.dst_stack.get_layers_of_type(["img", "mask"])
            mask = helpers.read_mask_list(mask_layers, self.bcube, self.mip)
        else:
            mask = None

        if self.render_masks:
            write_layers = self.dst_stack.get_layers_of_type(["img", "mask"])
        else:
            write_layers = self.dst_stack.get_layers_of_type("img")

        for l in write_layers:
            src = src_data_dict[f"src_{l.name}"]

            if agg_field is not None:
                warped_src = residuals.res_warp_img(src.float(), agg_field)
            else:
                warped_src = src

            if mask is not None:
                warped_src[mask] = 0.0

            cropped_out = helpers.crop(warped_src, self.pad)
            l.write(cropped_out, bcube=self.bcube, mip=self.mip)

        for f in self.additional_fields:
            self.src_stack.remove_layer(f.name)
예제 #4
0
    def execute(self):
        padded_bcube = self.bcube.uncrop(self.pad, self.mip)
        seethrough_bcube = self.bcube.translate(
            z_offset=self.seethrough_offset)

        for f in self.additional_fields:
            #just in case the "additional" field is actually already a part of src_stack
            if f not in self.src_stack.layers.values():
                self.src_stack.add_layer(f)

        src_translation, src_data_dict = self.src_stack.read_data_dict(
            padded_bcube, mip=self.mip, add_prefix=False)
        agg_field = src_data_dict[f"agg_field"]
        agg_mask = None

        if self.blackout_masks or self.seethrough:
            mask_layers = self.dst_stack.get_layers_of_type(["mask"])
            mask_layer_names = [l.name for l in mask_layers]
            for n, d in six.iteritems(src_data_dict):
                if n in mask_layer_names:
                    if agg_mask is None:
                        agg_mask = d
                    else:
                        agg_mask = ((agg_mask + d) > 0).byte()

            if agg_mask is not None:
                coarsen_factor = int(2**(6 - self.mip))
                agg_mask = helpers.coarsen_mask(agg_mask, coarsen_factor)
                if agg_field is not None:
                    warped_mask = residuals.res_warp_img(
                        agg_mask.float(), agg_field)
                else:
                    warped_mask = agg_mask

                warped_mask = (warped_mask > 0.4).byte()
            else:
                warped_mask = None

        if self.render_masks:
            write_layers = self.dst_stack.get_layers_of_type(["img", "mask"])
        else:
            write_layers = self.dst_stack.get_layers_of_type("img")

        for l in write_layers:
            src = src_data_dict[f"{l.name}"]
            if agg_field is not None:
                warped_src = residuals.res_warp_img(src.float(), agg_field)
            else:
                warped_src = src

            cropped_out = helpers.crop(warped_src, self.pad)

            if self.blackout_masks or self.seethrough:
                if warped_mask is not None:
                    warped_mask = helpers.crop(warped_mask, self.pad)

                if l.get_layer_type(
                ) == "img" and self.blackout_masks and warped_mask is not None:
                    cropped_out[warped_mask] = self.blackout_value

                if l.get_layer_type(
                ) == "img" and self.seethrough and warped_mask is not None:
                    seethrough_data = l.read(bcube=seethrough_bcube,
                                             mip=self.mip)

                    coarsen_factor = int(2**(6 - self.mip))
                    seethrough_mask = helpers.coarsen_mask(
                        warped_mask, coarsen_factor)
                    seethrough_mask[cropped_out == 0] = True

                    cropped_out[seethrough_mask] = \
                            seethrough_data[seethrough_mask]

                    seenthru = (cropped_out[seethrough_mask] != 0).sum()
                    corgie_logger.debug(f"Seenthrough {seenthru} pixels")

            l.write(cropped_out, bcube=self.bcube, mip=self.mip)
예제 #5
0
파일: render.py 프로젝트: seung-lab/corgie
    def execute(self):
        padded_bcube = self.bcube.uncrop(self.pad, self.mip)

        for f in self.additional_fields:
            # just in case the "additional" field is actually already a part of src_stack
            if f not in self.src_stack.layers.values():
                self.src_stack.add_layer(f)

        src_translation, src_data_dict = self.src_stack.read_data_dict(
            padded_bcube,
            mip=self.mip,
            add_prefix=False,
            translation_adjuster=helpers.percentile_trans_adjuster,
        )
        agg_field = src_data_dict[f"agg_field"]
        if agg_field is not None:
            agg_field[:, 0] -= src_translation.x
            agg_field[:, 1] -= src_translation.y

        # Produce and aggregate mask if there's a need to blackout all masks
        agg_mask = None
        if self.blackout_masks:
            mask_layers = self.dst_stack.get_layers_of_type(["mask"])
            mask_layer_names = [l.name for l in mask_layers]
            for n, d in six.iteritems(src_data_dict):
                if n in mask_layer_names:
                    if agg_mask is None:
                        agg_mask = d
                    else:
                        agg_mask = ((agg_mask + d) > 0).byte()

            # if the mask is not empty, warp it and coarsen it for seethrough
            if agg_mask is not None:
                coarsen_factor = int(2**(6 - self.mip))
                agg_mask = helpers.coarsen_mask(agg_mask, coarsen_factor)
                if agg_field is not None:
                    warped_mask = residuals.res_warp_img(
                        agg_mask.float(), agg_field)
                else:
                    warped_mask = agg_mask

                warped_mask = (warped_mask > 0.4).byte()
                # To save time, we won't be blacking out stuff that gets cropped anyway
                warped_mask = helpers.crop(warped_mask, self.pad)
            else:
                warped_mask = None

        if self.render_masks:
            write_layers = self.dst_stack.get_layers_of_type(["img", "mask"])
        else:
            write_layers = self.dst_stack.get_layers_of_type("img")

        for l in write_layers:
            src = src_data_dict[f"{l.name}"]
            '''
            if (src != 0).sum():
                import pdb; pdb.set_trace()
            else:
                return
            '''
            if agg_field is not None:
                warped_src = residuals.res_warp_img(src.float(), agg_field)
            else:
                warped_src = src

            cropped_out = helpers.crop(warped_src, self.pad)

            if l.get_layer_type() == "img":
                if self.blackout_masks and warped_mask is not None:
                    cropped_out[warped_mask] = self.blackout_value

                if self.preserve_zeros and agg_field is not None:
                    src_zero_mask = src == 0
                    warped_zero_mask = residuals.res_warp_img(
                        src_zero_mask.float(), agg_field)
                    warped_zero_mask = (warped_zero_mask > 0.4).byte()
                    warped_zero_mask = helpers.crop(warped_zero_mask, self.pad)
                    cropped_out[warped_zero_mask] = 0

                if self.seethrough_mask_layer is not None:
                    seethrough_mask = (self.seethrough_mask_layer.read(
                        bcube=self.bcube, mip=self.mip) != 0)
                    seethrough_bcube = self.bcube.translate(
                        z_offset=self.seethrough_offset)
                    seethrough_data = l.read(bcube=seethrough_bcube,
                                             mip=self.mip)

                    cropped_out[seethrough_mask] = seethrough_data[
                        seethrough_mask]

                    seenthru = (cropped_out[seethrough_mask] != 0).sum()
                    corgie_logger.debug(f"Seenthrough {seenthru} pixels")

            l.write(cropped_out, bcube=self.bcube, mip=self.mip)

        for f in self.additional_fields:
            # remove fields we added
            self.src_stack.remove_layer(f.name)
예제 #6
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)