Skip to main content

Patchly

License Apache Software License 2.0 PyPI Python Version Unit Tests codecov

Patchly is a grid sampler for N-dimensional images enabling inference and other processing steps on extremely large images. Especially for 3D images, it has been proven successfully to inference large images patch-wise in a sliding-window approach. Patchly does just that with a very simple interface to sample and aggregate images.

Features

Patchly supports:

  • N-dimensional images (1D, 2D, 3D, ...)
  • Sampling and aggregation of images
  • Any array-like images (Numpy, Tensor, Zarr, Dask, ...)
  • Memory-mapped images
  • Patch overlap (here referred to as patch offset)
  • All numpy padding techniques
  • Images with non-spatial dimensions (color dimension, batch dimension, etc)
  • Chunk sampling to minimize memory consumption

Installation

You can install patchly via pip:

pip install patchly

Usage

Demonstration on how to use Patchly for sliding-window patchification and subsequent aggregation:

sampler = GridSampler(spatial_size, patch_size, step_size, image)
aggregator = Aggregator(sampler, output_size)

for patch, patch_bbox in sampler:
    aggregator.append(patch, patch_bbox)

prediction = aggregator.get_output()

Example

Working example for inference of a 2D RGB image with Patchly in PyTorch:

import numpy as np
from patchly.sampler import GridSampler
from patchly.aggregator import Aggregator
from torch.utils.data import DataLoader, Dataset
import torch

class ExampleDataset(Dataset):
    def __init__(self, sampler):
        self.sampler = sampler

    def __getitem__(self, idx):
        # Get patch
        patch, patch_bbox = self.sampler.__getitem__(idx)
        # Preprocess patch
        patch = patch.transpose(2, 0, 1)
        return patch, patch_bbox

    def __len__(self):
        return len(self.sampler)

def model(x):
    y = torch.rand((x.shape[0], 8, x.shape[2], x.shape[3]))  # Batch, Class, Width, Height
    return y

# Init GridSampler
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
aggregator = Aggregator(sampler=sampler, output_size=(8, 1000, 1000), spatial_first=False, has_batch_dim=True)

# Run inference
with torch.no_grad():
    for patch, patch_bbox in loader:
        patch_prediction = model(patch)
        aggregator.append(patch_prediction, patch_bbox)

# Finalize aggregation
prediction = aggregator.get_output()
print("Inference completed!")
print("Prediction shape: ", prediction.shape)

License

Distributed under the terms of the Apache Software License 2.0 license, "Patchly" is free and open source software

Acknowledgements

Patchly is developed and maintained by the Applied Computer Vision Lab (ACVL) of Helmholtz Imaging and the Division of Medical Image Computing at the German Cancer Research Center (DKFZ).

Metadata

Release files for patchly 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for patchly 0.0.7
File Size Uploaded
patchly-0.0.7.tar.gz 29.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for patchly 0.0.7
File Interpreter ABI Platform
patchly-0.0.7-py3-none-any.whl Python 3 none any Details

Total release size: 66.8 kB

Release files / patchly-0.0.7.tar.gz

Download URL patchly-0.0.7.tar.gz
Size 29.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c58b661a87ba018262336c496e683f1c23d35fd29a67ff80b0830484c0212597
BLAKE2b-256 checksum
How to use checksums
c242a59778069dda367dc92ad021142b25d61f1266e35274777ff9e5d429a017
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Release files / patchly-0.0.7-py3-none-any.whl

Download URL patchly-0.0.7-py3-none-any.whl
Size 37.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
45089a2e43f1eefe8cd980cb3b5d4c3809b67b53af8788081729aa1c8521c967
BLAKE2b-256 checksum
How to use checksums
ecf559d2c30096ed1a4671eb69ff743089cc6c3db76f2f06d7f130c04b8260f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Release history Release notifications | RSS feed

0.0.19

1 release file

0.0.17

2 release files

0.0.15

2 release files

0.0.9

2 release files

This release

0.0.7 This release

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page