We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
np.product is deprecated. Use np.prod instead.
**Describe the bug**
```
monai/networks/nets/regressor.py:146:32: error: Module has no attribute "product"  [attr-defined]
monai/networks/nets/varautoencoder.py:123:27: error: Module has no attribute "product"  [attr-defined]
```
np.product is deprecated. Use np.prod instead. https://github.com/numpy/numpy/pull/23314
np.product is deprecated. Use np.prod instead.
**Describe the bug**
```
monai/networks/nets/regressor.py:146:32: error: Module has no attribute "product"  [attr-defined]
monai/networks/nets/varautoencoder.py:123:27: error: Module has no attribute "product"  [attr-defined]
```
np.product is deprecated. Use np.prod instead. https://github.com/numpy/numpy/pull/23314

--- 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/networks/nets/varautoencoder.py`
Content:
```
1 # Copyright (c) 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 __future__ import annotations
13 
14 from collections.abc import Sequence
15 
16 import numpy as np
17 import torch
18 import torch.nn as nn
19 from torch.nn import functional as F
20 
21 from monai.networks.layers.convutils import calculate_out_shape, same_padding
22 from monai.networks.layers.factories import Act, Norm
23 from monai.networks.nets import AutoEncoder
24 
25 __all__ = ["VarAutoEncoder"]
26 
27 
28 class VarAutoEncoder(AutoEncoder):
29     """
30     Variational Autoencoder based on the paper - https://arxiv.org/abs/1312.6114
31 
32     Args:
33         spatial_dims: number of spatial dimensions.
34         in_shape: shape of input data starting with channel dimension.
35         out_channels: number of output channels.
36         latent_size: size of the latent variable.
37         channels: sequence of channels. Top block first. The length of `channels` should be no less than 2.
38         strides: sequence of convolution strides. The length of `stride` should equal to `len(channels) - 1`.
39         kernel_size: convolution kernel size, the value(s) should be odd. If sequence,
40             its length should equal to dimensions. Defaults to 3.
41         up_kernel_size: upsampling convolution kernel size, the value(s) should be odd. If sequence,
42             its length should equal to dimensions. Defaults to 3.
43         num_res_units: number of residual units. Defaults to 0.
44         inter_channels: sequence of channels defining the blocks in the intermediate layer between encode and decode.
45         inter_dilations: defines the dilation value for each block of the intermediate layer. Defaults to 1.
46         num_inter_units: number of residual units for each block of the intermediate layer. Defaults to 0.
47         act: activation type and arguments. Defaults to PReLU.
48         norm: feature normalization type and arguments. Defaults to instance norm.
49         dropout: dropout ratio. Defaults to no dropout.
50         bias: whether to have a bias term in convolution blocks. Defaults to True.
51             According to `Performance Tuning Guide <https://pytorch.org/tutorials/recipes/recipes/tuning_guide.html>`_,
52             if a conv layer is directly followed by a batch norm layer, bias should be False.
53         use_sigmoid: whether to use the sigmoid function on final output. Defaults to True.
54 
55     Examples::
56 
57         from monai.networks.nets import VarAutoEncoder
58 
59         # 3 layer network accepting images with dimensions (1, 32, 32) and using a latent vector with 2 values
60         model = VarAutoEncoder(
61             spatial_dims=2,
62             in_shape=(32, 32),  # image spatial shape
63             out_channels=1,
64             latent_size=2,
65             channels=(16, 32, 64),
66             strides=(1, 2, 2),
67         )
68 
69     see also:
70         - Variational autoencoder network with MedNIST Dataset
71           https://github.com/Project-MONAI/tutorials/blob/master/modules/varautoencoder_mednist.ipynb
72     """
73 
74     def __init__(
75         self,
76         spatial_dims: int,
77         in_shape: Sequence[int],
78         out_channels: int,
79         latent_size: int,
80         channels: Sequence[int],
81         strides: Sequence[int],
82         kernel_size: Sequence[int] | int = 3,
83         up_kernel_size: Sequence[int] | int = 3,
84         num_res_units: int = 0,
85         inter_channels: list | None = None,
86         inter_dilations: list | None = None,
87         num_inter_units: int = 2,
88         act: tuple | str | None = Act.PRELU,
89         norm: tuple | str = Norm.INSTANCE,
90         dropout: tuple | str | float | None = None,
91         bias: bool = True,
92         use_sigmoid: bool = True,
93     ) -> None:
94         self.in_channels, *self.in_shape = in_shape
95         self.use_sigmoid = use_sigmoid
96 
97         self.latent_size = latent_size
98         self.final_size = np.asarray(self.in_shape, dtype=int)
99 
100         super().__init__(
101             spatial_dims,
102             self.in_channels,
103             out_channels,
104             channels,
105             strides,
106             kernel_size,
107             up_kernel_size,
108             num_res_units,
109             inter_channels,
110             inter_dilations,
111             num_inter_units,
112             act,
113             norm,
114             dropout,
115             bias,
116         )
117 
118         padding = same_padding(self.kernel_size)
119 
120         for s in strides:
121             self.final_size = calculate_out_shape(self.final_size, self.kernel_size, s, padding)  # type: ignore
122 
123         linear_size = int(np.product(self.final_size)) * self.encoded_channels
124         self.mu = nn.Linear(linear_size, self.latent_size)
125         self.logvar = nn.Linear(linear_size, self.latent_size)
126         self.decodeL = nn.Linear(self.latent_size, linear_size)
127 
128     def encode_forward(self, x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
129         x = self.encode(x)
130         x = self.intermediate(x)
131         x = x.view(x.shape[0], -1)
132         mu = self.mu(x)
133         logvar = self.logvar(x)
134         return mu, logvar
135 
136     def decode_forward(self, z: torch.Tensor, use_sigmoid: bool = True) -> torch.Tensor:
137         x = F.relu(self.decodeL(z))
138         x = x.view(x.shape[0], self.channels[-1], *self.final_size)
139         x = self.decode(x)
140         if use_sigmoid:
141             x = torch.sigmoid(x)
142         return x
143 
144     def reparameterize(self, mu: torch.Tensor, logvar: torch.Tensor) -> torch.Tensor:
145         std = torch.exp(0.5 * logvar)
146 
147         if self.training:  # multiply random noise with std only during training
148             std = torch.randn_like(std).mul(std)
149 
150         return std.add_(mu)
151 
152     def forward(self, x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
153         mu, logvar = self.encode_forward(x)
154         z = self.reparameterize(mu, logvar)
155         return self.decode_forward(z, self.use_sigmoid), mu, logvar, z
156
```

Path: `monai/networks/nets/regressor.py`
Content:
```
1 # Copyright (c) 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 __future__ import annotations
13 
14 from collections.abc import Sequence
15 
16 import numpy as np
17 import torch
18 import torch.nn as nn
19 
20 from monai.networks.blocks import Convolution, ResidualUnit
21 from monai.networks.layers.convutils import calculate_out_shape, same_padding
22 from monai.networks.layers.factories import Act, Norm
23 from monai.networks.layers.simplelayers import Reshape
24 from monai.utils import ensure_tuple, ensure_tuple_rep
25 
26 __all__ = ["Regressor"]
27 
28 
29 class Regressor(nn.Module):
30     """
31     This defines a network for relating large-sized input tensors to small output tensors, ie. regressing large
32     values to a prediction. An output of a single dimension can be used as value regression or multi-label
33     classification prediction, an output of a single value can be used as a discriminator or critic prediction.
34 
35     The network is constructed as a sequence of layers, either :py:class:`monai.networks.blocks.Convolution` or
36     :py:class:`monai.networks.blocks.ResidualUnit`, with a final fully-connected layer resizing the output from the
37     blocks to the final size. Each block is defined with a stride value typically used to downsample the input using
38     strided convolutions. In this way each block progressively condenses information from the input into a deep
39     representation the final fully-connected layer relates to a final result.
40 
41     Args:
42         in_shape: tuple of integers stating the dimension of the input tensor (minus batch dimension)
43         out_shape: tuple of integers stating the dimension of the final output tensor (minus batch dimension)
44         channels: tuple of integers stating the output channels of each convolutional layer
45         strides: tuple of integers stating the stride (downscale factor) of each convolutional layer
46         kernel_size: integer or tuple of integers stating size of convolutional kernels
47         num_res_units: integer stating number of convolutions in residual units, 0 means no residual units
48         act: name or type defining activation layers
49         norm: name or type defining normalization layers
50         dropout: optional float value in range [0, 1] stating dropout probability for layers, None for no dropout
51         bias: boolean stating if convolution layers should have a bias component
52 
53     Examples::
54 
55         # infers a 2-value result (eg. a 2D cartesian coordinate) from a 64x64 image
56         net = Regressor((1, 64, 64), (2,), (2, 4, 8), (2, 2, 2))
57 
58     """
59 
60     def __init__(
61         self,
62         in_shape: Sequence[int],
63         out_shape: Sequence[int],
64         channels: Sequence[int],
65         strides: Sequence[int],
66         kernel_size: Sequence[int] | int = 3,
67         num_res_units: int = 2,
68         act=Act.PRELU,
69         norm=Norm.INSTANCE,
70         dropout: float | None = None,
71         bias: bool = True,
72     ) -> None:
73         super().__init__()
74 
75         self.in_channels, *self.in_shape = ensure_tuple(in_shape)
76         self.dimensions = len(self.in_shape)
77         self.channels = ensure_tuple(channels)
78         self.strides = ensure_tuple(strides)
79         self.out_shape = ensure_tuple(out_shape)
80         self.kernel_size = ensure_tuple_rep(kernel_size, self.dimensions)
81         self.num_res_units = num_res_units
82         self.act = act
83         self.norm = norm
84         self.dropout = dropout
85         self.bias = bias
86         self.net = nn.Sequential()
87 
88         echannel = self.in_channels
89 
90         padding = same_padding(kernel_size)
91 
92         self.final_size = np.asarray(self.in_shape, dtype=int)
93         self.reshape = Reshape(*self.out_shape)
94 
95         # encode stage
96         for i, (c, s) in enumerate(zip(self.channels, self.strides)):
97             layer = self._get_layer(echannel, c, s, i == len(channels) - 1)
98             echannel = c  # use the output channel number as the input for the next loop
99             self.net.add_module("layer_%i" % i, layer)
100             self.final_size = calculate_out_shape(self.final_size, kernel_size, s, padding)  # type: ignore
101 
102         self.final = self._get_final_layer((echannel,) + self.final_size)
103 
104     def _get_layer(
105         self, in_channels: int, out_channels: int, strides: int, is_last: bool
106     ) -> ResidualUnit | Convolution:
107         """
108         Returns a layer accepting inputs with `in_channels` number of channels and producing outputs of `out_channels`
109         number of channels. The `strides` indicates downsampling factor, ie. convolutional stride. If `is_last`
110         is True this is the final layer and is not expected to include activation and normalization layers.
111         """
112 
113         layer: ResidualUnit | Convolution
114 
115         if self.num_res_units > 0:
116             layer = ResidualUnit(
117                 subunits=self.num_res_units,
118                 last_conv_only=is_last,
119                 spatial_dims=self.dimensions,
120                 in_channels=in_channels,
121                 out_channels=out_channels,
122                 strides=strides,
123                 kernel_size=self.kernel_size,
124                 act=self.act,
125                 norm=self.norm,
126                 dropout=self.dropout,
127                 bias=self.bias,
128             )
129         else:
130             layer = Convolution(
131                 conv_only=is_last,
132                 spatial_dims=self.dimensions,
133                 in_channels=in_channels,
134                 out_channels=out_channels,
135                 strides=strides,
136                 kernel_size=self.kernel_size,
137                 act=self.act,
138                 norm=self.norm,
139                 dropout=self.dropout,
140                 bias=self.bias,
141             )
142 
143         return layer
144 
145     def _get_final_layer(self, in_shape: Sequence[int]):
146         linear = nn.Linear(int(np.product(in_shape)), int(np.product(self.out_shape)))
147         return nn.Sequential(nn.Flatten(), linear)
148 
149     def forward(self, x: torch.Tensor) -> torch.Tensor:
150         x = self.net(x)
151         x = self.final(x)
152         x = self.reshape(x)
153         return x
154
```
--- 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/networks/nets/regressor.py b/monai/networks/nets/regressor.py
--- a/monai/networks/nets/regressor.py
+++ b/monai/networks/nets/regressor.py
@@ -143,7 +143,7 @@
         return layer
 
     def _get_final_layer(self, in_shape: Sequence[int]):
-        linear = nn.Linear(int(np.product(in_shape)), int(np.product(self.out_shape)))
+        linear = nn.Linear(int(np.prod(in_shape)), int(np.prod(self.out_shape)))
         return nn.Sequential(nn.Flatten(), linear)
 
     def forward(self, x: torch.Tensor) -> torch.Tensor:
diff --git a/monai/networks/nets/varautoencoder.py b/monai/networks/nets/varautoencoder.py
--- a/monai/networks/nets/varautoencoder.py
+++ b/monai/networks/nets/varautoencoder.py
@@ -120,7 +120,7 @@
         for s in strides:
             self.final_size = calculate_out_shape(self.final_size, self.kernel_size, s, padding)  # type: ignore
 
-        linear_size = int(np.product(self.final_size)) * self.encoded_channels
+        linear_size = int(np.prod(self.final_size)) * self.encoded_channels
         self.mu = nn.Linear(linear_size, self.latent_size)
         self.logvar = nn.Linear(linear_size, self.latent_size)
         self.decodeL = nn.Linear(self.latent_size, linear_size)

{"golden_diff": "diff --git a/monai/networks/nets/regressor.py b/monai/networks/nets/regressor.py\n--- a/monai/networks/nets/regressor.py\n+++ b/monai/networks/nets/regressor.py\n@@ -143,7 +143,7 @@\n         return layer\n \n     def _get_final_layer(self, in_shape: Sequence[int]):\n-        linear = nn.Linear(int(np.product(in_shape)), int(np.product(self.out_shape)))\n+        linear = nn.Linear(int(np.prod(in_shape)), int(np.prod(self.out_shape)))\n         return nn.Sequential(nn.Flatten(), linear)\n \n     def forward(self, x: torch.Tensor) -> torch.Tensor:\ndiff --git a/monai/networks/nets/varautoencoder.py b/monai/networks/nets/varautoencoder.py\n--- a/monai/networks/nets/varautoencoder.py\n+++ b/monai/networks/nets/varautoencoder.py\n@@ -120,7 +120,7 @@\n         for s in strides:\n             self.final_size = calculate_out_shape(self.final_size, self.kernel_size, s, padding)  # type: ignore\n \n-        linear_size = int(np.product(self.final_size)) * self.encoded_channels\n+        linear_size = int(np.prod(self.final_size)) * self.encoded_channels\n         self.mu = nn.Linear(linear_size, self.latent_size)\n         self.logvar = nn.Linear(linear_size, self.latent_size)\n         self.decodeL = nn.Linear(self.latent_size, linear_size)\n", "issue": "np.product is deprecated. Use np.prod instead.\n**Describe the bug**\r\n```\r\nmonai/networks/nets/regressor.py:146:32: error: Module has no attribute \"product\"  [attr-defined]\r\nmonai/networks/nets/varautoencoder.py:123:27: error: Module has no attribute \"product\"  [attr-defined]\r\n```\r\nnp.product is deprecated. Use np.prod instead. https://github.com/numpy/numpy/pull/23314\nnp.product is deprecated. Use np.prod instead.\n**Describe the bug**\r\n```\r\nmonai/networks/nets/regressor.py:146:32: error: Module has no attribute \"product\"  [attr-defined]\r\nmonai/networks/nets/varautoencoder.py:123:27: error: Module has no attribute \"product\"  [attr-defined]\r\n```\r\nnp.product is deprecated. Use np.prod instead. https://github.com/numpy/numpy/pull/23314\n", "before_files": [{"content": "# Copyright (c) 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 __future__ import annotations\n\nfrom collections.abc import Sequence\n\nimport numpy as np\nimport torch\nimport torch.nn as nn\nfrom torch.nn import functional as F\n\nfrom monai.networks.layers.convutils import calculate_out_shape, same_padding\nfrom monai.networks.layers.factories import Act, Norm\nfrom monai.networks.nets import AutoEncoder\n\n__all__ = [\"VarAutoEncoder\"]\n\n\nclass VarAutoEncoder(AutoEncoder):\n    \"\"\"\n    Variational Autoencoder based on the paper - https://arxiv.org/abs/1312.6114\n\n    Args:\n        spatial_dims: number of spatial dimensions.\n        in_shape: shape of input data starting with channel dimension.\n        out_channels: number of output channels.\n        latent_size: size of the latent variable.\n        channels: sequence of channels. Top block first. The length of `channels` should be no less than 2.\n        strides: sequence of convolution strides. The length of `stride` should equal to `len(channels) - 1`.\n        kernel_size: convolution kernel size, the value(s) should be odd. If sequence,\n            its length should equal to dimensions. Defaults to 3.\n        up_kernel_size: upsampling convolution kernel size, the value(s) should be odd. If sequence,\n            its length should equal to dimensions. Defaults to 3.\n        num_res_units: number of residual units. Defaults to 0.\n        inter_channels: sequence of channels defining the blocks in the intermediate layer between encode and decode.\n        inter_dilations: defines the dilation value for each block of the intermediate layer. Defaults to 1.\n        num_inter_units: number of residual units for each block of the intermediate layer. Defaults to 0.\n        act: activation type and arguments. Defaults to PReLU.\n        norm: feature normalization type and arguments. Defaults to instance norm.\n        dropout: dropout ratio. Defaults to no dropout.\n        bias: whether to have a bias term in convolution blocks. Defaults to True.\n            According to `Performance Tuning Guide <https://pytorch.org/tutorials/recipes/recipes/tuning_guide.html>`_,\n            if a conv layer is directly followed by a batch norm layer, bias should be False.\n        use_sigmoid: whether to use the sigmoid function on final output. Defaults to True.\n\n    Examples::\n\n        from monai.networks.nets import VarAutoEncoder\n\n        # 3 layer network accepting images with dimensions (1, 32, 32) and using a latent vector with 2 values\n        model = VarAutoEncoder(\n            spatial_dims=2,\n            in_shape=(32, 32),  # image spatial shape\n            out_channels=1,\n            latent_size=2,\n            channels=(16, 32, 64),\n            strides=(1, 2, 2),\n        )\n\n    see also:\n        - Variational autoencoder network with MedNIST Dataset\n          https://github.com/Project-MONAI/tutorials/blob/master/modules/varautoencoder_mednist.ipynb\n    \"\"\"\n\n    def __init__(\n        self,\n        spatial_dims: int,\n        in_shape: Sequence[int],\n        out_channels: int,\n        latent_size: int,\n        channels: Sequence[int],\n        strides: Sequence[int],\n        kernel_size: Sequence[int] | int = 3,\n        up_kernel_size: Sequence[int] | int = 3,\n        num_res_units: int = 0,\n        inter_channels: list | None = None,\n        inter_dilations: list | None = None,\n        num_inter_units: int = 2,\n        act: tuple | str | None = Act.PRELU,\n        norm: tuple | str = Norm.INSTANCE,\n        dropout: tuple | str | float | None = None,\n        bias: bool = True,\n        use_sigmoid: bool = True,\n    ) -> None:\n        self.in_channels, *self.in_shape = in_shape\n        self.use_sigmoid = use_sigmoid\n\n        self.latent_size = latent_size\n        self.final_size = np.asarray(self.in_shape, dtype=int)\n\n        super().__init__(\n            spatial_dims,\n            self.in_channels,\n            out_channels,\n            channels,\n            strides,\n            kernel_size,\n            up_kernel_size,\n            num_res_units,\n            inter_channels,\n            inter_dilations,\n            num_inter_units,\n            act,\n            norm,\n            dropout,\n            bias,\n        )\n\n        padding = same_padding(self.kernel_size)\n\n        for s in strides:\n            self.final_size = calculate_out_shape(self.final_size, self.kernel_size, s, padding)  # type: ignore\n\n        linear_size = int(np.product(self.final_size)) * self.encoded_channels\n        self.mu = nn.Linear(linear_size, self.latent_size)\n        self.logvar = nn.Linear(linear_size, self.latent_size)\n        self.decodeL = nn.Linear(self.latent_size, linear_size)\n\n    def encode_forward(self, x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:\n        x = self.encode(x)\n        x = self.intermediate(x)\n        x = x.view(x.shape[0], -1)\n        mu = self.mu(x)\n        logvar = self.logvar(x)\n        return mu, logvar\n\n    def decode_forward(self, z: torch.Tensor, use_sigmoid: bool = True) -> torch.Tensor:\n        x = F.relu(self.decodeL(z))\n        x = x.view(x.shape[0], self.channels[-1], *self.final_size)\n        x = self.decode(x)\n        if use_sigmoid:\n            x = torch.sigmoid(x)\n        return x\n\n    def reparameterize(self, mu: torch.Tensor, logvar: torch.Tensor) -> torch.Tensor:\n        std = torch.exp(0.5 * logvar)\n\n        if self.training:  # multiply random noise with std only during training\n            std = torch.randn_like(std).mul(std)\n\n        return std.add_(mu)\n\n    def forward(self, x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:\n        mu, logvar = self.encode_forward(x)\n        z = self.reparameterize(mu, logvar)\n        return self.decode_forward(z, self.use_sigmoid), mu, logvar, z\n", "path": "monai/networks/nets/varautoencoder.py"}, {"content": "# Copyright (c) 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 __future__ import annotations\n\nfrom collections.abc import Sequence\n\nimport numpy as np\nimport torch\nimport torch.nn as nn\n\nfrom monai.networks.blocks import Convolution, ResidualUnit\nfrom monai.networks.layers.convutils import calculate_out_shape, same_padding\nfrom monai.networks.layers.factories import Act, Norm\nfrom monai.networks.layers.simplelayers import Reshape\nfrom monai.utils import ensure_tuple, ensure_tuple_rep\n\n__all__ = [\"Regressor\"]\n\n\nclass Regressor(nn.Module):\n    \"\"\"\n    This defines a network for relating large-sized input tensors to small output tensors, ie. regressing large\n    values to a prediction. An output of a single dimension can be used as value regression or multi-label\n    classification prediction, an output of a single value can be used as a discriminator or critic prediction.\n\n    The network is constructed as a sequence of layers, either :py:class:`monai.networks.blocks.Convolution` or\n    :py:class:`monai.networks.blocks.ResidualUnit`, with a final fully-connected layer resizing the output from the\n    blocks to the final size. Each block is defined with a stride value typically used to downsample the input using\n    strided convolutions. In this way each block progressively condenses information from the input into a deep\n    representation the final fully-connected layer relates to a final result.\n\n    Args:\n        in_shape: tuple of integers stating the dimension of the input tensor (minus batch dimension)\n        out_shape: tuple of integers stating the dimension of the final output tensor (minus batch dimension)\n        channels: tuple of integers stating the output channels of each convolutional layer\n        strides: tuple of integers stating the stride (downscale factor) of each convolutional layer\n        kernel_size: integer or tuple of integers stating size of convolutional kernels\n        num_res_units: integer stating number of convolutions in residual units, 0 means no residual units\n        act: name or type defining activation layers\n        norm: name or type defining normalization layers\n        dropout: optional float value in range [0, 1] stating dropout probability for layers, None for no dropout\n        bias: boolean stating if convolution layers should have a bias component\n\n    Examples::\n\n        # infers a 2-value result (eg. a 2D cartesian coordinate) from a 64x64 image\n        net = Regressor((1, 64, 64), (2,), (2, 4, 8), (2, 2, 2))\n\n    \"\"\"\n\n    def __init__(\n        self,\n        in_shape: Sequence[int],\n        out_shape: Sequence[int],\n        channels: Sequence[int],\n        strides: Sequence[int],\n        kernel_size: Sequence[int] | int = 3,\n        num_res_units: int = 2,\n        act=Act.PRELU,\n        norm=Norm.INSTANCE,\n        dropout: float | None = None,\n        bias: bool = True,\n    ) -> None:\n        super().__init__()\n\n        self.in_channels, *self.in_shape = ensure_tuple(in_shape)\n        self.dimensions = len(self.in_shape)\n        self.channels = ensure_tuple(channels)\n        self.strides = ensure_tuple(strides)\n        self.out_shape = ensure_tuple(out_shape)\n        self.kernel_size = ensure_tuple_rep(kernel_size, self.dimensions)\n        self.num_res_units = num_res_units\n        self.act = act\n        self.norm = norm\n        self.dropout = dropout\n        self.bias = bias\n        self.net = nn.Sequential()\n\n        echannel = self.in_channels\n\n        padding = same_padding(kernel_size)\n\n        self.final_size = np.asarray(self.in_shape, dtype=int)\n        self.reshape = Reshape(*self.out_shape)\n\n        # encode stage\n        for i, (c, s) in enumerate(zip(self.channels, self.strides)):\n            layer = self._get_layer(echannel, c, s, i == len(channels) - 1)\n            echannel = c  # use the output channel number as the input for the next loop\n            self.net.add_module(\"layer_%i\" % i, layer)\n            self.final_size = calculate_out_shape(self.final_size, kernel_size, s, padding)  # type: ignore\n\n        self.final = self._get_final_layer((echannel,) + self.final_size)\n\n    def _get_layer(\n        self, in_channels: int, out_channels: int, strides: int, is_last: bool\n    ) -> ResidualUnit | Convolution:\n        \"\"\"\n        Returns a layer accepting inputs with `in_channels` number of channels and producing outputs of `out_channels`\n        number of channels. The `strides` indicates downsampling factor, ie. convolutional stride. If `is_last`\n        is True this is the final layer and is not expected to include activation and normalization layers.\n        \"\"\"\n\n        layer: ResidualUnit | Convolution\n\n        if self.num_res_units > 0:\n            layer = ResidualUnit(\n                subunits=self.num_res_units,\n                last_conv_only=is_last,\n                spatial_dims=self.dimensions,\n                in_channels=in_channels,\n                out_channels=out_channels,\n                strides=strides,\n                kernel_size=self.kernel_size,\n                act=self.act,\n                norm=self.norm,\n                dropout=self.dropout,\n                bias=self.bias,\n            )\n        else:\n            layer = Convolution(\n                conv_only=is_last,\n                spatial_dims=self.dimensions,\n                in_channels=in_channels,\n                out_channels=out_channels,\n                strides=strides,\n                kernel_size=self.kernel_size,\n                act=self.act,\n                norm=self.norm,\n                dropout=self.dropout,\n                bias=self.bias,\n            )\n\n        return layer\n\n    def _get_final_layer(self, in_shape: Sequence[int]):\n        linear = nn.Linear(int(np.product(in_shape)), int(np.product(self.out_shape)))\n        return nn.Sequential(nn.Flatten(), linear)\n\n    def forward(self, x: torch.Tensor) -> torch.Tensor:\n        x = self.net(x)\n        x = self.final(x)\n        x = self.reshape(x)\n        return x\n", "path": "monai/networks/nets/regressor.py"}], "after_files": [{"content": "# Copyright (c) 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 __future__ import annotations\n\nfrom collections.abc import Sequence\n\nimport numpy as np\nimport torch\nimport torch.nn as nn\nfrom torch.nn import functional as F\n\nfrom monai.networks.layers.convutils import calculate_out_shape, same_padding\nfrom monai.networks.layers.factories import Act, Norm\nfrom monai.networks.nets import AutoEncoder\n\n__all__ = [\"VarAutoEncoder\"]\n\n\nclass VarAutoEncoder(AutoEncoder):\n    \"\"\"\n    Variational Autoencoder based on the paper - https://arxiv.org/abs/1312.6114\n\n    Args:\n        spatial_dims: number of spatial dimensions.\n        in_shape: shape of input data starting with channel dimension.\n        out_channels: number of output channels.\n        latent_size: size of the latent variable.\n        channels: sequence of channels. Top block first. The length of `channels` should be no less than 2.\n        strides: sequence of convolution strides. The length of `stride` should equal to `len(channels) - 1`.\n        kernel_size: convolution kernel size, the value(s) should be odd. If sequence,\n            its length should equal to dimensions. Defaults to 3.\n        up_kernel_size: upsampling convolution kernel size, the value(s) should be odd. If sequence,\n            its length should equal to dimensions. Defaults to 3.\n        num_res_units: number of residual units. Defaults to 0.\n        inter_channels: sequence of channels defining the blocks in the intermediate layer between encode and decode.\n        inter_dilations: defines the dilation value for each block of the intermediate layer. Defaults to 1.\n        num_inter_units: number of residual units for each block of the intermediate layer. Defaults to 0.\n        act: activation type and arguments. Defaults to PReLU.\n        norm: feature normalization type and arguments. Defaults to instance norm.\n        dropout: dropout ratio. Defaults to no dropout.\n        bias: whether to have a bias term in convolution blocks. Defaults to True.\n            According to `Performance Tuning Guide <https://pytorch.org/tutorials/recipes/recipes/tuning_guide.html>`_,\n            if a conv layer is directly followed by a batch norm layer, bias should be False.\n        use_sigmoid: whether to use the sigmoid function on final output. Defaults to True.\n\n    Examples::\n\n        from monai.networks.nets import VarAutoEncoder\n\n        # 3 layer network accepting images with dimensions (1, 32, 32) and using a latent vector with 2 values\n        model = VarAutoEncoder(\n            spatial_dims=2,\n            in_shape=(32, 32),  # image spatial shape\n            out_channels=1,\n            latent_size=2,\n            channels=(16, 32, 64),\n            strides=(1, 2, 2),\n        )\n\n    see also:\n        - Variational autoencoder network with MedNIST Dataset\n          https://github.com/Project-MONAI/tutorials/blob/master/modules/varautoencoder_mednist.ipynb\n    \"\"\"\n\n    def __init__(\n        self,\n        spatial_dims: int,\n        in_shape: Sequence[int],\n        out_channels: int,\n        latent_size: int,\n        channels: Sequence[int],\n        strides: Sequence[int],\n        kernel_size: Sequence[int] | int = 3,\n        up_kernel_size: Sequence[int] | int = 3,\n        num_res_units: int = 0,\n        inter_channels: list | None = None,\n        inter_dilations: list | None = None,\n        num_inter_units: int = 2,\n        act: tuple | str | None = Act.PRELU,\n        norm: tuple | str = Norm.INSTANCE,\n        dropout: tuple | str | float | None = None,\n        bias: bool = True,\n        use_sigmoid: bool = True,\n    ) -> None:\n        self.in_channels, *self.in_shape = in_shape\n        self.use_sigmoid = use_sigmoid\n\n        self.latent_size = latent_size\n        self.final_size = np.asarray(self.in_shape, dtype=int)\n\n        super().__init__(\n            spatial_dims,\n            self.in_channels,\n            out_channels,\n            channels,\n            strides,\n            kernel_size,\n            up_kernel_size,\n            num_res_units,\n            inter_channels,\n            inter_dilations,\n            num_inter_units,\n            act,\n            norm,\n            dropout,\n            bias,\n        )\n\n        padding = same_padding(self.kernel_size)\n\n        for s in strides:\n            self.final_size = calculate_out_shape(self.final_size, self.kernel_size, s, padding)  # type: ignore\n\n        linear_size = int(np.prod(self.final_size)) * self.encoded_channels\n        self.mu = nn.Linear(linear_size, self.latent_size)\n        self.logvar = nn.Linear(linear_size, self.latent_size)\n        self.decodeL = nn.Linear(self.latent_size, linear_size)\n\n    def encode_forward(self, x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:\n        x = self.encode(x)\n        x = self.intermediate(x)\n        x = x.view(x.shape[0], -1)\n        mu = self.mu(x)\n        logvar = self.logvar(x)\n        return mu, logvar\n\n    def decode_forward(self, z: torch.Tensor, use_sigmoid: bool = True) -> torch.Tensor:\n        x = F.relu(self.decodeL(z))\n        x = x.view(x.shape[0], self.channels[-1], *self.final_size)\n        x = self.decode(x)\n        if use_sigmoid:\n            x = torch.sigmoid(x)\n        return x\n\n    def reparameterize(self, mu: torch.Tensor, logvar: torch.Tensor) -> torch.Tensor:\n        std = torch.exp(0.5 * logvar)\n\n        if self.training:  # multiply random noise with std only during training\n            std = torch.randn_like(std).mul(std)\n\n        return std.add_(mu)\n\n    def forward(self, x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:\n        mu, logvar = self.encode_forward(x)\n        z = self.reparameterize(mu, logvar)\n        return self.decode_forward(z, self.use_sigmoid), mu, logvar, z\n", "path": "monai/networks/nets/varautoencoder.py"}, {"content": "# Copyright (c) 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 __future__ import annotations\n\nfrom collections.abc import Sequence\n\nimport numpy as np\nimport torch\nimport torch.nn as nn\n\nfrom monai.networks.blocks import Convolution, ResidualUnit\nfrom monai.networks.layers.convutils import calculate_out_shape, same_padding\nfrom monai.networks.layers.factories import Act, Norm\nfrom monai.networks.layers.simplelayers import Reshape\nfrom monai.utils import ensure_tuple, ensure_tuple_rep\n\n__all__ = [\"Regressor\"]\n\n\nclass Regressor(nn.Module):\n    \"\"\"\n    This defines a network for relating large-sized input tensors to small output tensors, ie. regressing large\n    values to a prediction. An output of a single dimension can be used as value regression or multi-label\n    classification prediction, an output of a single value can be used as a discriminator or critic prediction.\n\n    The network is constructed as a sequence of layers, either :py:class:`monai.networks.blocks.Convolution` or\n    :py:class:`monai.networks.blocks.ResidualUnit`, with a final fully-connected layer resizing the output from the\n    blocks to the final size. Each block is defined with a stride value typically used to downsample the input using\n    strided convolutions. In this way each block progressively condenses information from the input into a deep\n    representation the final fully-connected layer relates to a final result.\n\n    Args:\n        in_shape: tuple of integers stating the dimension of the input tensor (minus batch dimension)\n        out_shape: tuple of integers stating the dimension of the final output tensor (minus batch dimension)\n        channels: tuple of integers stating the output channels of each convolutional layer\n        strides: tuple of integers stating the stride (downscale factor) of each convolutional layer\n        kernel_size: integer or tuple of integers stating size of convolutional kernels\n        num_res_units: integer stating number of convolutions in residual units, 0 means no residual units\n        act: name or type defining activation layers\n        norm: name or type defining normalization layers\n        dropout: optional float value in range [0, 1] stating dropout probability for layers, None for no dropout\n        bias: boolean stating if convolution layers should have a bias component\n\n    Examples::\n\n        # infers a 2-value result (eg. a 2D cartesian coordinate) from a 64x64 image\n        net = Regressor((1, 64, 64), (2,), (2, 4, 8), (2, 2, 2))\n\n    \"\"\"\n\n    def __init__(\n        self,\n        in_shape: Sequence[int],\n        out_shape: Sequence[int],\n        channels: Sequence[int],\n        strides: Sequence[int],\n        kernel_size: Sequence[int] | int = 3,\n        num_res_units: int = 2,\n        act=Act.PRELU,\n        norm=Norm.INSTANCE,\n        dropout: float | None = None,\n        bias: bool = True,\n    ) -> None:\n        super().__init__()\n\n        self.in_channels, *self.in_shape = ensure_tuple(in_shape)\n        self.dimensions = len(self.in_shape)\n        self.channels = ensure_tuple(channels)\n        self.strides = ensure_tuple(strides)\n        self.out_shape = ensure_tuple(out_shape)\n        self.kernel_size = ensure_tuple_rep(kernel_size, self.dimensions)\n        self.num_res_units = num_res_units\n        self.act = act\n        self.norm = norm\n        self.dropout = dropout\n        self.bias = bias\n        self.net = nn.Sequential()\n\n        echannel = self.in_channels\n\n        padding = same_padding(kernel_size)\n\n        self.final_size = np.asarray(self.in_shape, dtype=int)\n        self.reshape = Reshape(*self.out_shape)\n\n        # encode stage\n        for i, (c, s) in enumerate(zip(self.channels, self.strides)):\n            layer = self._get_layer(echannel, c, s, i == len(channels) - 1)\n            echannel = c  # use the output channel number as the input for the next loop\n            self.net.add_module(\"layer_%i\" % i, layer)\n            self.final_size = calculate_out_shape(self.final_size, kernel_size, s, padding)  # type: ignore\n\n        self.final = self._get_final_layer((echannel,) + self.final_size)\n\n    def _get_layer(\n        self, in_channels: int, out_channels: int, strides: int, is_last: bool\n    ) -> ResidualUnit | Convolution:\n        \"\"\"\n        Returns a layer accepting inputs with `in_channels` number of channels and producing outputs of `out_channels`\n        number of channels. The `strides` indicates downsampling factor, ie. convolutional stride. If `is_last`\n        is True this is the final layer and is not expected to include activation and normalization layers.\n        \"\"\"\n\n        layer: ResidualUnit | Convolution\n\n        if self.num_res_units > 0:\n            layer = ResidualUnit(\n                subunits=self.num_res_units,\n                last_conv_only=is_last,\n                spatial_dims=self.dimensions,\n                in_channels=in_channels,\n                out_channels=out_channels,\n                strides=strides,\n                kernel_size=self.kernel_size,\n                act=self.act,\n                norm=self.norm,\n                dropout=self.dropout,\n                bias=self.bias,\n            )\n        else:\n            layer = Convolution(\n                conv_only=is_last,\n                spatial_dims=self.dimensions,\n                in_channels=in_channels,\n                out_channels=out_channels,\n                strides=strides,\n                kernel_size=self.kernel_size,\n                act=self.act,\n                norm=self.norm,\n                dropout=self.dropout,\n                bias=self.bias,\n            )\n\n        return layer\n\n    def _get_final_layer(self, in_shape: Sequence[int]):\n        linear = nn.Linear(int(np.prod(in_shape)), int(np.prod(self.out_shape)))\n        return nn.Sequential(nn.Flatten(), linear)\n\n    def forward(self, x: torch.Tensor) -> torch.Tensor:\n        x = self.net(x)\n        x = self.final(x)\n        x = self.reshape(x)\n        return x\n", "path": "monai/networks/nets/regressor.py"}]}