We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Wildcard imports in `monai.losses`
**Is your feature request related to a problem? Please describe.**
`monai.losses` uses wildcard imports to import the losses from the individual files (e.g. 
 `monai.losses.dice`) This will also import quite a lot of unnecessary things which are imported in those files.

**Describe the solution you'd like**
Use explicit imports like in `monai.metrics` OR add `__all__` to the individual files to specify which functions should be imported by the wildcard import.

**Describe alternatives you've considered**
See above.

**Additional context**
//

Wildcard imports in `monai.losses`
**Is your feature request related to a problem? Please describe.**
`monai.losses` uses wildcard imports to import the losses from the individual files (e.g. 
 `monai.losses.dice`) This will also import quite a lot of unnecessary things which are imported in those files.

**Describe the solution you'd like**
Use explicit imports like in `monai.metrics` OR add `__all__` to the individual files to specify which functions should be imported by the wildcard import.

**Describe alternatives you've considered**
See above.

**Additional context**
//


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `monai/losses/dice.py`
Content:
```
1 # Copyright 2020 MONAI Consortium
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at
5 #     http://www.apache.org/licenses/LICENSE-2.0
6 # Unless required by applicable law or agreed to in writing, software
7 # distributed under the License is distributed on an "AS IS" BASIS,
8 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9 # See the License for the specific language governing permissions and
10 # limitations under the License.
11 
12 import warnings
13 from typing import Callable
14 
15 import torch
16 from torch.nn.modules.loss import _Loss
17 
18 from monai.networks.utils import one_hot
19 
20 
21 class DiceLoss(_Loss):
22     """
23     Compute average Dice loss between two tensors. It can support both multi-classes and multi-labels tasks.
24     Input logits `input` (BNHW[D] where N is number of classes) is compared with ground truth `target` (BNHW[D]).
25     Axis N of `input` is expected to have logit predictions for each class rather than being image channels,
26     while the same axis of `target` can be 1 or N (one-hot format). The `smooth` parameter is a value added to the
27     intersection and union components of the inter-over-union calculation to smooth results and prevent divide by 0,
28     this value should be small. The `include_background` class attribute can be set to False for an instance of
29     DiceLoss to exclude the first category (channel index 0) which is by convention assumed to be background.
30     If the non-background segmentations are small compared to the total image size they can get overwhelmed by
31     the signal from the background so excluding it in such cases helps convergence.
32     """
33 
34     def __init__(
35         self,
36         include_background: bool = True,
37         to_onehot_y: bool = False,
38         do_sigmoid: bool = False,
39         do_softmax: bool = False,
40         squared_pred: bool = False,
41         jaccard: bool = False,
42         reduction: str = "mean",
43     ):
44         """
45         Args:
46             include_background (bool): If False channel index 0 (background category) is excluded from the calculation.
47             to_onehot_y (bool): whether to convert `y` into the one-hot format. Defaults to False.
48             do_sigmoid (bool): If True, apply a sigmoid function to the prediction.
49             do_softmax (bool): If True, apply a softmax function to the prediction.
50             squared_pred (bool): use squared versions of targets and predictions in the denominator or not.
51             jaccard (bool): compute Jaccard Index (soft IoU) instead of dice or not.
52             reduction (`none|mean|sum`): Specifies the reduction to apply to the output:
53                 ``'none'``: no reduction will be applied,
54                 ``'mean'``: the sum of the output will be divided by the number of elements in the output,
55                 ``'sum'``: the output will be summed.
56                 Default: ``'mean'``.
57         """
58         super().__init__(reduction=reduction)
59         self.include_background = include_background
60         self.to_onehot_y = to_onehot_y
61         if do_sigmoid and do_softmax:
62             raise ValueError("do_sigmoid=True and do_softmax=True are not compatible.")
63         self.do_sigmoid = do_sigmoid
64         self.do_softmax = do_softmax
65         self.squared_pred = squared_pred
66         self.jaccard = jaccard
67 
68     def forward(self, input: torch.Tensor, target: torch.Tensor, smooth: float = 1e-5):
69         """
70         Args:
71             input (tensor): the shape should be BNH[WD].
72             target (tensor): the shape should be BNH[WD].
73             smooth (float): a small constant to avoid nan.
74         """
75         if self.do_sigmoid:
76             input = torch.sigmoid(input)
77         n_pred_ch = input.shape[1]
78         if n_pred_ch == 1:
79             if self.do_softmax:
80                 warnings.warn("single channel prediction, `do_softmax=True` ignored.")
81             if self.to_onehot_y:
82                 warnings.warn("single channel prediction, `to_onehot_y=True` ignored.")
83             if not self.include_background:
84                 warnings.warn("single channel prediction, `include_background=False` ignored.")
85         else:
86             if self.do_softmax:
87                 input = torch.softmax(input, 1)
88             if self.to_onehot_y:
89                 target = one_hot(target, n_pred_ch)
90             if not self.include_background:
91                 # if skipping background, removing first channel
92                 target = target[:, 1:]
93                 input = input[:, 1:]
94         assert (
95             target.shape == input.shape
96         ), f"ground truth has differing shape ({target.shape}) from input ({input.shape})"
97 
98         # reducing only spatial dimensions (not batch nor channels)
99         reduce_axis = list(range(2, len(input.shape)))
100         intersection = torch.sum(target * input, reduce_axis)
101 
102         if self.squared_pred:
103             target = torch.pow(target, 2)
104             input = torch.pow(input, 2)
105 
106         ground_o = torch.sum(target, reduce_axis)
107         pred_o = torch.sum(input, reduce_axis)
108 
109         denominator = ground_o + pred_o
110 
111         if self.jaccard:
112             denominator -= intersection
113 
114         f = 1.0 - (2.0 * intersection + smooth) / (denominator + smooth)
115         if self.reduction == "sum":
116             return f.sum()  # sum over the batch and channel dims
117         if self.reduction == "none":
118             return f  # returns [N, n_classes] losses
119         if self.reduction == "mean":
120             return f.mean()  # the batch and channel average
121         raise ValueError(f"reduction={self.reduction} is invalid.")
122 
123 
124 class GeneralizedDiceLoss(_Loss):
125     """
126     Compute the generalised Dice loss defined in:
127 
128         Sudre, C. et. al. (2017) Generalised Dice overlap as a deep learning
129         loss function for highly unbalanced segmentations. DLMIA 2017.
130 
131     Adapted from:
132         https://github.com/NifTK/NiftyNet/blob/v0.6.0/niftynet/layer/loss_segmentation.py#L279
133     """
134 
135     def __init__(
136         self,
137         include_background: bool = True,
138         to_onehot_y: bool = False,
139         do_sigmoid: bool = False,
140         do_softmax: bool = False,
141         w_type: str = "square",
142         reduction: str = "mean",
143     ):
144         """
145         Args:
146             include_background (bool): If False channel index 0 (background category) is excluded from the calculation.
147             to_onehot_y (bool): whether to convert `y` into the one-hot format. Defaults to False.
148             do_sigmoid (bool): If True, apply a sigmoid function to the prediction.
149             do_softmax (bool): If True, apply a softmax function to the prediction.
150             w_type ('square'|'simple'|'uniform'): type of function to transform ground truth volume to a weight factor.
151                 Default: `'square'`
152             reduction (`none|mean|sum`): Specifies the reduction to apply to the output:
153                 ``'none'``: no reduction will be applied,
154                 ``'mean'``: the sum of the output will be divided by the batch size in the output,
155                 ``'sum'``: the output will be summed over the batch dim.
156                 Default: ``'mean'``.
157         """
158         super().__init__(reduction=reduction)
159         self.include_background = include_background
160         self.to_onehot_y = to_onehot_y
161         if do_sigmoid and do_softmax:
162             raise ValueError("do_sigmoid=True and do_softmax=True are not compatible.")
163         self.do_sigmoid = do_sigmoid
164         self.do_softmax = do_softmax
165         self.w_func: Callable = torch.ones_like
166         if w_type == "simple":
167             self.w_func = torch.reciprocal
168         elif w_type == "square":
169             self.w_func = lambda x: torch.reciprocal(x * x)
170 
171     def forward(self, input: torch.Tensor, target: torch.Tensor, smooth: float = 1e-5):
172         """
173         Args:
174             input (tensor): the shape should be BNH[WD].
175             target (tensor): the shape should be BNH[WD].
176             smooth (float): a small constant to avoid nan.
177         """
178         if self.do_sigmoid:
179             input = torch.sigmoid(input)
180         n_pred_ch = input.shape[1]
181         if n_pred_ch == 1:
182             if self.do_softmax:
183                 warnings.warn("single channel prediction, `do_softmax=True` ignored.")
184             if self.to_onehot_y:
185                 warnings.warn("single channel prediction, `to_onehot_y=True` ignored.")
186             if not self.include_background:
187                 warnings.warn("single channel prediction, `include_background=False` ignored.")
188         else:
189             if self.do_softmax:
190                 input = torch.softmax(input, 1)
191             if self.to_onehot_y:
192                 target = one_hot(target, n_pred_ch)
193             if not self.include_background:
194                 # if skipping background, removing first channel
195                 target = target[:, 1:]
196                 input = input[:, 1:]
197         assert (
198             target.shape == input.shape
199         ), f"ground truth has differing shape ({target.shape}) from input ({input.shape})"
200 
201         # reducing only spatial dimensions (not batch nor channels)
202         reduce_axis = list(range(2, len(input.shape)))
203         intersection = torch.sum(target * input, reduce_axis)
204 
205         ground_o = torch.sum(target, reduce_axis)
206         pred_o = torch.sum(input, reduce_axis)
207 
208         denominator = ground_o + pred_o
209 
210         w = self.w_func(ground_o.float())
211         for b in w:
212             infs = torch.isinf(b)
213             b[infs] = 0.0
214             b[infs] = torch.max(b)
215 
216         f = 1.0 - (2.0 * (intersection * w).sum(1) + smooth) / ((denominator * w).sum(1) + smooth)
217         if self.reduction == "sum":
218             return f.sum()  # sum over the batch dim
219         if self.reduction == "none":
220             return f  # returns [N] losses
221         if self.reduction == "mean":
222             return f.mean()  # the batch and channel average
223         raise ValueError(f"reduction={self.reduction} is invalid.")
224 
225 
226 dice = Dice = DiceLoss
227 generalized_dice = generalised_dice = GeneralizedDiceLoss
228
```

Path: `monai/losses/__init__.py`
Content:
```
1 # Copyright 2020 MONAI Consortium
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at
5 #     http://www.apache.org/licenses/LICENSE-2.0
6 # Unless required by applicable law or agreed to in writing, software
7 # distributed under the License is distributed on an "AS IS" BASIS,
8 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9 # See the License for the specific language governing permissions and
10 # limitations under the License.
11 
12 from .dice import *
13 from .focal_loss import *
14 from .tversky import *
15
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/monai/losses/__init__.py b/monai/losses/__init__.py
--- a/monai/losses/__init__.py
+++ b/monai/losses/__init__.py
@@ -9,6 +9,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from .dice import *
-from .focal_loss import *
-from .tversky import *
+from .dice import Dice, DiceLoss, GeneralizedDiceLoss, dice, generalized_dice
+from .focal_loss import FocalLoss
+from .tversky import TverskyLoss
diff --git a/monai/losses/dice.py b/monai/losses/dice.py
--- a/monai/losses/dice.py
+++ b/monai/losses/dice.py
@@ -224,4 +224,4 @@
 
 
 dice = Dice = DiceLoss
-generalized_dice = generalised_dice = GeneralizedDiceLoss
+generalized_dice = GeneralizedDiceLoss

{"golden_diff": "diff --git a/monai/losses/__init__.py b/monai/losses/__init__.py\n--- a/monai/losses/__init__.py\n+++ b/monai/losses/__init__.py\n@@ -9,6 +9,6 @@\n # See the License for the specific language governing permissions and\n # limitations under the License.\n \n-from .dice import *\n-from .focal_loss import *\n-from .tversky import *\n+from .dice import Dice, DiceLoss, GeneralizedDiceLoss, dice, generalized_dice\n+from .focal_loss import FocalLoss\n+from .tversky import TverskyLoss\ndiff --git a/monai/losses/dice.py b/monai/losses/dice.py\n--- a/monai/losses/dice.py\n+++ b/monai/losses/dice.py\n@@ -224,4 +224,4 @@\n \n \n dice = Dice = DiceLoss\n-generalized_dice = generalised_dice = GeneralizedDiceLoss\n+generalized_dice = GeneralizedDiceLoss\n", "issue": "Wildcard imports in `monai.losses`\n**Is your feature request related to a problem? Please describe.**\r\n`monai.losses` uses wildcard imports to import the losses from the individual files (e.g. \r\n `monai.losses.dice`) This will also import quite a lot of unnecessary things which are imported in those files.\r\n\r\n**Describe the solution you'd like**\r\nUse explicit imports like in `monai.metrics` OR add `__all__` to the individual files to specify which functions should be imported by the wildcard import.\r\n\r\n**Describe alternatives you've considered**\r\nSee above.\r\n\r\n**Additional context**\r\n//\r\n\nWildcard imports in `monai.losses`\n**Is your feature request related to a problem? Please describe.**\r\n`monai.losses` uses wildcard imports to import the losses from the individual files (e.g. \r\n `monai.losses.dice`) This will also import quite a lot of unnecessary things which are imported in those files.\r\n\r\n**Describe the solution you'd like**\r\nUse explicit imports like in `monai.metrics` OR add `__all__` to the individual files to specify which functions should be imported by the wildcard import.\r\n\r\n**Describe alternatives you've considered**\r\nSee above.\r\n\r\n**Additional context**\r\n//\r\n\n", "before_files": [{"content": "# Copyright 2020 MONAI Consortium\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#     http://www.apache.org/licenses/LICENSE-2.0\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport warnings\nfrom typing import Callable\n\nimport torch\nfrom torch.nn.modules.loss import _Loss\n\nfrom monai.networks.utils import one_hot\n\n\nclass DiceLoss(_Loss):\n    \"\"\"\n    Compute average Dice loss between two tensors. It can support both multi-classes and multi-labels tasks.\n    Input logits `input` (BNHW[D] where N is number of classes) is compared with ground truth `target` (BNHW[D]).\n    Axis N of `input` is expected to have logit predictions for each class rather than being image channels,\n    while the same axis of `target` can be 1 or N (one-hot format). The `smooth` parameter is a value added to the\n    intersection and union components of the inter-over-union calculation to smooth results and prevent divide by 0,\n    this value should be small. The `include_background` class attribute can be set to False for an instance of\n    DiceLoss to exclude the first category (channel index 0) which is by convention assumed to be background.\n    If the non-background segmentations are small compared to the total image size they can get overwhelmed by\n    the signal from the background so excluding it in such cases helps convergence.\n    \"\"\"\n\n    def __init__(\n        self,\n        include_background: bool = True,\n        to_onehot_y: bool = False,\n        do_sigmoid: bool = False,\n        do_softmax: bool = False,\n        squared_pred: bool = False,\n        jaccard: bool = False,\n        reduction: str = \"mean\",\n    ):\n        \"\"\"\n        Args:\n            include_background (bool): If False channel index 0 (background category) is excluded from the calculation.\n            to_onehot_y (bool): whether to convert `y` into the one-hot format. Defaults to False.\n            do_sigmoid (bool): If True, apply a sigmoid function to the prediction.\n            do_softmax (bool): If True, apply a softmax function to the prediction.\n            squared_pred (bool): use squared versions of targets and predictions in the denominator or not.\n            jaccard (bool): compute Jaccard Index (soft IoU) instead of dice or not.\n            reduction (`none|mean|sum`): Specifies the reduction to apply to the output:\n                ``'none'``: no reduction will be applied,\n                ``'mean'``: the sum of the output will be divided by the number of elements in the output,\n                ``'sum'``: the output will be summed.\n                Default: ``'mean'``.\n        \"\"\"\n        super().__init__(reduction=reduction)\n        self.include_background = include_background\n        self.to_onehot_y = to_onehot_y\n        if do_sigmoid and do_softmax:\n            raise ValueError(\"do_sigmoid=True and do_softmax=True are not compatible.\")\n        self.do_sigmoid = do_sigmoid\n        self.do_softmax = do_softmax\n        self.squared_pred = squared_pred\n        self.jaccard = jaccard\n\n    def forward(self, input: torch.Tensor, target: torch.Tensor, smooth: float = 1e-5):\n        \"\"\"\n        Args:\n            input (tensor): the shape should be BNH[WD].\n            target (tensor): the shape should be BNH[WD].\n            smooth (float): a small constant to avoid nan.\n        \"\"\"\n        if self.do_sigmoid:\n            input = torch.sigmoid(input)\n        n_pred_ch = input.shape[1]\n        if n_pred_ch == 1:\n            if self.do_softmax:\n                warnings.warn(\"single channel prediction, `do_softmax=True` ignored.\")\n            if self.to_onehot_y:\n                warnings.warn(\"single channel prediction, `to_onehot_y=True` ignored.\")\n            if not self.include_background:\n                warnings.warn(\"single channel prediction, `include_background=False` ignored.\")\n        else:\n            if self.do_softmax:\n                input = torch.softmax(input, 1)\n            if self.to_onehot_y:\n                target = one_hot(target, n_pred_ch)\n            if not self.include_background:\n                # if skipping background, removing first channel\n                target = target[:, 1:]\n                input = input[:, 1:]\n        assert (\n            target.shape == input.shape\n        ), f\"ground truth has differing shape ({target.shape}) from input ({input.shape})\"\n\n        # reducing only spatial dimensions (not batch nor channels)\n        reduce_axis = list(range(2, len(input.shape)))\n        intersection = torch.sum(target * input, reduce_axis)\n\n        if self.squared_pred:\n            target = torch.pow(target, 2)\n            input = torch.pow(input, 2)\n\n        ground_o = torch.sum(target, reduce_axis)\n        pred_o = torch.sum(input, reduce_axis)\n\n        denominator = ground_o + pred_o\n\n        if self.jaccard:\n            denominator -= intersection\n\n        f = 1.0 - (2.0 * intersection + smooth) / (denominator + smooth)\n        if self.reduction == \"sum\":\n            return f.sum()  # sum over the batch and channel dims\n        if self.reduction == \"none\":\n            return f  # returns [N, n_classes] losses\n        if self.reduction == \"mean\":\n            return f.mean()  # the batch and channel average\n        raise ValueError(f\"reduction={self.reduction} is invalid.\")\n\n\nclass GeneralizedDiceLoss(_Loss):\n    \"\"\"\n    Compute the generalised Dice loss defined in:\n\n        Sudre, C. et. al. (2017) Generalised Dice overlap as a deep learning\n        loss function for highly unbalanced segmentations. DLMIA 2017.\n\n    Adapted from:\n        https://github.com/NifTK/NiftyNet/blob/v0.6.0/niftynet/layer/loss_segmentation.py#L279\n    \"\"\"\n\n    def __init__(\n        self,\n        include_background: bool = True,\n        to_onehot_y: bool = False,\n        do_sigmoid: bool = False,\n        do_softmax: bool = False,\n        w_type: str = \"square\",\n        reduction: str = \"mean\",\n    ):\n        \"\"\"\n        Args:\n            include_background (bool): If False channel index 0 (background category) is excluded from the calculation.\n            to_onehot_y (bool): whether to convert `y` into the one-hot format. Defaults to False.\n            do_sigmoid (bool): If True, apply a sigmoid function to the prediction.\n            do_softmax (bool): If True, apply a softmax function to the prediction.\n            w_type ('square'|'simple'|'uniform'): type of function to transform ground truth volume to a weight factor.\n                Default: `'square'`\n            reduction (`none|mean|sum`): Specifies the reduction to apply to the output:\n                ``'none'``: no reduction will be applied,\n                ``'mean'``: the sum of the output will be divided by the batch size in the output,\n                ``'sum'``: the output will be summed over the batch dim.\n                Default: ``'mean'``.\n        \"\"\"\n        super().__init__(reduction=reduction)\n        self.include_background = include_background\n        self.to_onehot_y = to_onehot_y\n        if do_sigmoid and do_softmax:\n            raise ValueError(\"do_sigmoid=True and do_softmax=True are not compatible.\")\n        self.do_sigmoid = do_sigmoid\n        self.do_softmax = do_softmax\n        self.w_func: Callable = torch.ones_like\n        if w_type == \"simple\":\n            self.w_func = torch.reciprocal\n        elif w_type == \"square\":\n            self.w_func = lambda x: torch.reciprocal(x * x)\n\n    def forward(self, input: torch.Tensor, target: torch.Tensor, smooth: float = 1e-5):\n        \"\"\"\n        Args:\n            input (tensor): the shape should be BNH[WD].\n            target (tensor): the shape should be BNH[WD].\n            smooth (float): a small constant to avoid nan.\n        \"\"\"\n        if self.do_sigmoid:\n            input = torch.sigmoid(input)\n        n_pred_ch = input.shape[1]\n        if n_pred_ch == 1:\n            if self.do_softmax:\n                warnings.warn(\"single channel prediction, `do_softmax=True` ignored.\")\n            if self.to_onehot_y:\n                warnings.warn(\"single channel prediction, `to_onehot_y=True` ignored.\")\n            if not self.include_background:\n                warnings.warn(\"single channel prediction, `include_background=False` ignored.\")\n        else:\n            if self.do_softmax:\n                input = torch.softmax(input, 1)\n            if self.to_onehot_y:\n                target = one_hot(target, n_pred_ch)\n            if not self.include_background:\n                # if skipping background, removing first channel\n                target = target[:, 1:]\n                input = input[:, 1:]\n        assert (\n            target.shape == input.shape\n        ), f\"ground truth has differing shape ({target.shape}) from input ({input.shape})\"\n\n        # reducing only spatial dimensions (not batch nor channels)\n        reduce_axis = list(range(2, len(input.shape)))\n        intersection = torch.sum(target * input, reduce_axis)\n\n        ground_o = torch.sum(target, reduce_axis)\n        pred_o = torch.sum(input, reduce_axis)\n\n        denominator = ground_o + pred_o\n\n        w = self.w_func(ground_o.float())\n        for b in w:\n            infs = torch.isinf(b)\n            b[infs] = 0.0\n            b[infs] = torch.max(b)\n\n        f = 1.0 - (2.0 * (intersection * w).sum(1) + smooth) / ((denominator * w).sum(1) + smooth)\n        if self.reduction == \"sum\":\n            return f.sum()  # sum over the batch dim\n        if self.reduction == \"none\":\n            return f  # returns [N] losses\n        if self.reduction == \"mean\":\n            return f.mean()  # the batch and channel average\n        raise ValueError(f\"reduction={self.reduction} is invalid.\")\n\n\ndice = Dice = DiceLoss\ngeneralized_dice = generalised_dice = GeneralizedDiceLoss\n", "path": "monai/losses/dice.py"}, {"content": "# Copyright 2020 MONAI Consortium\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#     http://www.apache.org/licenses/LICENSE-2.0\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom .dice import *\nfrom .focal_loss import *\nfrom .tversky import *\n", "path": "monai/losses/__init__.py"}], "after_files": [{"content": "# Copyright 2020 MONAI Consortium\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#     http://www.apache.org/licenses/LICENSE-2.0\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport warnings\nfrom typing import Callable\n\nimport torch\nfrom torch.nn.modules.loss import _Loss\n\nfrom monai.networks.utils import one_hot\n\n\nclass DiceLoss(_Loss):\n    \"\"\"\n    Compute average Dice loss between two tensors. It can support both multi-classes and multi-labels tasks.\n    Input logits `input` (BNHW[D] where N is number of classes) is compared with ground truth `target` (BNHW[D]).\n    Axis N of `input` is expected to have logit predictions for each class rather than being image channels,\n    while the same axis of `target` can be 1 or N (one-hot format). The `smooth` parameter is a value added to the\n    intersection and union components of the inter-over-union calculation to smooth results and prevent divide by 0,\n    this value should be small. The `include_background` class attribute can be set to False for an instance of\n    DiceLoss to exclude the first category (channel index 0) which is by convention assumed to be background.\n    If the non-background segmentations are small compared to the total image size they can get overwhelmed by\n    the signal from the background so excluding it in such cases helps convergence.\n    \"\"\"\n\n    def __init__(\n        self,\n        include_background: bool = True,\n        to_onehot_y: bool = False,\n        do_sigmoid: bool = False,\n        do_softmax: bool = False,\n        squared_pred: bool = False,\n        jaccard: bool = False,\n        reduction: str = \"mean\",\n    ):\n        \"\"\"\n        Args:\n            include_background (bool): If False channel index 0 (background category) is excluded from the calculation.\n            to_onehot_y (bool): whether to convert `y` into the one-hot format. Defaults to False.\n            do_sigmoid (bool): If True, apply a sigmoid function to the prediction.\n            do_softmax (bool): If True, apply a softmax function to the prediction.\n            squared_pred (bool): use squared versions of targets and predictions in the denominator or not.\n            jaccard (bool): compute Jaccard Index (soft IoU) instead of dice or not.\n            reduction (`none|mean|sum`): Specifies the reduction to apply to the output:\n                ``'none'``: no reduction will be applied,\n                ``'mean'``: the sum of the output will be divided by the number of elements in the output,\n                ``'sum'``: the output will be summed.\n                Default: ``'mean'``.\n        \"\"\"\n        super().__init__(reduction=reduction)\n        self.include_background = include_background\n        self.to_onehot_y = to_onehot_y\n        if do_sigmoid and do_softmax:\n            raise ValueError(\"do_sigmoid=True and do_softmax=True are not compatible.\")\n        self.do_sigmoid = do_sigmoid\n        self.do_softmax = do_softmax\n        self.squared_pred = squared_pred\n        self.jaccard = jaccard\n\n    def forward(self, input: torch.Tensor, target: torch.Tensor, smooth: float = 1e-5):\n        \"\"\"\n        Args:\n            input (tensor): the shape should be BNH[WD].\n            target (tensor): the shape should be BNH[WD].\n            smooth (float): a small constant to avoid nan.\n        \"\"\"\n        if self.do_sigmoid:\n            input = torch.sigmoid(input)\n        n_pred_ch = input.shape[1]\n        if n_pred_ch == 1:\n            if self.do_softmax:\n                warnings.warn(\"single channel prediction, `do_softmax=True` ignored.\")\n            if self.to_onehot_y:\n                warnings.warn(\"single channel prediction, `to_onehot_y=True` ignored.\")\n            if not self.include_background:\n                warnings.warn(\"single channel prediction, `include_background=False` ignored.\")\n        else:\n            if self.do_softmax:\n                input = torch.softmax(input, 1)\n            if self.to_onehot_y:\n                target = one_hot(target, n_pred_ch)\n            if not self.include_background:\n                # if skipping background, removing first channel\n                target = target[:, 1:]\n                input = input[:, 1:]\n        assert (\n            target.shape == input.shape\n        ), f\"ground truth has differing shape ({target.shape}) from input ({input.shape})\"\n\n        # reducing only spatial dimensions (not batch nor channels)\n        reduce_axis = list(range(2, len(input.shape)))\n        intersection = torch.sum(target * input, reduce_axis)\n\n        if self.squared_pred:\n            target = torch.pow(target, 2)\n            input = torch.pow(input, 2)\n\n        ground_o = torch.sum(target, reduce_axis)\n        pred_o = torch.sum(input, reduce_axis)\n\n        denominator = ground_o + pred_o\n\n        if self.jaccard:\n            denominator -= intersection\n\n        f = 1.0 - (2.0 * intersection + smooth) / (denominator + smooth)\n        if self.reduction == \"sum\":\n            return f.sum()  # sum over the batch and channel dims\n        if self.reduction == \"none\":\n            return f  # returns [N, n_classes] losses\n        if self.reduction == \"mean\":\n            return f.mean()  # the batch and channel average\n        raise ValueError(f\"reduction={self.reduction} is invalid.\")\n\n\nclass GeneralizedDiceLoss(_Loss):\n    \"\"\"\n    Compute the generalised Dice loss defined in:\n\n        Sudre, C. et. al. (2017) Generalised Dice overlap as a deep learning\n        loss function for highly unbalanced segmentations. DLMIA 2017.\n\n    Adapted from:\n        https://github.com/NifTK/NiftyNet/blob/v0.6.0/niftynet/layer/loss_segmentation.py#L279\n    \"\"\"\n\n    def __init__(\n        self,\n        include_background: bool = True,\n        to_onehot_y: bool = False,\n        do_sigmoid: bool = False,\n        do_softmax: bool = False,\n        w_type: str = \"square\",\n        reduction: str = \"mean\",\n    ):\n        \"\"\"\n        Args:\n            include_background (bool): If False channel index 0 (background category) is excluded from the calculation.\n            to_onehot_y (bool): whether to convert `y` into the one-hot format. Defaults to False.\n            do_sigmoid (bool): If True, apply a sigmoid function to the prediction.\n            do_softmax (bool): If True, apply a softmax function to the prediction.\n            w_type ('square'|'simple'|'uniform'): type of function to transform ground truth volume to a weight factor.\n                Default: `'square'`\n            reduction (`none|mean|sum`): Specifies the reduction to apply to the output:\n                ``'none'``: no reduction will be applied,\n                ``'mean'``: the sum of the output will be divided by the batch size in the output,\n                ``'sum'``: the output will be summed over the batch dim.\n                Default: ``'mean'``.\n        \"\"\"\n        super().__init__(reduction=reduction)\n        self.include_background = include_background\n        self.to_onehot_y = to_onehot_y\n        if do_sigmoid and do_softmax:\n            raise ValueError(\"do_sigmoid=True and do_softmax=True are not compatible.\")\n        self.do_sigmoid = do_sigmoid\n        self.do_softmax = do_softmax\n        self.w_func: Callable = torch.ones_like\n        if w_type == \"simple\":\n            self.w_func = torch.reciprocal\n        elif w_type == \"square\":\n            self.w_func = lambda x: torch.reciprocal(x * x)\n\n    def forward(self, input: torch.Tensor, target: torch.Tensor, smooth: float = 1e-5):\n        \"\"\"\n        Args:\n            input (tensor): the shape should be BNH[WD].\n            target (tensor): the shape should be BNH[WD].\n            smooth (float): a small constant to avoid nan.\n        \"\"\"\n        if self.do_sigmoid:\n            input = torch.sigmoid(input)\n        n_pred_ch = input.shape[1]\n        if n_pred_ch == 1:\n            if self.do_softmax:\n                warnings.warn(\"single channel prediction, `do_softmax=True` ignored.\")\n            if self.to_onehot_y:\n                warnings.warn(\"single channel prediction, `to_onehot_y=True` ignored.\")\n            if not self.include_background:\n                warnings.warn(\"single channel prediction, `include_background=False` ignored.\")\n        else:\n            if self.do_softmax:\n                input = torch.softmax(input, 1)\n            if self.to_onehot_y:\n                target = one_hot(target, n_pred_ch)\n            if not self.include_background:\n                # if skipping background, removing first channel\n                target = target[:, 1:]\n                input = input[:, 1:]\n        assert (\n            target.shape == input.shape\n        ), f\"ground truth has differing shape ({target.shape}) from input ({input.shape})\"\n\n        # reducing only spatial dimensions (not batch nor channels)\n        reduce_axis = list(range(2, len(input.shape)))\n        intersection = torch.sum(target * input, reduce_axis)\n\n        ground_o = torch.sum(target, reduce_axis)\n        pred_o = torch.sum(input, reduce_axis)\n\n        denominator = ground_o + pred_o\n\n        w = self.w_func(ground_o.float())\n        for b in w:\n            infs = torch.isinf(b)\n            b[infs] = 0.0\n            b[infs] = torch.max(b)\n\n        f = 1.0 - (2.0 * (intersection * w).sum(1) + smooth) / ((denominator * w).sum(1) + smooth)\n        if self.reduction == \"sum\":\n            return f.sum()  # sum over the batch dim\n        if self.reduction == \"none\":\n            return f  # returns [N] losses\n        if self.reduction == \"mean\":\n            return f.mean()  # the batch and channel average\n        raise ValueError(f\"reduction={self.reduction} is invalid.\")\n\n\ndice = Dice = DiceLoss\ngeneralized_dice = GeneralizedDiceLoss\n", "path": "monai/losses/dice.py"}, {"content": "# Copyright 2020 MONAI Consortium\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#     http://www.apache.org/licenses/LICENSE-2.0\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom .dice import Dice, DiceLoss, GeneralizedDiceLoss, dice, generalized_dice\nfrom .focal_loss import FocalLoss\nfrom .tversky import TverskyLoss\n", "path": "monai/losses/__init__.py"}]}