def __fold(self, tiles: torch.Tensor) -> torch.Tensor:
        """Fold tiles back into original tensor.

        Args:
            tiles: Tiles generated by ``__unfold()``

        Returns:
            torch.Tensor: Reconstructed tensor
        """
        _, num_channels, tile_size_h, tile_size_w = tiles.shape
        scale_h, scale_w = (tile_size_h / self.tile_size_h), (tile_size_w / self.tile_size_w)
        device = tiles.device
        reduced_tile_h = tile_size_h - (2 * self.remove_border_count)
        reduced_tile_w = tile_size_w - (2 * self.remove_border_count)
        image_size = (
            self.batch_size,
            num_channels,
            int(self.resized_h * scale_h),
            int(self.resized_w * scale_w),
        )

        tiles = tiles.contiguous().view(
            self.batch_size,
            self.num_patches_h,
            self.num_patches_w,
            num_channels,
            tile_size_h,
            tile_size_w,
        )
        tiles = tiles.permute(0, 3, 1, 2, 4, 5)
        tiles = tiles.contiguous().view(self.batch_size, num_channels, -1, tile_size_h, tile_size_w)
        tiles = tiles.permute(2, 0, 1, 3, 4)

        tiles = tiles[
            :,
            :,
            :,
            self.remove_border_count : reduced_tile_h + self.remove_border_count,
            self.remove_border_count : reduced_tile_w + self.remove_border_count,
        ]

        img = torch.zeros(image_size, device=device)
        lookup = torch.zeros(image_size, device=device)
        ones = torch.ones(reduced_tile_h, reduced_tile_w, device=device)

        for patch, (loc_i, loc_j) in zip(
            tiles,
            product(
                range(
                    self.remove_border_count,
                    int(self.resized_h * scale_h) - reduced_tile_h + 1,
                    int(self.stride_h * scale_h),
                ),
                range(
                    self.remove_border_count,
                    int(self.resized_w * scale_w) - reduced_tile_w + 1,
                    int(self.stride_w * scale_w),
                ),
            ),
            strict=True,
        ):
            img[
                :,
                :,
                loc_i : (loc_i + reduced_tile_h),
                loc_j : (loc_j + reduced_tile_w),
            ] += patch
            lookup[
                :,
                :,
                loc_i : (loc_i + reduced_tile_h),
                loc_j : (loc_j + reduced_tile_w),
            ] += ones

        img = torch.divide(img, lookup)
        img[img != img] = 0  # noqa: PLR0124

        return img
Fold tiles back into original tensor.

        Args:
            tiles: Tiles generated by ``__unfold()``

        Returns:
            torch.Tensor: Reconstructed tensor
        
src/anomalib/data/utils/tiler.py