### PyTorch Inference with Patchly for 2D RGB Images Source: https://github.com/mic-dkfz/patchly/blob/main/README.md An example showcasing how to integrate Patchly with PyTorch for performing inference on a 2D RGB image. It defines a custom Dataset for Patchly's sampler, a dummy model, and demonstrates the inference loop with aggregation. ```Python 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) ``` -------------------------------- ### Basic Patchly Usage: Sampling and Aggregation Source: https://github.com/mic-dkfz/patchly/blob/main/README.md Demonstrates the fundamental usage of Patchly's GridSampler and Aggregator for patchifying an image and then reassembling the processed patches. This is the core workflow for patch-based processing. ```Python from patchly.sampler import GridSampler from patchly.aggregator import Aggregator # Assuming 'image', 'spatial_size', 'patch_size', 'step_size', and 'output_size' are defined 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() ``` -------------------------------- ### Sample N-dimensional Images with Patchly Source: https://github.com/mic-dkfz/patchly/blob/main/OVERVIEW.md The Patchly Sampler extracts patches from N-dimensional images based on a grid. It supports various array-like formats and provides patches along with their locations on-the-fly. ```Python from patchly.sampler import Sampler # Example usage (assuming 'image' is a compatible array-like object) # sampler = Sampler(image, patch_size=(64, 64), step_size=(32, 32)) # for patch, location in sampler: # # Process patch and location # pass ``` -------------------------------- ### Aggregate Patches into Original Image Shape with Patchly Source: https://github.com/mic-dkfz/patchly/blob/main/OVERVIEW.md The Patchly Aggregator reconstructs an image from its constituent patches. It works in conjunction with the Sampler, assembling patches based on their provided locations. ```Python from patchly.aggregator import Aggregator # Example usage (assuming 'patches' is an iterable of (patch, location) tuples) # aggregator = Aggregator(original_shape=(512, 512), patch_size=(64, 64)) # reconstructed_image = aggregator.aggregate(patches) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.