Base data pre-processor used for copying data to the target device. Subclasses inherit from ``BaseDataPreprocessor`` could override the forward method to implement custom data pre-processing, such as batch-resize, MixUp, or CutMix. Args: non_blocking (bool): Whether block c
| 17 | |
| 18 | @MODELS.register_module() |
| 19 | class BaseDataPreprocessor(nn.Module): |
| 20 | """Base data pre-processor used for copying data to the target device. |
| 21 | |
| 22 | Subclasses inherit from ``BaseDataPreprocessor`` could override the |
| 23 | forward method to implement custom data pre-processing, such as |
| 24 | batch-resize, MixUp, or CutMix. |
| 25 | |
| 26 | Args: |
| 27 | non_blocking (bool): Whether block current process |
| 28 | when transferring data to device. |
| 29 | New in version 0.3.0. |
| 30 | |
| 31 | Note: |
| 32 | Data dictionary returned by dataloader must be a dict and at least |
| 33 | contain the ``inputs`` key. |
| 34 | """ |
| 35 | |
| 36 | def __init__(self, non_blocking: Optional[bool] = False): |
| 37 | super().__init__() |
| 38 | self._non_blocking = non_blocking |
| 39 | self._device = torch.device('cpu') |
| 40 | |
| 41 | def cast_data(self, data: CastData) -> CastData: |
| 42 | """Copying data to the target device. |
| 43 | |
| 44 | Args: |
| 45 | data (dict): Data returned by ``DataLoader``. |
| 46 | |
| 47 | Returns: |
| 48 | CollatedResult: Inputs and data sample at target device. |
| 49 | """ |
| 50 | if isinstance(data, Mapping): |
| 51 | return {key: self.cast_data(data[key]) for key in data} |
| 52 | elif isinstance(data, (str, bytes)) or data is None: |
| 53 | return data |
| 54 | elif isinstance(data, tuple) and hasattr(data, '_fields'): |
| 55 | # namedtuple |
| 56 | return type(data)(*(self.cast_data(sample) for sample in data)) # type: ignore # noqa: E501 # yapf:disable |
| 57 | elif isinstance(data, Sequence): |
| 58 | return type(data)(self.cast_data(sample) for sample in data) # type: ignore # noqa: E501 # yapf:disable |
| 59 | elif isinstance(data, (torch.Tensor, BaseDataElement)): |
| 60 | return data.to(self.device, non_blocking=self._non_blocking) |
| 61 | else: |
| 62 | return data |
| 63 | |
| 64 | def forward(self, data: dict, training: bool = False) -> Union[dict, list]: |
| 65 | """Preprocesses the data into the model input format. |
| 66 | |
| 67 | After the data pre-processing of :meth:`cast_data`, ``forward`` |
| 68 | will stack the input tensor list to a batch tensor at the first |
| 69 | dimension. |
| 70 | |
| 71 | Args: |
| 72 | data (dict): Data returned by dataloader |
| 73 | training (bool): Whether to enable training time augmentation. |
| 74 | |
| 75 | Returns: |
| 76 | dict or list: Data in the same format as the model input. |
no outgoing calls
searching dependent graphs…