Skip to content

Commit

Permalink
refactor: Renamed patch_offset to step_size
Browse files Browse the repository at this point in the history
  • Loading branch information
Karol-G committed Oct 24, 2023
1 parent c0ce970 commit 1f7dbe6
Show file tree
Hide file tree
Showing 17 changed files with 245 additions and 245 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ You can install `patchly` via [pip](https://pypi.org/project/patchly/):

Demonstration on how to use Patchly for sliding-window patchification and subsequent aggregation:
```python
sampler = GridSampler(spatial_size, patch_size, patch_offset, image)
sampler = GridSampler(spatial_size, patch_size, step_size, image)
aggregator = Aggregator(sampler, output_size)

for patch, patch_bbox in sampler:
Expand Down Expand Up @@ -67,7 +67,7 @@ def model(x):
return y

# Init GridSampler
sampler = GridSampler(image=np.random.random((1000, 1000, 3)), spatial_size=(1000, 1000), patch_size=(100, 100), patch_offset=(50, 50))
sampler = GridSampler(image=np.random.random((1000, 1000, 3)), spatial_size=(1000, 1000), patch_size=(100, 100), step_size=(50, 50))
# Init dataloader
loader = DataLoader(ExampleDataset(sampler), batch_size=4, num_workers=0, shuffle=False)
# Init aggregator
Expand Down
4 changes: 2 additions & 2 deletions examples/inference_chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def example():
image = np.random.random((3, 1000, 1000)) # Channel, Width, Height
spatial_size = image.shape[-2:]
patch_size = (100, 100)
patch_offset = (50, 50)
step_size = (50, 50)
chunk_size = (500, 500)

# Init GridSampler
sampler = GridSampler(image=image, spatial_size=spatial_size, patch_size=patch_size, patch_offset=patch_offset, spatial_first=False)
sampler = GridSampler(image=image, spatial_size=spatial_size, patch_size=patch_size, step_size=step_size, spatial_first=False)
# Convert sampler into a PyTorch dataset
loader = SamplerDataset(sampler)
# Init dataloader
Expand Down
2 changes: 1 addition & 1 deletion examples/pytorch_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

def example():
# Init GridSampler
sampler = GridSampler(image=np.random.random((1000, 1000, 3)), spatial_size=(1000, 1000), patch_size=(100, 100), patch_offset=(50, 50))
sampler = GridSampler(image=np.random.random((1000, 1000, 3)), spatial_size=(1000, 1000), patch_size=(100, 100), step_size=(50, 50))
# Init dataloader
loader = DataLoader(ExampleDataset(sampler), batch_size=4, num_workers=0, shuffle=False)
# Init aggregator
Expand Down
10 changes: 5 additions & 5 deletions misc/tmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import copy


def _test_aggregator(image, spatial_size, patch_size, patch_offset=None, spatial_first_sampler=True, spatial_first_aggregator=True, output=None, weights='avg', multiply_patch_by_index=False, softmax_dim=None):
def _test_aggregator(image, spatial_size, patch_size, step_size=None, spatial_first_sampler=True, spatial_first_aggregator=True, output=None, weights='avg', multiply_patch_by_index=False, softmax_dim=None):
# Test with output size
sampler = GridSampler(image=copy.deepcopy(image), spatial_size=spatial_size, patch_size=patch_size, patch_offset=patch_offset, spatial_first=spatial_first_sampler, mode=SamplingMode.SAMPLE_SQUEEZE)
sampler = GridSampler(image=copy.deepcopy(image), spatial_size=spatial_size, patch_size=patch_size, step_size=step_size, spatial_first=spatial_first_sampler, mode=SamplingMode.SAMPLE_SQUEEZE)
aggregator = Aggregator(sampler=sampler, output_size=image.shape, weights=weights, spatial_first=spatial_first_aggregator, softmax_dim=softmax_dim)

for i, (patch, patch_bbox) in enumerate(sampler):
Expand All @@ -28,7 +28,7 @@ def _test_aggregator(image, spatial_size, patch_size, patch_offset=None, spatial
# Test without output array
if output is None:
output = np.zeros_like(image)
sampler = GridSampler(image=copy.deepcopy(image), spatial_size=spatial_size, patch_size=patch_size, patch_offset=patch_offset, spatial_first=spatial_first_sampler, mode=SamplingMode.SAMPLE_SQUEEZE)
sampler = GridSampler(image=copy.deepcopy(image), spatial_size=spatial_size, patch_size=patch_size, step_size=step_size, spatial_first=spatial_first_sampler, mode=SamplingMode.SAMPLE_SQUEEZE)
aggregator = Aggregator(sampler=sampler, output=output, weights=weights, spatial_first=spatial_first_aggregator, softmax_dim=softmax_dim)

for i, (patch, patch_bbox) in enumerate(sampler):
Expand All @@ -48,9 +48,9 @@ def _test_aggregator(image, spatial_size, patch_size, patch_offset=None, spatial

return output1, output2

patch_offset = (3, 3)
step_size = (3, 3)
patch_size = (10, 10)
spatial_size = (103, 107)
image = np.random.random(spatial_size)

_test_aggregator(image, spatial_size, patch_size, patch_offset=patch_offset)
_test_aggregator(image, spatial_size, patch_size, step_size=step_size)
2 changes: 1 addition & 1 deletion patchly/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.0.4"
__version__ = "0.0.5"

from patchly.sampler import GridSampler, SamplingMode
from patchly.aggregator import Aggregator
Expand Down
10 changes: 5 additions & 5 deletions patchly/aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(self, sampler: GridSampler, output_size: Optional[Union[Tuple, npt.
self.sampler = sampler
self.image_size_s = sampler.image_size_s
self.patch_size_s = sampler.patch_size_s
self.patch_offset_s = sampler.patch_offset_s
self.step_size_s = sampler.step_size_s
self.chunk_size_s = chunk_size
self.spatial_first = spatial_first
self.mode = sampler.mode
Expand Down Expand Up @@ -114,7 +114,7 @@ def set_aggregator(self, sampler, output_h, softmax_dim):
output_h=output_h, spatial_first=self.spatial_first, softmax_dim=softmax_dim, has_batch_dim=self.has_batch_dim,
weight_patch_s=self.weight_patch_s, weight_map_s=self.weight_map_s, device=self.device, array_type=self.array_type)
elif self.mode.name.startswith('SAMPLE_') and self.chunk_size_s is not None:
aggregator = _ChunkAggregator(sampler=sampler, image_size_s=self.image_size_s, patch_size_s=self.patch_size_s, patch_offset_s=self.patch_offset_s,
aggregator = _ChunkAggregator(sampler=sampler, image_size_s=self.image_size_s, patch_size_s=self.patch_size_s, step_size_s=self.step_size_s,
chunk_size_s=self.chunk_size_s, output_h=output_h, spatial_first=self.spatial_first, softmax_dim=softmax_dim,
has_batch_dim=self.has_batch_dim, weight_patch_s=self.weight_patch_s, device=self.device, array_type=self.array_type)
elif self.mode.name.startswith('PAD_') and self.chunk_size_s is None:
Expand Down Expand Up @@ -263,7 +263,7 @@ def verify_array_types(self, array_type):


class _ChunkAggregator(_Aggregator):
def __init__(self, sampler: GridSampler, image_size_s: Union[Tuple, npt.ArrayLike], patch_size_s: Union[Tuple, npt.ArrayLike], patch_offset_s: Union[Tuple, npt.ArrayLike], chunk_size_s: Union[Tuple, npt.ArrayLike],
def __init__(self, sampler: GridSampler, image_size_s: Union[Tuple, npt.ArrayLike], patch_size_s: Union[Tuple, npt.ArrayLike], step_size_s: Union[Tuple, npt.ArrayLike], chunk_size_s: Union[Tuple, npt.ArrayLike],
output_h: Optional[npt.ArrayLike] = None, spatial_first: bool = True,
softmax_dim: Optional[int] = None, has_batch_dim: bool = False, weight_patch_s: npt.ArrayLike = None, device = 'cpu', array_type = None):
"""
Expand All @@ -278,7 +278,7 @@ def __init__(self, sampler: GridSampler, image_size_s: Union[Tuple, npt.ArrayLik
"""
super().__init__(sampler=sampler, image_size_s=image_size_s, patch_size_s=patch_size_s, output_h=output_h, spatial_first=spatial_first,
softmax_dim=softmax_dim, has_batch_dim=has_batch_dim, weight_patch_s=weight_patch_s, weight_map_s=None, device=device, array_type=array_type)
self.patch_offset_s = patch_offset_s
self.step_size_s = step_size_s
self.chunk_size_s = chunk_size_s
self.chunk_dtype = self.set_chunk_dtype()
self.chunk_sampler, self.chunk_patch_dict, self.patch_chunk_dict = self.compute_patches()
Expand All @@ -292,7 +292,7 @@ def set_chunk_dtype(self):

def compute_patches(self):
patch_sampler = self.sampler
chunk_sampler = _AdaptiveGridSampler(image_size_s=self.image_size_s, patch_size_s=self.chunk_size_s, patch_offset_s=self.chunk_size_s)
chunk_sampler = _AdaptiveGridSampler(image_size_s=self.image_size_s, patch_size_s=self.chunk_size_s, step_size_s=self.chunk_size_s)
chunk_patch_dict = defaultdict(dict)
patch_chunk_dict = defaultdict(dict)

Expand Down
Loading

0 comments on commit 1f7dbe6

Please sign in to comment.