Skip to content

GridPatch Performance Issue #4472

@bhashemian

Description

@bhashemian

Describe the bug
GridPatch and RandGridPatch are slower than their similar component TileOnGrid.

To Reproduce

import numpy as np
from monai.transforms import GridPatchd
from monai.apps.pathology.transforms import TileOnGridd
from numpy.testing import assert_array_equal, assert_equal

# input configs
patch_size = 256
num_patches = 90
h, w = 10000, 1000
img = {"image": np.random.randint(0, 255, (3, h, w))}

# create components objects
patcher = GridPatchd(
    keys=["image"],
    patch_size=(patch_size, patch_size),
    num_patches=num_patches,
    sort_fn="min",
    threshold=0.999 * 3 * 255 * patch_size * patch_size,
    pad_mode=None,
    constant_values=255,
)
tiler = TileOnGridd(
    keys=["image"],
    tile_count=num_patches,
    tile_size=patch_size,
    background_val=255,
    return_list_of_dicts=True,
)

# Make sure the outputs are identical
patch_output = patcher(img)
tile_output = tiler(img)
assert_equal(len(patch_output), len(tile_output))
for p, t in zip(patch_output, tile_output):
    assert_array_equal(p["image"], t["image"])
%%timeit -n 10 -r 10
patch_output = patcher(img)
208 ms ± 16.4 ms per loop (mean ± std. dev. of 10 runs, 10 loops each)
%%timeit -n 10 -r 10
tile_output = tiler(img)
104 ms ± 2.38 ms per loop (mean ± std. dev. of 10 runs, 10 loops each)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Status

💯 Complete

Relationships

None yet

Development

No branches or pull requests

Issue actions