det_dataloader.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import six
  15. import sys
  16. from paddle.io import DataLoader
  17. class BaseDataLoader(object):
  18. def __init__(self, dataset, batch_sampler, use_shared_memory):
  19. self._batch_transforms = dataset.batch_transforms
  20. self._batch_sampler = batch_sampler
  21. self.dataset = dataset
  22. self.dataloader = DataLoader(
  23. dataset=self.dataset,
  24. batch_sampler=self._batch_sampler,
  25. collate_fn=self._batch_transforms,
  26. num_workers=self.dataset.num_workers,
  27. return_list=True,
  28. use_shared_memory=use_shared_memory)
  29. self.loader = iter(self.dataloader)
  30. def __call__(self):
  31. return self
  32. def __len__(self):
  33. return len(self._batch_sampler)
  34. def __iter__(self):
  35. return self
  36. def __next__(self):
  37. try:
  38. return next(self.loader)
  39. except StopIteration:
  40. self.loader = iter(self.dataloader)
  41. six.reraise(*sys.exc_info())
  42. def next(self):
  43. # python2 compatibility
  44. return self.__next__()