Recursive device conversion of model inputs and targets
Is your feature request related to a problem? Please describe.
I want to conduct fine-tuning of the faster R-CNN model that is implemented in torchvision by following this tutorial (https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html).
Inputs and targets of the faster R-CNN are a list of tensors and a list of dict of tensors, respectively (See example in https://pytorch.org/docs/stable/torchvision/models.html#faster-r-cnn ).
I found that the above data structures cause type error because a condition if isinstance(x, torch.Tensor) (see follows) cannot catch such cases.
https://github.com/williamFalcon/pytorch-lightning/blob/590282f2b04a9ca2470296cb5f4a4351f46d5496/pytorch_lightning/models/trainer.py#L390-L395
Describe the solution you'd like
I want to move a list (or dict, etc.) of tensors into GPUs recursively.
Describe alternatives you've considered
Temporarily, I'm addressing the above problem by using the following code:
             for i, x in enumerate(data_batch):
                 if isinstance(x, torch.Tensor):
                     data_batch[i] = x.cuda(gpu_id)
+                elif isinstance(x, list):
+                    for j, element in enumerate(x):
+                        if isinstance(element, torch.Tensor):
+                            x[j] = element.cuda(gpu_id)
+                        elif isinstance(element, dict):
+                            x[j] = {k: v.cuda(gpu_id) \
+                                    for k, v in element.items()}