Activation operations, typically `Sigmoid` or `Softmax`. Args: sigmoid: whether to execute sigmoid function on model output before transform. Defaults to ``False``. softmax: whether to execute softmax function on model output before transform. Defaul
| 67 | |
| 68 | |
| 69 | class Activations(Transform): |
| 70 | """ |
| 71 | Activation operations, typically `Sigmoid` or `Softmax`. |
| 72 | |
| 73 | Args: |
| 74 | sigmoid: whether to execute sigmoid function on model output before transform. |
| 75 | Defaults to ``False``. |
| 76 | softmax: whether to execute softmax function on model output before transform. |
| 77 | Defaults to ``False``. |
| 78 | other: callable function to execute other activation layers, for example: |
| 79 | `other = lambda x: torch.tanh(x)`. Defaults to ``None``. |
| 80 | kwargs: additional parameters to `torch.softmax` (used when ``softmax=True``). |
| 81 | Defaults to ``dim=0``, unrecognized parameters will be ignored. |
| 82 | |
| 83 | Raises: |
| 84 | TypeError: When ``other`` is not an ``Optional[Callable]``. |
| 85 | |
| 86 | """ |
| 87 | |
| 88 | backend = [TransformBackends.TORCH] |
| 89 | |
| 90 | def __init__(self, sigmoid: bool = False, softmax: bool = False, other: Callable | None = None, **kwargs) -> None: |
| 91 | self.sigmoid = sigmoid |
| 92 | self.softmax = softmax |
| 93 | self.kwargs = kwargs |
| 94 | if other is not None and not callable(other): |
| 95 | raise TypeError(f"other must be None or callable but is {type(other).__name__}.") |
| 96 | self.other = other |
| 97 | |
| 98 | def __call__( |
| 99 | self, |
| 100 | img: NdarrayOrTensor, |
| 101 | sigmoid: bool | None = None, |
| 102 | softmax: bool | None = None, |
| 103 | other: Callable | None = None, |
| 104 | ) -> NdarrayOrTensor: |
| 105 | """ |
| 106 | Args: |
| 107 | sigmoid: whether to execute sigmoid function on model output before transform. |
| 108 | Defaults to ``self.sigmoid``. |
| 109 | softmax: whether to execute softmax function on model output before transform. |
| 110 | Defaults to ``self.softmax``. |
| 111 | other: callable function to execute other activation layers, for example: |
| 112 | `other = torch.tanh`. Defaults to ``self.other``. |
| 113 | |
| 114 | Raises: |
| 115 | ValueError: When ``sigmoid=True`` and ``softmax=True``. Incompatible values. |
| 116 | TypeError: When ``other`` is not an ``Optional[Callable]``. |
| 117 | ValueError: When ``self.other=None`` and ``other=None``. Incompatible values. |
| 118 | |
| 119 | """ |
| 120 | if sigmoid and softmax: |
| 121 | raise ValueError("Incompatible values: sigmoid=True and softmax=True.") |
| 122 | if other is not None and not callable(other): |
| 123 | raise TypeError(f"other must be None or callable but is {type(other).__name__}.") |
| 124 | |
| 125 | # convert to float as activation must operate on float tensor |
| 126 | img = convert_to_tensor(img, track_meta=get_track_meta()) |
no outgoing calls
searching dependent graphs…