Repository: Project-MONAI/MONAI

Issue:
Pickling Error: Can't pickle <function SpatialCropForegroundd.<lambda> on monai.apps.deepgrow.transforms
**Describe the bug**

```
Traceback (most recent call last):
  File "train_3d.py", line 71, in <module>
    train.run(args)
  File "D:\workspace\HDD-Codebase\Experiments\MONAI\WHS\DeepGrow\train.py", line 345, in run
    trainer.run()
  File "d:\workspace\hdd-codebase\experiments\monai\monai\monai\engines\trainer.py", line 56, in run
    super().run()
  File "d:\workspace\hdd-codebase\experiments\monai\monai\monai\engines\workflow.py", line 258, in run
    super().run(data=self.data_loader, max_epochs=self.state.max_epochs)
  File "C:\Users\Admin\.conda\envs\monai\lib\site-packages\ignite\engine\engine.py", line 698, in run
    return self._internal_run()
  File "C:\Users\Admin\.conda\envs\monai\lib\site-packages\ignite\engine\engine.py", line 771, in _internal_run
    self._handle_exception(e)
  File "C:\Users\Admin\.conda\envs\monai\lib\site-packages\ignite\engine\engine.py", line 464, in _handle_exception
    self._fire_event(Events.EXCEPTION_RAISED, e)
  File "C:\Users\Admin\.conda\envs\monai\lib\site-packages\ignite\engine\engine.py", line 421, in _fire_event
    func(*first, *(event_args + others), **kwargs)
  File "d:\workspace\hdd-codebase\experiments\monai\monai\monai\handlers\stats_handler.py", line 158, in exception_raised
    raise e
  File "C:\Users\Admin\.conda\envs\monai\lib\site-packages\ignite\engine\engine.py", line 739, in _internal_run
    self._setup_engine()
  File "C:\Users\Admin\.conda\envs\monai\lib\site-packages\ignite\engine\engine.py", line 721, in _setup_engine
    self._dataloader_iter = iter(self.state.dataloader)
  File "C:\Users\Admin\.conda\envs\monai\lib\site-packages\torch\utils\data\dataloader.py", line 359, in __iter__
    return self._get_iterator()
  File "C:\Users\Admin\.conda\envs\monai\lib\site-packages\torch\utils\data\dataloader.py", line 305, in _get_iterator
    return _MultiProcessingDataLoaderIter(self)
  File "C:\Users\Admin\.conda\envs\monai\lib\site-packages\torch\utils\data\dataloader.py", line 918, in __init__
    w.start()
  File "C:\Users\Admin\.conda\envs\monai\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)
  File "C:\Users\Admin\.conda\envs\monai\lib\multiprocessing\context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Users\Admin\.conda\envs\monai\lib\multiprocessing\context.py", line 326, in _Popen
    return Popen(process_obj)
  File "C:\Users\Admin\.conda\envs\monai\lib\multiprocessing\popen_spawn_win32.py", line 93, in __init__
    reduction.dump(process_obj, to_child)
  File "C:\Users\Admin\.conda\envs\monai\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <function SpatialCropForegroundd.<lambda> at 0x000002C4321B9310>: attribute lookup SpatialCropForegroundd.<lambda> on monai.apps.deepgrow.transforms failed
```



**To Reproduce**
Steps to reproduce the behavior:
1. Go to [DeepGrow Tutorial](https://github.com/Project-MONAI/tutorials/tree/master/deepgrow/ignite)
2. Install  `monai==0.7.0` & `monai dev branch`
3. Run commands `python train_3d.py`

**Expected behavior**
The problem is similar to [Issue: Unable to Train Model in PyTorch Subprocess #2122](https://github.com/Project-MONAI/MONAI/issues/2122)

Following fix [2128](https://github.com/Project-MONAI/MONAI/pull/2128) need to be added in 
/monai/apps/deepgrow/transforms.py b/monai/apps/deepgrow/transforms.py
Line 398 has to be changed from 
> `select_fn: Callable = lambda x: x > 0`
to
> `select_fn: Callable = is_positive`





**Screenshots**
![image](https://user-images.githubusercontent.com/35004830/141312220-6ef5b29f-140f-44ae-a35a-bfbf313183fd.png)
**Environment**

```
================================
Printing MONAI config...
================================
MONAI version: 0.7.0+126.g80763723.dirty
Numpy version: 1.21.2
Pytorch version: 1.10.0
MONAI flags: HAS_EXT = False, USE_COMPILED = False
MONAI rev id: 80763723f040dc949554a36074f15a444df5199e

Optional dependencies:
Pytorch Ignite version: 0.4.7
Nibabel version: 3.2.1
scikit-image version: 0.18.3
Pillow version: 8.4.0
Tensorboard version: 2.7.0
gdown version: NOT INSTALLED or UNKNOWN VERSION.
TorchVision version: 0.11.1
tqdm version: NOT INSTALLED or UNKNOWN VERSION.
lmdb version: NOT INSTALLED or UNKNOWN VERSION.
psutil version: NOT INSTALLED or UNKNOWN VERSION.
pandas version: NOT INSTALLED or UNKNOWN VERSION.
einops version: NOT INSTALLED or UNKNOWN VERSION.
transformers version: NOT INSTALLED or UNKNOWN VERSION.
mlflow 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: 11.3
cuDNN enabled: True
cuDNN version: 8200
Current device: 0
Library compiled for CUDA architectures: ['sm_37', 'sm_50', 'sm_60', 'sm_61', 'sm_70', 'sm_75', 'sm_80', 'sm_86', 'compute_37']
GPU 0 Name: GeForce RTX 3080 Laptop GPU
GPU 0 Is integrated: False
GPU 0 Is multi GPU board: False
GPU 0 Multi processor count: 48
GPU 0 Total memory (GB): 16.0
GPU 0 CUDA capability (maj.min): 8.6
```

**Additional context**
I have added the changes patch in text format [fix.patch.txt](https://github.com/Project-MONAI/MONAI/files/7520987/fix.patch.txt)



Discussion:
Hi @architraj29 ,

Thanks for your bug feedback, good finding!
I will try to update it ASAP.
CC @SachidanandAlle .

Thanks.

Your patch looks good @architraj29, in case you are interested in submitting a pull request (https://github.com/Project-MONAI/MONAI/blob/dev/CONTRIBUTING.md#submitting-pull-requests), we are also happy to help merge it...

Resolving patch:
diff --git a/monai/apps/deepgrow/transforms.py b/monai/apps/deepgrow/transforms.py
--- a/monai/apps/deepgrow/transforms.py
+++ b/monai/apps/deepgrow/transforms.py
@@ -18,7 +18,7 @@
 from monai.networks.layers import GaussianFilter
 from monai.transforms import Resize, SpatialCrop
 from monai.transforms.transform import MapTransform, Randomizable, Transform
-from monai.transforms.utils import generate_spatial_bounding_box
+from monai.transforms.utils import generate_spatial_bounding_box, is_positive
 from monai.utils import (
     InterpolateMode,
     deprecated_arg,
@@ -395,7 +395,7 @@ def __init__(
         keys: KeysCollection,
         source_key: str,
         spatial_size: Union[Sequence[int], np.ndarray],
-        select_fn: Callable = lambda x: x > 0,
+        select_fn: Callable = is_positive,
         channel_indices: Optional[IndexSelection] = None,
         margin: int = 0,
         meta_keys: Optional[KeysCollection] = None,


Test patch: