MONAI APP¶
-
class
MONAILabelApp
(app_dir, studies, train_stats_path=None)¶ Base Class for Any MONAI Label App
- Parameters
app_dir – path for your App directory
studies – path for studies/datalist
train_stats_path – Path for Training stats json
-
batch_infer
(request, datastore=None)¶ Run batch inference for an existing pre-trained model.
- Parameters
request – JSON object which contains model, params and device
datastore –
Datastore object. If None then use default app level datastore to fetch the images
For example:
{ "device": "cuda" "model": "segmentation_spleen", "images": "unlabeled", "label_tag": "original" }
- Raises
MONAILabelException – When
model
is not found- Returns
JSON containing label and params
-
static
deepgrow_infer_tasks
(model_dir, pipeline=True)¶ Dictionary of Default Infer Tasks for Deepgrow 2D/3D
-
infer
(request, datastore=None)¶ Run Inference for an exiting pre-trained model.
- Parameters
request – JSON object which contains model, image, params and device
datastore –
Datastore object. If None then use default app level datastore to save labels if applicable
For example:
{ "device": "cuda" "model": "segmentation_spleen", "image": "file://xyz", "save_label": "true/false", "label_tag": "original" }
- Raises
MONAILabelException – When
model
is not found- Returns
JSON containing label and params
-
info
()¶ Provide basic information about APP. This information is passed to client. Default implementation is to pass the contents of info.yaml present in APP_DIR
-
next_sample
(request)¶ Run Active Learning selection. User APP has to implement this method to provide next sample for labelling.
- Parameters
request –
JSON object which contains active learning configs that are part APP info
For example:
{ "strategy": "random" }
- Returns
JSON containing next image info that is selected for labeling
-
on_save_label
(image_id, label_id) → Dict[str, Any]¶ Callback method when label is saved into datastore by a remote client
-
scoring
(request, datastore=None)¶ Run scoring task over labels.
- Parameters
request – JSON object which contains model, params and device
datastore –
Datastore object. If None then use default app level datastore to fetch the images
For example:
{ "device": "cuda" "method": "dice", "y": "final", "y_pred": "original", }
- Raises
MONAILabelException – When
method
is not found- Returns
JSON containing result of scoring method
-
abstract
train
(request)¶ Run Training. User APP has to implement this method to run training
- Parameters
request –
JSON object which contains train configs that are part APP info
For example:
{ "device": "cuda" "epochs": 1, "amp": False, "lr": 0.0001, }
- Returns
JSON containing train stats
-
class
Datastore
¶ -
abstract
add_image
(image_id: str, image_filename: str) → str¶ Save a image for the given image id and return the newly saved image’s id
- Parameters
image_id – the image id for the image; If None then base filename will be used
image_filename – the path to the image file
- Returns
the image id for the saved image filename
-
abstract
datalist
() → List[Dict[str, str]]¶ Return a dictionary of image and label pairs corresponding to the ‘image’ and ‘label’ keys respectively
- Returns
the {‘label’: image, ‘label’: label} pairs for training
-
abstract
description
() → str¶ Return the user-set description of the dataset
- Returns
the user-set description of the dataset
-
abstract
get_image
(image_id: str) → Any¶ Retrieve image object based on image id
- Parameters
image_id – the desired image’s id
- Returns
return the “image”
-
abstract
get_image_info
(image_id: str) → Dict[str, Any]¶ Get the image information for the given image id
- Parameters
image_id – the desired image id
- Returns
image info as a list of dictionaries Dict[str, Any]
-
abstract
get_image_uri
(image_id: str) → str¶ Retrieve image uri based on image id
- Parameters
image_id – the desired image’s id
- Returns
return the image uri
-
abstract
get_label
(label_id: str) → Any¶ Retrieve image object based on label id
- Parameters
label_id – the desired label’s id
- Returns
return the “label”
-
abstract
get_label_by_image_id
(image_id: str, tag: str) → str¶ Retrieve label id for the given image id and tag
- Parameters
image_id – the desired image’s id
tag – matching tag name
- Returns
label id
-
abstract
get_label_info
(label_id: str) → Dict[str, Any]¶ Get the label information for the given label id
- Parameters
label_id – the desired label id
- Returns
label info as a list of dictionaries Dict[str, Any]
-
abstract
get_label_uri
(label_id: str) → str¶ Retrieve label uri based on image id
- Parameters
label_id – the desired label’s id
- Returns
return the label uri
-
abstract
get_labeled_images
() → List[str]¶ Get all images that have a corresponding label
- Returns
list of image ids List[str]
-
abstract
get_labels_by_image_id
(image_id: str) → Dict[str, str]¶ Retrieve all label ids for the given image id
- Parameters
image_id – the desired image’s id
- Returns
label ids mapped to the appropriate LabelTag as Dict[str, LabelTag]
-
abstract
get_unlabeled_images
() → List[str]¶ Get all images that have no corresponding label
- Returns
list of image ids List[str]
-
abstract
list_images
() → List[str]¶ Return list of image ids available in the datastore
- Returns
list of image ids List[str]
-
abstract
name
() → str¶ Return the human-readable name of the datastore
- Returns
the name of the dataset
-
abstract
refresh
() → None¶ Refresh the datastore
-
abstract
remove_image
(image_id: str) → None¶ Remove image for the datastore. This will also remove all associated labels.
- Parameters
image_id – the image id for the image to be removed from datastore
-
abstract
remove_label
(label_id: str) → None¶ Remove label from the datastore
- Parameters
label_id – the label id for the label to be removed from datastore
-
abstract
remove_label_by_tag
(label_tag: str) → None¶ Remove all labels for matching tags from the datastore
- Parameters
label_tag – the label tag for the label to be removed from datastore
-
abstract
save_label
(image_id: str, label_filename: str, label_tag: str) → str¶ Save a label for the given image id and return the newly saved label’s id
- Parameters
image_id – the image id for the label
label_filename – the path to the label file
label_tag – the user-provided tag for the label
- Returns
the label id for the given label filename
-
abstract
set_description
(description: str)¶ A human-readable description of the datastore
- Parameters
description – string for description
-
abstract
set_name
(name: str)¶ Set the name of the datastore
- Parameters
name – a human-readable name for the datastore
-
abstract
status
() → Dict[str, Any]¶ Return current statistics of datastore
-
abstract
update_image_info
(image_id: str, info: Dict[str, Any]) → None¶ Update (or create a new) info tag for the desired image
- Parameters
image_id – the id of the image we want to add/update info
info – a dictionary of custom image information Dict[str, Any]
-
abstract
update_label_info
(label_id: str, info: Dict[str, Any]) → None¶ Update (or create a new) info tag for the desired label
- Parameters
label_id – the id of the label we want to add/update info
info – a dictionary of custom label information Dict[str, Any]
-
abstract
-
class
MONAILabelError
(value)¶ -
SERVER_ERROR - Server Error
-
UNKNOWN_ERROR - Unknown Error
-
CLASS_INIT_ERROR - Class Initialization Error
-
MODEL_IMPORT_ERROR - Model Import Error
-
INFERENCE_ERROR - Inference Error
-
APP_INIT_ERROR - Initialization Error
-
APP_INFERENCE_FAILED - Inference Failed
-
APP_TRAIN_FAILED - Train Failed
-
APP_ERROR APP - General Error
-
-
class
MONAILabelException
(error: monailabel.interfaces.exception.MONAILabelError, msg: str)¶ MONAI Label Exception
Tasks¶
-
class
InferType
¶ Type of Inference Model .. attribute:: SEGMENTATION - Segmentation Model
-
CLASSIFICATION - Classification Model
-
DEEPGROW - Deepgrow Interactive Model
-
DEEPEDIT - DeepEdit Interactive Model
-
POSTPROCS - Post-Processor Model
-
OTHERS - Other Model Type
-
-
class
InferTask
(path, network, type: monailabel.interfaces.tasks.infer.InferType, labels, dimension, description, model_state_dict='model', input_key='image', output_label_key='pred', output_json_key='result')¶ Basic Inference Task Helper
- Parameters
path – Model File Path. Supports multiple paths to support versions (Last item will be picked as latest)
network – Model Network (e.g. monai.networks.xyz). None in case if you use TorchScript (torch.jit).
type – Type of Infer (segmentation, deepgrow etc..)
dimension – Input dimension
description – Description
model_state_dict – Key for loading the model state from checkpoint
input_key – Input key for running inference
output_label_key – Output key for storing result/label of inference
output_json_key – Output key for storing result/label of inference
-
abstract
inferer
()¶ Provide Inferer Class
For Example:
return monai.inferers.SlidingWindowInferer(roi_size=[160, 160, 160])
-
inverse_transforms
()¶ Provide List of inverse-transforms. They are normally subset of pre-transforms. This task is performed on output_label (using the references from input_key)
- Return one of the following.
None: Return None to disable running any inverse transforms (default behavior).
Empty: Return [] to run all applicable pre-transforms which has inverse method
list: Return list of specific pre-transforms names/classes to run inverse method
For Example:
return [ monai.transforms.Spacingd, ]
-
abstract
post_transforms
()¶ Provide List of post-transforms
For Example:
return [ monai.transforms.AddChanneld(keys='pred'), monai.transforms.Activationsd(keys='pred', softmax=True), monai.transforms.AsDiscreted(keys='pred', argmax=True), monai.transforms.SqueezeDimd(keys='pred', dim=0), monai.transforms.ToNumpyd(keys='pred'), monailabel.interface.utils.Restored(keys='pred', ref_image='image'), monailabel.interface.utils.ExtremePointsd(keys='pred', result='result', points='points'), monailabel.interface.utils.BoundingBoxd(keys='pred', result='result', bbox='bbox'), ]
-
abstract
pre_transforms
()¶ Provide List of pre-transforms
For Example:
return [ monai.transforms.LoadImaged(keys='image'), monai.transforms.AddChanneld(keys='image'), monai.transforms.Spacingd(keys='image', pixdim=[1.0, 1.0, 1.0]), monai.transforms.ScaleIntensityRanged(keys='image', a_min=-57, a_max=164, b_min=0.0, b_max=1.0, clip=True), ]
-
run_inferer
(data, convert_to_batch=True, device='cuda')¶ Run Inferer over pre-processed Data. Derive this logic to customize the normal behavior. In some cases, you want to implement your own for running chained inferers over pre-processed data
- Parameters
data – pre-processed data
convert_to_batch – convert input to batched input
device – device type run load the model and run inferer
- Returns
updated data with output_key stored that will be used for post-processing
-
static
run_transforms
(data, transforms, inverse=False, log_prefix='POST')¶ Run Transforms
- Parameters
data – Input data dictionary
transforms – List of transforms to run
inverse – Run inverse instead of call/forward function
log_prefix – Logging prefix (POST or PRE)
- Returns
Processed data after running transforms
-
writer
(data, extension=None, dtype=None)¶ You can provide your own writer. However this writer saves the prediction/label mask to file and fetches result json
- Parameters
data – typically it is post processed data
extension – output label extension
dtype – output label dtype
- Returns
tuple of output_file and result_json
-
class
TrainTask
¶ -
abstract
amp
()¶ Use AMP
-
abstract
device
()¶ Provide device name
-
abstract
event_names
()¶ Provide iteration update while training
-
abstract
loss_function
()¶ Provide Lost function
-
abstract
max_epochs
()¶ Max number of epochs to run
-
abstract
network
()¶ Provide network
-
abstract
optimizer
()¶ Provide Optimizer
-
abstract
train_additional_metrics
()¶ Provide any additional metrics to be collected while training
-
abstract
train_data_loader
()¶ Provide Dataloader for training samples
-
abstract
train_handlers
()¶ Provide List of training handlers
-
abstract
train_inferer
()¶ Provide Inferer to be used while training
-
abstract
train_iteration_update
()¶ Provide iteration update while training
-
abstract
train_key_metric
()¶ Provide List of Key Metrics to be collected while training
-
abstract
train_post_transforms
()¶ Provide List of Post-Transforms for training
-
abstract
train_pre_transforms
()¶ Provide List of Pre-Transforms for training
-
abstract
val_additional_metrics
()¶ Provide any additional metrics to be collected while validation
-
abstract
val_data_loader
()¶ Provide Dataloader for validation samples
-
abstract
val_handlers
()¶ Provide List of handlers for validation
-
abstract
val_inferer
()¶ Provide Inferer to be used for validation step
-
abstract
val_iteration_update
()¶ Provide iteration update while validation
-
abstract
val_key_metric
()¶ Provide List of Key metrics to be collected during validation
-
abstract
val_post_transforms
()¶ Provide List of Post-Transforms for validation step
-
abstract
val_pre_transforms
()¶ Provide List of Pre-Transforms for validation step
-
abstract
-
class
Strategy
(description)¶ Basic Active Learning Strategy
Utils¶
-
class
BasicTrainTask
(output_dir, train_datalist, val_datalist, network, load_path=None, load_dict=None, publish_path=None, stats_path=None, device='cuda', max_epochs=1, amp=True, lr=0.0001, train_batch_size=1, train_num_workers=0, train_save_interval=50, val_interval=1, val_batch_size=1, val_num_workers=0, final_filename='checkpoint_final.pt', key_metric_filename='model.pt', **kwargs)¶ This provides Basic Train Task to train segmentation models over MSD Dataset.
- Parameters
output_dir – Output to save the model checkpoints, events etc…
train_datalist – Training List of dictionary that normally contains {image, label}
val_datalist – Validation List of dictionary that normally contains {image, label}
network – If None then UNet with channels(16, 32, 64, 128, 256) is used
load_path – Initialize model from existing checkpoint
load_dict – Provide dictionary to load from checkpoint. If None, then net will be loaded
publish_path – Publish path for best trained model (based on best key metric)
stats_path – Path to save the train stats
device – device name
max_epochs – maximum epochs to run
amp – use amp
lr – Learning Rate (LR)
train_batch_size – train batch size
train_num_workers – number of workers for training
train_save_interval – checkpoint save interval for training
val_interval – validation interval (run every x epochs)
val_batch_size – batch size for validation step
val_num_workers – number of workers for validation step
final_filename – name of final checkpoint that will be saved
-
amp
()¶ Use AMP
-
device
()¶ Provide device name
-
event_names
()¶ Provide iteration update while training
-
loss_function
()¶ Provide Lost function
-
max_epochs
()¶ Max number of epochs to run
-
network
()¶ Provide network
-
optimizer
()¶ Provide Optimizer
-
train_additional_metrics
()¶ Provide any additional metrics to be collected while training
-
train_data_loader
()¶ Provide Dataloader for training samples
-
train_handlers
()¶ Provide List of training handlers
-
train_inferer
()¶ Provide Inferer to be used while training
-
train_iteration_update
()¶ Provide iteration update while training
-
train_key_metric
()¶ Provide List of Key Metrics to be collected while training
-
abstract
train_post_transforms
()¶ Provide List of Post-Transforms for training
-
abstract
train_pre_transforms
()¶ Provide List of Pre-Transforms for training
-
val_additional_metrics
()¶ Provide any additional metrics to be collected while validation
-
val_data_loader
()¶ Provide Dataloader for validation samples
-
val_handlers
()¶ Provide List of handlers for validation
-
abstract
val_inferer
()¶ Provide Inferer to be used for validation step
-
val_iteration_update
()¶ Provide iteration update while validation
-
val_key_metric
()¶ Provide List of Key metrics to be collected during validation
-
val_post_transforms
()¶ Provide List of Post-Transforms for validation step
-
val_pre_transforms
()¶ Provide List of Pre-Transforms for validation step