From 8fdfd2bb635996f7e4eca3c2a7820e8b032681a6 Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Wed, 20 Oct 2021 17:28:24 +0800 Subject: [PATCH 1/5] [DLMED] add example in doc-string Signed-off-by: Nic Ma --- monai/handlers/classification_saver.py | 4 +++ monai/handlers/confusion_matrix.py | 6 +++-- monai/handlers/hausdorff_distance.py | 6 +++-- monai/handlers/ignite_metric.py | 6 +++-- monai/handlers/mean_dice.py | 6 +++-- monai/handlers/metrics_saver.py | 2 ++ monai/handlers/mlflow_handler.py | 2 ++ monai/handlers/regression_metrics.py | 36 ++++++++++++++++++++------ monai/handlers/roc_auc.py | 6 +++-- monai/handlers/segmentation_saver.py | 4 +++ monai/handlers/stats_handler.py | 2 ++ monai/handlers/surface_distance.py | 6 +++-- monai/handlers/tensorboard_handlers.py | 6 +++++ 13 files changed, 72 insertions(+), 20 deletions(-) diff --git a/monai/handlers/classification_saver.py b/monai/handlers/classification_saver.py index 815be87754..fbba9b4148 100644 --- a/monai/handlers/classification_saver.py +++ b/monai/handlers/classification_saver.py @@ -55,9 +55,13 @@ def __init__( batch_transform: a callable that is used to extract the `meta_data` dictionary of the input images from `ignite.engine.state.batch`. the purpose is to get the input filenames from the `meta_data` and store with classification results together. + for example, if already decollated data in postprocessing, the batch_transform can be: + `lambda x: [i["image_meta_dict"] for i in x]` or `from_engine("image_meta_dict")`. output_transform: a callable that is used to extract the model prediction data from `ignite.engine.state.output`. the first dimension of its output will be treated as the batch dimension. each item in the batch will be saved individually. + for example, if already decollated data in postprocessing, the output_transform can be: + `lambda x: [i["pred"] for i in x]` or `from_engine("pred")`. name: identifier of logging.logger to use, defaulting to `engine.logger`. save_rank: only the handler on specified rank will save to CSV file in multi-gpus validation, default to 0. diff --git a/monai/handlers/confusion_matrix.py b/monai/handlers/confusion_matrix.py index 44edcae00f..629d5ed007 100644 --- a/monai/handlers/confusion_matrix.py +++ b/monai/handlers/confusion_matrix.py @@ -43,8 +43,10 @@ def __init__( output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. - for example: if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, - output_transform can be `lambda x: (x["pred"], x["label"])`. + for example, if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, + output_transform can be `lambda x: (x["pred"], x["label"])` or `from_engine(["pred", "label"])`. + if already decollated data in postprocessing, the output_transform can be: + `lambda x: ([i["pred"] for i in x], [i["label"] for i in x])` or `from_engine(["pred", "label"])`. save_details: whether to save metric computation details per image, for example: TP/TN/FP/FN of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. diff --git a/monai/handlers/hausdorff_distance.py b/monai/handlers/hausdorff_distance.py index ea505ef02e..71ff2f74c0 100644 --- a/monai/handlers/hausdorff_distance.py +++ b/monai/handlers/hausdorff_distance.py @@ -44,8 +44,10 @@ def __init__( output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. - for example: if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, - output_transform can be `lambda x: (x["pred"], x["label"])`. + for example, if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, + output_transform can be `lambda x: (x["pred"], x["label"])` or `from_engine(["pred", "label"])`. + if already decollated data in postprocessing, the output_transform can be: + `lambda x: ([i["pred"] for i in x], [i["label"] for i in x])` or `from_engine(["pred", "label"])`. save_details: whether to save metric computation details per image, for example: hausdorff distance of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. diff --git a/monai/handlers/ignite_metric.py b/monai/handlers/ignite_metric.py index cc87842fa5..f6d15580a3 100644 --- a/monai/handlers/ignite_metric.py +++ b/monai/handlers/ignite_metric.py @@ -41,8 +41,10 @@ class IgniteMetric(Metric): # type: ignore[valid-type, misc] # due to optional_ output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. - for example: if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, - output_transform can be `lambda x: (x["pred"], x["label"])`. + for example, if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, + output_transform can be `lambda x: (x["pred"], x["label"])` or `from_engine(["pred", "label"])`. + if already decollated data in postprocessing, the output_transform can be: + `lambda x: ([i["pred"] for i in x], [i["label"] for i in x])` or `from_engine(["pred", "label"])`. save_details: whether to save metric computation details per image, for example: mean_dice of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. diff --git a/monai/handlers/mean_dice.py b/monai/handlers/mean_dice.py index 9d3b95a735..028d65a6d7 100644 --- a/monai/handlers/mean_dice.py +++ b/monai/handlers/mean_dice.py @@ -32,8 +32,10 @@ def __init__( output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. - for example: if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, - output_transform can be `lambda x: (x["pred"], x["label"])`. + for example, if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, + output_transform can be `lambda x: (x["pred"], x["label"])` or `from_engine(["pred", "label"])`. + if already decollated data in postprocessing, the output_transform can be: + `lambda x: ([i["pred"] for i in x], [i["label"] for i in x])` or `from_engine(["pred", "label"])`. save_details: whether to save metric computation details per image, for example: mean dice of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. diff --git a/monai/handlers/metrics_saver.py b/monai/handlers/metrics_saver.py index 4c722eb35b..08c0fc5e21 100644 --- a/monai/handlers/metrics_saver.py +++ b/monai/handlers/metrics_saver.py @@ -50,6 +50,8 @@ class MetricsSaver: batch_transform: a callable that is used to extract the `meta_data` dictionary of the input images from `ignite.engine.state.batch` if saving metric details. the purpose is to get the input filenames from the `meta_data` and store with metric details together. + for example, if already decollated data in postprocessing, the batch_transform can be: + `lambda x: [i["image_meta_dict"] for i in x]` or `from_engine("image_meta_dict")`. summary_ops: expected computation operations to generate the summary report. it can be: None, "*" or list of strings, default to None. None - don't generate summary report for every expected metric_details. diff --git a/monai/handlers/mlflow_handler.py b/monai/handlers/mlflow_handler.py index b3ee887983..6ead84f1ec 100644 --- a/monai/handlers/mlflow_handler.py +++ b/monai/handlers/mlflow_handler.py @@ -56,6 +56,8 @@ class MLFlowHandler: By default this value logging happens when every iteration completed. The default behavior is to track loss from output[0] as output is a decollated list and we replicated loss value for every item of the decollated list. + if output is list of dictionaries, the output_transform can be: + `lambda x: x[0]["loss"]` or `from_engine(["loss"], first=True)`. global_epoch_transform: a callable that is used to customize global epoch number. For example, in evaluation, the evaluator engine might want to track synced epoch number with the trainer engine. diff --git a/monai/handlers/regression_metrics.py b/monai/handlers/regression_metrics.py index bef3123bd8..980db4bf67 100644 --- a/monai/handlers/regression_metrics.py +++ b/monai/handlers/regression_metrics.py @@ -28,8 +28,10 @@ def __init__(self, output_transform: Callable = lambda x: x, save_details: bool output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. - for example: if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, - output_transform can be `lambda x: (x["pred"], x["label"])`. + for example, if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, + output_transform can be `lambda x: (x["pred"], x["label"])` or `from_engine(["pred", "label"])`. + if already decollated data in postprocessing, the output_transform can be: + `lambda x: ([i["pred"] for i in x], [i["label"] for i in x])` or `from_engine(["pred", "label"])`. save_details: whether to save metric computation details per image, for example: mean squared error of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. @@ -49,8 +51,14 @@ def __init__(self, output_transform: Callable = lambda x: x, save_details: bool """ Args: - output_transform: transform the ignite.engine.state.output into [y_pred, y] pair. - save_details: whether to save metric computation details per image, for example: mean absolute error of every image. + output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then + construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or + lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. + for example, if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, + output_transform can be `lambda x: (x["pred"], x["label"])` or `from_engine(["pred", "label"])`. + if already decollated data in postprocessing, the output_transform can be: + `lambda x: ([i["pred"] for i in x], [i["label"] for i in x])` or `from_engine(["pred", "label"])`. + save_details: whether to save metric computation details per image, for example: mean squared error of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. See also: @@ -69,8 +77,14 @@ def __init__(self, output_transform: Callable = lambda x: x, save_details: bool """ Args: - output_transform: transform the ignite.engine.state.output into [y_pred, y] pair. - save_details: whether to save metric computation details per image, for example: root mean squared error of every image. + output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then + construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or + lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. + for example, if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, + output_transform can be `lambda x: (x["pred"], x["label"])` or `from_engine(["pred", "label"])`. + if already decollated data in postprocessing, the output_transform can be: + `lambda x: ([i["pred"] for i in x], [i["label"] for i in x])` or `from_engine(["pred", "label"])`. + save_details: whether to save metric computation details per image, for example: mean squared error of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. See also: @@ -93,8 +107,14 @@ def __init__( Args: max_val: The dynamic range of the images/volumes (i.e., the difference between the maximum and the minimum allowed values e.g. 255 for a uint8 image). - output_transform: transform the ignite.engine.state.output into [y_pred, y] pair. - save_details: whether to save metric computation details per image, for example: PSNR of every image. + output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then + construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or + lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. + for example, if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, + output_transform can be `lambda x: (x["pred"], x["label"])` or `from_engine(["pred", "label"])`. + if already decollated data in postprocessing, the output_transform can be: + `lambda x: ([i["pred"] for i in x], [i["label"] for i in x])` or `from_engine(["pred", "label"])`. + save_details: whether to save metric computation details per image, for example: mean squared error of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. reduction: {``"none"``, ``"mean"``, ``"sum"``, ``"mean_batch"``, ``"sum_batch"``, diff --git a/monai/handlers/roc_auc.py b/monai/handlers/roc_auc.py index 90c5fe2f03..a12f20e6f1 100644 --- a/monai/handlers/roc_auc.py +++ b/monai/handlers/roc_auc.py @@ -36,8 +36,10 @@ class ROCAUC(IgniteMetric): # type: ignore[valid-type, misc] # due to optional output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. - for example: if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, - output_transform can be `lambda x: (x["pred"], x["label"])`. + for example, if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, + output_transform can be `lambda x: (x["pred"], x["label"])` or `from_engine(["pred", "label"])`. + if already decollated data in postprocessing, the output_transform can be: + `lambda x: ([i["pred"] for i in x], [i["label"] for i in x])` or `from_engine(["pred", "label"])`. Note: ROCAUC expects y to be comprised of 0's and 1's. diff --git a/monai/handlers/segmentation_saver.py b/monai/handlers/segmentation_saver.py index 27b2cbc039..f7976633dd 100644 --- a/monai/handlers/segmentation_saver.py +++ b/monai/handlers/segmentation_saver.py @@ -113,9 +113,13 @@ def __init__( batch_transform: a callable that is used to extract the `meta_data` dictionary of the input images from `ignite.engine.state.batch`. the purpose is to extract necessary information from the meta data: filename, affine, original_shape, etc. + for example, if already decollated data in postprocessing, the batch_transform can be: + `lambda x: [i["image_meta_dict"] for i in x]` or `from_engine("image_meta_dict")`. output_transform: a callable that is used to extract the model prediction data from `ignite.engine.state.output`. the first dimension of its output will be treated as the batch dimension. each item in the batch will be saved individually. + for example, if already decollated data in postprocessing, the output_transform can be: + `lambda x: [i["pred"] for i in x]` or `from_engine("pred")`. name: identifier of logging.logger to use, defaulting to `engine.logger`. """ diff --git a/monai/handlers/stats_handler.py b/monai/handlers/stats_handler.py index f0fcf166e8..130d8e1751 100644 --- a/monai/handlers/stats_handler.py +++ b/monai/handlers/stats_handler.py @@ -66,6 +66,8 @@ def __init__( By default this value logging happens when every iteration completed. The default behavior is to print loss from output[0] as output is a decollated list and we replicated loss value for every item of the decollated list. + if output is list of dictionaries, the output_transform can be: + `lambda x: x[0]["loss"]` or `from_engine(["loss"], first=True)`. global_epoch_transform: a callable that is used to customize global epoch number. For example, in evaluation, the evaluator engine might want to print synced epoch number with the trainer engine. diff --git a/monai/handlers/surface_distance.py b/monai/handlers/surface_distance.py index 3d010888c1..6bcff7465f 100644 --- a/monai/handlers/surface_distance.py +++ b/monai/handlers/surface_distance.py @@ -41,8 +41,10 @@ def __init__( output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. - for example: if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, - output_transform can be `lambda x: (x["pred"], x["label"])`. + for example, if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, + output_transform can be `lambda x: (x["pred"], x["label"])` or `from_engine(["pred", "label"])`. + if already decollated data in postprocessing, the output_transform can be: + `lambda x: ([i["pred"] for i in x], [i["label"] for i in x])` or `from_engine(["pred", "label"])`. save_details: whether to save metric computation details per image, for example: surface dice of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. diff --git a/monai/handlers/tensorboard_handlers.py b/monai/handlers/tensorboard_handlers.py index 1dd89c7efb..9dac76faed 100644 --- a/monai/handlers/tensorboard_handlers.py +++ b/monai/handlers/tensorboard_handlers.py @@ -105,6 +105,8 @@ def __init__( By default this value plotting happens when every iteration completed. The default behavior is to print loss from output[0] as output is a decollated list and we replicated loss value for every item of the decollated list. + if output is list of dictionaries, the output_transform can be: + `lambda x: x[0]["loss"]` or `from_engine(["loss"], first=True)`. global_epoch_transform: a callable that is used to customize global epoch number. For example, in evaluation, the evaluator engine might want to use trainer engines epoch number when plotting epoch vs metric curves. @@ -275,9 +277,13 @@ def __init__( batch_transform: a callable that is used to extract `image` and `label` from `ignite.engine.state.batch`, then construct `(image, label)` pair. for example: if `ignite.engine.state.batch` is `{"image": xxx, "label": xxx, "other": xxx}`, `batch_transform` can be `lambda x: (x["image"], x["label"])`. + if already decollated data in postprocessing, the batch_transform can be: + `lambda x: ([i["image"] for i in x], [i["label"] for i in x])` or `from_engine(["image", "label"])`. will use the result to plot image from `result[0][index]` and plot label from `result[1][index]`. output_transform: a callable that is used to extract the `predictions` data from `ignite.engine.state.output`, will use the result to plot output from `result[index]`. + for example, if already decollated data in postprocessing, the output_transform can be: + `lambda x: [i["pred"] for i in x]` or `from_engine("pred")`. global_iter_transform: a callable that is used to customize global step number for TensorBoard. For example, in evaluation, the evaluator engine needs to know current epoch from trainer. index: plot which element in a data batch, default is the first element. From 0b4579b557e4c512931848e5d3e683b077f0c777 Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Wed, 20 Oct 2021 20:02:49 +0800 Subject: [PATCH 2/5] [DLMED] add example about copy_cache Signed-off-by: Nic Ma --- monai/data/dataset.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/monai/data/dataset.py b/monai/data/dataset.py index 474684c4e2..debf9c6aa4 100644 --- a/monai/data/dataset.py +++ b/monai/data/dataset.py @@ -589,8 +589,9 @@ def __init__( If num_workers is None then the number returned by os.cpu_count() is used. progress: whether to display a progress bar. copy_cache: whether to `deepcopy` the cache content before applying the random transforms, - default to `True`. if the random transforms don't modify the cache content - or every cache item is only used once in a `multi-processing` environment, + default to `True`. if the random transforms don't modify the cached content + (for example, randomly crop from the cached image and deepcopy the crop region) + or if every cache item is only used once in a `multi-processing` environment, may set `copy=False` for better performance. """ if not isinstance(transform, Compose): From e73b26f4ff13bdd6aaae5d8078990777b4e9661e Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Fri, 22 Oct 2021 09:25:08 +0800 Subject: [PATCH 3/5] [DLMED] update according to comments Signed-off-by: Nic Ma --- monai/handlers/classification_saver.py | 10 +++++---- monai/handlers/confusion_matrix.py | 7 +++---- monai/handlers/hausdorff_distance.py | 7 +++---- monai/handlers/ignite_metric.py | 7 +++---- monai/handlers/mean_dice.py | 7 +++---- monai/handlers/metric_logger.py | 3 +++ monai/handlers/metrics_saver.py | 5 +++-- monai/handlers/mlflow_handler.py | 5 +++-- monai/handlers/regression_metrics.py | 28 +++++++++++--------------- monai/handlers/roc_auc.py | 7 +++---- monai/handlers/segmentation_saver.py | 10 +++++---- monai/handlers/stats_handler.py | 5 +++-- monai/handlers/surface_distance.py | 7 +++---- monai/handlers/tensorboard_handlers.py | 15 ++++++++------ 14 files changed, 63 insertions(+), 60 deletions(-) diff --git a/monai/handlers/classification_saver.py b/monai/handlers/classification_saver.py index fbba9b4148..4481ae0fec 100644 --- a/monai/handlers/classification_saver.py +++ b/monai/handlers/classification_saver.py @@ -55,13 +55,15 @@ def __init__( batch_transform: a callable that is used to extract the `meta_data` dictionary of the input images from `ignite.engine.state.batch`. the purpose is to get the input filenames from the `meta_data` and store with classification results together. - for example, if already decollated data in postprocessing, the batch_transform can be: - `lambda x: [i["image_meta_dict"] for i in x]` or `from_engine("image_meta_dict")`. + `engine.state` and `batch_transform` inherit from the ignite concept: + https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: + https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. output_transform: a callable that is used to extract the model prediction data from `ignite.engine.state.output`. the first dimension of its output will be treated as the batch dimension. each item in the batch will be saved individually. - for example, if already decollated data in postprocessing, the output_transform can be: - `lambda x: [i["pred"] for i in x]` or `from_engine("pred")`. + `engine.state` and `output_transform` inherit from the ignite concept: + https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: + https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. name: identifier of logging.logger to use, defaulting to `engine.logger`. save_rank: only the handler on specified rank will save to CSV file in multi-gpus validation, default to 0. diff --git a/monai/handlers/confusion_matrix.py b/monai/handlers/confusion_matrix.py index 629d5ed007..24adeb879c 100644 --- a/monai/handlers/confusion_matrix.py +++ b/monai/handlers/confusion_matrix.py @@ -43,10 +43,9 @@ def __init__( output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. - for example, if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, - output_transform can be `lambda x: (x["pred"], x["label"])` or `from_engine(["pred", "label"])`. - if already decollated data in postprocessing, the output_transform can be: - `lambda x: ([i["pred"] for i in x], [i["label"] for i in x])` or `from_engine(["pred", "label"])`. + `engine.state` and `output_transform` inherit from the ignite concept: + https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: + https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. save_details: whether to save metric computation details per image, for example: TP/TN/FP/FN of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. diff --git a/monai/handlers/hausdorff_distance.py b/monai/handlers/hausdorff_distance.py index 71ff2f74c0..321e840353 100644 --- a/monai/handlers/hausdorff_distance.py +++ b/monai/handlers/hausdorff_distance.py @@ -44,10 +44,9 @@ def __init__( output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. - for example, if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, - output_transform can be `lambda x: (x["pred"], x["label"])` or `from_engine(["pred", "label"])`. - if already decollated data in postprocessing, the output_transform can be: - `lambda x: ([i["pred"] for i in x], [i["label"] for i in x])` or `from_engine(["pred", "label"])`. + `engine.state` and `output_transform` inherit from the ignite concept: + https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: + https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. save_details: whether to save metric computation details per image, for example: hausdorff distance of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. diff --git a/monai/handlers/ignite_metric.py b/monai/handlers/ignite_metric.py index f6d15580a3..ec99e83752 100644 --- a/monai/handlers/ignite_metric.py +++ b/monai/handlers/ignite_metric.py @@ -41,10 +41,9 @@ class IgniteMetric(Metric): # type: ignore[valid-type, misc] # due to optional_ output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. - for example, if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, - output_transform can be `lambda x: (x["pred"], x["label"])` or `from_engine(["pred", "label"])`. - if already decollated data in postprocessing, the output_transform can be: - `lambda x: ([i["pred"] for i in x], [i["label"] for i in x])` or `from_engine(["pred", "label"])`. + `engine.state` and `output_transform` inherit from the ignite concept: + https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: + https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. save_details: whether to save metric computation details per image, for example: mean_dice of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. diff --git a/monai/handlers/mean_dice.py b/monai/handlers/mean_dice.py index 028d65a6d7..6c270caa4c 100644 --- a/monai/handlers/mean_dice.py +++ b/monai/handlers/mean_dice.py @@ -32,10 +32,9 @@ def __init__( output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. - for example, if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, - output_transform can be `lambda x: (x["pred"], x["label"])` or `from_engine(["pred", "label"])`. - if already decollated data in postprocessing, the output_transform can be: - `lambda x: ([i["pred"] for i in x], [i["label"] for i in x])` or `from_engine(["pred", "label"])`. + `engine.state` and `output_transform` inherit from the ignite concept: + https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: + https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. save_details: whether to save metric computation details per image, for example: mean dice of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. diff --git a/monai/handlers/metric_logger.py b/monai/handlers/metric_logger.py index 64553955b7..048f230d1a 100644 --- a/monai/handlers/metric_logger.py +++ b/monai/handlers/metric_logger.py @@ -57,6 +57,9 @@ class MetricLogger: Args: loss_transform: Converts the `output` value from the trainer's state into a loss value + `engine.state` and `loss_transform` inherit from the ignite concept: + https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: + https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. metric_transform: Converts the metric value coming from the trainer/evaluator's state into a storable value evaluator: Optional evaluator to consume metric results from at the end of its evaluation run """ diff --git a/monai/handlers/metrics_saver.py b/monai/handlers/metrics_saver.py index 08c0fc5e21..d6aa0c7b9f 100644 --- a/monai/handlers/metrics_saver.py +++ b/monai/handlers/metrics_saver.py @@ -50,8 +50,9 @@ class MetricsSaver: batch_transform: a callable that is used to extract the `meta_data` dictionary of the input images from `ignite.engine.state.batch` if saving metric details. the purpose is to get the input filenames from the `meta_data` and store with metric details together. - for example, if already decollated data in postprocessing, the batch_transform can be: - `lambda x: [i["image_meta_dict"] for i in x]` or `from_engine("image_meta_dict")`. + `engine.state` and `batch_transform` inherit from the ignite concept: + https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: + https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. summary_ops: expected computation operations to generate the summary report. it can be: None, "*" or list of strings, default to None. None - don't generate summary report for every expected metric_details. diff --git a/monai/handlers/mlflow_handler.py b/monai/handlers/mlflow_handler.py index d82c09719f..7bf6596437 100644 --- a/monai/handlers/mlflow_handler.py +++ b/monai/handlers/mlflow_handler.py @@ -59,8 +59,9 @@ class MLFlowHandler: By default this value logging happens when every iteration completed. The default behavior is to track loss from output[0] as output is a decollated list and we replicated loss value for every item of the decollated list. - if output is list of dictionaries, the output_transform can be: - `lambda x: x[0]["loss"]` or `from_engine(["loss"], first=True)`. + `engine.state` and `output_transform` inherit from the ignite concept: + https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: + https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. global_epoch_transform: a callable that is used to customize global epoch number. For example, in evaluation, the evaluator engine might want to track synced epoch number with the trainer engine. diff --git a/monai/handlers/regression_metrics.py b/monai/handlers/regression_metrics.py index 980db4bf67..9758d86bae 100644 --- a/monai/handlers/regression_metrics.py +++ b/monai/handlers/regression_metrics.py @@ -28,10 +28,9 @@ def __init__(self, output_transform: Callable = lambda x: x, save_details: bool output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. - for example, if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, - output_transform can be `lambda x: (x["pred"], x["label"])` or `from_engine(["pred", "label"])`. - if already decollated data in postprocessing, the output_transform can be: - `lambda x: ([i["pred"] for i in x], [i["label"] for i in x])` or `from_engine(["pred", "label"])`. + `engine.state` and `output_transform` inherit from the ignite concept: + https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: + https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. save_details: whether to save metric computation details per image, for example: mean squared error of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. @@ -54,10 +53,9 @@ def __init__(self, output_transform: Callable = lambda x: x, save_details: bool output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. - for example, if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, - output_transform can be `lambda x: (x["pred"], x["label"])` or `from_engine(["pred", "label"])`. - if already decollated data in postprocessing, the output_transform can be: - `lambda x: ([i["pred"] for i in x], [i["label"] for i in x])` or `from_engine(["pred", "label"])`. + `engine.state` and `output_transform` inherit from the ignite concept: + https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: + https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. save_details: whether to save metric computation details per image, for example: mean squared error of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. @@ -80,10 +78,9 @@ def __init__(self, output_transform: Callable = lambda x: x, save_details: bool output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. - for example, if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, - output_transform can be `lambda x: (x["pred"], x["label"])` or `from_engine(["pred", "label"])`. - if already decollated data in postprocessing, the output_transform can be: - `lambda x: ([i["pred"] for i in x], [i["label"] for i in x])` or `from_engine(["pred", "label"])`. + `engine.state` and `output_transform` inherit from the ignite concept: + https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: + https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. save_details: whether to save metric computation details per image, for example: mean squared error of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. @@ -110,10 +107,9 @@ def __init__( output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. - for example, if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, - output_transform can be `lambda x: (x["pred"], x["label"])` or `from_engine(["pred", "label"])`. - if already decollated data in postprocessing, the output_transform can be: - `lambda x: ([i["pred"] for i in x], [i["label"] for i in x])` or `from_engine(["pred", "label"])`. + `engine.state` and `output_transform` inherit from the ignite concept: + https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: + https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. save_details: whether to save metric computation details per image, for example: mean squared error of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. reduction: {``"none"``, ``"mean"``, ``"sum"``, ``"mean_batch"``, ``"sum_batch"``, diff --git a/monai/handlers/roc_auc.py b/monai/handlers/roc_auc.py index a12f20e6f1..125a4991ea 100644 --- a/monai/handlers/roc_auc.py +++ b/monai/handlers/roc_auc.py @@ -36,10 +36,9 @@ class ROCAUC(IgniteMetric): # type: ignore[valid-type, misc] # due to optional output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. - for example, if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, - output_transform can be `lambda x: (x["pred"], x["label"])` or `from_engine(["pred", "label"])`. - if already decollated data in postprocessing, the output_transform can be: - `lambda x: ([i["pred"] for i in x], [i["label"] for i in x])` or `from_engine(["pred", "label"])`. + `engine.state` and `output_transform` inherit from the ignite concept: + https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: + https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. Note: ROCAUC expects y to be comprised of 0's and 1's. diff --git a/monai/handlers/segmentation_saver.py b/monai/handlers/segmentation_saver.py index f7976633dd..479cc1408a 100644 --- a/monai/handlers/segmentation_saver.py +++ b/monai/handlers/segmentation_saver.py @@ -113,13 +113,15 @@ def __init__( batch_transform: a callable that is used to extract the `meta_data` dictionary of the input images from `ignite.engine.state.batch`. the purpose is to extract necessary information from the meta data: filename, affine, original_shape, etc. - for example, if already decollated data in postprocessing, the batch_transform can be: - `lambda x: [i["image_meta_dict"] for i in x]` or `from_engine("image_meta_dict")`. + `engine.state` and `batch_transform` inherit from the ignite concept: + https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: + https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. output_transform: a callable that is used to extract the model prediction data from `ignite.engine.state.output`. the first dimension of its output will be treated as the batch dimension. each item in the batch will be saved individually. - for example, if already decollated data in postprocessing, the output_transform can be: - `lambda x: [i["pred"] for i in x]` or `from_engine("pred")`. + `engine.state` and `output_transform` inherit from the ignite concept: + https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: + https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. name: identifier of logging.logger to use, defaulting to `engine.logger`. """ diff --git a/monai/handlers/stats_handler.py b/monai/handlers/stats_handler.py index dcc21208f4..7c88634820 100644 --- a/monai/handlers/stats_handler.py +++ b/monai/handlers/stats_handler.py @@ -69,8 +69,9 @@ def __init__( By default this value logging happens when every iteration completed. The default behavior is to print loss from output[0] as output is a decollated list and we replicated loss value for every item of the decollated list. - if output is list of dictionaries, the output_transform can be: - `lambda x: x[0]["loss"]` or `from_engine(["loss"], first=True)`. + `engine.state` and `output_transform` inherit from the ignite concept: + https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: + https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. global_epoch_transform: a callable that is used to customize global epoch number. For example, in evaluation, the evaluator engine might want to print synced epoch number with the trainer engine. diff --git a/monai/handlers/surface_distance.py b/monai/handlers/surface_distance.py index 6bcff7465f..aee1475ae7 100644 --- a/monai/handlers/surface_distance.py +++ b/monai/handlers/surface_distance.py @@ -41,10 +41,9 @@ def __init__( output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`. - for example, if `ignite.engine.state.output` is `{"pred": xxx, "label": xxx, "other": xxx}`, - output_transform can be `lambda x: (x["pred"], x["label"])` or `from_engine(["pred", "label"])`. - if already decollated data in postprocessing, the output_transform can be: - `lambda x: ([i["pred"] for i in x], [i["label"] for i in x])` or `from_engine(["pred", "label"])`. + `engine.state` and `output_transform` inherit from the ignite concept: + https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: + https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. save_details: whether to save metric computation details per image, for example: surface dice of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. diff --git a/monai/handlers/tensorboard_handlers.py b/monai/handlers/tensorboard_handlers.py index b933ee2ff4..42bed14c83 100644 --- a/monai/handlers/tensorboard_handlers.py +++ b/monai/handlers/tensorboard_handlers.py @@ -109,8 +109,9 @@ def __init__( By default this value plotting happens when every iteration completed. The default behavior is to print loss from output[0] as output is a decollated list and we replicated loss value for every item of the decollated list. - if output is list of dictionaries, the output_transform can be: - `lambda x: x[0]["loss"]` or `from_engine(["loss"], first=True)`. + `engine.state` and `output_transform` inherit from the ignite concept: + https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: + https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. global_epoch_transform: a callable that is used to customize global epoch number. For example, in evaluation, the evaluator engine might want to use trainer engines epoch number when plotting epoch vs metric curves. @@ -284,13 +285,15 @@ def __init__( batch_transform: a callable that is used to extract `image` and `label` from `ignite.engine.state.batch`, then construct `(image, label)` pair. for example: if `ignite.engine.state.batch` is `{"image": xxx, "label": xxx, "other": xxx}`, `batch_transform` can be `lambda x: (x["image"], x["label"])`. - if already decollated data in postprocessing, the batch_transform can be: - `lambda x: ([i["image"] for i in x], [i["label"] for i in x])` or `from_engine(["image", "label"])`. will use the result to plot image from `result[0][index]` and plot label from `result[1][index]`. + `engine.state` and `batch_transform` inherit from the ignite concept: + https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: + https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. output_transform: a callable that is used to extract the `predictions` data from `ignite.engine.state.output`, will use the result to plot output from `result[index]`. - for example, if already decollated data in postprocessing, the output_transform can be: - `lambda x: [i["pred"] for i in x]` or `from_engine("pred")`. + `engine.state` and `output_transform` inherit from the ignite concept: + https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: + https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. global_iter_transform: a callable that is used to customize global step number for TensorBoard. For example, in evaluation, the evaluator engine needs to know current epoch from trainer. index: plot which element in a data batch, default is the first element. From 4cbc8650438b1ac8ed4f546df466a59c1f18e485 Mon Sep 17 00:00:00 2001 From: monai-bot Date: Fri, 22 Oct 2021 01:33:45 +0000 Subject: [PATCH 4/5] [MONAI] python code formatting Signed-off-by: monai-bot --- monai/transforms/croppad/array.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/monai/transforms/croppad/array.py b/monai/transforms/croppad/array.py index 8b4b400aab..69bc5e776b 100644 --- a/monai/transforms/croppad/array.py +++ b/monai/transforms/croppad/array.py @@ -397,7 +397,9 @@ def __init__( data=roi_center, output_type=torch.Tensor, dtype=torch.int16, wrap_sequence=True ) roi_size, *_ = convert_to_dst_type(src=roi_size, dst=roi_center, wrap_sequence=True) - roi_start_torch = maximum(roi_center - floor_divide(roi_size, 2), torch.zeros_like(roi_center)) # type: ignore + roi_start_torch = maximum( + roi_center - floor_divide(roi_size, 2), torch.zeros_like(roi_center) + ) # type: ignore roi_end_torch = maximum(roi_start_torch + roi_size, roi_start_torch) else: if roi_start is None or roi_end is None: From f74c2888fe950f7d7423dadac8d0d4e355990788 Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Fri, 22 Oct 2021 16:40:00 +0800 Subject: [PATCH 5/5] [DLMED] fix flake8 Signed-off-by: Nic Ma --- monai/transforms/croppad/array.py | 4 ++-- monai/utils/misc.py | 14 +++++--------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/monai/transforms/croppad/array.py b/monai/transforms/croppad/array.py index 69bc5e776b..cc8dd677da 100644 --- a/monai/transforms/croppad/array.py +++ b/monai/transforms/croppad/array.py @@ -398,8 +398,8 @@ def __init__( ) roi_size, *_ = convert_to_dst_type(src=roi_size, dst=roi_center, wrap_sequence=True) roi_start_torch = maximum( - roi_center - floor_divide(roi_size, 2), torch.zeros_like(roi_center) - ) # type: ignore + roi_center - floor_divide(roi_size, 2), torch.zeros_like(roi_center) # type: ignore + ) roi_end_torch = maximum(roi_start_torch + roi_size, roi_start_torch) else: if roi_start is None or roi_end is None: diff --git a/monai/utils/misc.py b/monai/utils/misc.py index 20466fb91d..2940c102ff 100644 --- a/monai/utils/misc.py +++ b/monai/utils/misc.py @@ -22,7 +22,7 @@ import numpy as np import torch -from monai.utils.module import get_torch_version_tuple, version_leq +from monai.utils.module import version_leq __all__ = [ "zip_with", @@ -256,13 +256,11 @@ def set_determinism( else: # restore the original flags torch.backends.cudnn.deterministic = _flag_deterministic torch.backends.cudnn.benchmark = _flag_cudnn_benchmark - if use_deterministic_algorithms is not None: - torch_ver = get_torch_version_tuple() - if torch_ver >= (1, 9): + if hasattr(torch, "use_deterministic_algorithms"): torch.use_deterministic_algorithms(use_deterministic_algorithms) - elif torch_ver >= (1, 7): - torch.set_deterministic(use_deterministic_algorithms) # beta feature + elif hasattr(torch, "set_deterministic"): + torch.set_deterministic(use_deterministic_algorithms) # type: ignore else: warnings.warn("use_deterministic_algorithms=True, but PyTorch version is too old to set the mode.") @@ -279,9 +277,7 @@ def list_to_dict(items): def _parse_var(s): items = s.split("=", maxsplit=1) key = items[0].strip(" \n\r\t'") - value = None - if len(items) > 1: - value = items[1].strip(" \n\r\t'") + value = items[1].strip(" \n\r\t'") if len(items) > 1 else None return key, value d = {}