learning.Patchwise#
- class bartorch.learning.Patchwise#
Bases:
ModuleNetwork applied to an image patch by patch on its own device.
The image stays on its device, normally the host, and is cut into non-overlapping patches over its trailing
len(patch)axes. Groups ofbatchpatches are copied todevice, passed through the network under automatic mixed precision and copied back into place, so a volume or a time series of volumes larger than the device’s memory passes through a network trained on patches. The device holds the network andbatchpatches, or underoverlaptwo groups and the outputs not yet placed. The result is float32 on the input’s device and differentiable with respect to the input and the weights.With
shiftthe patch grid is offset by a random amount at every call, drawn from PyTorch’s generator, so seams fall in a different place at each iteration of a reconstruction, andUnrolledrecomputes a checkpointed step exactly. The image is zero-padded by the offset in front and to a whole patch behind.The network is moved to
deviceat every call if it is elsewhere, so a training loop that moves the whole model does not move it; its parameters remain the same objects.- Parameters:
net (nn.Module) – Called on
(n, channels, [frames,] *patch)asnet(x)ornet(x, sigma), with the keywords this module is called with.patch (sequence of int) – Patch extent along each trailing axis, two or three of them.
device (torch.device or str, default=None) – Where the network runs; the first CUDA device when there is one, the host otherwise.
dtype (torch.dtype or None or "auto", default="auto") – Precision of automatic mixed precision;
Nonedisables it."auto"is bfloat16 on a card that supports it, float16 on one that does not (such as a T4), and none on the host.batch (int, default=1) – Patches per call of the network.
shift (bool, default=True) – Offset the patch grid at random at every call.
overlap (bool, default=True) – Without gradient, with the image on the host and the network on a card, copy one group of patches on a second stream while the network computes on another.
- Raises:
ValueError –
patchnot of two or three positive extents, orbatchbelow one.
Examples
>>> net = learning.Patchwise(learning.UNet(10), patch=(64, 64, 64), batch=4) >>> prior = priors.ImplicitPrior(learning.ComplexNet(net, channels=1))
- forward()#
Apply the network to every patch of
x.- Parameters:
x (torch.Tensor) –
(n, channels, ..., *patched), with the patched axes last.sigma (float or torch.Tensor, default=None) – Passed to the network; one value per item of
xis repeated for each of that item’s patches.**keywords – Passed to the network, repeated per patch like
sigma.
- Returns:
Float32, of
x’s shape, onx’s device.- Return type:
torch.Tensor
- extra_repr()#
Return the extra representation of the module.
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.