You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ei_dataset.py 2.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  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. # httpwww.apache.orglicensesLICENSE-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. # ============================================================================
  15. """Process Dataset."""
  16. import abc
  17. import os
  18. import time
  19. from .utils.adapter import get_raw_samples, read_image
  20. class BaseDataset:
  21. """
  22. Create dataset.
  23. Args:
  24. data_url (str): The path of data.
  25. usage (str): Whether to use train or eval (default='train').
  26. Returns:
  27. Dataset.
  28. """
  29. def __init__(self, data_url, usage):
  30. self.data_url = data_url
  31. self.usage = usage
  32. self.cur_index = 0
  33. self.samples = []
  34. _s_time = time.time()
  35. self._load_samples()
  36. _e_time = time.time()
  37. print(f"load samples success~, time cost = {_e_time - _s_time}")
  38. def __getitem__(self, item):
  39. sample = self.samples[item]
  40. return self._next_data(sample)
  41. def __len__(self):
  42. return len(self.samples)
  43. @staticmethod
  44. def _next_data(sample):
  45. image_path = sample[0]
  46. mask_image_path = sample[1]
  47. image = read_image(image_path)
  48. mask_image = read_image(mask_image_path)
  49. return [image, mask_image]
  50. @abc.abstractmethod
  51. def _load_samples(self):
  52. pass
  53. class HwVocRawDataset(BaseDataset):
  54. """
  55. Create dataset with raw data.
  56. Args:
  57. data_url (str): The path of data.
  58. usage (str): Whether to use train or eval (default='train').
  59. Returns:
  60. Dataset.
  61. """
  62. def __init__(self, data_url, usage="train"):
  63. super().__init__(data_url, usage)
  64. def _load_samples(self):
  65. try:
  66. self.samples = get_raw_samples(os.path.join(self.data_url, self.usage))
  67. except Exception as e:
  68. print("load HwVocRawDataset failed!!!")
  69. raise e