Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add non_blocking feature to BaseDataPreprocessor #618

Merged
merged 5 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions mmengine/model/base_model/data_preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ class BaseDataPreprocessor(nn.Module):
forward method to implement custom data pre-processing, such as
batch-resize, MixUp, or CutMix.

Args:
non_blocking (bool): Whether block current process
when transferring data to device.
shenmishajing marked this conversation as resolved.
Show resolved Hide resolved

Note:
Data dictionary returned by dataloader must be a dict and at least
contain the ``inputs`` key.
"""

def __init__(self):
def __init__(self, non_blocking: Optional[bool] = False):
super().__init__()
self._non_blocking = non_blocking
self._device = torch.device('cpu')

def cast_data(self, data: CastData) -> CastData:
Expand All @@ -48,9 +53,9 @@ def cast_data(self, data: CastData) -> CastData:
elif isinstance(data, Sequence):
return [self.cast_data(sample) for sample in data]
elif isinstance(data, torch.Tensor):
return data.to(self.device)
return data.to(self.device, non_blocking=self._non_blocking)
elif isinstance(data, BaseDataElement):
return data.to(self.device)
return data.to(self.device, non_blocking=self._non_blocking)
else:
return data

Expand Down Expand Up @@ -150,6 +155,8 @@ class ImgDataPreprocessor(BaseDataPreprocessor):
Defaults to False.
rgb_to_bgr (bool): whether to convert image from RGB to RGB.
Defaults to False.
non_blocking (bool): Whether block current process
when transferring data to device.
shenmishajing marked this conversation as resolved.
Show resolved Hide resolved

Note:
if images do not need to be normalized, `std` and `mean` should be
Expand All @@ -163,8 +170,9 @@ def __init__(self,
pad_size_divisor: int = 1,
pad_value: Union[float, int] = 0,
bgr_to_rgb: bool = False,
rgb_to_bgr: bool = False):
super().__init__()
rgb_to_bgr: bool = False,
non_blocking: Optional[bool] = False):
super().__init__(non_blocking)
assert not (bgr_to_rgb and rgb_to_bgr), (
'`bgr2rgb` and `rgb2bgr` cannot be set to True at the same time')
assert (mean is None) == (std is None), (
Expand Down
5 changes: 5 additions & 0 deletions tests/test_model/test_base_model/test_data_preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class TestBaseDataPreprocessor(TestCase):
def test_init(self):
base_data_preprocessor = BaseDataPreprocessor()
self.assertEqual(base_data_preprocessor._device.type, 'cpu')
self.assertEqual(base_data_preprocessor._non_blocking, False)

base_data_preprocessor = BaseDataPreprocessor(True)
self.assertEqual(base_data_preprocessor._device.type, 'cpu')
self.assertEqual(base_data_preprocessor._non_blocking, True)

def test_forward(self):
# Test cpu forward with list of data samples.
Expand Down