pymic.net_run package

Subpackages

Submodules

pymic.net_run.agent_abstract module

class pymic.net_run.agent_abstract.NetRunAgent(config, stage='train')

Bases: object

The abstract class for medical image segmentation.

Parameters:
  • config – (dict) A dictionary containing the configuration.

  • stage – (str) One of the stage in train (default), inference or test.

Note

The config dictionary should have at least four sections: dataset, network, training and inference. See Quick Start and Fully Supervised Learning for example.

convert_tensor_type(input_tensor)

Convert the type of an input tensor to float or double based on configuration.

create_dataset()

Create datasets for training, validation or testing based on configuraiton.

abstract create_loss_calculator()

Create loss function object.

abstract create_network()

Create network based on configuration.

create_optimizer(params, checkpoint=None)

Create optimizer based on configuration.

Parameters:
  • params – network parameters for optimization. Usually it is obtained by self.get_parameters_to_update().

  • checkpoint – A previous checkpoint to load. Default is None.

get_checkpoint_name()

Get the checkpoint name for inference based on config[‘testing’][‘ckpt_mode’].

abstract get_loss_value(data, pred, gt, param=None)

Get the loss value. Assume pred and gt has been sent to self.device. data is obtained by dataloader, and is a dictionary containing extra information, such as pixel-level weight. By default, such information is not used by standard loss functions such as Dice loss and cross entropy loss.

Parameters:
  • data – (dictionary) A data dictionary obtained by dataloader.

  • pred – (tensor) Prediction result by the network.

  • gt – (tensor) Ground truth.

  • param – (dictionary) Other parameters if needed.

abstract get_parameters_to_update()

Get parameters for update.

abstract get_stage_dataset_from_config(stage)

Create dataset based on training, validation or inference stage.

Parameters:

stage – (str) train, valid or test.

abstract infer()

Inference on testing set.

run()

Run the training or inference code according to configuration.

set_datasets(train_set, valid_set, test_set)

Set customized datasets for training and inference.

Parameters:
  • train_set – (torch.utils.data.Dataset) The training set.

  • valid_set – (torch.utils.data.Dataset) The validation set.

  • test_set – (torch.utils.data.Dataset) The testing set.

set_inferer(inferer)

Set the inferer.

Parameters:

inferer – An inferer object.

set_loss_dict(loss_dict)

Set the available loss functions, including customized loss functions.

Parameters:

loss_dict – (dictionary) A dictionary of available loss functions.

set_net_dict(net_dict)

Set the available networks, including customized networks.

Parameters:

net_dict – (dictionary) A dictionary of available networks.

set_network(net)

Set the network.

Parameters:

net – (nn.Module) A deep learning network.

set_optimizer(optimizer)

Set the optimizer.

Parameters:

optimizer – An optimizer.

set_scheduler(scheduler)

Set the learning rate scheduler.

Parameters:

scheduler – A learning rate scheduler.

set_transform_dict(custom_transform_dict)

Set the available Transforms, including customized Transforms.

Parameters:

custom_transform_dict – (dictionary) A dictionary of available Transforms.

abstract train_valid()

Train and valid.

abstract training()

Train the network

abstract validation()

Evaluate the performance on the validation set.

abstract write_scalars(train_scalars, valid_scalars, lr_value, glob_it)

Write scalars using SummaryWriter.

Parameters:
  • train_scalars – (dictionary) Scalars for training set.

  • valid_scalars – (dictionary) Scalars for validation set.

  • lr_value – (float) Current learning rate.

  • glob_it – (int) Current iteration number.

pymic.net_run.agent_abstract.seed_torch(seed=1)

Set random seed.

Parameters:

seed – (int) the seed for random.

pymic.net_run.agent_cls module

class pymic.net_run.agent_cls.ClassificationAgent(config, stage='train')

Bases: NetRunAgent

The agent for image classificaiton tasks.

Parameters:
  • config – (dict) A dictionary containing the configuration.

  • stage – (str) One of the stage in train (default), inference or test.

Note

The config dictionary should have at least four sections: dataset, network, training and inference. See Quick Start and Fully Supervised Learning for example.

create_loss_calculator()

Create loss function object.

create_network()

Create network based on configuration.

get_evaluation_score(outputs, labels)

Get evaluation score for a prediction.

Parameters:
  • outputs – (tensor) Prediction obtained by a network with size N X C.

  • labels – (tensor) The ground truth with size N X C.

get_loss_value(data, pred, gt, param=None)

Get the loss value. Assume pred and gt has been sent to self.device. data is obtained by dataloader, and is a dictionary containing extra information, such as pixel-level weight. By default, such information is not used by standard loss functions such as Dice loss and cross entropy loss.

Parameters:
  • data – (dictionary) A data dictionary obtained by dataloader.

  • pred – (tensor) Prediction result by the network.

  • gt – (tensor) Ground truth.

  • param – (dictionary) Other parameters if needed.

get_parameters_to_update()

Get parameters for update.

get_stage_dataset_from_config(stage)

Create dataset based on training, validation or inference stage.

Parameters:

stage – (str) train, valid or test.

infer()

Inference on testing set.

train_valid()

Train and valid.

training()

Train the network

validation()

Evaluate the performance on the validation set.

write_scalars(train_scalars, valid_scalars, lr_value, glob_it)

Write scalars using SummaryWriter.

Parameters:
  • train_scalars – (dictionary) Scalars for training set.

  • valid_scalars – (dictionary) Scalars for validation set.

  • lr_value – (float) Current learning rate.

  • glob_it – (int) Current iteration number.

pymic.net_run.agent_cls.random() x in the interval [0, 1).

pymic.net_run.agent_seg module

class pymic.net_run.agent_seg.SegmentationAgent(config, stage='train')

Bases: NetRunAgent

create_loss_calculator()

Create loss function object.

create_network()

Create network based on configuration.

get_loss_value(data, pred, gt, param=None)

Get the loss value. Assume pred and gt has been sent to self.device. data is obtained by dataloader, and is a dictionary containing extra information, such as pixel-level weight. By default, such information is not used by standard loss functions such as Dice loss and cross entropy loss.

Parameters:
  • data – (dictionary) A data dictionary obtained by dataloader.

  • pred – (tensor) Prediction result by the network.

  • gt – (tensor) Ground truth.

  • param – (dictionary) Other parameters if needed.

get_parameters_to_update()

Get parameters for update.

get_stage_dataset_from_config(stage)

Create dataset based on training, validation or inference stage.

Parameters:

stage – (str) train, valid or test.

infer()

Inference on testing set.

infer_with_multiple_checkpoints()

Inference with ensemble of multilple check points.

save_outputs(data)

Save prediction output.

Parameters:

data – (dictionary) A data dictionary with prediciton result and other information such as input image name.

set_postprocessor(postprocessor)

Set post processor after prediction.

Parameters:

postprocessor – post processor, such as an instance of pymic.util.post_process.PostProcess.

train_valid()

Train and valid.

training()

Train the network

validation()

Evaluate the performance on the validation set.

write_scalars(train_scalars, valid_scalars, lr_value, glob_it)

Write scalars using SummaryWriter.

Parameters:
  • train_scalars – (dictionary) Scalars for training set.

  • valid_scalars – (dictionary) Scalars for validation set.

  • lr_value – (float) Current learning rate.

  • glob_it – (int) Current iteration number.

pymic.net_run.agent_seg.random() x in the interval [0, 1).

pymic.net_run.get_optimizer module

pymic.net_run.get_optimizer.get_lr_scheduler(optimizer, sched_params)

Create learning rate scheduler for an optimizer

Parameters:
  • optimizer – An optimizer instance.

  • sched_params – (dict) The parameters required for the scheduler.

Returns:

An instance of the target learning rate scheduler.

pymic.net_run.get_optimizer.get_optimizer(name, net_params, optim_params)

Create an optimizer for learnable parameters.

Parameters:
  • name – (string) Name of the optimizer. Should be one of {SGD, Adam, SparseAdam, Adadelta, Adagrad, Adamax, ASGD, LBFGS, RMSprop, Rprop}.

  • net_params – Learnable parameters that need to be set for an optimizer.

  • optim_params – (dict) The parameters required for the target optimizer.

Returns:

An instance of the target optimizer.

pymic.net_run.infer_func module

class pymic.net_run.infer_func.Inferer(config)

Bases: object

The class for inference. The arguments should be written in the config dictionary, and it has the following fields:

Parameters:
  • sliding_window_enable – (optional, bool) Default is False.

  • sliding_window_size – (optional, list) The sliding window size.

  • sliding_window_stride – (optional, list) The sliding window stride.

  • tta_mode – (optional, int) The test time augmentation mode. Default is 0 (no test time augmentation). The other option is 1 (augmentation with horinzontal and vertical flipping) and 2 (ensemble of inference in axial, sagittal and coronal views for 2D networks applied to 3D volumes)

run(model, image)

Using model for inference on image.

Parameters:
  • model – (nn.Module) a network.

  • image – (tensor) An image.

Module contents