Repository: Project-MONAI/MONAI

Issue:
Multiple transform return values are not properly passed as parameters for next transform
**Describe the bug**

`transform.apply_transform` does currently not handle the case where the return value of a transform call includes multiple return values.

```
if isinstance(data, (list, tuple)) and map_items:
    return [transform(item) for item in data]
```

It just passes the return values of the last transform as tuple.

**To Reproduce**
Steps to reproduce the behavior:
1. Compose the `LoadImage` and `Spacing` transforms
2. `LoadImage` will return the array and meta_data
3. `Spacing` expects both parameters but will fail

**Expected behavior**
The parameters are unpacked:

```
if isinstance(data, (list, tuple)) and map_items:
    return [transform(*items) for items in data]
```

It might be necessary to either always return a list from all transforms or to make a check here to not unpack something that should not be unpacked.
The compose could accept a `unpack` boolean flag which is provided to the `apply_transform` function.

**Environment**

```
================================
Printing MONAI config...
================================
MONAI version: 0.5.2
Numpy version: 1.20.1
Pytorch version: 1.6.0+cu101
MONAI flags: HAS_EXT = False, USE_COMPILED = False
MONAI rev id: feb3a334b7bbf302b13a6da80e0b022a4cf75a4e

Optional dependencies:
Pytorch Ignite version: NOT INSTALLED or UNKNOWN VERSION.
Nibabel version: 3.2.1
scikit-image version: 0.17.2
Pillow version: 8.1.0
Tensorboard version: 2.2.2
gdown version: NOT INSTALLED or UNKNOWN VERSION.
TorchVision version: NOT INSTALLED or UNKNOWN VERSION.
ITK version: NOT INSTALLED or UNKNOWN VERSION.
tqdm version: 4.61.1
lmdb version: NOT INSTALLED or UNKNOWN VERSION.
psutil 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...
================================
`psutil` required for `print_system_info`

================================
Printing GPU config...
================================
Num GPUs: 1
Has CUDA: True
CUDA version: 10.1
cuDNN enabled: True
cuDNN version: 7604
Current device: 0
Library compiled for CUDA architectures: ['sm_37', 'sm_50', 'sm_60', 'sm_61', 'sm_70', 'sm_75', 'compute_37']
GPU 0 Name: Quadro K2200
GPU 0 Is integrated: False
GPU 0 Is multi GPU board: False
GPU 0 Multi processor count: 5
GPU 0 Total memory (GB): 4.0
GPU 0 CUDA capability (maj.min): 5.0
```



Discussion:

Resolving patch:
diff --git a/monai/data/dataset.py b/monai/data/dataset.py
--- a/monai/data/dataset.py
+++ b/monai/data/dataset.py
@@ -1061,12 +1061,11 @@ def _transform(self, index: int):
         if not self.transform:
             return data
 
-        transformed_data = apply_transform(self.transform, data)
+        result = apply_transform(self.transform, data)
 
-        if not isinstance(transformed_data, dict):
-            raise AssertionError("With a dict supplied to apply_transform a single dict return is expected.")
-
-        return transformed_data
+        if isinstance(result, dict) or (isinstance(result, list) and isinstance(result[0], dict)):
+            return result
+        raise AssertionError("With a dict supplied to apply_transform, should return a dict or a list of dicts.")
 
 
 class CSVDataset(Dataset):


Test patch: