ncalab.models.wrappers.cascadeNCA

Classes

CascadeNCA

Chain multiple instances of the same NCA model, operating at different

Functions

upscale(→ torch.Tensor)

Upsamples an image.

downscale(→ torch.Tensor)

Downsamples an image.

Module Contents

ncalab.models.wrappers.cascadeNCA.upscale(image: torch.Tensor, scale: float, mode: str = 'nearest') torch.Tensor

Upsamples an image.

Parameters:
  • image (torch.Tensor) – Image tensor in BCWH order.

  • scale (float) – Scale factor. Must be >= 1.0

  • mode (str) – Interpolation mode, defaults to “nearest”.

Returns:

Image tensor in BCWH order.

Return type:

torch.Tensor

ncalab.models.wrappers.cascadeNCA.downscale(image: torch.Tensor, scale: float, mode: str = 'bilinear') torch.Tensor

Downsamples an image.

Parameters:
  • image (torch.Tensor) – Image tensor in BCWH order.

  • scale (float) – Scale factor, must be >= 1.0

  • mode – Interpolation mode, defaults to “bilinear”.

Returns:

Image tensor in BCWH order.

Return type:

torch.Tensor

class ncalab.models.wrappers.cascadeNCA.CascadeNCA(wrapped: ncalab.models.basicNCA.AbstractNCAModel, scales: List[int], steps: List[int], single_model: bool = True)

Bases: ncalab.models.basicNCA.AbstractNCAModel

Chain multiple instances of the same NCA model, operating at different image scales.

The idea is to use this model as a wrapper and drop-in replacement for an existing model. For instance, if we created a model nca = SegmentationNCA(…) and all code to interface with it, we could instead write cascade = CascadeNCA(SegmentationNCA(…), scales, steps) without the need for adjusting any of the interfacing code.

This is still highly experimental. In the future, we’ll work on a cleaner interface for this.

Parameters:
  • wrapped (ncalab.AbstractNCAModel) – Backbone model based on AbstractNCAModel.

  • scales (List[int]) – List of scales to operate at, e.g. [4, 2, 1].

  • steps (List[int]) – List of number of NCA inference time steps.

  • single_model (bool) – Only train a single instance of the NCA model

loss

Compute loss. Needs to be overloaded by any subclass. Please note that the returned dict needs to hold “total” key in which the total loss is stored, which is typically a weighted sum of other losses. The total loss is backpropagated, whereas the other losses are sent to tensorboard.

Parameters:
  • [torch.Tensor] (label) – Input image, BCWH.

  • [torch.Tensor] – Ground truth, BCWH.

Returns:

Dictionary of identifiers mapped to computed losses.

finetune

Prepare model for fine tuning by freezing everything except the final layer, and setting to “train” mode.

Param:

freeze_head

prepare_input

Preprocess input. Intended to be overwritten by subclass, if preprocessing is necessary.

Parameters:

[torch.Tensor] (x) – Input tensor to preprocess.

Returns:

Processed tensor.

head
metrics
wrapped
scales
steps
single_model = True
models: List[ncalab.models.basicNCA.AbstractNCAModel]
forward(x: torch.Tensor, *args, **kwargs) ncalab.prediction.Prediction
Parameters:
  • x (torch.Tensor) – Input image tensor, BCWH.

  • steps (torch.Tensor) – Unused, as steps are defined in constructor.

Returns:

Prediction object

Return type:

Prediction

record(image: torch.Tensor, steps: int | Tuple[int, int] | None = None) List[ncalab.prediction.Prediction]

Records predictions for all time steps and returns the resulting sequence of predictions.

Takes care of scaling the image in between steps.

Parameters:

image (torch.Tensor) – Input image, BCWH.

Returns:

List of Prediction objects.

Return type:

List[Prediction]