示例#1
0
    def show_image_grid_heatmap(self,
                                heatmap,
                                background=None,
                                ratio=0.3,
                                normalize=True,
                                colormap=cm.jet,
                                name="heatmap",
                                n_iter=None,
                                prefix=False,
                                iter_format="{:05d}",
                                image_args=None,
                                **kwargs):
        """
        Creates heat map from the given map and if given combines it with the background and then
        displays results with as image grid.

        Args:
           heatmap:  4d- tensor (N, C, H, W) to be converted to a heatmap
           background: 4d- tensor (N, C, H, W) background/ context of the heatmap (to be underlayed)
           name: The name of the window
           ratio: The ratio to mix the map with the background (0 = only background, 1 = only map)
           n_iter: The iteration number, formatted with the iter_format and added to the model name (if not None)
           iter_format: The format string, which indicates how n_iter will be formated as a string
           prefix: If True, the formated n_iter will be appended as a prefix, otherwise as a suffix
           image_args: Arguments for the tensorvision save image method

        """

        if image_args is None:
            image_args = {}
        if "normalize" not in image_args:
            image_args["normalize"] = normalize

        if n_iter is not None:
            name = name_and_iter_to_filename(name=name,
                                             n_iter=n_iter,
                                             ending=".png",
                                             iter_format=iter_format,
                                             prefix=prefix)
        elif not name.endswith(".png"):
            name += ".png"

        file_name = os.path.join(self.img_dir, name)

        map_grid = np_make_grid(
            heatmap, normalize=normalize)  # map_grid.shape is (3, X, Y)
        if heatmap.shape[1] != 3:
            map_ = colormap(map_grid[0])[..., :-1].transpose(2, 0, 1)
        else:  # heatmap was already RGB, so don't apply colormap
            map_ = map_grid

        if background is not None:
            img_grid = np_make_grid(background, **image_args)
            fuse_img = (1.0 - ratio) * img_grid + ratio * map_
        else:
            fuse_img = map_

        fuse_img = np.clip(fuse_img * 255, a_min=0, a_max=255).astype(np.uint8)

        imwrite(file_name, fuse_img.transpose(1, 2, 0))
示例#2
0
    def __show_image_grid_heatmap(self,
                                  heatmap,
                                  tensor=None,
                                  ratio=0.3,
                                  colormap=2,
                                  normalize=True,
                                  name=None,
                                  caption=None,
                                  env_appendix="",
                                  opts=None,
                                  image_args=None,
                                  **kwargs):
        """
          Internal show_image_grid_heatmap method, called by the internal process.
          This function does all the magic.
        """

        from cv2 import cv2

        if opts is None: opts = {}
        if image_args is None: image_args = {}

        map_grid = np_make_grid(heatmap, normalize=normalize)
        map_ = np.clip(map_grid * 255, a_min=0, a_max=255)
        map_ = map_.astype(np.uint8)

        map_ = cv2.applyColorMap(map_.transpose(1, 2, 0), colormap=colormap)
        map_ = cv2.cvtColor(map_, cv2.COLOR_BGR2RGB)
        map_ = map_.transpose(2, 0, 1)

        fuse_img = map_

        if tensor is not None:
            img_grid = np_make_grid(tensor, **image_args)
            image = np.clip(img_grid * 255, a_min=0, a_max=255)
            image = image.astype(np.uint8)

            fuse_img = (1.0 - ratio) * image + ratio * map_

        opts = opts.copy()
        opts.update(dict(title=name, caption=caption))

        win = self.vis.image(img=fuse_img,
                             win=name,
                             env=self.name + env_appendix,
                             opts=opts)

        return win
示例#3
0
    def show_image_grid(self, image_array, name="Image-Grid", counter=None, nrow=8, padding=2,
                        normalize=False, range=None, scale_each=False, pad_value=0,
                        *args, **kwargs):
        """
        Sends an array of images to tensorboard as a grid. Like :meth:`.show_image`, but generates
        image grid before.

        Args:
            image_array (np.narray/torch.tensor): Image array/tensor which will be sent as an image grid
            name (str): Identifier for the image grid
            counter (int): Global step value
            nrow (int): Items per row in grid
            padding (int): Padding between images in grid
            normalize (bool): Normalize images in grid
            range (tuple): Tuple (min, max), so images will be normalized to this range
            scale_each (bool): If True, each image will be normalized separately instead of using
                min and max of whole tensor
            pad_value (float): Fill padding with this value
        """

        image_args = dict(nrow=nrow,
                          padding=padding,
                          normalize=normalize,
                          range=range,
                          scale_each=scale_each,
                          pad_value=pad_value)

        if counter is not None:
            self.val_dict["{}-image".format(name)] = counter
        else:
            self.val_dict["{}-image".format(name)] += 1

        grid = np_make_grid(image_array, **image_args)
        self.writer.add_image(tag=name, img_tensor=grid, global_step=self.val_dict["{}-image".format(name)])
        self.val_dict[name] += 1
示例#4
0
    def show_image_grid_heatmap(self, heatmap, background=None, ratio=0.3, normalize=True,
                                colormap=cv2.COLORMAP_JET, name="heatmap", n_iter=None,
                                prefix=False, iter_format="{:05d}", image_args=None, **kwargs):
        """
        Creates heat map from the given map and if given combines it with the background and then
        displays results with as image grid.

        Args:
           heatmap:  4d- tensor (N, C, H, W) to be converted to a heatmap
           background: 4d- tensor (N, C, H, W) background/ context of the heatmap (to be underlayed)
           name: The name of the window
           ratio: The ratio to mix the map with the background (0 = only background, 1 = only map)
           n_iter: The iteration number, formatted with the iter_format and added to the model name (if not None)
           iter_format: The format string, which indicates how n_iter will be formated as a string
           prefix: If True, the formated n_iter will be appended as a prefix, otherwise as a suffix
           image_args: Arguments for the tensorvision save image method

        """

        if image_args is None: image_args = {}

        if n_iter is not None:
            name = name_and_iter_to_filename(name=name, n_iter=n_iter, ending=".png", iter_format=iter_format,
                                             prefix=prefix)
        elif not name.endswith(".png"):
            name += ".png"

        file_name = os.path.join(self.img_dir, name)

        map_grid = np_make_grid(heatmap, normalize=normalize)
        map_ = np.clip(map_grid * 255, a_min=0, a_max=255)
        map_ = map_.astype(np.uint8)

        map_ = cv2.applyColorMap(map_.transpose(1, 2, 0), colormap=colormap)
        map_ = cv2.cvtColor(map_, cv2.COLOR_BGR2RGB)
        map_ = map_.transpose(2, 0, 1)

        fuse_img = map_

        if background is not None:
            img_grid = np_make_grid(background, **image_args)
            image = np.clip(img_grid * 255, a_min=0, a_max=255)
            image = image.astype(np.uint8)

            fuse_img = (1.0 - ratio) * image + ratio * map_

        imsave(file_name, fuse_img.transpose(1, 2, 0))
示例#5
0
    def show_image_grid(self,
                        tensor,
                        name=None,
                        caption=None,
                        env_appendix="",
                        opts=None,
                        image_args=None,
                        **kwargs):
        """
        Calls the save image grid method (for abstract logger combatibility)

        Args:
           images: 4d- tensor (N, C, H, W)
           name: The name of the window
           caption: Caption of the generated image grid
           env_appendix: appendix to the environment name, if used the new env is env+env_appendix
           opts: opts dict for the ploty/ visdom plot, i.e. can set window size, en/disable ticks,...
           image_args: Arguments for the tensorvision save image method


        """

        if opts is None: opts = {}
        if image_args is None: image_args = {}

        if isinstance(tensor, Variable):

            tensor = tensor.detach()

        if torch.is_tensor(tensor):
            assert torch.is_tensor(
                tensor), "tensor has to be a pytorch tensor or variable"
            assert tensor.dim() == 4, "tensor has to have 4 dimensions"
            if not (tensor.size(1) == 1 or tensor.size(1) == 3):
                warnings.warn(
                    "The 1. dimension (channel) has to be either 1 (gray) or 3 (rgb), taking the first "
                    "dimension now !!!")
                tensor = tensor[:, 0:1, ]

            grid = make_grid(tensor, **image_args)
            image = grid.mul(255).clamp(0, 255).byte().numpy()
        elif isinstance(tensor, np.ndarray):
            grid = np_make_grid(tensor, **image_args)
            image = np.clip(grid * 255, a_min=0, a_max=255)
            image = image.astype(np.uint8)

        else:
            raise ValueError(
                "Tensor has to be a torch tensor or a numpy array")

        opts = opts.copy()
        opts.update(dict(title=name, caption=caption))

        win = self.vis.image(img=image,
                             win=name,
                             env=self.name + env_appendix,
                             opts=opts)

        return win
示例#6
0
    def __show_image_grid(self,
                          tensor,
                          name=None,
                          caption=None,
                          env_appendix="",
                          opts=None,
                          image_args=None,
                          **kwargs):
        """
          Internal show_image_grid method, called by the internal process.
          This function does all the magic.
        """

        if opts is None: opts = {}
        if image_args is None: image_args = {}

        if isinstance(tensor, Variable):

            tensor = tensor.detach()

        if torch.is_tensor(tensor):
            assert torch.is_tensor(
                tensor), "tensor has to be a pytorch tensor or variable"
            assert tensor.dim() == 4, "tensor has to have 4 dimensions"
            if not (tensor.size(1) == 1 or tensor.size(1) == 3):
                warnings.warn(
                    "The 1. dimension (channel) has to be either 1 (gray) or 3 (rgb), taking the first "
                    "dimension now !!!")
                tensor = tensor[:, 0:1, ]

            grid = make_grid(tensor, **image_args)
            image = grid.mul(255).clamp(0, 255).byte().numpy()
        elif isinstance(tensor, np.ndarray):
            grid = np_make_grid(tensor, **image_args)
            image = np.clip(grid * 255, a_min=0, a_max=255)
            image = image.astype(np.uint8)

        else:
            raise ValueError(
                "Tensor has to be a torch tensor or a numpy array")

        opts = opts.copy()
        opts.update(dict(title=name, caption=caption))

        win = self.vis.image(img=image,
                             win=name,
                             env=self.name + env_appendix,
                             opts=opts)

        return win
示例#7
0
    def show_image_grid_heatmap(self,
                                heatmap,
                                background=None,
                                ratio=0.3,
                                colormap=cm.jet,
                                normalize=True,
                                name="heatmap",
                                caption=None,
                                env_appendix="",
                                opts=None,
                                image_args=None,
                                **kwargs):
        """
        Creates heat map from the given map and if given combines it with the background and then
        displays results with as image grid.

        Args:
           heatmap:  4d- tensor (N, C, H, W), if C = 3, colormap won't be applied.
           background: 4d- tensor (N, C, H, W)
           name: The name of the window
           ratio: The ratio to mix the map with the background (0 = only background, 1 = only map)
           caption: Caption of the generated image grid
           env_appendix: appendix to the environment name, if used the new env is env+env_appendix
           opts: opts dict for the ploty/ visdom plot, i.e. can set window size, en/disable ticks,...
           image_args: Arguments for the tensorvision save image method

        """

        if opts is None:
            opts = {}
        if image_args is None:
            image_args = {}
        if "normalize" not in image_args:
            image_args["normalize"] = normalize

        # if len(heatmap.shape) != 4:
        #     raise IndexError("'heatmap' must have dimensions BxCxHxW!")

        map_grid = np_make_grid(
            heatmap, normalize=normalize)  # map_grid.shape is (3, X, Y)
        if heatmap.shape[1] != 3:
            map_ = colormap(map_grid[0])[..., :-1].transpose(2, 0, 1)
        else:  # heatmap was already RGB, so don't apply colormap
            map_ = map_grid

        if background is not None:
            img_grid = np_make_grid(background, **image_args)
            fuse_img = (1.0 - ratio) * img_grid + ratio * map_
        else:
            fuse_img = map_

        fuse_img = np.clip(fuse_img * 255, a_min=0, a_max=255).astype(np.uint8)

        opts = opts.copy()
        opts.update(dict(title=name, caption=caption))

        win = self.vis.image(img=fuse_img,
                             win=name,
                             env=self.name + env_appendix,
                             opts=opts)

        return win