Giter Club home page Giter Club logo

patchly's Introduction

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.

The main functionalities of Patchly consist of a Sampler, which samples patches from an image based on a grid, and an Aggregator, which assembles the patches back into the shape of the original image. There is a multitude of libraries providing similar functionality already. However, they tend to work only for a limited number of usage scenarios before becoming unusable.

Patchly is the first library providing an advanced set of features for users working with sophisticated image processing pipelines requiring patch-based processing.

A complete overview of how the Sampler and Aggregator work and an in-depth explanation of the features can be found here.

Feature Summary

Patchly provides the following advanced features:

  • N-dimensional image handling (1D, 2D, 3D, ...)
  • Multiple border-handling strategies
  • Support for any array-like images (Numpy, Tensor, Zarr, Dask, ...)
  • Memory-mapped image support
  • Patch overlap
  • Gaussian patch averaging
  • Support for images with non-spatial dimensions (channel dimension, batch dimension, ...)
  • Chunk aggregation to minimize memory consumption
  • Numpy padding techniques

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(image, spatial_size, patch_size, step_size)
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)

Further examples can be found in examples.

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).

patchly's People

Contributors

karol-g avatar

Stargazers

Aloys Portafaix avatar xqz-u avatar Shuhan Xiao avatar  avatar  avatar dongwiki avatar Lukas Klein avatar Jet avatar ziyan huang avatar Rohan Banerjee avatar Fabian Isensee avatar Carsten L avatar Guillaume Mougeot avatar Christoph Haarburger avatar Tabris avatar Sebastian Ziegler avatar Justus Schock avatar Dominik Müller avatar Michael Baumgartner avatar Lőrincz-Molnár Szabolcs-Botond avatar Howard Pang avatar  avatar

Watchers

 avatar  avatar Fabian Isensee avatar David Zimmerer avatar  avatar

Forkers

ykkim777

patchly's Issues

List of Warnings

Hey @Karol-G,

I have a list of warnings that occur when using patchly.

/home/l727r/anaconda3/envs/sem_seg/lib/python3.10/site-packages/patchly/array_like.py:23: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
data = torch.tensor(data)

These two are probably caused when patch_size==img_size

/home/l727r/anaconda3/envs/sem_seg/lib/python3.10/site-packages/patchly/sampler.py:392: RuntimeWarning: divide by zero encountered in scalar floor_divide
additional_offset_s = [axis_squeeze_s[axis] // (len(axis_positions_s[axis]) - 1) for axis in range(n_axis_s)]

/home/l727r/anaconda3/envs/sem_seg/lib/python3.10/site-packages/patchly/sampler.py:393: RuntimeWarning: divide by zero encountered in scalar remainder
remainder_offset_s = [axis_squeeze_s[axis] % (len(axis_positions_s[axis]) - 1) for axis in range(n_axis_s)]

Best,
Lars

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.