Repository: Project-MONAI/MONAI

Issue:
RuntimeError for SwinUNETR version 2 but not for version 1.
**Describe the bug**

RuntimeError for SwinUNETR version 2 but not for version 1.

**To Reproduce**

Code to reproduce:

```python
import torch
from monai.networks.nets.swin_unetr import SwinUNETR

params = {
    "img_size": (608, 576),
    "in_channels": 3,
    "out_channels": 3,
    "spatial_dims": 2,
    "use_v2": False,
}

model = SwinUNETR(**params)
input = torch.randn(1, 3, 608, 576)
out = model(input)  # Works as expected

params["use_v2"] = True
modelv2 = SwinUNETR(**params)
outv2 = modelv2(input)  # RuntimeError
```

Output:
```
2.0.1+cu117
1.3.0
Traceback (most recent call last):
  File "/home/morano/bug.py", line 23, in <module>
    outv2 = modelv2(input)
            ^^^^^^^^^^^^^^
  File "/home/morano/.local/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/morano/.local/lib/python3.11/site-packages/monai/networks/nets/swin_unetr.py", line 325, in forward
    hidden_states_out = self.swinViT(x_in, self.normalize)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/morano/.local/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/morano/.local/lib/python3.11/site-packages/monai/networks/nets/swin_unetr.py", line 1066, in forward
    x0 = self.layers1c[0](x0.contiguous())
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/morano/.local/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/morano/.local/lib/python3.11/site-packages/monai/networks/blocks/unetr_block.py", line 259, in forward
    return self.layer(inp)
           ^^^^^^^^^^^^^^^
  File "/home/morano/.local/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/morano/.local/lib/python3.11/site-packages/monai/networks/blocks/dynunet_block.py", line 100, in forward
    out = self.conv1(inp)
          ^^^^^^^^^^^^^^^
  File "/home/morano/.local/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/morano/.local/lib/python3.11/site-packages/torch/nn/modules/container.py", line 217, in forward
    input = module(input)
            ^^^^^^^^^^^^^
  File "/home/morano/.local/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/morano/.local/lib/python3.11/site-packages/torch/nn/modules/conv.py", line 613, in forward
    return self._conv_forward(input, self.weight, self.bias)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/morano/.local/lib/python3.11/site-packages/torch/nn/modules/conv.py", line 608, in _conv_forward
    return F.conv3d(
           ^^^^^^^^^
RuntimeError: Given groups=1, weight of size [24, 24, 3, 3, 3], expected input[1, 1, 24, 304, 288] to have 24 channels, but got 1 channels instead
```


**Expected behavior**

I expected it to work as SwinUNETR version 1.


**Environment**

```
python -c "import monai; monai.config.print_debug_info()"
```

```
================================
Printing MONAI config...
================================
MONAI version: 1.3.0
Numpy version: 1.24.4
Pytorch version: 2.0.1+cu117
MONAI flags: HAS_EXT = False, USE_COMPILED = False, USE_META_DICT = False
MONAI rev id: 865972f7a791bf7b42efbcd87c8402bd865b329e
MONAI __file__: /home/<username>/.local/lib/python3.11/site-packages/monai/__init__.py

Optional dependencies:
Pytorch Ignite version: NOT INSTALLED or UNKNOWN VERSION.
ITK version: NOT INSTALLED or UNKNOWN VERSION.
Nibabel version: NOT INSTALLED or UNKNOWN VERSION.
scikit-image version: 0.21.0
scipy version: 1.11.1
Pillow version: 9.5.0
Tensorboard version: NOT INSTALLED or UNKNOWN VERSION.
gdown version: NOT INSTALLED or UNKNOWN VERSION.
TorchVision version: 0.15.2+cu117
tqdm version: 4.65.0
lmdb version: NOT INSTALLED or UNKNOWN VERSION.
psutil version: 5.9.5
pandas version: 2.0.2
einops version: 0.7.0
transformers version: NOT INSTALLED or UNKNOWN VERSION.
mlflow version: NOT INSTALLED or UNKNOWN VERSION.
pynrrd version: NOT INSTALLED or UNKNOWN VERSION.
clearml version: NOT INSTALLED or UNKNOWN VERSION.

For details about installing the optional dependencies, please visit:
    https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies


================================
Printing system config...
================================
System: Linux
Linux version: Fedora Linux 38 (Workstation Edition)
Platform: Linux-6.5.12-200.fc38.x86_64-x86_64-with-glibc2.37
Processor:
Machine: x86_64
Python version: 3.11.6
Process name: python
Command: ['python', '-c', 'import monai; monai.config.print_debug_info()']
Open files: []
Num physical CPUs: 8
Num logical CPUs: 16
Num usable CPUs: 16
CPU usage (%): [8.6, 9.5, 6.1, 7.6, 6.0, 7.7, 7.6, 99.2, 6.1, 6.0, 6.1, 7.7, 6.8, 11.2, 10.3, 5.2]
CPU freq. (MHz): 3756
Load avg. in last 1, 5, 15 mins (%): [2.0, 4.1, 13.3]
Disk usage (%): 8.0
Avg. sensor temp. (Celsius): UNKNOWN for given OS
Total physical memory (GB): 31.2
Available memory (GB): 18.8
Used memory (GB): 11.7

================================
Printing GPU config...
================================
Num GPUs: 1
Has CUDA: True
CUDA version: 11.7
cuDNN enabled: True
NVIDIA_TF32_OVERRIDE: None
TORCH_ALLOW_TF32_CUBLAS_OVERRIDE: None
cuDNN version: 8500
Current device: 0
Library compiled for CUDA architectures: ['sm_37', 'sm_50', 'sm_60', 'sm_70', 'sm_75', 'sm_80', 'sm_86']
GPU 0 Name: NVIDIA GeForce RTX 3060 Ti
GPU 0 Is integrated: False
GPU 0 Is multi GPU board: False
GPU 0 Multi processor count: 38
GPU 0 Total memory (GB): 7.8
GPU 0 CUDA capability (maj.min): 8.6
```



Discussion:
Thanks for the reporting, the error is caused by this hard coding here.
https://github.com/Project-MONAI/MONAI/blob/2fc012687f428c7dacbc14e5d57e92d5d5f49013/monai/networks/nets/swin_unetr.py#L1027
If there is no special reason, we may need to update it.
cc @heyufan1995, could you please help confirm it? Thanks!

Resolving patch:
diff --git a/monai/networks/nets/swin_unetr.py b/monai/networks/nets/swin_unetr.py
--- a/monai/networks/nets/swin_unetr.py
+++ b/monai/networks/nets/swin_unetr.py
@@ -1024,7 +1024,7 @@ def __init__(
                 self.layers4.append(layer)
             if self.use_v2:
                 layerc = UnetrBasicBlock(
-                    spatial_dims=3,
+                    spatial_dims=spatial_dims,
                     in_channels=embed_dim * 2**i_layer,
                     out_channels=embed_dim * 2**i_layer,
                     kernel_size=3,


Test patch: