MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / AsDiscrete

Class AsDiscrete

monai/transforms/post/array.py:140–244  ·  view source on GitHub ↗

Convert the input tensor/array into discrete values, possible operations are: - `argmax`. - threshold input value to binary values. - convert input value to One-Hot format (set ``to_one_hot=N``, `N` is the number of classes). - round the value to the closest

Source from the content-addressed store, hash-verified

138
139
140class AsDiscrete(Transform):
141 """
142 Convert the input tensor/array into discrete values, possible operations are:
143
144 - `argmax`.
145 - threshold input value to binary values.
146 - convert input value to One-Hot format (set ``to_one_hot=N``, `N` is the number of classes).
147 - round the value to the closest integer.
148
149 Args:
150 argmax: whether to execute argmax function on input data before transform.
151 Defaults to ``False``.
152 to_onehot: if not None, convert input data into the one-hot format with specified number of classes.
153 Defaults to ``None``.
154 threshold: if not None, threshold the float values to int number 0 or 1 with specified threshold.
155 Defaults to ``None``.
156 rounding: if not None, round the data according to the specified option,
157 available options: ["torchrounding"].
158 kwargs: additional parameters to `torch.argmax`, `monai.networks.one_hot`.
159 currently ``dim``, ``keepdim``, ``dtype`` are supported, unrecognized parameters will be ignored.
160 These default to ``0``, ``True``, ``torch.float`` respectively.
161
162 Example:
163
164 >>> transform = AsDiscrete(argmax=True)
165 >>> print(transform(np.array([[[0.0, 1.0]], [[2.0, 3.0]]])))
166 # [[[1.0, 1.0]]]
167
168 >>> transform = AsDiscrete(threshold=0.6)
169 >>> print(transform(np.array([[[0.0, 0.5], [0.8, 3.0]]])))
170 # [[[0.0, 0.0], [1.0, 1.0]]]
171
172 >>> transform = AsDiscrete(argmax=True, to_onehot=2, threshold=0.5)
173 >>> print(transform(np.array([[[0.0, 1.0]], [[2.0, 3.0]]])))
174 # [[[0.0, 0.0]], [[1.0, 1.0]]]
175
176 """
177
178 backend = [TransformBackends.TORCH]
179
180 def __init__(
181 self,
182 argmax: bool = False,
183 to_onehot: int | None = None,
184 threshold: float | None = None,
185 rounding: str | None = None,
186 **kwargs,
187 ) -> None:
188 self.argmax = argmax
189 if isinstance(to_onehot, bool): # for backward compatibility
190 raise ValueError("`to_onehot=True/False` is deprecated, please use `to_onehot=num_classes` instead.")
191 self.to_onehot = to_onehot
192 self.threshold = threshold
193 self.rounding = rounding
194 self.kwargs = kwargs
195
196 def __call__(
197 self,

Callers 15

__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
test_train_timingMethod · 0.90
run_training_testFunction · 0.90
run_training_testFunction · 0.90
run_inference_testFunction · 0.90
test_valueMethod · 0.90
test_class_valueMethod · 0.90
test_valueMethod · 0.90
test_class_valueMethod · 0.90

Calls

no outgoing calls

Tested by 15

test_train_timingMethod · 0.72
run_training_testFunction · 0.72
run_training_testFunction · 0.72
run_inference_testFunction · 0.72
test_valueMethod · 0.72
test_class_valueMethod · 0.72
test_valueMethod · 0.72
test_class_valueMethod · 0.72
test_value_shapeMethod · 0.72
test_additionalMethod · 0.72
test_computeMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…