Network

DDF / DVF Network

deepreg.model.network.ddf_dvf.build_ddf_dvf_model(moving_image_size: tuple, fixed_image_size: tuple, index_size: int, labeled: bool, batch_size: int, model_config: dict, loss_config: dict) → tensorflow.keras.Model

Build a model which outputs DDF/DVF.

Parameters
  • moving_image_size – (m_dim1, m_dim2, m_dim3)

  • fixed_image_size – (f_dim1, f_dim2, f_dim3)

  • index_size – int, the number of indices for identifying a sample

  • labeled – bool, indicating if the data is labeled

  • batch_size – int, size of mini-batch

  • model_config – config for the model

  • loss_config – config for the loss

Returns

the built tf.keras.Model

deepreg.model.network.ddf_dvf.ddf_dvf_forward(backbone: tensorflow.keras.Model, moving_image: tensorflow.Tensor, fixed_image: tensorflow.Tensor, moving_label: tensorflow.Tensor, None, moving_image_size: tuple, fixed_image_size: tuple, output_dvf: bool)[tensorflow.Tensor, None, tensorflow.Tensor, tensorflow.Tensor, tensorflow.Tensor, None, tensorflow.Tensor]

Perform the network forward pass.

Parameters
  • backbone – model architecture object, e.g. model.backbone.local_net

  • moving_image – tensor of shape (batch, m_dim1, m_dim2, m_dim3)

  • fixed_image – tensor of shape (batch, f_dim1, f_dim2, f_dim3)

  • moving_label – tensor of shape (batch, m_dim1, m_dim2, m_dim3) or None

  • moving_image_size – tuple like (m_dim1, m_dim2, m_dim3)

  • fixed_image_size – tuple like (f_dim1, f_dim2, f_dim3)

  • output_dvf – bool, if true, model outputs dvf, if false, model outputs ddf

Returns

(dvf, ddf, pred_fixed_image, pred_fixed_label, fixed_grid), where

  • dvf is the dense velocity field of shape (batch, f_dim1, f_dim2, f_dim3, 3)

  • ddf is the dense displacement field of shape (batch, f_dim1, f_dim2, f_dim3, 3)

  • pred_fixed_image is the predicted (warped) moving image of shape (batch, f_dim1, f_dim2, f_dim3)

  • pred_fixed_label is the predicted (warped) moving label of shape (batch, f_dim1, f_dim2, f_dim3)

  • fixed_grid is the grid of shape(f_dim1, f_dim2, f_dim3, 3)

Conditional Network

deepreg.model.network.cond.build_conditional_model(moving_image_size: tuple, fixed_image_size: tuple, index_size: int, labeled: bool, batch_size: int, model_config: dict, loss_config: dict) → tensorflow.keras.Model

Build a model which outputs predicted fixed label.

Parameters
  • moving_image_size – (m_dim1, m_dim2, m_dim3)

  • fixed_image_size – (f_dim1, f_dim2, f_dim3)

  • index_size – int, the number of indices for identifying a sample

  • labeled – bool, indicating if the data is labeled

  • batch_size – int, size of mini-batch

  • model_config – config for the model

  • loss_config – config for the loss

Returns

the built tf.keras.Model

deepreg.model.network.cond.conditional_forward(backbone: tensorflow.keras.Model, moving_image: tensorflow.Tensor, fixed_image: tensorflow.Tensor, moving_label: tensorflow.Tensor, None, moving_image_size: tuple, fixed_image_size: tuple)[tensorflow.Tensor, tensorflow.Tensor]

Perform the network forward pass.

Parameters
  • backbone – model architecture object, e.g. model.backbone.local_net

  • moving_image – tensor of shape (batch, m_dim1, m_dim2, m_dim3)

  • fixed_image – tensor of shape (batch, f_dim1, f_dim2, f_dim3)

  • moving_label – tensor of shape (batch, m_dim1, m_dim2, m_dim3) or None

  • moving_image_size – tuple like (m_dim1, m_dim2, m_dim3)

  • fixed_image_size – tuple like (f_dim1, f_dim2, f_dim3)

Returns

(pred_fixed_label, fixed_grid), where

  • pred_fixed_label is the predicted (warped) moving label of shape (batch, f_dim1, f_dim2, f_dim3)

  • fixed_grid is the grid of shape(f_dim1, f_dim2, f_dim3, 3)

Affine Network

deepreg.model.network.affine.affine_forward(backbone: tensorflow.keras.Model, moving_image: tensorflow.Tensor, fixed_image: tensorflow.Tensor, moving_label: tensorflow.Tensor, None, moving_image_size: tuple, fixed_image_size: tuple)

Perform the network forward pass.

Parameters
  • backbone – model architecture object, e.g. model.backbone.local_net

  • moving_image – tensor of shape (batch, m_dim1, m_dim2, m_dim3)

  • fixed_image – tensor of shape (batch, f_dim1, f_dim2, f_dim3)

  • moving_label – tensor of shape (batch, m_dim1, m_dim2, m_dim3) or None

  • moving_image_size – tuple like (m_dim1, m_dim2, m_dim3)

  • fixed_image_size – tuple like (f_dim1, f_dim2, f_dim3)

Returns

tuple(affine, ddf, pred_fixed_image, pred_fixed_label, fixed_grid), where

  • affine is the affine transformation matrix predicted by the network (batch, 4, 3)

  • ddf is the dense displacement field of shape (batch, f_dim1, f_dim2, f_dim3, 3)

  • pred_fixed_image is the predicted (warped) moving image of shape (batch, f_dim1, f_dim2, f_dim3)

  • pred_fixed_label is the predicted (warped) moving label of shape (batch, f_dim1, f_dim2, f_dim3)

  • fixed_grid is the grid of shape(f_dim1, f_dim2, f_dim3, 3)

deepreg.model.network.affine.build_affine_model(moving_image_size: tuple, fixed_image_size: tuple, index_size: int, labeled: bool, batch_size: int, model_config: dict, loss_config: dict)

Build a model which outputs the parameters for affine transformation.

Parameters
  • moving_image_size – (m_dim1, m_dim2, m_dim3)

  • fixed_image_size – (f_dim1, f_dim2, f_dim3)

  • index_size – int, the number of indices for identifying a sample

  • labeled – bool, indicating if the data is labeled

  • batch_size – int, size of mini-batch

  • model_config – config for the model

  • loss_config – config for the loss

Returns

the built tf.keras.Model

Utils

deepreg.model.network.build.build_model(moving_image_size: tuple, fixed_image_size: tuple, index_size: int, labeled: bool, batch_size: int, model_config: dict, loss_config: dict)

Parsing algorithm types to model building functions.

Parameters
  • moving_image_size – [m_dim1, m_dim2, m_dim3]

  • fixed_image_size – [f_dim1, f_dim2, f_dim3]

  • index_size – dataset size

  • labeled – true if the label of moving/fixed images are provided

  • batch_size – mini-batch size

  • model_config – model configuration, e.g. dictionary return from parser.yaml.load

  • loss_config – loss configuration, e.g. dictionary return from parser.yaml.load

Returns

the built tf.keras.Model

Module to build backbone modules based on passed inputs.

deepreg.model.network.util.add_ddf_loss(model: tensorflow.keras.Model, ddf: tensorflow.Tensor, loss_config: dict) → tensorflow.keras.Model

Add regularization loss of ddf into model.

Parameters
  • model – tf.keras.Model

  • ddf – tensor of shape (batch, m_dim1, m_dim2, m_dim3, 3)

  • loss_config – config for loss

deepreg.model.network.util.add_image_loss(model: tensorflow.keras.Model, fixed_image: tensorflow.Tensor, pred_fixed_image: tensorflow.Tensor, loss_config: dict) → tensorflow.keras.Model

Add image dissimilarity loss of ddf into model.

Parameters
  • model – tf.keras.Model

  • fixed_image – tensor of shape (batch, f_dim1, f_dim2, f_dim3)

  • pred_fixed_image – tensor of shape (batch, f_dim1, f_dim2, f_dim3)

  • loss_config – config for loss

deepreg.model.network.util.add_label_loss(model: tensorflow.keras.Model, grid_fixed: tensorflow.Tensor, fixed_label: tensorflow.Tensor, None, pred_fixed_label: tensorflow.Tensor, None, loss_config: dict) → tensorflow.keras.Model

Add label dissimilarity loss of ddf into model.

Parameters
  • model – tf.keras.Model

  • grid_fixed – tensor of shape (f_dim1, f_dim2, f_dim3, 3)

  • fixed_label – tensor of shape (batch, f_dim1, f_dim2, f_dim3)

  • pred_fixed_label – tensor of shape (batch, f_dim1, f_dim2, f_dim3)

  • loss_config – config for loss

deepreg.model.network.util.build_backbone(image_size: tuple, out_channels: int, model_config: dict, method_name: str) → tensorflow.keras.Model

Backbone model accepts a single input of shape (batch, dim1, dim2, dim3, ch_in) and returns a single output of shape (batch, dim1, dim2, dim3, ch_out).

Parameters
  • image_size – tuple, dims of image, (dim1, dim2, dim3)

  • out_channels – int, number of out channels, ch_out

  • method_name – str, one of ddf, dvf and conditional

  • model_config – dict, model configuration, returned from parser.yaml.load

Returns

tf.keras.Model

deepreg.model.network.util.build_inputs(moving_image_size: tuple, fixed_image_size: tuple, index_size: int, batch_size: int, labeled: bool)[tensorflow.keras.Input, tensorflow.keras.Input, tensorflow.keras.Input, tensorflow.keras.Input, tensorflow.keras.Input]

Configure a pair of moving and fixed images and a pair of moving and fixed labels as model input and returns model input tf.keras.Input.

Parameters
  • moving_image_size – tuple, dims of moving images, [m_dim1, m_dim2, m_dim3]

  • fixed_image_size – tuple, dims of fixed images, [f_dim1, f_dim2, f_dim3]

  • index_size – int, dataset size (number of images)

  • batch_size – int, mini-batch size

  • labeled – Boolean, true if we have label data

Returns

5 (if labeled=True) or 3 (if labeled=False) tf.keras.Input objects