Package: patchly
PatchlyPatchly 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 foundhere.Feature SummaryPatchly provides the following advanced features:N-dimensional image handling (1D, 2D, 3D, ...)Multiple border-handling strategiesSupport for any array-like images (Numpy, Tensor, Zarr, Dask, ...)Memory-mapped image supportPatch overlapGaussian patch averagingSupport for images with non-spatial dimensions (channel dimension, batch dimension, ...)Chunk aggregation to minimize memory consumptionNumpy padding techniquesInstallationYou can installpatchlyviapip:pip install patchlyUsageDemonstration 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)forpatch,patch_bboxinsampler:aggregator.append(patch,patch_bbox)prediction=aggregator.get_output()ExampleWorking example for inference of a 2D RGB image with Patchly in PyTorch:importnumpyasnpfrompatchly.samplerimportGridSamplerfrompatchly.aggregatorimportAggregatorfromtorch.utils.dataimportDataLoader,DatasetimporttorchclassExampleDataset(Dataset):def__init__(self,sampler):self.sampler=samplerdef__getitem__(self,idx):# Get patchpatch,patch_bbox=self.sampler.__getitem__(idx)# Preprocess patchpatch=patch.transpose(2,0,1)returnpatch,patch_bboxdef__len__(self):returnlen(self.sampler)defmodel(x):y=torch.rand((x.shape[0],8,x.shape[2],x.shape[3]))# Batch, Class, Width, Heightreturny# Init GridSamplersampler=GridSampler(image=np.random.random((1000,1000,3)),spatial_size=(1000,1000),patch_size=(100,100),step_size=(50,50))# Init dataloaderloader=DataLoader(ExampleDataset(sampler),batch_size=4,num_workers=0,shuffle=False)# Init aggregatoraggregator=Aggregator(sampler=sampler,output_size=(8,1000,1000),spatial_first=False,has_batch_dim=True)# Run inferencewithtorch.no_grad():forpatch,patch_bboxinloader:patch_prediction=model(patch)aggregator.append(patch_prediction,patch_bbox)# Finalize aggregationprediction=aggregator.get_output()print("Inference completed!")print("Prediction shape: ",prediction.shape)Further examples can be found inexamples.LicenseDistributed under the terms of theApache Software License 2.0license,
"Patchly" is free and open source softwareAcknowledgementsPatchly is developed and maintained by the Applied Computer Vision Lab (ACVL) ofHelmholtz Imagingand theDivision of Medical Image Computingat theGerman Cancer Research Center (DKFZ).