prompt: 
  We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Update TileOnGrid backend
  **Is your feature request related to a problem? Please describe.**
  Update `TileOnGrid` to support input/output type of `torch.Tensor` and `numpy` while its backend is `numpy` only.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  
  --- BEGIN FILE ---
  ```
  ### monai/apps/pathology/transforms/spatial/dictionary.py
  # Copyright 2020 - 2021 MONAI Consortium
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #     http://www.apache.org/licenses/LICENSE-2.0
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  import copy
  from typing import Any, Dict, Hashable, List, Mapping, Optional, Tuple, Union
  
  import numpy as np
  
  from monai.config import KeysCollection
  from monai.config.type_definitions import NdarrayOrTensor
  from monai.transforms.transform import MapTransform, Randomizable
  
  from .array import SplitOnGrid, TileOnGrid
  
  __all__ = ["SplitOnGridd", "SplitOnGridD", "SplitOnGridDict", "TileOnGridd", "TileOnGridD", "TileOnGridDict"]
  
  
  class SplitOnGridd(MapTransform):
      """
      Split the image into patches based on the provided grid shape.
      This transform works only with torch.Tensor inputs.
  
      Args:
          grid_size: a tuple or an integer define the shape of the grid upon which to extract patches.
              If it's an integer, the value will be repeated for each dimension. Default is 2x2
          patch_size: a tuple or an integer that defines the output patch sizes.
              If it's an integer, the value will be repeated for each dimension.
              The default is (0, 0), where the patch size will be inferred from the grid shape.
  
      Note: the shape of the input image is inferred based on the first image used.
      """
  
      backend = SplitOnGrid.backend
  
      def __init__(
          self,
          keys: KeysCollection,
          grid_size: Union[int, Tuple[int, int]] = (2, 2),
          patch_size: Optional[Union[int, Tuple[int, int]]] = None,
          allow_missing_keys: bool = False,
      ):
          super().__init__(keys, allow_missing_keys)
          self.splitter = SplitOnGrid(grid_size=grid_size, patch_size=patch_size)
  
      def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> Dict[Hashable, NdarrayOrTensor]:
          d = dict(data)
          for key in self.key_iterator(d):
              d[key] = self.splitter(d[key])
          return d
  
  
  class TileOnGridd(Randomizable, MapTransform):
      """
      Tile the 2D image into patches on a grid and maintain a subset of it.
      This transform works only with np.ndarray inputs for 2D images.
  
      Args:
          tile_count: number of tiles to extract, if None extracts all non-background tiles
              Defaults to ``None``.
          tile_size: size of the square tile
              Defaults to ``256``.
          step: step size
              Defaults to ``None`` (same as tile_size)
          random_offset: Randomize position of the grid, instead of starting from the top-left corner
              Defaults to ``False``.
          pad_full: pad image to the size evenly divisible by tile_size
              Defaults to ``False``.
          background_val: the background constant (e.g. 255 for white background)
              Defaults to ``255``.
          filter_mode: mode must be in ["min", "max", "random"]. If total number of tiles is more than tile_size,
              then sort by intensity sum, and take the smallest (for min), largest (for max) or random (for random) subset
              Defaults to ``min`` (which assumes background is high value)
  
      """
  
      def __init__(
          self,
          keys: KeysCollection,
          tile_count: Optional[int] = None,
          tile_size: int = 256,
          step: Optional[int] = None,
          random_offset: bool = False,
          pad_full: bool = False,
          background_val: int = 255,
          filter_mode: str = "min",
          allow_missing_keys: bool = False,
          return_list_of_dicts: bool = False,
      ):
          super().__init__(keys, allow_missing_keys)
  
          self.return_list_of_dicts = return_list_of_dicts
          self.seed = None
  
          self.splitter = TileOnGrid(
              tile_count=tile_count,
              tile_size=tile_size,
              step=step,
              random_offset=random_offset,
              pad_full=pad_full,
              background_val=background_val,
              filter_mode=filter_mode,
          )
  
      def randomize(self, data: Any = None) -> None:
          self.seed = self.R.randint(10000)  # type: ignore
  
      def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Union[Dict[Hashable, np.ndarray], List[Dict]]:
  
          self.randomize()
  
          d = dict(data)
          for key in self.key_iterator(d):
              self.splitter.set_random_state(seed=self.seed)  # same random seed for all keys
              d[key] = self.splitter(d[key])
  
          if self.return_list_of_dicts:
              d_list = []
              for i in range(len(d[self.keys[0]])):
                  d_list.append({k: d[k][i] if k in self.keys else copy.deepcopy(d[k]) for k in d.keys()})
              d = d_list  # type: ignore
  
          return d
  
  
  SplitOnGridDict = SplitOnGridD = SplitOnGridd
  TileOnGridDict = TileOnGridD = TileOnGridd
  
  
  ### monai/apps/pathology/transforms/spatial/array.py
  # Copyright 2020 - 2021 MONAI Consortium
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #     http://www.apache.org/licenses/LICENSE-2.0
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  from typing import Optional, Sequence, Tuple, Union
  
  import numpy as np
  import torch
  from numpy.lib.stride_tricks import as_strided
  
  from monai.config.type_definitions import NdarrayOrTensor
  from monai.transforms.transform import Randomizable, Transform
  from monai.utils.enums import TransformBackends
  
  __all__ = ["SplitOnGrid", "TileOnGrid"]
  
  
  class SplitOnGrid(Transform):
      """
      Split the image into patches based on the provided grid shape.
      This transform works only with torch.Tensor inputs.
  
      Args:
          grid_shape: a tuple or an integer define the shape of the grid upon which to extract patches.
              If it's an integer, the value will be repeated for each dimension. Default is 2x2
          patch_size: a tuple or an integer that defines the output patch sizes.
              If it's an integer, the value will be repeated for each dimension.
              The default is (0, 0), where the patch size will be inferred from the grid shape.
  
      Note: the shape of the input image is inferred based on the first image used.
      """
  
      backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
  
      def __init__(
          self, grid_size: Union[int, Tuple[int, int]] = (2, 2), patch_size: Optional[Union[int, Tuple[int, int]]] = None
      ):
          # Grid size
          if isinstance(grid_size, int):
              self.grid_size = (grid_size, grid_size)
          else:
              self.grid_size = grid_size
          # Patch size
          self.patch_size = None
          if isinstance(patch_size, int):
              self.patch_size = (patch_size, patch_size)
          else:
              self.patch_size = patch_size
  
      def __call__(self, image: NdarrayOrTensor) -> NdarrayOrTensor:
          if self.grid_size == (1, 1) and self.patch_size is None:
              if isinstance(image, torch.Tensor):
                  return torch.stack([image])
              elif isinstance(image, np.ndarray):
                  return np.stack([image])
              else:
                  raise ValueError(f"Input type [{type(image)}] is not supported.")
  
          patch_size, steps = self.get_params(image.shape[1:])
          patches: NdarrayOrTensor
          if isinstance(image, torch.Tensor):
              patches = (
                  image.unfold(1, patch_size[0], steps[0])
                  .unfold(2, patch_size[1], steps[1])
                  .flatten(1, 2)
                  .transpose(0, 1)
                  .contiguous()
              )
          elif isinstance(image, np.ndarray):
              h_step, w_step = steps
              c_stride, h_stride, w_stride = image.strides
              patches = as_strided(
                  image,
                  shape=(*self.grid_size, 3, patch_size[0], patch_size[1]),
                  strides=(h_stride * h_step, w_stride * w_step, c_stride, h_stride, w_stride),
                  writeable=False,
              )
              # flatten the first two dimensions
              patches = patches.reshape(np.prod(patches.shape[:2]), *patches.shape[2:])
              # make it a contiguous array
              patches = np.ascontiguousarray(patches)
          else:
              raise ValueError(f"Input type [{type(image)}] is not supported.")
  
          return patches
  
      def get_params(self, image_size):
          if self.patch_size is None:
              patch_size = tuple(image_size[i] // self.grid_size[i] for i in range(2))
          else:
              patch_size = self.patch_size
  
          steps = tuple(
              (image_size[i] - patch_size[i]) // (self.grid_size[i] - 1) if self.grid_size[i] > 1 else image_size[i]
              for i in range(2)
          )
  
          return patch_size, steps
  
  
  class TileOnGrid(Randomizable, Transform):
      """
      Tile the 2D image into patches on a grid and maintain a subset of it.
      This transform works only with np.ndarray inputs for 2D images.
  
      Args:
          tile_count: number of tiles to extract, if None extracts all non-background tiles
              Defaults to ``None``.
          tile_size: size of the square tile
              Defaults to ``256``.
          step: step size
              Defaults to ``None`` (same as tile_size)
          random_offset: Randomize position of the grid, instead of starting from the top-left corner
              Defaults to ``False``.
          pad_full: pad image to the size evenly divisible by tile_size
              Defaults to ``False``.
          background_val: the background constant (e.g. 255 for white background)
              Defaults to ``255``.
          filter_mode: mode must be in ["min", "max", "random"]. If total number of tiles is more than tile_size,
              then sort by intensity sum, and take the smallest (for min), largest (for max) or random (for random) subset
              Defaults to ``min`` (which assumes background is high value)
  
      """
  
      def __init__(
          self,
          tile_count: Optional[int] = None,
          tile_size: int = 256,
          step: Optional[int] = None,
          random_offset: bool = False,
          pad_full: bool = False,
          background_val: int = 255,
          filter_mode: str = "min",
      ):
          self.tile_count = tile_count
          self.tile_size = tile_size
          self.random_offset = random_offset
          self.pad_full = pad_full
          self.background_val = background_val
          self.filter_mode = filter_mode
  
          if step is None:
              # non-overlapping grid
              self.step = self.tile_size
          else:
              self.step = step
  
          self.offset = (0, 0)
          self.random_idxs = np.array((0,))
  
          if self.filter_mode not in ["min", "max", "random"]:
              raise ValueError("Unsupported filter_mode, must be [min, max or random]: " + str(self.filter_mode))
  
      def randomize(self, img_size: Sequence[int]) -> None:
  
          c, h, w = img_size
  
          self.offset = (0, 0)
          if self.random_offset:
              pad_h = h % self.tile_size
              pad_w = w % self.tile_size
              self.offset = (self.R.randint(pad_h) if pad_h > 0 else 0, self.R.randint(pad_w) if pad_w > 0 else 0)
              h = h - self.offset[0]
              w = w - self.offset[1]
  
          if self.pad_full:
              pad_h = (self.tile_size - h % self.tile_size) % self.tile_size
              pad_w = (self.tile_size - w % self.tile_size) % self.tile_size
              h = h + pad_h
              w = w + pad_w
  
          h_n = (h - self.tile_size + self.step) // self.step
          w_n = (w - self.tile_size + self.step) // self.step
          tile_total = h_n * w_n
  
          if self.tile_count is not None and tile_total > self.tile_count:
              self.random_idxs = self.R.choice(range(tile_total), self.tile_count, replace=False)
          else:
              self.random_idxs = np.array((0,))
  
      def __call__(self, image: np.ndarray) -> np.ndarray:
  
          # add random offset
          self.randomize(img_size=image.shape)
  
          if self.random_offset and (self.offset[0] > 0 or self.offset[1] > 0):
              image = image[:, self.offset[0] :, self.offset[1] :]
  
          # pad to full size, divisible by tile_size
          if self.pad_full:
              c, h, w = image.shape
              pad_h = (self.tile_size - h % self.tile_size) % self.tile_size
              pad_w = (self.tile_size - w % self.tile_size) % self.tile_size
              image = np.pad(
                  image,
                  [[0, 0], [pad_h // 2, pad_h - pad_h // 2], [pad_w // 2, pad_w - pad_w // 2]],
                  constant_values=self.background_val,
              )
  
          # extact tiles
          x_step, y_step = self.step, self.step
          x_size, y_size = self.tile_size, self.tile_size
          c_len, x_len, y_len = image.shape
          c_stride, x_stride, y_stride = image.strides
          llw = as_strided(
              image,
              shape=((x_len - x_size) // x_step + 1, (y_len - y_size) // y_step + 1, c_len, x_size, y_size),
              strides=(x_stride * x_step, y_stride * y_step, c_stride, x_stride, y_stride),
              writeable=False,
          )
          image = llw.reshape(-1, c_len, x_size, y_size)
  
          # if keeping all patches
          if self.tile_count is None:
              # retain only patches with significant foreground content to speed up inference
              # FYI, this returns a variable number of tiles, so the batch_size must be 1 (per gpu), e.g during inference
              thresh = 0.999 * 3 * self.background_val * self.tile_size * self.tile_size
              if self.filter_mode == "min":
                  # default, keep non-background tiles (small values)
                  idxs = np.argwhere(image.sum(axis=(1, 2, 3)) < thresh)
                  image = image[idxs.reshape(-1)]
              elif self.filter_mode == "max":
                  idxs = np.argwhere(image.sum(axis=(1, 2, 3)) >= thresh)
                  image = image[idxs.reshape(-1)]
  
          else:
              if len(image) > self.tile_count:
  
                  if self.filter_mode == "min":
                      # default, keep non-background tiles (smallest values)
                      idxs = np.argsort(image.sum(axis=(1, 2, 3)))[: self.tile_count]
                      image = image[idxs]
                  elif self.filter_mode == "max":
                      idxs = np.argsort(image.sum(axis=(1, 2, 3)))[-self.tile_count :]
                      image = image[idxs]
                  else:
                      # random subset (more appropriate for WSIs without distinct background)
                      if self.random_idxs is not None:
                          image = image[self.random_idxs]
  
              elif len(image) < self.tile_count:
                  image = np.pad(
                      image,
                      [[0, self.tile_count - len(image)], [0, 0], [0, 0], [0, 0]],
                      constant_values=self.background_val,
                  )
  
          return image
  
  ```
  --- END FILE ---
  
  Please first localize the bug based on the issue statement, and then generate *SEARCH/REPLACE* edits to fix the issue.
  
  Every *SEARCH/REPLACE* edit must use this format:
  1. The file path
  2. The start of search block: <<<<<<< SEARCH
  3. A contiguous chunk of lines to search for in the existing source code
  4. The dividing line: =======
  5. The lines to replace into the source code
  6. The end of the replace block: >>>>>>> REPLACE
  
  Here is an example:
  
  ```python
  ### mathweb/flask/app.py
  <<<<<<< SEARCH
  from flask import Flask
  =======
  import math
  from flask import Flask
  >>>>>>> REPLACE
  ```
  
  Please note that the *SEARCH/REPLACE* edit REQUIRES PROPER INDENTATION. If you would like to add the line '        print(x)', you must fully write that out, with all those spaces before the code!
  Wrap the *SEARCH/REPLACE* edit in blocks ```python...```.
  
instance: {"FAIL_TO_PASS": ["tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_48", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_55", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_36", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_88", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_67", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_86", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_93", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_81", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_069", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_095", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_085", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_71", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_33", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_091", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_60", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_35", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_103", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_46", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_054", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_53", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_46", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_56", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_80", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_41", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_52", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_74", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_077", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_053", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_088", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_32", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_099", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_38", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_084", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_36", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_065", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_31", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_87", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_49", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_78", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_47", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_49", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_65", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_54", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_057", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_34", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_41", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_55", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_82", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_080", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_44", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_39", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_062", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_35", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_26", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_57", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_060", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_62", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_094", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_72", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_50", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_92", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_093", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_51", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_66", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_37", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_40", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_070", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_072", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_079", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_61", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_097", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_31", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_45", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_27", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_69", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_40", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_28", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_50", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_83", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_33", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_096", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_066", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_30", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_68", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_95", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_055", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_43", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_059", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_073", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_102", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_082", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_058", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_078", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_064", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_052", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_70", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_074", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_063", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_075", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_48", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_42", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_76", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_63", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_25", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_29", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_090", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_89", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_089", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_51", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_58", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_79", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_086", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_24", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_75", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_85", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_90", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_54", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_100", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_056", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_37", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_47", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_067", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_29", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_44", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_28", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_94", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_45", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_91", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_071", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_34", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_30", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_59", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_84", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_061", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_52", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_42", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_087", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_77", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_092", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_076", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_39", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_73", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_32", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_098", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_083", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_101", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_081", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_38", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_068", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_64", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_53", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_43"], "PASS_TO_PASS": ["tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_25", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_045", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_29", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_02", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_030", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_40", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_28", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_31", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_08", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_23", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_14", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_27", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_040", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_011", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_10", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_039", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_30", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_010", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_021", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_033", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_45", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_05", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_17", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_37", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_032", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_004", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_04", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_017", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_022", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_25", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_11", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_027", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_07", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_09", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_18", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_41", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_046", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_025", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_11", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_00", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_16", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_23", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_46", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_20", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_24", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_14", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_03", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_13", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_22", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_008", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_09", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_028", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_42", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_036", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_15", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_01", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_44", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_07", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_04", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_43", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_049", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_034", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_06", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_029", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_014", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_05", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_007", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_013", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_02", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_031", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_13", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_024", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_16", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_15", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_019", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_006", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_06", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_19", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_36", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_07", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_15", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_005", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_04", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_001", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_012", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_14", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_12", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_47", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_22", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_009", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_27", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_037", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_20", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_22", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_39", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_17", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_32", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_05", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_08", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_17", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_21", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_035", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_023", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_047", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_00", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_10", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_13", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_015", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_23", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_12", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_20", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_03", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_18", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_041", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_043", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_19", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_16", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_01", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_10", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_02", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_34", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_048", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_18", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_038", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_09", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_11", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_26", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_016", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_018", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_044", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_042", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_24", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_35", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_051", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_03", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_000", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_12", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_08", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_38", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_19", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_002", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_026", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_020", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_26", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_01", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_06", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_00", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_random_call_33", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_random_call_21", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_003", "tests/test_tile_on_grid.py::TestTileOnGrid::test_tile_patch_single_call_21", "tests/test_tile_on_grid_dict.py::TestTileOnGridDict::test_tile_patch_single_call_050"], "base_commit": "5adf30beb3581e951556fd8c4f027828bf367a70", "created_at": "2021-11-23T00:10:14Z", "hints_text": "", "instance_id": "Project-MONAI__MONAI-3384", "patch": "diff --git a/monai/apps/pathology/transforms/spatial/array.py b/monai/apps/pathology/transforms/spatial/array.py\n--- a/monai/apps/pathology/transforms/spatial/array.py\n+++ b/monai/apps/pathology/transforms/spatial/array.py\n@@ -17,6 +17,7 @@\n \n from monai.config.type_definitions import NdarrayOrTensor\n from monai.transforms.transform import Randomizable, Transform\n+from monai.utils import convert_data_type, convert_to_dst_type\n from monai.utils.enums import TransformBackends\n \n __all__ = [\"SplitOnGrid\", \"TileOnGrid\"]\n@@ -129,6 +130,8 @@ class TileOnGrid(Randomizable, Transform):\n \n     \"\"\"\n \n+    backend = [TransformBackends.NUMPY]\n+\n     def __init__(\n         self,\n         tile_count: Optional[int] = None,\n@@ -185,37 +188,39 @@ def randomize(self, img_size: Sequence[int]) -> None:\n         else:\n             self.random_idxs = np.array((0,))\n \n-    def __call__(self, image: np.ndarray) -> np.ndarray:\n+    def __call__(self, image: NdarrayOrTensor) -> NdarrayOrTensor:\n+        img_np: np.ndarray\n+        img_np, *_ = convert_data_type(image, np.ndarray)  # type: ignore\n \n         # add random offset\n-        self.randomize(img_size=image.shape)\n+        self.randomize(img_size=img_np.shape)\n \n         if self.random_offset and (self.offset[0] > 0 or self.offset[1] > 0):\n-            image = image[:, self.offset[0] :, self.offset[1] :]\n+            img_np = img_np[:, self.offset[0] :, self.offset[1] :]\n \n         # pad to full size, divisible by tile_size\n         if self.pad_full:\n-            c, h, w = image.shape\n+            c, h, w = img_np.shape\n             pad_h = (self.tile_size - h % self.tile_size) % self.tile_size\n             pad_w = (self.tile_size - w % self.tile_size) % self.tile_size\n-            image = np.pad(\n-                image,\n+            img_np = np.pad(\n+                img_np,\n                 [[0, 0], [pad_h // 2, pad_h - pad_h // 2], [pad_w // 2, pad_w - pad_w // 2]],\n                 constant_values=self.background_val,\n             )\n \n         # extact tiles\n-        x_step, y_step = self.step, self.step\n-        x_size, y_size = self.tile_size, self.tile_size\n-        c_len, x_len, y_len = image.shape\n-        c_stride, x_stride, y_stride = image.strides\n+        h_step, w_step = self.step, self.step\n+        h_size, w_size = self.tile_size, self.tile_size\n+        c_len, h_len, w_len = img_np.shape\n+        c_stride, h_stride, w_stride = img_np.strides\n         llw = as_strided(\n-            image,\n-            shape=((x_len - x_size) // x_step + 1, (y_len - y_size) // y_step + 1, c_len, x_size, y_size),\n-            strides=(x_stride * x_step, y_stride * y_step, c_stride, x_stride, y_stride),\n+            img_np,\n+            shape=((h_len - h_size) // h_step + 1, (w_len - w_size) // w_step + 1, c_len, h_size, w_size),\n+            strides=(h_stride * h_step, w_stride * w_step, c_stride, h_stride, w_stride),\n             writeable=False,\n         )\n-        image = llw.reshape(-1, c_len, x_size, y_size)\n+        img_np = llw.reshape(-1, c_len, h_size, w_size)\n \n         # if keeping all patches\n         if self.tile_count is None:\n@@ -224,32 +229,34 @@ def __call__(self, image: np.ndarray) -> np.ndarray:\n             thresh = 0.999 * 3 * self.background_val * self.tile_size * self.tile_size\n             if self.filter_mode == \"min\":\n                 # default, keep non-background tiles (small values)\n-                idxs = np.argwhere(image.sum(axis=(1, 2, 3)) < thresh)\n-                image = image[idxs.reshape(-1)]\n+                idxs = np.argwhere(img_np.sum(axis=(1, 2, 3)) < thresh)\n+                img_np = img_np[idxs.reshape(-1)]\n             elif self.filter_mode == \"max\":\n-                idxs = np.argwhere(image.sum(axis=(1, 2, 3)) >= thresh)\n-                image = image[idxs.reshape(-1)]\n+                idxs = np.argwhere(img_np.sum(axis=(1, 2, 3)) >= thresh)\n+                img_np = img_np[idxs.reshape(-1)]\n \n         else:\n-            if len(image) > self.tile_count:\n+            if len(img_np) > self.tile_count:\n \n                 if self.filter_mode == \"min\":\n                     # default, keep non-background tiles (smallest values)\n-                    idxs = np.argsort(image.sum(axis=(1, 2, 3)))[: self.tile_count]\n-                    image = image[idxs]\n+                    idxs = np.argsort(img_np.sum(axis=(1, 2, 3)))[: self.tile_count]\n+                    img_np = img_np[idxs]\n                 elif self.filter_mode == \"max\":\n-                    idxs = np.argsort(image.sum(axis=(1, 2, 3)))[-self.tile_count :]\n-                    image = image[idxs]\n+                    idxs = np.argsort(img_np.sum(axis=(1, 2, 3)))[-self.tile_count :]\n+                    img_np = img_np[idxs]\n                 else:\n                     # random subset (more appropriate for WSIs without distinct background)\n                     if self.random_idxs is not None:\n-                        image = image[self.random_idxs]\n+                        img_np = img_np[self.random_idxs]\n \n-            elif len(image) < self.tile_count:\n-                image = np.pad(\n-                    image,\n-                    [[0, self.tile_count - len(image)], [0, 0], [0, 0], [0, 0]],\n+            elif len(img_np) < self.tile_count:\n+                img_np = np.pad(\n+                    img_np,\n+                    [[0, self.tile_count - len(img_np)], [0, 0], [0, 0], [0, 0]],\n                     constant_values=self.background_val,\n                 )\n \n+        image, *_ = convert_to_dst_type(src=img_np, dst=image, dtype=image.dtype)\n+\n         return image\ndiff --git a/monai/apps/pathology/transforms/spatial/dictionary.py b/monai/apps/pathology/transforms/spatial/dictionary.py\n--- a/monai/apps/pathology/transforms/spatial/dictionary.py\n+++ b/monai/apps/pathology/transforms/spatial/dictionary.py\n@@ -12,8 +12,6 @@\n import copy\n from typing import Any, Dict, Hashable, List, Mapping, Optional, Tuple, Union\n \n-import numpy as np\n-\n from monai.config import KeysCollection\n from monai.config.type_definitions import NdarrayOrTensor\n from monai.transforms.transform import MapTransform, Randomizable\n@@ -81,6 +79,8 @@ class TileOnGridd(Randomizable, MapTransform):\n \n     \"\"\"\n \n+    backend = SplitOnGrid.backend\n+\n     def __init__(\n         self,\n         keys: KeysCollection,\n@@ -112,7 +112,9 @@ def __init__(\n     def randomize(self, data: Any = None) -> None:\n         self.seed = self.R.randint(10000)  # type: ignore\n \n-    def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Union[Dict[Hashable, np.ndarray], List[Dict]]:\n+    def __call__(\n+        self, data: Mapping[Hashable, NdarrayOrTensor]\n+    ) -> Union[Dict[Hashable, NdarrayOrTensor], List[Dict[Hashable, NdarrayOrTensor]]]:\n \n         self.randomize()\n \n", "problem_statement": "Update TileOnGrid backend\n**Is your feature request related to a problem? Please describe.**\r\nUpdate `TileOnGrid` to support input/output type of `torch.Tensor` and `numpy` while its backend is `numpy` only.\n", "repo": "Project-MONAI/MONAI", "test_patch": "diff --git a/tests/test_tile_on_grid.py b/tests/test_tile_on_grid.py\n--- a/tests/test_tile_on_grid.py\n+++ b/tests/test_tile_on_grid.py\n@@ -16,6 +16,7 @@\n from parameterized import parameterized\n \n from monai.apps.pathology.transforms import TileOnGrid\n+from tests.utils import TEST_NDARRAYS, assert_allclose\n \n TEST_CASES = []\n for tile_count in [16, 64]:\n@@ -38,6 +39,10 @@\n     for step in [4, 8]:\n         TEST_CASES.append([{\"tile_count\": 16, \"step\": step, \"tile_size\": tile_size}])\n \n+TESTS = []\n+for p in TEST_NDARRAYS:\n+    for tc in TEST_CASES:\n+        TESTS.append([p, *tc])\n \n TEST_CASES2 = []\n for tile_count in [16, 64]:\n@@ -56,6 +61,11 @@\n                     ]\n                 )\n \n+TESTS2 = []\n+for p in TEST_NDARRAYS:\n+    for tc in TEST_CASES2:\n+        TESTS2.append([p, *tc])\n+\n \n def make_image(\n     tile_count: int,\n@@ -104,25 +114,27 @@ def make_image(\n \n \n class TestTileOnGrid(unittest.TestCase):\n-    @parameterized.expand(TEST_CASES)\n-    def test_tile_patch_single_call(self, input_parameters):\n+    @parameterized.expand(TESTS)\n+    def test_tile_patch_single_call(self, in_type, input_parameters):\n \n         img, tiles = make_image(**input_parameters)\n+        input_img = in_type(img)\n \n         tiler = TileOnGrid(**input_parameters)\n-        output = tiler(img)\n-        np.testing.assert_equal(output, tiles)\n+        output = tiler(input_img)\n+        assert_allclose(output, tiles, type_test=False)\n \n-    @parameterized.expand(TEST_CASES2)\n-    def test_tile_patch_random_call(self, input_parameters):\n+    @parameterized.expand(TESTS2)\n+    def test_tile_patch_random_call(self, in_type, input_parameters):\n \n         img, tiles = make_image(**input_parameters, seed=123)\n+        input_img = in_type(img)\n \n         tiler = TileOnGrid(**input_parameters)\n         tiler.set_random_state(seed=123)\n \n-        output = tiler(img)\n-        np.testing.assert_equal(output, tiles)\n+        output = tiler(input_img)\n+        assert_allclose(output, tiles, type_test=False)\n \n \n if __name__ == \"__main__\":\ndiff --git a/tests/test_tile_on_grid_dict.py b/tests/test_tile_on_grid_dict.py\n--- a/tests/test_tile_on_grid_dict.py\n+++ b/tests/test_tile_on_grid_dict.py\n@@ -13,9 +13,11 @@\n from typing import Optional\n \n import numpy as np\n+import torch\n from parameterized import parameterized\n \n from monai.apps.pathology.transforms import TileOnGridDict\n+from tests.utils import TEST_NDARRAYS, assert_allclose\n \n TEST_CASES = []\n for tile_count in [16, 64]:\n@@ -36,11 +38,14 @@\n                         ]\n                     )\n \n-\n for tile_size in [8, 16]:\n     for step in [4, 8]:\n         TEST_CASES.append([{\"tile_count\": 16, \"step\": step, \"tile_size\": tile_size}])\n \n+TESTS = []\n+for p in TEST_NDARRAYS:\n+    for tc in TEST_CASES:\n+        TESTS.append([p, *tc])\n \n TEST_CASES2 = []\n for tile_count in [16, 64]:\n@@ -61,6 +66,10 @@\n                         ]\n                     )\n \n+TESTS2 = []\n+for p in TEST_NDARRAYS:\n+    for tc in TEST_CASES2:\n+        TESTS2.append([p, *tc])\n \n for tile_size in [8, 16]:\n     for step in [4, 8]:\n@@ -114,27 +123,31 @@ def make_image(\n \n \n class TestTileOnGridDict(unittest.TestCase):\n-    @parameterized.expand(TEST_CASES)\n-    def test_tile_patch_single_call(self, input_parameters):\n+    @parameterized.expand(TESTS)\n+    def test_tile_patch_single_call(self, in_type, input_parameters):\n \n         key = \"image\"\n         input_parameters[\"keys\"] = key\n \n         img, tiles = make_image(**input_parameters)\n+        input_img = in_type(img)\n \n         splitter = TileOnGridDict(**input_parameters)\n \n-        output = splitter({key: img})\n+        output = splitter({key: input_img})\n \n         if input_parameters.get(\"return_list_of_dicts\", False):\n-            output = np.stack([ix[key] for ix in output], axis=0)\n+            if isinstance(input_img, torch.Tensor):\n+                output = torch.stack([ix[key] for ix in output], axis=0)\n+            else:\n+                output = np.stack([ix[key] for ix in output], axis=0)\n         else:\n             output = output[key]\n \n-        np.testing.assert_equal(tiles, output)\n+        assert_allclose(output, tiles, type_test=False)\n \n-    @parameterized.expand(TEST_CASES2)\n-    def test_tile_patch_random_call(self, input_parameters):\n+    @parameterized.expand(TESTS2)\n+    def test_tile_patch_random_call(self, in_type, input_parameters):\n \n         key = \"image\"\n         input_parameters[\"keys\"] = key\n@@ -142,18 +155,21 @@ def test_tile_patch_random_call(self, input_parameters):\n         random_state = np.random.RandomState(123)\n         seed = random_state.randint(10000)\n         img, tiles = make_image(**input_parameters, seed=seed)\n+        input_img = in_type(img)\n \n         splitter = TileOnGridDict(**input_parameters)\n         splitter.set_random_state(seed=123)\n \n-        output = splitter({key: img})\n+        output = splitter({key: input_img})\n \n         if input_parameters.get(\"return_list_of_dicts\", False):\n-            output = np.stack([ix[key] for ix in output], axis=0)\n+            if isinstance(input_img, torch.Tensor):\n+                output = torch.stack([ix[key] for ix in output], axis=0)\n+            else:\n+                output = np.stack([ix[key] for ix in output], axis=0)\n         else:\n             output = output[key]\n-\n-        np.testing.assert_equal(tiles, output)\n+        assert_allclose(output, tiles, type_test=False)\n \n \n if __name__ == \"__main__\":\n", "version": "0.8"}
parsed_commit_content: {"file_diffs":[{"old_file_content":"# Copyright 2020 - 2021 MONAI Consortium\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#     http://www.apache.org/licenses/LICENSE-2.0\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom typing import Optional, Sequence, Tuple, Union\n\nimport numpy as np\nimport torch\nfrom numpy.lib.stride_tricks import as_strided\n\nfrom monai.config.type_definitions import NdarrayOrTensor\nfrom monai.transforms.transform import Randomizable, Transform\nfrom monai.utils.enums import TransformBackends\n\n__all__ = [\"SplitOnGrid\", \"TileOnGrid\"]\n\n\nclass SplitOnGrid(Transform):\n    \"\"\"\n    Split the image into patches based on the provided grid shape.\n    This transform works only with torch.Tensor inputs.\n\n    Args:\n        grid_shape: a tuple or an integer define the shape of the grid upon which to extract patches.\n            If it's an integer, the value will be repeated for each dimension. Default is 2x2\n        patch_size: a tuple or an integer that defines the output patch sizes.\n            If it's an integer, the value will be repeated for each dimension.\n            The default is (0, 0), where the patch size will be inferred from the grid shape.\n\n    Note: the shape of the input image is inferred based on the first image used.\n    \"\"\"\n\n    backend = [TransformBackends.TORCH, TransformBackends.NUMPY]\n\n    def __init__(\n        self, grid_size: Union[int, Tuple[int, int]] = (2, 2), patch_size: Optional[Union[int, Tuple[int, int]]] = None\n    ):\n        # Grid size\n        if isinstance(grid_size, int):\n            self.grid_size = (grid_size, grid_size)\n        else:\n            self.grid_size = grid_size\n        # Patch size\n        self.patch_size = None\n        if isinstance(patch_size, int):\n            self.patch_size = (patch_size, patch_size)\n        else:\n            self.patch_size = patch_size\n\n    def __call__(self, image: NdarrayOrTensor) -> NdarrayOrTensor:\n        if self.grid_size == (1, 1) and self.patch_size is None:\n            if isinstance(image, torch.Tensor):\n                return torch.stack([image])\n            elif isinstance(image, np.ndarray):\n                return np.stack([image])\n            else:\n                raise ValueError(f\"Input type [{type(image)}] is not supported.\")\n\n        patch_size, steps = self.get_params(image.shape[1:])\n        patches: NdarrayOrTensor\n        if isinstance(image, torch.Tensor):\n            patches = (\n                image.unfold(1, patch_size[0], steps[0])\n                .unfold(2, patch_size[1], steps[1])\n                .flatten(1, 2)\n                .transpose(0, 1)\n                .contiguous()\n            )\n        elif isinstance(image, np.ndarray):\n            h_step, w_step = steps\n            c_stride, h_stride, w_stride = image.strides\n            patches = as_strided(\n                image,\n                shape=(*self.grid_size, 3, patch_size[0], patch_size[1]),\n                strides=(h_stride * h_step, w_stride * w_step, c_stride, h_stride, w_stride),\n                writeable=False,\n            )\n            # flatten the first two dimensions\n            patches = patches.reshape(np.prod(patches.shape[:2]), *patches.shape[2:])\n            # make it a contiguous array\n            patches = np.ascontiguousarray(patches)\n        else:\n            raise ValueError(f\"Input type [{type(image)}] is not supported.\")\n\n        return patches\n\n    def get_params(self, image_size):\n        if self.patch_size is None:\n            patch_size = tuple(image_size[i] // self.grid_size[i] for i in range(2))\n        else:\n            patch_size = self.patch_size\n\n        steps = tuple(\n            (image_size[i] - patch_size[i]) // (self.grid_size[i] - 1) if self.grid_size[i] > 1 else image_size[i]\n            for i in range(2)\n        )\n\n        return patch_size, steps\n\n\nclass TileOnGrid(Randomizable, Transform):\n    \"\"\"\n    Tile the 2D image into patches on a grid and maintain a subset of it.\n    This transform works only with np.ndarray inputs for 2D images.\n\n    Args:\n        tile_count: number of tiles to extract, if None extracts all non-background tiles\n            Defaults to ``None``.\n        tile_size: size of the square tile\n            Defaults to ``256``.\n        step: step size\n            Defaults to ``None`` (same as tile_size)\n        random_offset: Randomize position of the grid, instead of starting from the top-left corner\n            Defaults to ``False``.\n        pad_full: pad image to the size evenly divisible by tile_size\n            Defaults to ``False``.\n        background_val: the background constant (e.g. 255 for white background)\n            Defaults to ``255``.\n        filter_mode: mode must be in [\"min\", \"max\", \"random\"]. If total number of tiles is more than tile_size,\n            then sort by intensity sum, and take the smallest (for min), largest (for max) or random (for random) subset\n            Defaults to ``min`` (which assumes background is high value)\n\n    \"\"\"\n\n    def __init__(\n        self,\n        tile_count: Optional[int] = None,\n        tile_size: int = 256,\n        step: Optional[int] = None,\n        random_offset: bool = False,\n        pad_full: bool = False,\n        background_val: int = 255,\n        filter_mode: str = \"min\",\n    ):\n        self.tile_count = tile_count\n        self.tile_size = tile_size\n        self.random_offset = random_offset\n        self.pad_full = pad_full\n        self.background_val = background_val\n        self.filter_mode = filter_mode\n\n        if step is None:\n            # non-overlapping grid\n            self.step = self.tile_size\n        else:\n            self.step = step\n\n        self.offset = (0, 0)\n        self.random_idxs = np.array((0,))\n\n        if self.filter_mode not in [\"min\", \"max\", \"random\"]:\n            raise ValueError(\"Unsupported filter_mode, must be [min, max or random]: \" + str(self.filter_mode))\n\n    def randomize(self, img_size: Sequence[int]) -> None:\n\n        c, h, w = img_size\n\n        self.offset = (0, 0)\n        if self.random_offset:\n            pad_h = h % self.tile_size\n            pad_w = w % self.tile_size\n            self.offset = (self.R.randint(pad_h) if pad_h > 0 else 0, self.R.randint(pad_w) if pad_w > 0 else 0)\n            h = h - self.offset[0]\n            w = w - self.offset[1]\n\n        if self.pad_full:\n            pad_h = (self.tile_size - h % self.tile_size) % self.tile_size\n            pad_w = (self.tile_size - w % self.tile_size) % self.tile_size\n            h = h + pad_h\n            w = w + pad_w\n\n        h_n = (h - self.tile_size + self.step) // self.step\n        w_n = (w - self.tile_size + self.step) // self.step\n        tile_total = h_n * w_n\n\n        if self.tile_count is not None and tile_total > self.tile_count:\n            self.random_idxs = self.R.choice(range(tile_total), self.tile_count, replace=False)\n        else:\n            self.random_idxs = np.array((0,))\n\n    def __call__(self, image: np.ndarray) -> np.ndarray:\n\n        # add random offset\n        self.randomize(img_size=image.shape)\n\n        if self.random_offset and (self.offset[0] > 0 or self.offset[1] > 0):\n            image = image[:, self.offset[0] :, self.offset[1] :]\n\n        # pad to full size, divisible by tile_size\n        if self.pad_full:\n            c, h, w = image.shape\n            pad_h = (self.tile_size - h % self.tile_size) % self.tile_size\n            pad_w = (self.tile_size - w % self.tile_size) % self.tile_size\n            image = np.pad(\n                image,\n                [[0, 0], [pad_h // 2, pad_h - pad_h // 2], [pad_w // 2, pad_w - pad_w // 2]],\n                constant_values=self.background_val,\n            )\n\n        # extact tiles\n        x_step, y_step = self.step, self.step\n        x_size, y_size = self.tile_size, self.tile_size\n        c_len, x_len, y_len = image.shape\n        c_stride, x_stride, y_stride = image.strides\n        llw = as_strided(\n            image,\n            shape=((x_len - x_size) // x_step + 1, (y_len - y_size) // y_step + 1, c_len, x_size, y_size),\n            strides=(x_stride * x_step, y_stride * y_step, c_stride, x_stride, y_stride),\n            writeable=False,\n        )\n        image = llw.reshape(-1, c_len, x_size, y_size)\n\n        # if keeping all patches\n        if self.tile_count is None:\n            # retain only patches with significant foreground content to speed up inference\n            # FYI, this returns a variable number of tiles, so the batch_size must be 1 (per gpu), e.g during inference\n            thresh = 0.999 * 3 * self.background_val * self.tile_size * self.tile_size\n            if self.filter_mode == \"min\":\n                # default, keep non-background tiles (small values)\n                idxs = np.argwhere(image.sum(axis=(1, 2, 3)) < thresh)\n                image = image[idxs.reshape(-1)]\n            elif self.filter_mode == \"max\":\n                idxs = np.argwhere(image.sum(axis=(1, 2, 3)) >= thresh)\n                image = image[idxs.reshape(-1)]\n\n        else:\n            if len(image) > self.tile_count:\n\n                if self.filter_mode == \"min\":\n                    # default, keep non-background tiles (smallest values)\n                    idxs = np.argsort(image.sum(axis=(1, 2, 3)))[: self.tile_count]\n                    image = image[idxs]\n                elif self.filter_mode == \"max\":\n                    idxs = np.argsort(image.sum(axis=(1, 2, 3)))[-self.tile_count :]\n                    image = image[idxs]\n                else:\n                    # random subset (more appropriate for WSIs without distinct background)\n                    if self.random_idxs is not None:\n                        image = image[self.random_idxs]\n\n            elif len(image) < self.tile_count:\n                image = np.pad(\n                    image,\n                    [[0, self.tile_count - len(image)], [0, 0], [0, 0], [0, 0]],\n                    constant_values=self.background_val,\n                )\n\n        return image\n","new_file_content":"# Copyright 2020 - 2021 MONAI Consortium\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#     http://www.apache.org/licenses/LICENSE-2.0\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom typing import Optional, Sequence, Tuple, Union\n\nimport numpy as np\nimport torch\nfrom numpy.lib.stride_tricks import as_strided\n\nfrom monai.config.type_definitions import NdarrayOrTensor\nfrom monai.transforms.transform import Randomizable, Transform\nfrom monai.utils import convert_data_type, convert_to_dst_type\nfrom monai.utils.enums import TransformBackends\n\n__all__ = [\"SplitOnGrid\", \"TileOnGrid\"]\n\n\nclass SplitOnGrid(Transform):\n    \"\"\"\n    Split the image into patches based on the provided grid shape.\n    This transform works only with torch.Tensor inputs.\n\n    Args:\n        grid_shape: a tuple or an integer define the shape of the grid upon which to extract patches.\n            If it's an integer, the value will be repeated for each dimension. Default is 2x2\n        patch_size: a tuple or an integer that defines the output patch sizes.\n            If it's an integer, the value will be repeated for each dimension.\n            The default is (0, 0), where the patch size will be inferred from the grid shape.\n\n    Note: the shape of the input image is inferred based on the first image used.\n    \"\"\"\n\n    backend = [TransformBackends.TORCH, TransformBackends.NUMPY]\n\n    def __init__(\n        self, grid_size: Union[int, Tuple[int, int]] = (2, 2), patch_size: Optional[Union[int, Tuple[int, int]]] = None\n    ):\n        # Grid size\n        if isinstance(grid_size, int):\n            self.grid_size = (grid_size, grid_size)\n        else:\n            self.grid_size = grid_size\n        # Patch size\n        self.patch_size = None\n        if isinstance(patch_size, int):\n            self.patch_size = (patch_size, patch_size)\n        else:\n            self.patch_size = patch_size\n\n    def __call__(self, image: NdarrayOrTensor) -> NdarrayOrTensor:\n        if self.grid_size == (1, 1) and self.patch_size is None:\n            if isinstance(image, torch.Tensor):\n                return torch.stack([image])\n            elif isinstance(image, np.ndarray):\n                return np.stack([image])\n            else:\n                raise ValueError(f\"Input type [{type(image)}] is not supported.\")\n\n        patch_size, steps = self.get_params(image.shape[1:])\n        patches: NdarrayOrTensor\n        if isinstance(image, torch.Tensor):\n            patches = (\n                image.unfold(1, patch_size[0], steps[0])\n                .unfold(2, patch_size[1], steps[1])\n                .flatten(1, 2)\n                .transpose(0, 1)\n                .contiguous()\n            )\n        elif isinstance(image, np.ndarray):\n            h_step, w_step = steps\n            c_stride, h_stride, w_stride = image.strides\n            patches = as_strided(\n                image,\n                shape=(*self.grid_size, 3, patch_size[0], patch_size[1]),\n                strides=(h_stride * h_step, w_stride * w_step, c_stride, h_stride, w_stride),\n                writeable=False,\n            )\n            # flatten the first two dimensions\n            patches = patches.reshape(np.prod(patches.shape[:2]), *patches.shape[2:])\n            # make it a contiguous array\n            patches = np.ascontiguousarray(patches)\n        else:\n            raise ValueError(f\"Input type [{type(image)}] is not supported.\")\n\n        return patches\n\n    def get_params(self, image_size):\n        if self.patch_size is None:\n            patch_size = tuple(image_size[i] // self.grid_size[i] for i in range(2))\n        else:\n            patch_size = self.patch_size\n\n        steps = tuple(\n            (image_size[i] - patch_size[i]) // (self.grid_size[i] - 1) if self.grid_size[i] > 1 else image_size[i]\n            for i in range(2)\n        )\n\n        return patch_size, steps\n\n\nclass TileOnGrid(Randomizable, Transform):\n    \"\"\"\n    Tile the 2D image into patches on a grid and maintain a subset of it.\n    This transform works only with np.ndarray inputs for 2D images.\n\n    Args:\n        tile_count: number of tiles to extract, if None extracts all non-background tiles\n            Defaults to ``None``.\n        tile_size: size of the square tile\n            Defaults to ``256``.\n        step: step size\n            Defaults to ``None`` (same as tile_size)\n        random_offset: Randomize position of the grid, instead of starting from the top-left corner\n            Defaults to ``False``.\n        pad_full: pad image to the size evenly divisible by tile_size\n            Defaults to ``False``.\n        background_val: the background constant (e.g. 255 for white background)\n            Defaults to ``255``.\n        filter_mode: mode must be in [\"min\", \"max\", \"random\"]. If total number of tiles is more than tile_size,\n            then sort by intensity sum, and take the smallest (for min), largest (for max) or random (for random) subset\n            Defaults to ``min`` (which assumes background is high value)\n\n    \"\"\"\n\n    backend = [TransformBackends.NUMPY]\n\n    def __init__(\n        self,\n        tile_count: Optional[int] = None,\n        tile_size: int = 256,\n        step: Optional[int] = None,\n        random_offset: bool = False,\n        pad_full: bool = False,\n        background_val: int = 255,\n        filter_mode: str = \"min\",\n    ):\n        self.tile_count = tile_count\n        self.tile_size = tile_size\n        self.random_offset = random_offset\n        self.pad_full = pad_full\n        self.background_val = background_val\n        self.filter_mode = filter_mode\n\n        if step is None:\n            # non-overlapping grid\n            self.step = self.tile_size\n        else:\n            self.step = step\n\n        self.offset = (0, 0)\n        self.random_idxs = np.array((0,))\n\n        if self.filter_mode not in [\"min\", \"max\", \"random\"]:\n            raise ValueError(\"Unsupported filter_mode, must be [min, max or random]: \" + str(self.filter_mode))\n\n    def randomize(self, img_size: Sequence[int]) -> None:\n\n        c, h, w = img_size\n\n        self.offset = (0, 0)\n        if self.random_offset:\n            pad_h = h % self.tile_size\n            pad_w = w % self.tile_size\n            self.offset = (self.R.randint(pad_h) if pad_h > 0 else 0, self.R.randint(pad_w) if pad_w > 0 else 0)\n            h = h - self.offset[0]\n            w = w - self.offset[1]\n\n        if self.pad_full:\n            pad_h = (self.tile_size - h % self.tile_size) % self.tile_size\n            pad_w = (self.tile_size - w % self.tile_size) % self.tile_size\n            h = h + pad_h\n            w = w + pad_w\n\n        h_n = (h - self.tile_size + self.step) // self.step\n        w_n = (w - self.tile_size + self.step) // self.step\n        tile_total = h_n * w_n\n\n        if self.tile_count is not None and tile_total > self.tile_count:\n            self.random_idxs = self.R.choice(range(tile_total), self.tile_count, replace=False)\n        else:\n            self.random_idxs = np.array((0,))\n\n    def __call__(self, image: NdarrayOrTensor) -> NdarrayOrTensor:\n        img_np: np.ndarray\n        img_np, *_ = convert_data_type(image, np.ndarray)  # type: ignore\n\n        # add random offset\n        self.randomize(img_size=img_np.shape)\n\n        if self.random_offset and (self.offset[0] > 0 or self.offset[1] > 0):\n            img_np = img_np[:, self.offset[0] :, self.offset[1] :]\n\n        # pad to full size, divisible by tile_size\n        if self.pad_full:\n            c, h, w = img_np.shape\n            pad_h = (self.tile_size - h % self.tile_size) % self.tile_size\n            pad_w = (self.tile_size - w % self.tile_size) % self.tile_size\n            img_np = np.pad(\n                img_np,\n                [[0, 0], [pad_h // 2, pad_h - pad_h // 2], [pad_w // 2, pad_w - pad_w // 2]],\n                constant_values=self.background_val,\n            )\n\n        # extact tiles\n        h_step, w_step = self.step, self.step\n        h_size, w_size = self.tile_size, self.tile_size\n        c_len, h_len, w_len = img_np.shape\n        c_stride, h_stride, w_stride = img_np.strides\n        llw = as_strided(\n            img_np,\n            shape=((h_len - h_size) // h_step + 1, (w_len - w_size) // w_step + 1, c_len, h_size, w_size),\n            strides=(h_stride * h_step, w_stride * w_step, c_stride, h_stride, w_stride),\n            writeable=False,\n        )\n        img_np = llw.reshape(-1, c_len, h_size, w_size)\n\n        # if keeping all patches\n        if self.tile_count is None:\n            # retain only patches with significant foreground content to speed up inference\n            # FYI, this returns a variable number of tiles, so the batch_size must be 1 (per gpu), e.g during inference\n            thresh = 0.999 * 3 * self.background_val * self.tile_size * self.tile_size\n            if self.filter_mode == \"min\":\n                # default, keep non-background tiles (small values)\n                idxs = np.argwhere(img_np.sum(axis=(1, 2, 3)) < thresh)\n                img_np = img_np[idxs.reshape(-1)]\n            elif self.filter_mode == \"max\":\n                idxs = np.argwhere(img_np.sum(axis=(1, 2, 3)) >= thresh)\n                img_np = img_np[idxs.reshape(-1)]\n\n        else:\n            if len(img_np) > self.tile_count:\n\n                if self.filter_mode == \"min\":\n                    # default, keep non-background tiles (smallest values)\n                    idxs = np.argsort(img_np.sum(axis=(1, 2, 3)))[: self.tile_count]\n                    img_np = img_np[idxs]\n                elif self.filter_mode == \"max\":\n                    idxs = np.argsort(img_np.sum(axis=(1, 2, 3)))[-self.tile_count :]\n                    img_np = img_np[idxs]\n                else:\n                    # random subset (more appropriate for WSIs without distinct background)\n                    if self.random_idxs is not None:\n                        img_np = img_np[self.random_idxs]\n\n            elif len(img_np) < self.tile_count:\n                img_np = np.pad(\n                    img_np,\n                    [[0, self.tile_count - len(img_np)], [0, 0], [0, 0], [0, 0]],\n                    constant_values=self.background_val,\n                )\n\n        image, *_ = convert_to_dst_type(src=img_np, dst=image, dtype=image.dtype)\n\n        return image\n","header":{"file":{"path":"monai/apps/pathology/transforms/spatial/array.py"},"misc_line":null},"index_line":null,"is_binary_file":false,"binary_line":null,"minus_file":{"path":"a/monai/apps/pathology/transforms/spatial/array.py"},"plus_file":{"path":"b/monai/apps/pathology/transforms/spatial/array.py"},"hunks":[{"descriptor":{"old_range":{"start":17,"length":6},"new_range":{"start":17,"length":7},"section":""},"line_group":{"all_lines":[{"content":"","type":"context"},{"content":"from monai.config.type_definitions import NdarrayOrTensor","type":"context"},{"content":"from monai.transforms.transform import Randomizable, Transform","type":"context"},{"content":"from monai.utils import convert_data_type, convert_to_dst_type","type":"added"},{"content":"from monai.utils.enums import TransformBackends","type":"context"},{"content":"","type":"context"},{"content":"__all__ = [\"SplitOnGrid\", \"TileOnGrid\"]","type":"context"}]},"modified_entities":[],"added_entities":[],"deleted_entities":[]},{"descriptor":{"old_range":{"start":129,"length":6},"new_range":{"start":130,"length":8},"section":"class TileOnGrid(Randomizable, Transform):"},"line_group":{"all_lines":[{"content":"","type":"context"},{"content":"    \"\"\"","type":"context"},{"content":"","type":"context"},{"content":"    backend = [TransformBackends.NUMPY]","type":"added"},{"content":"","type":"added"},{"content":"    def __init__(","type":"context"},{"content":"        self,","type":"context"},{"content":"        tile_count: Optional[int] = None,","type":"context"}]},"modified_entities":[],"added_entities":[],"deleted_entities":[]},{"descriptor":{"old_range":{"start":185,"length":37},"new_range":{"start":188,"length":39},"section":"def randomize(self, img_size: Sequence[int]) -> None:"},"line_group":{"all_lines":[{"content":"        else:","type":"context"},{"content":"            self.random_idxs = np.array((0,))","type":"context"},{"content":"","type":"context"},{"content":"    def __call__(self, image: np.ndarray) -> np.ndarray:","type":"deleted"},{"content":"    def __call__(self, image: NdarrayOrTensor) -> NdarrayOrTensor:","type":"added"},{"content":"        img_np: np.ndarray","type":"added"},{"content":"        img_np, *_ = convert_data_type(image, np.ndarray)  # type: ignore","type":"added"},{"content":"","type":"context"},{"content":"        # add random offset","type":"context"},{"content":"        self.randomize(img_size=image.shape)","type":"deleted"},{"content":"        self.randomize(img_size=img_np.shape)","type":"added"},{"content":"","type":"context"},{"content":"        if self.random_offset and (self.offset[0] > 0 or self.offset[1] > 0):","type":"context"},{"content":"            image = image[:, self.offset[0] :, self.offset[1] :]","type":"deleted"},{"content":"            img_np = img_np[:, self.offset[0] :, self.offset[1] :]","type":"added"},{"content":"","type":"context"},{"content":"        # pad to full size, divisible by tile_size","type":"context"},{"content":"        if self.pad_full:","type":"context"},{"content":"            c, h, w = image.shape","type":"deleted"},{"content":"            c, h, w = img_np.shape","type":"added"},{"content":"            pad_h = (self.tile_size - h % self.tile_size) % self.tile_size","type":"context"},{"content":"            pad_w = (self.tile_size - w % self.tile_size) % self.tile_size","type":"context"},{"content":"            image = np.pad(","type":"deleted"},{"content":"                image,","type":"deleted"},{"content":"            img_np = np.pad(","type":"added"},{"content":"                img_np,","type":"added"},{"content":"                [[0, 0], [pad_h // 2, pad_h - pad_h // 2], [pad_w // 2, pad_w - pad_w // 2]],","type":"context"},{"content":"                constant_values=self.background_val,","type":"context"},{"content":"            )","type":"context"},{"content":"","type":"context"},{"content":"        # extact tiles","type":"context"},{"content":"        x_step, y_step = self.step, self.step","type":"deleted"},{"content":"        x_size, y_size = self.tile_size, self.tile_size","type":"deleted"},{"content":"        c_len, x_len, y_len = image.shape","type":"deleted"},{"content":"        c_stride, x_stride, y_stride = image.strides","type":"deleted"},{"content":"        h_step, w_step = self.step, self.step","type":"added"},{"content":"        h_size, w_size = self.tile_size, self.tile_size","type":"added"},{"content":"        c_len, h_len, w_len = img_np.shape","type":"added"},{"content":"        c_stride, h_stride, w_stride = img_np.strides","type":"added"},{"content":"        llw = as_strided(","type":"context"},{"content":"            image,","type":"deleted"},{"content":"            shape=((x_len - x_size) // x_step + 1, (y_len - y_size) // y_step + 1, c_len, x_size, y_size),","type":"deleted"},{"content":"            strides=(x_stride * x_step, y_stride * y_step, c_stride, x_stride, y_stride),","type":"deleted"},{"content":"            img_np,","type":"added"},{"content":"            shape=((h_len - h_size) // h_step + 1, (w_len - w_size) // w_step + 1, c_len, h_size, w_size),","type":"added"},{"content":"            strides=(h_stride * h_step, w_stride * w_step, c_stride, h_stride, w_stride),","type":"added"},{"content":"            writeable=False,","type":"context"},{"content":"        )","type":"context"},{"content":"        image = llw.reshape(-1, c_len, x_size, y_size)","type":"deleted"},{"content":"        img_np = llw.reshape(-1, c_len, h_size, w_size)","type":"added"},{"content":"","type":"context"},{"content":"        # if keeping all patches","type":"context"},{"content":"        if self.tile_count is None:","type":"context"}]},"modified_entities":[],"added_entities":[],"deleted_entities":[]},{"descriptor":{"old_range":{"start":224,"length":32},"new_range":{"start":229,"length":34},"section":"def __call__(self, image: np.ndarray) -> np.ndarray:"},"line_group":{"all_lines":[{"content":"            thresh = 0.999 * 3 * self.background_val * self.tile_size * self.tile_size","type":"context"},{"content":"            if self.filter_mode == \"min\":","type":"context"},{"content":"                # default, keep non-background tiles (small values)","type":"context"},{"content":"                idxs = np.argwhere(image.sum(axis=(1, 2, 3)) < thresh)","type":"deleted"},{"content":"                image = image[idxs.reshape(-1)]","type":"deleted"},{"content":"                idxs = np.argwhere(img_np.sum(axis=(1, 2, 3)) < thresh)","type":"added"},{"content":"                img_np = img_np[idxs.reshape(-1)]","type":"added"},{"content":"            elif self.filter_mode == \"max\":","type":"context"},{"content":"                idxs = np.argwhere(image.sum(axis=(1, 2, 3)) >= thresh)","type":"deleted"},{"content":"                image = image[idxs.reshape(-1)]","type":"deleted"},{"content":"                idxs = np.argwhere(img_np.sum(axis=(1, 2, 3)) >= thresh)","type":"added"},{"content":"                img_np = img_np[idxs.reshape(-1)]","type":"added"},{"content":"","type":"context"},{"content":"        else:","type":"context"},{"content":"            if len(image) > self.tile_count:","type":"deleted"},{"content":"            if len(img_np) > self.tile_count:","type":"added"},{"content":"","type":"context"},{"content":"                if self.filter_mode == \"min\":","type":"context"},{"content":"                    # default, keep non-background tiles (smallest values)","type":"context"},{"content":"                    idxs = np.argsort(image.sum(axis=(1, 2, 3)))[: self.tile_count]","type":"deleted"},{"content":"                    image = image[idxs]","type":"deleted"},{"content":"                    idxs = np.argsort(img_np.sum(axis=(1, 2, 3)))[: self.tile_count]","type":"added"},{"content":"                    img_np = img_np[idxs]","type":"added"},{"content":"                elif self.filter_mode == \"max\":","type":"context"},{"content":"                    idxs = np.argsort(image.sum(axis=(1, 2, 3)))[-self.tile_count :]","type":"deleted"},{"content":"                    image = image[idxs]","type":"deleted"},{"content":"                    idxs = np.argsort(img_np.sum(axis=(1, 2, 3)))[-self.tile_count :]","type":"added"},{"content":"                    img_np = img_np[idxs]","type":"added"},{"content":"                else:","type":"context"},{"content":"                    # random subset (more appropriate for WSIs without distinct background)","type":"context"},{"content":"                    if self.random_idxs is not None:","type":"context"},{"content":"                        image = image[self.random_idxs]","type":"deleted"},{"content":"                        img_np = img_np[self.random_idxs]","type":"added"},{"content":"","type":"context"},{"content":"            elif len(image) < self.tile_count:","type":"deleted"},{"content":"                image = np.pad(","type":"deleted"},{"content":"                    image,","type":"deleted"},{"content":"                    [[0, self.tile_count - len(image)], [0, 0], [0, 0], [0, 0]],","type":"deleted"},{"content":"            elif len(img_np) < self.tile_count:","type":"added"},{"content":"                img_np = np.pad(","type":"added"},{"content":"                    img_np,","type":"added"},{"content":"                    [[0, self.tile_count - len(img_np)], [0, 0], [0, 0], [0, 0]],","type":"added"},{"content":"                    constant_values=self.background_val,","type":"context"},{"content":"                )","type":"context"},{"content":"","type":"context"},{"content":"        image, *_ = convert_to_dst_type(src=img_np, dst=image, dtype=image.dtype)","type":"added"},{"content":"","type":"added"},{"content":"        return image","type":"context"}]},"modified_entities":[],"added_entities":[],"deleted_entities":[]}]},{"old_file_content":"# Copyright 2020 - 2021 MONAI Consortium\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#     http://www.apache.org/licenses/LICENSE-2.0\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport copy\nfrom typing import Any, Dict, Hashable, List, Mapping, Optional, Tuple, Union\n\nimport numpy as np\n\nfrom monai.config import KeysCollection\nfrom monai.config.type_definitions import NdarrayOrTensor\nfrom monai.transforms.transform import MapTransform, Randomizable\n\nfrom .array import SplitOnGrid, TileOnGrid\n\n__all__ = [\"SplitOnGridd\", \"SplitOnGridD\", \"SplitOnGridDict\", \"TileOnGridd\", \"TileOnGridD\", \"TileOnGridDict\"]\n\n\nclass SplitOnGridd(MapTransform):\n    \"\"\"\n    Split the image into patches based on the provided grid shape.\n    This transform works only with torch.Tensor inputs.\n\n    Args:\n        grid_size: a tuple or an integer define the shape of the grid upon which to extract patches.\n            If it's an integer, the value will be repeated for each dimension. Default is 2x2\n        patch_size: a tuple or an integer that defines the output patch sizes.\n            If it's an integer, the value will be repeated for each dimension.\n            The default is (0, 0), where the patch size will be inferred from the grid shape.\n\n    Note: the shape of the input image is inferred based on the first image used.\n    \"\"\"\n\n    backend = SplitOnGrid.backend\n\n    def __init__(\n        self,\n        keys: KeysCollection,\n        grid_size: Union[int, Tuple[int, int]] = (2, 2),\n        patch_size: Optional[Union[int, Tuple[int, int]]] = None,\n        allow_missing_keys: bool = False,\n    ):\n        super().__init__(keys, allow_missing_keys)\n        self.splitter = SplitOnGrid(grid_size=grid_size, patch_size=patch_size)\n\n    def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> Dict[Hashable, NdarrayOrTensor]:\n        d = dict(data)\n        for key in self.key_iterator(d):\n            d[key] = self.splitter(d[key])\n        return d\n\n\nclass TileOnGridd(Randomizable, MapTransform):\n    \"\"\"\n    Tile the 2D image into patches on a grid and maintain a subset of it.\n    This transform works only with np.ndarray inputs for 2D images.\n\n    Args:\n        tile_count: number of tiles to extract, if None extracts all non-background tiles\n            Defaults to ``None``.\n        tile_size: size of the square tile\n            Defaults to ``256``.\n        step: step size\n            Defaults to ``None`` (same as tile_size)\n        random_offset: Randomize position of the grid, instead of starting from the top-left corner\n            Defaults to ``False``.\n        pad_full: pad image to the size evenly divisible by tile_size\n            Defaults to ``False``.\n        background_val: the background constant (e.g. 255 for white background)\n            Defaults to ``255``.\n        filter_mode: mode must be in [\"min\", \"max\", \"random\"]. If total number of tiles is more than tile_size,\n            then sort by intensity sum, and take the smallest (for min), largest (for max) or random (for random) subset\n            Defaults to ``min`` (which assumes background is high value)\n\n    \"\"\"\n\n    def __init__(\n        self,\n        keys: KeysCollection,\n        tile_count: Optional[int] = None,\n        tile_size: int = 256,\n        step: Optional[int] = None,\n        random_offset: bool = False,\n        pad_full: bool = False,\n        background_val: int = 255,\n        filter_mode: str = \"min\",\n        allow_missing_keys: bool = False,\n        return_list_of_dicts: bool = False,\n    ):\n        super().__init__(keys, allow_missing_keys)\n\n        self.return_list_of_dicts = return_list_of_dicts\n        self.seed = None\n\n        self.splitter = TileOnGrid(\n            tile_count=tile_count,\n            tile_size=tile_size,\n            step=step,\n            random_offset=random_offset,\n            pad_full=pad_full,\n            background_val=background_val,\n            filter_mode=filter_mode,\n        )\n\n    def randomize(self, data: Any = None) -> None:\n        self.seed = self.R.randint(10000)  # type: ignore\n\n    def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Union[Dict[Hashable, np.ndarray], List[Dict]]:\n\n        self.randomize()\n\n        d = dict(data)\n        for key in self.key_iterator(d):\n            self.splitter.set_random_state(seed=self.seed)  # same random seed for all keys\n            d[key] = self.splitter(d[key])\n\n        if self.return_list_of_dicts:\n            d_list = []\n            for i in range(len(d[self.keys[0]])):\n                d_list.append({k: d[k][i] if k in self.keys else copy.deepcopy(d[k]) for k in d.keys()})\n            d = d_list  # type: ignore\n\n        return d\n\n\nSplitOnGridDict = SplitOnGridD = SplitOnGridd\nTileOnGridDict = TileOnGridD = TileOnGridd\n","new_file_content":"# Copyright 2020 - 2021 MONAI Consortium\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#     http://www.apache.org/licenses/LICENSE-2.0\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport copy\nfrom typing import Any, Dict, Hashable, List, Mapping, Optional, Tuple, Union\n\nfrom monai.config import KeysCollection\nfrom monai.config.type_definitions import NdarrayOrTensor\nfrom monai.transforms.transform import MapTransform, Randomizable\n\nfrom .array import SplitOnGrid, TileOnGrid\n\n__all__ = [\"SplitOnGridd\", \"SplitOnGridD\", \"SplitOnGridDict\", \"TileOnGridd\", \"TileOnGridD\", \"TileOnGridDict\"]\n\n\nclass SplitOnGridd(MapTransform):\n    \"\"\"\n    Split the image into patches based on the provided grid shape.\n    This transform works only with torch.Tensor inputs.\n\n    Args:\n        grid_size: a tuple or an integer define the shape of the grid upon which to extract patches.\n            If it's an integer, the value will be repeated for each dimension. Default is 2x2\n        patch_size: a tuple or an integer that defines the output patch sizes.\n            If it's an integer, the value will be repeated for each dimension.\n            The default is (0, 0), where the patch size will be inferred from the grid shape.\n\n    Note: the shape of the input image is inferred based on the first image used.\n    \"\"\"\n\n    backend = SplitOnGrid.backend\n\n    def __init__(\n        self,\n        keys: KeysCollection,\n        grid_size: Union[int, Tuple[int, int]] = (2, 2),\n        patch_size: Optional[Union[int, Tuple[int, int]]] = None,\n        allow_missing_keys: bool = False,\n    ):\n        super().__init__(keys, allow_missing_keys)\n        self.splitter = SplitOnGrid(grid_size=grid_size, patch_size=patch_size)\n\n    def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> Dict[Hashable, NdarrayOrTensor]:\n        d = dict(data)\n        for key in self.key_iterator(d):\n            d[key] = self.splitter(d[key])\n        return d\n\n\nclass TileOnGridd(Randomizable, MapTransform):\n    \"\"\"\n    Tile the 2D image into patches on a grid and maintain a subset of it.\n    This transform works only with np.ndarray inputs for 2D images.\n\n    Args:\n        tile_count: number of tiles to extract, if None extracts all non-background tiles\n            Defaults to ``None``.\n        tile_size: size of the square tile\n            Defaults to ``256``.\n        step: step size\n            Defaults to ``None`` (same as tile_size)\n        random_offset: Randomize position of the grid, instead of starting from the top-left corner\n            Defaults to ``False``.\n        pad_full: pad image to the size evenly divisible by tile_size\n            Defaults to ``False``.\n        background_val: the background constant (e.g. 255 for white background)\n            Defaults to ``255``.\n        filter_mode: mode must be in [\"min\", \"max\", \"random\"]. If total number of tiles is more than tile_size,\n            then sort by intensity sum, and take the smallest (for min), largest (for max) or random (for random) subset\n            Defaults to ``min`` (which assumes background is high value)\n\n    \"\"\"\n\n    backend = SplitOnGrid.backend\n\n    def __init__(\n        self,\n        keys: KeysCollection,\n        tile_count: Optional[int] = None,\n        tile_size: int = 256,\n        step: Optional[int] = None,\n        random_offset: bool = False,\n        pad_full: bool = False,\n        background_val: int = 255,\n        filter_mode: str = \"min\",\n        allow_missing_keys: bool = False,\n        return_list_of_dicts: bool = False,\n    ):\n        super().__init__(keys, allow_missing_keys)\n\n        self.return_list_of_dicts = return_list_of_dicts\n        self.seed = None\n\n        self.splitter = TileOnGrid(\n            tile_count=tile_count,\n            tile_size=tile_size,\n            step=step,\n            random_offset=random_offset,\n            pad_full=pad_full,\n            background_val=background_val,\n            filter_mode=filter_mode,\n        )\n\n    def randomize(self, data: Any = None) -> None:\n        self.seed = self.R.randint(10000)  # type: ignore\n\n    def __call__(\n        self, data: Mapping[Hashable, NdarrayOrTensor]\n    ) -> Union[Dict[Hashable, NdarrayOrTensor], List[Dict[Hashable, NdarrayOrTensor]]]:\n\n        self.randomize()\n\n        d = dict(data)\n        for key in self.key_iterator(d):\n            self.splitter.set_random_state(seed=self.seed)  # same random seed for all keys\n            d[key] = self.splitter(d[key])\n\n        if self.return_list_of_dicts:\n            d_list = []\n            for i in range(len(d[self.keys[0]])):\n                d_list.append({k: d[k][i] if k in self.keys else copy.deepcopy(d[k]) for k in d.keys()})\n            d = d_list  # type: ignore\n\n        return d\n\n\nSplitOnGridDict = SplitOnGridD = SplitOnGridd\nTileOnGridDict = TileOnGridD = TileOnGridd\n","header":{"file":{"path":"monai/apps/pathology/transforms/spatial/dictionary.py"},"misc_line":null},"index_line":null,"is_binary_file":false,"binary_line":null,"minus_file":{"path":"a/monai/apps/pathology/transforms/spatial/dictionary.py"},"plus_file":{"path":"b/monai/apps/pathology/transforms/spatial/dictionary.py"},"hunks":[{"descriptor":{"old_range":{"start":12,"length":8},"new_range":{"start":12,"length":6},"section":""},"line_group":{"all_lines":[{"content":"import copy","type":"context"},{"content":"from typing import Any, Dict, Hashable, List, Mapping, Optional, Tuple, Union","type":"context"},{"content":"","type":"context"},{"content":"import numpy as np","type":"deleted"},{"content":"","type":"deleted"},{"content":"from monai.config import KeysCollection","type":"context"},{"content":"from monai.config.type_definitions import NdarrayOrTensor","type":"context"},{"content":"from monai.transforms.transform import MapTransform, Randomizable","type":"context"}]},"modified_entities":[],"added_entities":[],"deleted_entities":[]},{"descriptor":{"old_range":{"start":81,"length":6},"new_range":{"start":79,"length":8},"section":"class TileOnGridd(Randomizable, MapTransform):"},"line_group":{"all_lines":[{"content":"","type":"context"},{"content":"    \"\"\"","type":"context"},{"content":"","type":"context"},{"content":"    backend = SplitOnGrid.backend","type":"added"},{"content":"","type":"added"},{"content":"    def __init__(","type":"context"},{"content":"        self,","type":"context"},{"content":"        keys: KeysCollection,","type":"context"}]},"modified_entities":[],"added_entities":[],"deleted_entities":[]},{"descriptor":{"old_range":{"start":112,"length":7},"new_range":{"start":112,"length":9},"section":"def __init__("},"line_group":{"all_lines":[{"content":"    def randomize(self, data: Any = None) -> None:","type":"context"},{"content":"        self.seed = self.R.randint(10000)  # type: ignore","type":"context"},{"content":"","type":"context"},{"content":"    def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Union[Dict[Hashable, np.ndarray], List[Dict]]:","type":"deleted"},{"content":"    def __call__(","type":"added"},{"content":"        self, data: Mapping[Hashable, NdarrayOrTensor]","type":"added"},{"content":"    ) -> Union[Dict[Hashable, NdarrayOrTensor], List[Dict[Hashable, NdarrayOrTensor]]]:","type":"added"},{"content":"","type":"context"},{"content":"        self.randomize()","type":"context"},{"content":"","type":"context"},{"content":"","type":"context"}]},"modified_entities":[],"added_entities":[],"deleted_entities":[]}]}],"old_commit_hash":"5adf30beb3581e951556fd8c4f027828bf367a70","new_commit_hash":"722bc65b0e8cbac565643619f7fd1b62363b3866","commit_message":"Add backend to dict version","commit_date":"2021-11-23T02:39:46Z","metadata":{}}
problem_statement: Update TileOnGrid backend
  **Is your feature request related to a problem? Please describe.**
  Update `TileOnGrid` to support input/output type of `torch.Tensor` and `numpy` while its backend is `numpy` only.
  
patch: diff --git a/monai/apps/pathology/transforms/spatial/array.py b/monai/apps/pathology/transforms/spatial/array.py
  --- a/monai/apps/pathology/transforms/spatial/array.py
  +++ b/monai/apps/pathology/transforms/spatial/array.py
  @@ -17,6 +17,7 @@
   
   from monai.config.type_definitions import NdarrayOrTensor
   from monai.transforms.transform import Randomizable, Transform
  +from monai.utils import convert_data_type, convert_to_dst_type
   from monai.utils.enums import TransformBackends
   
   __all__ = ["SplitOnGrid", "TileOnGrid"]
  @@ -129,6 +130,8 @@ class TileOnGrid(Randomizable, Transform):
   
       """
   
  +    backend = [TransformBackends.NUMPY]
  +
       def __init__(
           self,
           tile_count: Optional[int] = None,
  @@ -185,37 +188,39 @@ def randomize(self, img_size: Sequence[int]) -> None:
           else:
               self.random_idxs = np.array((0,))
   
  -    def __call__(self, image: np.ndarray) -> np.ndarray:
  +    def __call__(self, image: NdarrayOrTensor) -> NdarrayOrTensor:
  +        img_np: np.ndarray
  +        img_np, *_ = convert_data_type(image, np.ndarray)  # type: ignore
   
           # add random offset
  -        self.randomize(img_size=image.shape)
  +        self.randomize(img_size=img_np.shape)
   
           if self.random_offset and (self.offset[0] > 0 or self.offset[1] > 0):
  -            image = image[:, self.offset[0] :, self.offset[1] :]
  +            img_np = img_np[:, self.offset[0] :, self.offset[1] :]
   
           # pad to full size, divisible by tile_size
           if self.pad_full:
  -            c, h, w = image.shape
  +            c, h, w = img_np.shape
               pad_h = (self.tile_size - h % self.tile_size) % self.tile_size
               pad_w = (self.tile_size - w % self.tile_size) % self.tile_size
  -            image = np.pad(
  -                image,
  +            img_np = np.pad(
  +                img_np,
                   [[0, 0], [pad_h // 2, pad_h - pad_h // 2], [pad_w // 2, pad_w - pad_w // 2]],
                   constant_values=self.background_val,
               )
   
           # extact tiles
  -        x_step, y_step = self.step, self.step
  -        x_size, y_size = self.tile_size, self.tile_size
  -        c_len, x_len, y_len = image.shape
  -        c_stride, x_stride, y_stride = image.strides
  +        h_step, w_step = self.step, self.step
  +        h_size, w_size = self.tile_size, self.tile_size
  +        c_len, h_len, w_len = img_np.shape
  +        c_stride, h_stride, w_stride = img_np.strides
           llw = as_strided(
  -            image,
  -            shape=((x_len - x_size) // x_step + 1, (y_len - y_size) // y_step + 1, c_len, x_size, y_size),
  -            strides=(x_stride * x_step, y_stride * y_step, c_stride, x_stride, y_stride),
  +            img_np,
  +            shape=((h_len - h_size) // h_step + 1, (w_len - w_size) // w_step + 1, c_len, h_size, w_size),
  +            strides=(h_stride * h_step, w_stride * w_step, c_stride, h_stride, w_stride),
               writeable=False,
           )
  -        image = llw.reshape(-1, c_len, x_size, y_size)
  +        img_np = llw.reshape(-1, c_len, h_size, w_size)
   
           # if keeping all patches
           if self.tile_count is None:
  @@ -224,32 +229,34 @@ def __call__(self, image: np.ndarray) -> np.ndarray:
               thresh = 0.999 * 3 * self.background_val * self.tile_size * self.tile_size
               if self.filter_mode == "min":
                   # default, keep non-background tiles (small values)
  -                idxs = np.argwhere(image.sum(axis=(1, 2, 3)) < thresh)
  -                image = image[idxs.reshape(-1)]
  +                idxs = np.argwhere(img_np.sum(axis=(1, 2, 3)) < thresh)
  +                img_np = img_np[idxs.reshape(-1)]
               elif self.filter_mode == "max":
  -                idxs = np.argwhere(image.sum(axis=(1, 2, 3)) >= thresh)
  -                image = image[idxs.reshape(-1)]
  +                idxs = np.argwhere(img_np.sum(axis=(1, 2, 3)) >= thresh)
  +                img_np = img_np[idxs.reshape(-1)]
   
           else:
  -            if len(image) > self.tile_count:
  +            if len(img_np) > self.tile_count:
   
                   if self.filter_mode == "min":
                       # default, keep non-background tiles (smallest values)
  -                    idxs = np.argsort(image.sum(axis=(1, 2, 3)))[: self.tile_count]
  -                    image = image[idxs]
  +                    idxs = np.argsort(img_np.sum(axis=(1, 2, 3)))[: self.tile_count]
  +                    img_np = img_np[idxs]
                   elif self.filter_mode == "max":
  -                    idxs = np.argsort(image.sum(axis=(1, 2, 3)))[-self.tile_count :]
  -                    image = image[idxs]
  +                    idxs = np.argsort(img_np.sum(axis=(1, 2, 3)))[-self.tile_count :]
  +                    img_np = img_np[idxs]
                   else:
                       # random subset (more appropriate for WSIs without distinct background)
                       if self.random_idxs is not None:
  -                        image = image[self.random_idxs]
  +                        img_np = img_np[self.random_idxs]
   
  -            elif len(image) < self.tile_count:
  -                image = np.pad(
  -                    image,
  -                    [[0, self.tile_count - len(image)], [0, 0], [0, 0], [0, 0]],
  +            elif len(img_np) < self.tile_count:
  +                img_np = np.pad(
  +                    img_np,
  +                    [[0, self.tile_count - len(img_np)], [0, 0], [0, 0], [0, 0]],
                       constant_values=self.background_val,
                   )
   
  +        image, *_ = convert_to_dst_type(src=img_np, dst=image, dtype=image.dtype)
  +
           return image
  diff --git a/monai/apps/pathology/transforms/spatial/dictionary.py b/monai/apps/pathology/transforms/spatial/dictionary.py
  --- a/monai/apps/pathology/transforms/spatial/dictionary.py
  +++ b/monai/apps/pathology/transforms/spatial/dictionary.py
  @@ -12,8 +12,6 @@
   import copy
   from typing import Any, Dict, Hashable, List, Mapping, Optional, Tuple, Union
   
  -import numpy as np
  -
   from monai.config import KeysCollection
   from monai.config.type_definitions import NdarrayOrTensor
   from monai.transforms.transform import MapTransform, Randomizable
  @@ -81,6 +79,8 @@ class TileOnGridd(Randomizable, MapTransform):
   
       """
   
  +    backend = SplitOnGrid.backend
  +
       def __init__(
           self,
           keys: KeysCollection,
  @@ -112,7 +112,9 @@ def __init__(
       def randomize(self, data: Any = None) -> None:
           self.seed = self.R.randint(10000)  # type: ignore
   
  -    def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Union[Dict[Hashable, np.ndarray], List[Dict]]:
  +    def __call__(
  +        self, data: Mapping[Hashable, NdarrayOrTensor]
  +    ) -> Union[Dict[Hashable, NdarrayOrTensor], List[Dict[Hashable, NdarrayOrTensor]]]:
   
           self.randomize()
   
   
  
file_context: {"monai/apps/pathology/transforms/spatial/array.py": "# Copyright 2020 - 2021 MONAI Consortium\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#     http://www.apache.org/licenses/LICENSE-2.0\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom typing import Optional, Sequence, Tuple, Union\n\nimport numpy as np\nimport torch\nfrom numpy.lib.stride_tricks import as_strided\n\nfrom monai.config.type_definitions import NdarrayOrTensor\nfrom monai.transforms.transform import Randomizable, Transform\nfrom monai.utils.enums import TransformBackends\n\n__all__ = [\"SplitOnGrid\", \"TileOnGrid\"]\n\n\nclass SplitOnGrid(Transform):\n    \"\"\"\n    Split the image into patches based on the provided grid shape.\n    This transform works only with torch.Tensor inputs.\n\n    Args:\n        grid_shape: a tuple or an integer define the shape of the grid upon which to extract patches.\n            If it's an integer, the value will be repeated for each dimension. Default is 2x2\n        patch_size: a tuple or an integer that defines the output patch sizes.\n            If it's an integer, the value will be repeated for each dimension.\n            The default is (0, 0), where the patch size will be inferred from the grid shape.\n\n    Note: the shape of the input image is inferred based on the first image used.\n    \"\"\"\n\n    backend = [TransformBackends.TORCH, TransformBackends.NUMPY]\n\n    def __init__(\n        self, grid_size: Union[int, Tuple[int, int]] = (2, 2), patch_size: Optional[Union[int, Tuple[int, int]]] = None\n    ):\n        # Grid size\n        if isinstance(grid_size, int):\n            self.grid_size = (grid_size, grid_size)\n        else:\n            self.grid_size = grid_size\n        # Patch size\n        self.patch_size = None\n        if isinstance(patch_size, int):\n            self.patch_size = (patch_size, patch_size)\n        else:\n            self.patch_size = patch_size\n\n    def __call__(self, image: NdarrayOrTensor) -> NdarrayOrTensor:\n        if self.grid_size == (1, 1) and self.patch_size is None:\n            if isinstance(image, torch.Tensor):\n                return torch.stack([image])\n            elif isinstance(image, np.ndarray):\n                return np.stack([image])\n            else:\n                raise ValueError(f\"Input type [{type(image)}] is not supported.\")\n\n        patch_size, steps = self.get_params(image.shape[1:])\n        patches: NdarrayOrTensor\n        if isinstance(image, torch.Tensor):\n            patches = (\n                image.unfold(1, patch_size[0], steps[0])\n                .unfold(2, patch_size[1], steps[1])\n                .flatten(1, 2)\n                .transpose(0, 1)\n                .contiguous()\n            )\n        elif isinstance(image, np.ndarray):\n            h_step, w_step = steps\n            c_stride, h_stride, w_stride = image.strides\n            patches = as_strided(\n                image,\n                shape=(*self.grid_size, 3, patch_size[0], patch_size[1]),\n                strides=(h_stride * h_step, w_stride * w_step, c_stride, h_stride, w_stride),\n                writeable=False,\n            )\n            # flatten the first two dimensions\n            patches = patches.reshape(np.prod(patches.shape[:2]), *patches.shape[2:])\n            # make it a contiguous array\n            patches = np.ascontiguousarray(patches)\n        else:\n            raise ValueError(f\"Input type [{type(image)}] is not supported.\")\n\n        return patches\n\n    def get_params(self, image_size):\n        if self.patch_size is None:\n            patch_size = tuple(image_size[i] // self.grid_size[i] for i in range(2))\n        else:\n            patch_size = self.patch_size\n\n        steps = tuple(\n            (image_size[i] - patch_size[i]) // (self.grid_size[i] - 1) if self.grid_size[i] > 1 else image_size[i]\n            for i in range(2)\n        )\n\n        return patch_size, steps\n\n\nclass TileOnGrid(Randomizable, Transform):\n    \"\"\"\n    Tile the 2D image into patches on a grid and maintain a subset of it.\n    This transform works only with np.ndarray inputs for 2D images.\n\n    Args:\n        tile_count: number of tiles to extract, if None extracts all non-background tiles\n            Defaults to ``None``.\n        tile_size: size of the square tile\n            Defaults to ``256``.\n        step: step size\n            Defaults to ``None`` (same as tile_size)\n        random_offset: Randomize position of the grid, instead of starting from the top-left corner\n            Defaults to ``False``.\n        pad_full: pad image to the size evenly divisible by tile_size\n            Defaults to ``False``.\n        background_val: the background constant (e.g. 255 for white background)\n            Defaults to ``255``.\n        filter_mode: mode must be in [\"min\", \"max\", \"random\"]. If total number of tiles is more than tile_size,\n            then sort by intensity sum, and take the smallest (for min), largest (for max) or random (for random) subset\n            Defaults to ``min`` (which assumes background is high value)\n\n    \"\"\"\n\n    def __init__(\n        self,\n        tile_count: Optional[int] = None,\n        tile_size: int = 256,\n        step: Optional[int] = None,\n        random_offset: bool = False,\n        pad_full: bool = False,\n        background_val: int = 255,\n        filter_mode: str = \"min\",\n    ):\n        self.tile_count = tile_count\n        self.tile_size = tile_size\n        self.random_offset = random_offset\n        self.pad_full = pad_full\n        self.background_val = background_val\n        self.filter_mode = filter_mode\n\n        if step is None:\n            # non-overlapping grid\n            self.step = self.tile_size\n        else:\n            self.step = step\n\n        self.offset = (0, 0)\n        self.random_idxs = np.array((0,))\n\n        if self.filter_mode not in [\"min\", \"max\", \"random\"]:\n            raise ValueError(\"Unsupported filter_mode, must be [min, max or random]: \" + str(self.filter_mode))\n\n    def randomize(self, img_size: Sequence[int]) -> None:\n\n        c, h, w = img_size\n\n        self.offset = (0, 0)\n        if self.random_offset:\n            pad_h = h % self.tile_size\n            pad_w = w % self.tile_size\n            self.offset = (self.R.randint(pad_h) if pad_h > 0 else 0, self.R.randint(pad_w) if pad_w > 0 else 0)\n            h = h - self.offset[0]\n            w = w - self.offset[1]\n\n        if self.pad_full:\n            pad_h = (self.tile_size - h % self.tile_size) % self.tile_size\n            pad_w = (self.tile_size - w % self.tile_size) % self.tile_size\n            h = h + pad_h\n            w = w + pad_w\n\n        h_n = (h - self.tile_size + self.step) // self.step\n        w_n = (w - self.tile_size + self.step) // self.step\n        tile_total = h_n * w_n\n\n        if self.tile_count is not None and tile_total > self.tile_count:\n            self.random_idxs = self.R.choice(range(tile_total), self.tile_count, replace=False)\n        else:\n            self.random_idxs = np.array((0,))\n\n    def __call__(self, image: np.ndarray) -> np.ndarray:\n\n        # add random offset\n        self.randomize(img_size=image.shape)\n\n        if self.random_offset and (self.offset[0] > 0 or self.offset[1] > 0):\n            image = image[:, self.offset[0] :, self.offset[1] :]\n\n        # pad to full size, divisible by tile_size\n        if self.pad_full:\n            c, h, w = image.shape\n            pad_h = (self.tile_size - h % self.tile_size) % self.tile_size\n            pad_w = (self.tile_size - w % self.tile_size) % self.tile_size\n            image = np.pad(\n                image,\n                [[0, 0], [pad_h // 2, pad_h - pad_h // 2], [pad_w // 2, pad_w - pad_w // 2]],\n                constant_values=self.background_val,\n            )\n\n        # extact tiles\n        x_step, y_step = self.step, self.step\n        x_size, y_size = self.tile_size, self.tile_size\n        c_len, x_len, y_len = image.shape\n        c_stride, x_stride, y_stride = image.strides\n        llw = as_strided(\n            image,\n            shape=((x_len - x_size) // x_step + 1, (y_len - y_size) // y_step + 1, c_len, x_size, y_size),\n            strides=(x_stride * x_step, y_stride * y_step, c_stride, x_stride, y_stride),\n            writeable=False,\n        )\n        image = llw.reshape(-1, c_len, x_size, y_size)\n\n        # if keeping all patches\n        if self.tile_count is None:\n            # retain only patches with significant foreground content to speed up inference\n            # FYI, this returns a variable number of tiles, so the batch_size must be 1 (per gpu), e.g during inference\n            thresh = 0.999 * 3 * self.background_val * self.tile_size * self.tile_size\n            if self.filter_mode == \"min\":\n                # default, keep non-background tiles (small values)\n                idxs = np.argwhere(image.sum(axis=(1, 2, 3)) < thresh)\n                image = image[idxs.reshape(-1)]\n            elif self.filter_mode == \"max\":\n                idxs = np.argwhere(image.sum(axis=(1, 2, 3)) >= thresh)\n                image = image[idxs.reshape(-1)]\n\n        else:\n            if len(image) > self.tile_count:\n\n                if self.filter_mode == \"min\":\n                    # default, keep non-background tiles (smallest values)\n                    idxs = np.argsort(image.sum(axis=(1, 2, 3)))[: self.tile_count]\n                    image = image[idxs]\n                elif self.filter_mode == \"max\":\n                    idxs = np.argsort(image.sum(axis=(1, 2, 3)))[-self.tile_count :]\n                    image = image[idxs]\n                else:\n                    # random subset (more appropriate for WSIs without distinct background)\n                    if self.random_idxs is not None:\n                        image = image[self.random_idxs]\n\n            elif len(image) < self.tile_count:\n                image = np.pad(\n                    image,\n                    [[0, self.tile_count - len(image)], [0, 0], [0, 0], [0, 0]],\n                    constant_values=self.background_val,\n                )\n\n        return image\n", "monai/apps/pathology/transforms/spatial/dictionary.py": "# Copyright 2020 - 2021 MONAI Consortium\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#     http://www.apache.org/licenses/LICENSE-2.0\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport copy\nfrom typing import Any, Dict, Hashable, List, Mapping, Optional, Tuple, Union\n\nimport numpy as np\n\nfrom monai.config import KeysCollection\nfrom monai.config.type_definitions import NdarrayOrTensor\nfrom monai.transforms.transform import MapTransform, Randomizable\n\nfrom .array import SplitOnGrid, TileOnGrid\n\n__all__ = [\"SplitOnGridd\", \"SplitOnGridD\", \"SplitOnGridDict\", \"TileOnGridd\", \"TileOnGridD\", \"TileOnGridDict\"]\n\n\nclass SplitOnGridd(MapTransform):\n    \"\"\"\n    Split the image into patches based on the provided grid shape.\n    This transform works only with torch.Tensor inputs.\n\n    Args:\n        grid_size: a tuple or an integer define the shape of the grid upon which to extract patches.\n            If it's an integer, the value will be repeated for each dimension. Default is 2x2\n        patch_size: a tuple or an integer that defines the output patch sizes.\n            If it's an integer, the value will be repeated for each dimension.\n            The default is (0, 0), where the patch size will be inferred from the grid shape.\n\n    Note: the shape of the input image is inferred based on the first image used.\n    \"\"\"\n\n    backend = SplitOnGrid.backend\n\n    def __init__(\n        self,\n        keys: KeysCollection,\n        grid_size: Union[int, Tuple[int, int]] = (2, 2),\n        patch_size: Optional[Union[int, Tuple[int, int]]] = None,\n        allow_missing_keys: bool = False,\n    ):\n        super().__init__(keys, allow_missing_keys)\n        self.splitter = SplitOnGrid(grid_size=grid_size, patch_size=patch_size)\n\n    def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> Dict[Hashable, NdarrayOrTensor]:\n        d = dict(data)\n        for key in self.key_iterator(d):\n            d[key] = self.splitter(d[key])\n        return d\n\n\nclass TileOnGridd(Randomizable, MapTransform):\n    \"\"\"\n    Tile the 2D image into patches on a grid and maintain a subset of it.\n    This transform works only with np.ndarray inputs for 2D images.\n\n    Args:\n        tile_count: number of tiles to extract, if None extracts all non-background tiles\n            Defaults to ``None``.\n        tile_size: size of the square tile\n            Defaults to ``256``.\n        step: step size\n            Defaults to ``None`` (same as tile_size)\n        random_offset: Randomize position of the grid, instead of starting from the top-left corner\n            Defaults to ``False``.\n        pad_full: pad image to the size evenly divisible by tile_size\n            Defaults to ``False``.\n        background_val: the background constant (e.g. 255 for white background)\n            Defaults to ``255``.\n        filter_mode: mode must be in [\"min\", \"max\", \"random\"]. If total number of tiles is more than tile_size,\n            then sort by intensity sum, and take the smallest (for min), largest (for max) or random (for random) subset\n            Defaults to ``min`` (which assumes background is high value)\n\n    \"\"\"\n\n    def __init__(\n        self,\n        keys: KeysCollection,\n        tile_count: Optional[int] = None,\n        tile_size: int = 256,\n        step: Optional[int] = None,\n        random_offset: bool = False,\n        pad_full: bool = False,\n        background_val: int = 255,\n        filter_mode: str = \"min\",\n        allow_missing_keys: bool = False,\n        return_list_of_dicts: bool = False,\n    ):\n        super().__init__(keys, allow_missing_keys)\n\n        self.return_list_of_dicts = return_list_of_dicts\n        self.seed = None\n\n        self.splitter = TileOnGrid(\n            tile_count=tile_count,\n            tile_size=tile_size,\n            step=step,\n            random_offset=random_offset,\n            pad_full=pad_full,\n            background_val=background_val,\n            filter_mode=filter_mode,\n        )\n\n    def randomize(self, data: Any = None) -> None:\n        self.seed = self.R.randint(10000)  # type: ignore\n\n    def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Union[Dict[Hashable, np.ndarray], List[Dict]]:\n\n        self.randomize()\n\n        d = dict(data)\n        for key in self.key_iterator(d):\n            self.splitter.set_random_state(seed=self.seed)  # same random seed for all keys\n            d[key] = self.splitter(d[key])\n\n        if self.return_list_of_dicts:\n            d_list = []\n            for i in range(len(d[self.keys[0]])):\n                d_list.append({k: d[k][i] if k in self.keys else copy.deepcopy(d[k]) for k in d.keys()})\n            d = d_list  # type: ignore\n\n        return d\n\n\nSplitOnGridDict = SplitOnGridD = SplitOnGridd\nTileOnGridDict = TileOnGridD = TileOnGridd\n"}
filelevel_patches: {"monai/apps/pathology/transforms/spatial/array.py": "@@ -17,6 +17,7 @@\n \n from monai.config.type_definitions import NdarrayOrTensor\n from monai.transforms.transform import Randomizable, Transform\n+from monai.utils import convert_data_type, convert_to_dst_type\n from monai.utils.enums import TransformBackends\n \n __all__ = [\"SplitOnGrid\", \"TileOnGrid\"]\n@@ -129,6 +130,8 @@ class TileOnGrid(Randomizable, Transform):\n \n     \"\"\"\n \n+    backend = [TransformBackends.NUMPY]\n+\n     def __init__(\n         self,\n         tile_count: Optional[int] = None,\n@@ -185,37 +188,39 @@ def randomize(self, img_size: Sequence[int]) -> None:\n         else:\n             self.random_idxs = np.array((0,))\n \n-    def __call__(self, image: np.ndarray) -> np.ndarray:\n+    def __call__(self, image: NdarrayOrTensor) -> NdarrayOrTensor:\n+        img_np: np.ndarray\n+        img_np, *_ = convert_data_type(image, np.ndarray)  # type: ignore\n \n         # add random offset\n-        self.randomize(img_size=image.shape)\n+        self.randomize(img_size=img_np.shape)\n \n         if self.random_offset and (self.offset[0] > 0 or self.offset[1] > 0):\n-            image = image[:, self.offset[0] :, self.offset[1] :]\n+            img_np = img_np[:, self.offset[0] :, self.offset[1] :]\n \n         # pad to full size, divisible by tile_size\n         if self.pad_full:\n-            c, h, w = image.shape\n+            c, h, w = img_np.shape\n             pad_h = (self.tile_size - h % self.tile_size) % self.tile_size\n             pad_w = (self.tile_size - w % self.tile_size) % self.tile_size\n-            image = np.pad(\n-                image,\n+            img_np = np.pad(\n+                img_np,\n                 [[0, 0], [pad_h // 2, pad_h - pad_h // 2], [pad_w // 2, pad_w - pad_w // 2]],\n                 constant_values=self.background_val,\n             )\n \n         # extact tiles\n-        x_step, y_step = self.step, self.step\n-        x_size, y_size = self.tile_size, self.tile_size\n-        c_len, x_len, y_len = image.shape\n-        c_stride, x_stride, y_stride = image.strides\n+        h_step, w_step = self.step, self.step\n+        h_size, w_size = self.tile_size, self.tile_size\n+        c_len, h_len, w_len = img_np.shape\n+        c_stride, h_stride, w_stride = img_np.strides\n         llw = as_strided(\n-            image,\n-            shape=((x_len - x_size) // x_step + 1, (y_len - y_size) // y_step + 1, c_len, x_size, y_size),\n-            strides=(x_stride * x_step, y_stride * y_step, c_stride, x_stride, y_stride),\n+            img_np,\n+            shape=((h_len - h_size) // h_step + 1, (w_len - w_size) // w_step + 1, c_len, h_size, w_size),\n+            strides=(h_stride * h_step, w_stride * w_step, c_stride, h_stride, w_stride),\n             writeable=False,\n         )\n-        image = llw.reshape(-1, c_len, x_size, y_size)\n+        img_np = llw.reshape(-1, c_len, h_size, w_size)\n \n         # if keeping all patches\n         if self.tile_count is None:\n@@ -224,32 +229,34 @@ def __call__(self, image: np.ndarray) -> np.ndarray:\n             thresh = 0.999 * 3 * self.background_val * self.tile_size * self.tile_size\n             if self.filter_mode == \"min\":\n                 # default, keep non-background tiles (small values)\n-                idxs = np.argwhere(image.sum(axis=(1, 2, 3)) < thresh)\n-                image = image[idxs.reshape(-1)]\n+                idxs = np.argwhere(img_np.sum(axis=(1, 2, 3)) < thresh)\n+                img_np = img_np[idxs.reshape(-1)]\n             elif self.filter_mode == \"max\":\n-                idxs = np.argwhere(image.sum(axis=(1, 2, 3)) >= thresh)\n-                image = image[idxs.reshape(-1)]\n+                idxs = np.argwhere(img_np.sum(axis=(1, 2, 3)) >= thresh)\n+                img_np = img_np[idxs.reshape(-1)]\n \n         else:\n-            if len(image) > self.tile_count:\n+            if len(img_np) > self.tile_count:\n \n                 if self.filter_mode == \"min\":\n                     # default, keep non-background tiles (smallest values)\n-                    idxs = np.argsort(image.sum(axis=(1, 2, 3)))[: self.tile_count]\n-                    image = image[idxs]\n+                    idxs = np.argsort(img_np.sum(axis=(1, 2, 3)))[: self.tile_count]\n+                    img_np = img_np[idxs]\n                 elif self.filter_mode == \"max\":\n-                    idxs = np.argsort(image.sum(axis=(1, 2, 3)))[-self.tile_count :]\n-                    image = image[idxs]\n+                    idxs = np.argsort(img_np.sum(axis=(1, 2, 3)))[-self.tile_count :]\n+                    img_np = img_np[idxs]\n                 else:\n                     # random subset (more appropriate for WSIs without distinct background)\n                     if self.random_idxs is not None:\n-                        image = image[self.random_idxs]\n+                        img_np = img_np[self.random_idxs]\n \n-            elif len(image) < self.tile_count:\n-                image = np.pad(\n-                    image,\n-                    [[0, self.tile_count - len(image)], [0, 0], [0, 0], [0, 0]],\n+            elif len(img_np) < self.tile_count:\n+                img_np = np.pad(\n+                    img_np,\n+                    [[0, self.tile_count - len(img_np)], [0, 0], [0, 0], [0, 0]],\n                     constant_values=self.background_val,\n                 )\n \n+        image, *_ = convert_to_dst_type(src=img_np, dst=image, dtype=image.dtype)\n+\n         return image", "monai/apps/pathology/transforms/spatial/dictionary.py": "@@ -12,8 +12,6 @@\n import copy\n from typing import Any, Dict, Hashable, List, Mapping, Optional, Tuple, Union\n \n-import numpy as np\n-\n from monai.config import KeysCollection\n from monai.config.type_definitions import NdarrayOrTensor\n from monai.transforms.transform import MapTransform, Randomizable\n@@ -81,6 +79,8 @@ class TileOnGridd(Randomizable, MapTransform):\n \n     \"\"\"\n \n+    backend = SplitOnGrid.backend\n+\n     def __init__(\n         self,\n         keys: KeysCollection,\n@@ -112,7 +112,9 @@ def __init__(\n     def randomize(self, data: Any = None) -> None:\n         self.seed = self.R.randint(10000)  # type: ignore\n \n-    def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Union[Dict[Hashable, np.ndarray], List[Dict]]:\n+    def __call__(\n+        self, data: Mapping[Hashable, NdarrayOrTensor]\n+    ) -> Union[Dict[Hashable, NdarrayOrTensor], List[Dict[Hashable, NdarrayOrTensor]]]:\n \n         self.randomize()"}
messages.content: 
  We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Update TileOnGrid backend
  **Is your feature request related to a problem? Please describe.**
  Update `TileOnGrid` to support input/output type of `torch.Tensor` and `numpy` while its backend is `numpy` only.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  
  --- BEGIN FILE ---
  ```
  ### monai/apps/pathology/transforms/spatial/dictionary.py
  # Copyright 2020 - 2021 MONAI Consortium
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #     http://www.apache.org/licenses/LICENSE-2.0
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  import copy
  from typing import Any, Dict, Hashable, List, Mapping, Optional, Tuple, Union
  
  import numpy as np
  
  from monai.config import KeysCollection
  from monai.config.type_definitions import NdarrayOrTensor
  from monai.transforms.transform import MapTransform, Randomizable
  
  from .array import SplitOnGrid, TileOnGrid
  
  __all__ = ["SplitOnGridd", "SplitOnGridD", "SplitOnGridDict", "TileOnGridd", "TileOnGridD", "TileOnGridDict"]
  
  
  class SplitOnGridd(MapTransform):
      """
      Split the image into patches based on the provided grid shape.
      This transform works only with torch.Tensor inputs.
  
      Args:
          grid_size: a tuple or an integer define the shape of the grid upon which to extract patches.
              If it's an integer, the value will be repeated for each dimension. Default is 2x2
          patch_size: a tuple or an integer that defines the output patch sizes.
              If it's an integer, the value will be repeated for each dimension.
              The default is (0, 0), where the patch size will be inferred from the grid shape.
  
      Note: the shape of the input image is inferred based on the first image used.
      """
  
      backend = SplitOnGrid.backend
  
      def __init__(
          self,
          keys: KeysCollection,
          grid_size: Union[int, Tuple[int, int]] = (2, 2),
          patch_size: Optional[Union[int, Tuple[int, int]]] = None,
          allow_missing_keys: bool = False,
      ):
          super().__init__(keys, allow_missing_keys)
          self.splitter = SplitOnGrid(grid_size=grid_size, patch_size=patch_size)
  
      def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> Dict[Hashable, NdarrayOrTensor]:
          d = dict(data)
          for key in self.key_iterator(d):
              d[key] = self.splitter(d[key])
          return d
  
  
  class TileOnGridd(Randomizable, MapTransform):
      """
      Tile the 2D image into patches on a grid and maintain a subset of it.
      This transform works only with np.ndarray inputs for 2D images.
  
      Args:
          tile_count: number of tiles to extract, if None extracts all non-background tiles
              Defaults to ``None``.
          tile_size: size of the square tile
              Defaults to ``256``.
          step: step size
              Defaults to ``None`` (same as tile_size)
          random_offset: Randomize position of the grid, instead of starting from the top-left corner
              Defaults to ``False``.
          pad_full: pad image to the size evenly divisible by tile_size
              Defaults to ``False``.
          background_val: the background constant (e.g. 255 for white background)
              Defaults to ``255``.
          filter_mode: mode must be in ["min", "max", "random"]. If total number of tiles is more than tile_size,
              then sort by intensity sum, and take the smallest (for min), largest (for max) or random (for random) subset
              Defaults to ``min`` (which assumes background is high value)
  
      """
  
      def __init__(
          self,
          keys: KeysCollection,
          tile_count: Optional[int] = None,
          tile_size: int = 256,
          step: Optional[int] = None,
          random_offset: bool = False,
          pad_full: bool = False,
          background_val: int = 255,
          filter_mode: str = "min",
          allow_missing_keys: bool = False,
          return_list_of_dicts: bool = False,
      ):
          super().__init__(keys, allow_missing_keys)
  
          self.return_list_of_dicts = return_list_of_dicts
          self.seed = None
  
          self.splitter = TileOnGrid(
              tile_count=tile_count,
              tile_size=tile_size,
              step=step,
              random_offset=random_offset,
              pad_full=pad_full,
              background_val=background_val,
              filter_mode=filter_mode,
          )
  
      def randomize(self, data: Any = None) -> None:
          self.seed = self.R.randint(10000)  # type: ignore
  
      def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Union[Dict[Hashable, np.ndarray], List[Dict]]:
  
          self.randomize()
  
          d = dict(data)
          for key in self.key_iterator(d):
              self.splitter.set_random_state(seed=self.seed)  # same random seed for all keys
              d[key] = self.splitter(d[key])
  
          if self.return_list_of_dicts:
              d_list = []
              for i in range(len(d[self.keys[0]])):
                  d_list.append({k: d[k][i] if k in self.keys else copy.deepcopy(d[k]) for k in d.keys()})
              d = d_list  # type: ignore
  
          return d
  
  
  SplitOnGridDict = SplitOnGridD = SplitOnGridd
  TileOnGridDict = TileOnGridD = TileOnGridd
  
  
  ### monai/apps/pathology/transforms/spatial/array.py
  # Copyright 2020 - 2021 MONAI Consortium
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #     http://www.apache.org/licenses/LICENSE-2.0
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  from typing import Optional, Sequence, Tuple, Union
  
  import numpy as np
  import torch
  from numpy.lib.stride_tricks import as_strided
  
  from monai.config.type_definitions import NdarrayOrTensor
  from monai.transforms.transform import Randomizable, Transform
  from monai.utils.enums import TransformBackends
  
  __all__ = ["SplitOnGrid", "TileOnGrid"]
  
  
  class SplitOnGrid(Transform):
      """
      Split the image into patches based on the provided grid shape.
      This transform works only with torch.Tensor inputs.
  
      Args:
          grid_shape: a tuple or an integer define the shape of the grid upon which to extract patches.
              If it's an integer, the value will be repeated for each dimension. Default is 2x2
          patch_size: a tuple or an integer that defines the output patch sizes.
              If it's an integer, the value will be repeated for each dimension.
              The default is (0, 0), where the patch size will be inferred from the grid shape.
  
      Note: the shape of the input image is inferred based on the first image used.
      """
  
      backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
  
      def __init__(
          self, grid_size: Union[int, Tuple[int, int]] = (2, 2), patch_size: Optional[Union[int, Tuple[int, int]]] = None
      ):
          # Grid size
          if isinstance(grid_size, int):
              self.grid_size = (grid_size, grid_size)
          else:
              self.grid_size = grid_size
          # Patch size
          self.patch_size = None
          if isinstance(patch_size, int):
              self.patch_size = (patch_size, patch_size)
          else:
              self.patch_size = patch_size
  
      def __call__(self, image: NdarrayOrTensor) -> NdarrayOrTensor:
          if self.grid_size == (1, 1) and self.patch_size is None:
              if isinstance(image, torch.Tensor):
                  return torch.stack([image])
              elif isinstance(image, np.ndarray):
                  return np.stack([image])
              else:
                  raise ValueError(f"Input type [{type(image)}] is not supported.")
  
          patch_size, steps = self.get_params(image.shape[1:])
          patches: NdarrayOrTensor
          if isinstance(image, torch.Tensor):
              patches = (
                  image.unfold(1, patch_size[0], steps[0])
                  .unfold(2, patch_size[1], steps[1])
                  .flatten(1, 2)
                  .transpose(0, 1)
                  .contiguous()
              )
          elif isinstance(image, np.ndarray):
              h_step, w_step = steps
              c_stride, h_stride, w_stride = image.strides
              patches = as_strided(
                  image,
                  shape=(*self.grid_size, 3, patch_size[0], patch_size[1]),
                  strides=(h_stride * h_step, w_stride * w_step, c_stride, h_stride, w_stride),
                  writeable=False,
              )
              # flatten the first two dimensions
              patches = patches.reshape(np.prod(patches.shape[:2]), *patches.shape[2:])
              # make it a contiguous array
              patches = np.ascontiguousarray(patches)
          else:
              raise ValueError(f"Input type [{type(image)}] is not supported.")
  
          return patches
  
      def get_params(self, image_size):
          if self.patch_size is None:
              patch_size = tuple(image_size[i] // self.grid_size[i] for i in range(2))
          else:
              patch_size = self.patch_size
  
          steps = tuple(
              (image_size[i] - patch_size[i]) // (self.grid_size[i] - 1) if self.grid_size[i] > 1 else image_size[i]
              for i in range(2)
          )
  
          return patch_size, steps
  
  
  class TileOnGrid(Randomizable, Transform):
      """
      Tile the 2D image into patches on a grid and maintain a subset of it.
      This transform works only with np.ndarray inputs for 2D images.
  
      Args:
          tile_count: number of tiles to extract, if None extracts all non-background tiles
              Defaults to ``None``.
          tile_size: size of the square tile
              Defaults to ``256``.
          step: step size
              Defaults to ``None`` (same as tile_size)
          random_offset: Randomize position of the grid, instead of starting from the top-left corner
              Defaults to ``False``.
          pad_full: pad image to the size evenly divisible by tile_size
              Defaults to ``False``.
          background_val: the background constant (e.g. 255 for white background)
              Defaults to ``255``.
          filter_mode: mode must be in ["min", "max", "random"]. If total number of tiles is more than tile_size,
              then sort by intensity sum, and take the smallest (for min), largest (for max) or random (for random) subset
              Defaults to ``min`` (which assumes background is high value)
  
      """
  
      def __init__(
          self,
          tile_count: Optional[int] = None,
          tile_size: int = 256,
          step: Optional[int] = None,
          random_offset: bool = False,
          pad_full: bool = False,
          background_val: int = 255,
          filter_mode: str = "min",
      ):
          self.tile_count = tile_count
          self.tile_size = tile_size
          self.random_offset = random_offset
          self.pad_full = pad_full
          self.background_val = background_val
          self.filter_mode = filter_mode
  
          if step is None:
              # non-overlapping grid
              self.step = self.tile_size
          else:
              self.step = step
  
          self.offset = (0, 0)
          self.random_idxs = np.array((0,))
  
          if self.filter_mode not in ["min", "max", "random"]:
              raise ValueError("Unsupported filter_mode, must be [min, max or random]: " + str(self.filter_mode))
  
      def randomize(self, img_size: Sequence[int]) -> None:
  
          c, h, w = img_size
  
          self.offset = (0, 0)
          if self.random_offset:
              pad_h = h % self.tile_size
              pad_w = w % self.tile_size
              self.offset = (self.R.randint(pad_h) if pad_h > 0 else 0, self.R.randint(pad_w) if pad_w > 0 else 0)
              h = h - self.offset[0]
              w = w - self.offset[1]
  
          if self.pad_full:
              pad_h = (self.tile_size - h % self.tile_size) % self.tile_size
              pad_w = (self.tile_size - w % self.tile_size) % self.tile_size
              h = h + pad_h
              w = w + pad_w
  
          h_n = (h - self.tile_size + self.step) // self.step
          w_n = (w - self.tile_size + self.step) // self.step
          tile_total = h_n * w_n
  
          if self.tile_count is not None and tile_total > self.tile_count:
              self.random_idxs = self.R.choice(range(tile_total), self.tile_count, replace=False)
          else:
              self.random_idxs = np.array((0,))
  
      def __call__(self, image: np.ndarray) -> np.ndarray:
  
          # add random offset
          self.randomize(img_size=image.shape)
  
          if self.random_offset and (self.offset[0] > 0 or self.offset[1] > 0):
              image = image[:, self.offset[0] :, self.offset[1] :]
  
          # pad to full size, divisible by tile_size
          if self.pad_full:
              c, h, w = image.shape
              pad_h = (self.tile_size - h % self.tile_size) % self.tile_size
              pad_w = (self.tile_size - w % self.tile_size) % self.tile_size
              image = np.pad(
                  image,
                  [[0, 0], [pad_h // 2, pad_h - pad_h // 2], [pad_w // 2, pad_w - pad_w // 2]],
                  constant_values=self.background_val,
              )
  
          # extact tiles
          x_step, y_step = self.step, self.step
          x_size, y_size = self.tile_size, self.tile_size
          c_len, x_len, y_len = image.shape
          c_stride, x_stride, y_stride = image.strides
          llw = as_strided(
              image,
              shape=((x_len - x_size) // x_step + 1, (y_len - y_size) // y_step + 1, c_len, x_size, y_size),
              strides=(x_stride * x_step, y_stride * y_step, c_stride, x_stride, y_stride),
              writeable=False,
          )
          image = llw.reshape(-1, c_len, x_size, y_size)
  
          # if keeping all patches
          if self.tile_count is None:
              # retain only patches with significant foreground content to speed up inference
              # FYI, this returns a variable number of tiles, so the batch_size must be 1 (per gpu), e.g during inference
              thresh = 0.999 * 3 * self.background_val * self.tile_size * self.tile_size
              if self.filter_mode == "min":
                  # default, keep non-background tiles (small values)
                  idxs = np.argwhere(image.sum(axis=(1, 2, 3)) < thresh)
                  image = image[idxs.reshape(-1)]
              elif self.filter_mode == "max":
                  idxs = np.argwhere(image.sum(axis=(1, 2, 3)) >= thresh)
                  image = image[idxs.reshape(-1)]
  
          else:
              if len(image) > self.tile_count:
  
                  if self.filter_mode == "min":
                      # default, keep non-background tiles (smallest values)
                      idxs = np.argsort(image.sum(axis=(1, 2, 3)))[: self.tile_count]
                      image = image[idxs]
                  elif self.filter_mode == "max":
                      idxs = np.argsort(image.sum(axis=(1, 2, 3)))[-self.tile_count :]
                      image = image[idxs]
                  else:
                      # random subset (more appropriate for WSIs without distinct background)
                      if self.random_idxs is not None:
                          image = image[self.random_idxs]
  
              elif len(image) < self.tile_count:
                  image = np.pad(
                      image,
                      [[0, self.tile_count - len(image)], [0, 0], [0, 0], [0, 0]],
                      constant_values=self.background_val,
                  )
  
          return image
  
  ```
  --- END FILE ---
  
  Please first localize the bug based on the issue statement, and then generate *SEARCH/REPLACE* edits to fix the issue.
  
  Every *SEARCH/REPLACE* edit must use this format:
  1. The file path
  2. The start of search block: <<<<<<< SEARCH
  3. A contiguous chunk of lines to search for in the existing source code
  4. The dividing line: =======
  5. The lines to replace into the source code
  6. The end of the replace block: >>>>>>> REPLACE
  
  Here is an example:
  
  ```python
  ### mathweb/flask/app.py
  <<<<<<< SEARCH
  from flask import Flask
  =======
  import math
  from flask import Flask
  >>>>>>> REPLACE
  ```
  
  Please note that the *SEARCH/REPLACE* edit REQUIRES PROPER INDENTATION. If you would like to add the line '        print(x)', you must fully write that out, with all those spaces before the code!
  Wrap the *SEARCH/REPLACE* edit in blocks ```python...```.
  
messages.role: user
verification_info: {"file_context": {"monai/apps/pathology/transforms/spatial/array.py": "# Copyright 2020 - 2021 MONAI Consortium\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#     http://www.apache.org/licenses/LICENSE-2.0\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom typing import Optional, Sequence, Tuple, Union\n\nimport numpy as np\nimport torch\nfrom numpy.lib.stride_tricks import as_strided\n\nfrom monai.config.type_definitions import NdarrayOrTensor\nfrom monai.transforms.transform import Randomizable, Transform\nfrom monai.utils.enums import TransformBackends\n\n__all__ = [\"SplitOnGrid\", \"TileOnGrid\"]\n\n\nclass SplitOnGrid(Transform):\n    \"\"\"\n    Split the image into patches based on the provided grid shape.\n    This transform works only with torch.Tensor inputs.\n\n    Args:\n        grid_shape: a tuple or an integer define the shape of the grid upon which to extract patches.\n            If it's an integer, the value will be repeated for each dimension. Default is 2x2\n        patch_size: a tuple or an integer that defines the output patch sizes.\n            If it's an integer, the value will be repeated for each dimension.\n            The default is (0, 0), where the patch size will be inferred from the grid shape.\n\n    Note: the shape of the input image is inferred based on the first image used.\n    \"\"\"\n\n    backend = [TransformBackends.TORCH, TransformBackends.NUMPY]\n\n    def __init__(\n        self, grid_size: Union[int, Tuple[int, int]] = (2, 2), patch_size: Optional[Union[int, Tuple[int, int]]] = None\n    ):\n        # Grid size\n        if isinstance(grid_size, int):\n            self.grid_size = (grid_size, grid_size)\n        else:\n            self.grid_size = grid_size\n        # Patch size\n        self.patch_size = None\n        if isinstance(patch_size, int):\n            self.patch_size = (patch_size, patch_size)\n        else:\n            self.patch_size = patch_size\n\n    def __call__(self, image: NdarrayOrTensor) -> NdarrayOrTensor:\n        if self.grid_size == (1, 1) and self.patch_size is None:\n            if isinstance(image, torch.Tensor):\n                return torch.stack([image])\n            elif isinstance(image, np.ndarray):\n                return np.stack([image])\n            else:\n                raise ValueError(f\"Input type [{type(image)}] is not supported.\")\n\n        patch_size, steps = self.get_params(image.shape[1:])\n        patches: NdarrayOrTensor\n        if isinstance(image, torch.Tensor):\n            patches = (\n                image.unfold(1, patch_size[0], steps[0])\n                .unfold(2, patch_size[1], steps[1])\n                .flatten(1, 2)\n                .transpose(0, 1)\n                .contiguous()\n            )\n        elif isinstance(image, np.ndarray):\n            h_step, w_step = steps\n            c_stride, h_stride, w_stride = image.strides\n            patches = as_strided(\n                image,\n                shape=(*self.grid_size, 3, patch_size[0], patch_size[1]),\n                strides=(h_stride * h_step, w_stride * w_step, c_stride, h_stride, w_stride),\n                writeable=False,\n            )\n            # flatten the first two dimensions\n            patches = patches.reshape(np.prod(patches.shape[:2]), *patches.shape[2:])\n            # make it a contiguous array\n            patches = np.ascontiguousarray(patches)\n        else:\n            raise ValueError(f\"Input type [{type(image)}] is not supported.\")\n\n        return patches\n\n    def get_params(self, image_size):\n        if self.patch_size is None:\n            patch_size = tuple(image_size[i] // self.grid_size[i] for i in range(2))\n        else:\n            patch_size = self.patch_size\n\n        steps = tuple(\n            (image_size[i] - patch_size[i]) // (self.grid_size[i] - 1) if self.grid_size[i] > 1 else image_size[i]\n            for i in range(2)\n        )\n\n        return patch_size, steps\n\n\nclass TileOnGrid(Randomizable, Transform):\n    \"\"\"\n    Tile the 2D image into patches on a grid and maintain a subset of it.\n    This transform works only with np.ndarray inputs for 2D images.\n\n    Args:\n        tile_count: number of tiles to extract, if None extracts all non-background tiles\n            Defaults to ``None``.\n        tile_size: size of the square tile\n            Defaults to ``256``.\n        step: step size\n            Defaults to ``None`` (same as tile_size)\n        random_offset: Randomize position of the grid, instead of starting from the top-left corner\n            Defaults to ``False``.\n        pad_full: pad image to the size evenly divisible by tile_size\n            Defaults to ``False``.\n        background_val: the background constant (e.g. 255 for white background)\n            Defaults to ``255``.\n        filter_mode: mode must be in [\"min\", \"max\", \"random\"]. If total number of tiles is more than tile_size,\n            then sort by intensity sum, and take the smallest (for min), largest (for max) or random (for random) subset\n            Defaults to ``min`` (which assumes background is high value)\n\n    \"\"\"\n\n    def __init__(\n        self,\n        tile_count: Optional[int] = None,\n        tile_size: int = 256,\n        step: Optional[int] = None,\n        random_offset: bool = False,\n        pad_full: bool = False,\n        background_val: int = 255,\n        filter_mode: str = \"min\",\n    ):\n        self.tile_count = tile_count\n        self.tile_size = tile_size\n        self.random_offset = random_offset\n        self.pad_full = pad_full\n        self.background_val = background_val\n        self.filter_mode = filter_mode\n\n        if step is None:\n            # non-overlapping grid\n            self.step = self.tile_size\n        else:\n            self.step = step\n\n        self.offset = (0, 0)\n        self.random_idxs = np.array((0,))\n\n        if self.filter_mode not in [\"min\", \"max\", \"random\"]:\n            raise ValueError(\"Unsupported filter_mode, must be [min, max or random]: \" + str(self.filter_mode))\n\n    def randomize(self, img_size: Sequence[int]) -> None:\n\n        c, h, w = img_size\n\n        self.offset = (0, 0)\n        if self.random_offset:\n            pad_h = h % self.tile_size\n            pad_w = w % self.tile_size\n            self.offset = (self.R.randint(pad_h) if pad_h > 0 else 0, self.R.randint(pad_w) if pad_w > 0 else 0)\n            h = h - self.offset[0]\n            w = w - self.offset[1]\n\n        if self.pad_full:\n            pad_h = (self.tile_size - h % self.tile_size) % self.tile_size\n            pad_w = (self.tile_size - w % self.tile_size) % self.tile_size\n            h = h + pad_h\n            w = w + pad_w\n\n        h_n = (h - self.tile_size + self.step) // self.step\n        w_n = (w - self.tile_size + self.step) // self.step\n        tile_total = h_n * w_n\n\n        if self.tile_count is not None and tile_total > self.tile_count:\n            self.random_idxs = self.R.choice(range(tile_total), self.tile_count, replace=False)\n        else:\n            self.random_idxs = np.array((0,))\n\n    def __call__(self, image: np.ndarray) -> np.ndarray:\n\n        # add random offset\n        self.randomize(img_size=image.shape)\n\n        if self.random_offset and (self.offset[0] > 0 or self.offset[1] > 0):\n            image = image[:, self.offset[0] :, self.offset[1] :]\n\n        # pad to full size, divisible by tile_size\n        if self.pad_full:\n            c, h, w = image.shape\n            pad_h = (self.tile_size - h % self.tile_size) % self.tile_size\n            pad_w = (self.tile_size - w % self.tile_size) % self.tile_size\n            image = np.pad(\n                image,\n                [[0, 0], [pad_h // 2, pad_h - pad_h // 2], [pad_w // 2, pad_w - pad_w // 2]],\n                constant_values=self.background_val,\n            )\n\n        # extact tiles\n        x_step, y_step = self.step, self.step\n        x_size, y_size = self.tile_size, self.tile_size\n        c_len, x_len, y_len = image.shape\n        c_stride, x_stride, y_stride = image.strides\n        llw = as_strided(\n            image,\n            shape=((x_len - x_size) // x_step + 1, (y_len - y_size) // y_step + 1, c_len, x_size, y_size),\n            strides=(x_stride * x_step, y_stride * y_step, c_stride, x_stride, y_stride),\n            writeable=False,\n        )\n        image = llw.reshape(-1, c_len, x_size, y_size)\n\n        # if keeping all patches\n        if self.tile_count is None:\n            # retain only patches with significant foreground content to speed up inference\n            # FYI, this returns a variable number of tiles, so the batch_size must be 1 (per gpu), e.g during inference\n            thresh = 0.999 * 3 * self.background_val * self.tile_size * self.tile_size\n            if self.filter_mode == \"min\":\n                # default, keep non-background tiles (small values)\n                idxs = np.argwhere(image.sum(axis=(1, 2, 3)) < thresh)\n                image = image[idxs.reshape(-1)]\n            elif self.filter_mode == \"max\":\n                idxs = np.argwhere(image.sum(axis=(1, 2, 3)) >= thresh)\n                image = image[idxs.reshape(-1)]\n\n        else:\n            if len(image) > self.tile_count:\n\n                if self.filter_mode == \"min\":\n                    # default, keep non-background tiles (smallest values)\n                    idxs = np.argsort(image.sum(axis=(1, 2, 3)))[: self.tile_count]\n                    image = image[idxs]\n                elif self.filter_mode == \"max\":\n                    idxs = np.argsort(image.sum(axis=(1, 2, 3)))[-self.tile_count :]\n                    image = image[idxs]\n                else:\n                    # random subset (more appropriate for WSIs without distinct background)\n                    if self.random_idxs is not None:\n                        image = image[self.random_idxs]\n\n            elif len(image) < self.tile_count:\n                image = np.pad(\n                    image,\n                    [[0, self.tile_count - len(image)], [0, 0], [0, 0], [0, 0]],\n                    constant_values=self.background_val,\n                )\n\n        return image\n", "monai/apps/pathology/transforms/spatial/dictionary.py": "# Copyright 2020 - 2021 MONAI Consortium\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#     http://www.apache.org/licenses/LICENSE-2.0\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport copy\nfrom typing import Any, Dict, Hashable, List, Mapping, Optional, Tuple, Union\n\nimport numpy as np\n\nfrom monai.config import KeysCollection\nfrom monai.config.type_definitions import NdarrayOrTensor\nfrom monai.transforms.transform import MapTransform, Randomizable\n\nfrom .array import SplitOnGrid, TileOnGrid\n\n__all__ = [\"SplitOnGridd\", \"SplitOnGridD\", \"SplitOnGridDict\", \"TileOnGridd\", \"TileOnGridD\", \"TileOnGridDict\"]\n\n\nclass SplitOnGridd(MapTransform):\n    \"\"\"\n    Split the image into patches based on the provided grid shape.\n    This transform works only with torch.Tensor inputs.\n\n    Args:\n        grid_size: a tuple or an integer define the shape of the grid upon which to extract patches.\n            If it's an integer, the value will be repeated for each dimension. Default is 2x2\n        patch_size: a tuple or an integer that defines the output patch sizes.\n            If it's an integer, the value will be repeated for each dimension.\n            The default is (0, 0), where the patch size will be inferred from the grid shape.\n\n    Note: the shape of the input image is inferred based on the first image used.\n    \"\"\"\n\n    backend = SplitOnGrid.backend\n\n    def __init__(\n        self,\n        keys: KeysCollection,\n        grid_size: Union[int, Tuple[int, int]] = (2, 2),\n        patch_size: Optional[Union[int, Tuple[int, int]]] = None,\n        allow_missing_keys: bool = False,\n    ):\n        super().__init__(keys, allow_missing_keys)\n        self.splitter = SplitOnGrid(grid_size=grid_size, patch_size=patch_size)\n\n    def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> Dict[Hashable, NdarrayOrTensor]:\n        d = dict(data)\n        for key in self.key_iterator(d):\n            d[key] = self.splitter(d[key])\n        return d\n\n\nclass TileOnGridd(Randomizable, MapTransform):\n    \"\"\"\n    Tile the 2D image into patches on a grid and maintain a subset of it.\n    This transform works only with np.ndarray inputs for 2D images.\n\n    Args:\n        tile_count: number of tiles to extract, if None extracts all non-background tiles\n            Defaults to ``None``.\n        tile_size: size of the square tile\n            Defaults to ``256``.\n        step: step size\n            Defaults to ``None`` (same as tile_size)\n        random_offset: Randomize position of the grid, instead of starting from the top-left corner\n            Defaults to ``False``.\n        pad_full: pad image to the size evenly divisible by tile_size\n            Defaults to ``False``.\n        background_val: the background constant (e.g. 255 for white background)\n            Defaults to ``255``.\n        filter_mode: mode must be in [\"min\", \"max\", \"random\"]. If total number of tiles is more than tile_size,\n            then sort by intensity sum, and take the smallest (for min), largest (for max) or random (for random) subset\n            Defaults to ``min`` (which assumes background is high value)\n\n    \"\"\"\n\n    def __init__(\n        self,\n        keys: KeysCollection,\n        tile_count: Optional[int] = None,\n        tile_size: int = 256,\n        step: Optional[int] = None,\n        random_offset: bool = False,\n        pad_full: bool = False,\n        background_val: int = 255,\n        filter_mode: str = \"min\",\n        allow_missing_keys: bool = False,\n        return_list_of_dicts: bool = False,\n    ):\n        super().__init__(keys, allow_missing_keys)\n\n        self.return_list_of_dicts = return_list_of_dicts\n        self.seed = None\n\n        self.splitter = TileOnGrid(\n            tile_count=tile_count,\n            tile_size=tile_size,\n            step=step,\n            random_offset=random_offset,\n            pad_full=pad_full,\n            background_val=background_val,\n            filter_mode=filter_mode,\n        )\n\n    def randomize(self, data: Any = None) -> None:\n        self.seed = self.R.randint(10000)  # type: ignore\n\n    def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Union[Dict[Hashable, np.ndarray], List[Dict]]:\n\n        self.randomize()\n\n        d = dict(data)\n        for key in self.key_iterator(d):\n            self.splitter.set_random_state(seed=self.seed)  # same random seed for all keys\n            d[key] = self.splitter(d[key])\n\n        if self.return_list_of_dicts:\n            d_list = []\n            for i in range(len(d[self.keys[0]])):\n                d_list.append({k: d[k][i] if k in self.keys else copy.deepcopy(d[k]) for k in d.keys()})\n            d = d_list  # type: ignore\n\n        return d\n\n\nSplitOnGridDict = SplitOnGridD = SplitOnGridd\nTileOnGridDict = TileOnGridD = TileOnGridd\n"}, "filelevel_patches": {"monai/apps/pathology/transforms/spatial/array.py": "@@ -17,6 +17,7 @@\n \n from monai.config.type_definitions import NdarrayOrTensor\n from monai.transforms.transform import Randomizable, Transform\n+from monai.utils import convert_data_type, convert_to_dst_type\n from monai.utils.enums import TransformBackends\n \n __all__ = [\"SplitOnGrid\", \"TileOnGrid\"]\n@@ -129,6 +130,8 @@ class TileOnGrid(Randomizable, Transform):\n \n     \"\"\"\n \n+    backend = [TransformBackends.NUMPY]\n+\n     def __init__(\n         self,\n         tile_count: Optional[int] = None,\n@@ -185,37 +188,39 @@ def randomize(self, img_size: Sequence[int]) -> None:\n         else:\n             self.random_idxs = np.array((0,))\n \n-    def __call__(self, image: np.ndarray) -> np.ndarray:\n+    def __call__(self, image: NdarrayOrTensor) -> NdarrayOrTensor:\n+        img_np: np.ndarray\n+        img_np, *_ = convert_data_type(image, np.ndarray)  # type: ignore\n \n         # add random offset\n-        self.randomize(img_size=image.shape)\n+        self.randomize(img_size=img_np.shape)\n \n         if self.random_offset and (self.offset[0] > 0 or self.offset[1] > 0):\n-            image = image[:, self.offset[0] :, self.offset[1] :]\n+            img_np = img_np[:, self.offset[0] :, self.offset[1] :]\n \n         # pad to full size, divisible by tile_size\n         if self.pad_full:\n-            c, h, w = image.shape\n+            c, h, w = img_np.shape\n             pad_h = (self.tile_size - h % self.tile_size) % self.tile_size\n             pad_w = (self.tile_size - w % self.tile_size) % self.tile_size\n-            image = np.pad(\n-                image,\n+            img_np = np.pad(\n+                img_np,\n                 [[0, 0], [pad_h // 2, pad_h - pad_h // 2], [pad_w // 2, pad_w - pad_w // 2]],\n                 constant_values=self.background_val,\n             )\n \n         # extact tiles\n-        x_step, y_step = self.step, self.step\n-        x_size, y_size = self.tile_size, self.tile_size\n-        c_len, x_len, y_len = image.shape\n-        c_stride, x_stride, y_stride = image.strides\n+        h_step, w_step = self.step, self.step\n+        h_size, w_size = self.tile_size, self.tile_size\n+        c_len, h_len, w_len = img_np.shape\n+        c_stride, h_stride, w_stride = img_np.strides\n         llw = as_strided(\n-            image,\n-            shape=((x_len - x_size) // x_step + 1, (y_len - y_size) // y_step + 1, c_len, x_size, y_size),\n-            strides=(x_stride * x_step, y_stride * y_step, c_stride, x_stride, y_stride),\n+            img_np,\n+            shape=((h_len - h_size) // h_step + 1, (w_len - w_size) // w_step + 1, c_len, h_size, w_size),\n+            strides=(h_stride * h_step, w_stride * w_step, c_stride, h_stride, w_stride),\n             writeable=False,\n         )\n-        image = llw.reshape(-1, c_len, x_size, y_size)\n+        img_np = llw.reshape(-1, c_len, h_size, w_size)\n \n         # if keeping all patches\n         if self.tile_count is None:\n@@ -224,32 +229,34 @@ def __call__(self, image: np.ndarray) -> np.ndarray:\n             thresh = 0.999 * 3 * self.background_val * self.tile_size * self.tile_size\n             if self.filter_mode == \"min\":\n                 # default, keep non-background tiles (small values)\n-                idxs = np.argwhere(image.sum(axis=(1, 2, 3)) < thresh)\n-                image = image[idxs.reshape(-1)]\n+                idxs = np.argwhere(img_np.sum(axis=(1, 2, 3)) < thresh)\n+                img_np = img_np[idxs.reshape(-1)]\n             elif self.filter_mode == \"max\":\n-                idxs = np.argwhere(image.sum(axis=(1, 2, 3)) >= thresh)\n-                image = image[idxs.reshape(-1)]\n+                idxs = np.argwhere(img_np.sum(axis=(1, 2, 3)) >= thresh)\n+                img_np = img_np[idxs.reshape(-1)]\n \n         else:\n-            if len(image) > self.tile_count:\n+            if len(img_np) > self.tile_count:\n \n                 if self.filter_mode == \"min\":\n                     # default, keep non-background tiles (smallest values)\n-                    idxs = np.argsort(image.sum(axis=(1, 2, 3)))[: self.tile_count]\n-                    image = image[idxs]\n+                    idxs = np.argsort(img_np.sum(axis=(1, 2, 3)))[: self.tile_count]\n+                    img_np = img_np[idxs]\n                 elif self.filter_mode == \"max\":\n-                    idxs = np.argsort(image.sum(axis=(1, 2, 3)))[-self.tile_count :]\n-                    image = image[idxs]\n+                    idxs = np.argsort(img_np.sum(axis=(1, 2, 3)))[-self.tile_count :]\n+                    img_np = img_np[idxs]\n                 else:\n                     # random subset (more appropriate for WSIs without distinct background)\n                     if self.random_idxs is not None:\n-                        image = image[self.random_idxs]\n+                        img_np = img_np[self.random_idxs]\n \n-            elif len(image) < self.tile_count:\n-                image = np.pad(\n-                    image,\n-                    [[0, self.tile_count - len(image)], [0, 0], [0, 0], [0, 0]],\n+            elif len(img_np) < self.tile_count:\n+                img_np = np.pad(\n+                    img_np,\n+                    [[0, self.tile_count - len(img_np)], [0, 0], [0, 0], [0, 0]],\n                     constant_values=self.background_val,\n                 )\n \n+        image, *_ = convert_to_dst_type(src=img_np, dst=image, dtype=image.dtype)\n+\n         return image", "monai/apps/pathology/transforms/spatial/dictionary.py": "@@ -12,8 +12,6 @@\n import copy\n from typing import Any, Dict, Hashable, List, Mapping, Optional, Tuple, Union\n \n-import numpy as np\n-\n from monai.config import KeysCollection\n from monai.config.type_definitions import NdarrayOrTensor\n from monai.transforms.transform import MapTransform, Randomizable\n@@ -81,6 +79,8 @@ class TileOnGridd(Randomizable, MapTransform):\n \n     \"\"\"\n \n+    backend = SplitOnGrid.backend\n+\n     def __init__(\n         self,\n         keys: KeysCollection,\n@@ -112,7 +112,9 @@ def __init__(\n     def randomize(self, data: Any = None) -> None:\n         self.seed = self.R.randint(10000)  # type: ignore\n \n-    def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Union[Dict[Hashable, np.ndarray], List[Dict]]:\n+    def __call__(\n+        self, data: Mapping[Hashable, NdarrayOrTensor]\n+    ) -> Union[Dict[Hashable, NdarrayOrTensor], List[Dict[Hashable, NdarrayOrTensor]]]:\n \n         self.randomize()"}}