multilabel_dataset.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. from __future__ import print_function
  15. import numpy as np
  16. import os
  17. import cv2
  18. from paddlex.ppcls.data.preprocess import transform
  19. from paddlex.ppcls.utils import logger
  20. from .common_dataset import CommonDataset
  21. class MultiLabelDataset(CommonDataset):
  22. def _load_anno(self):
  23. assert os.path.exists(self._cls_path)
  24. assert os.path.exists(self._img_root)
  25. self.images = []
  26. self.labels = []
  27. with open(self._cls_path) as fd:
  28. lines = fd.readlines()
  29. for l in lines:
  30. l = l.strip().split("\t")
  31. self.images.append(os.path.join(self._img_root, l[0]))
  32. labels = l[1].split(',')
  33. labels = [np.int64(i) for i in labels]
  34. self.labels.append(labels)
  35. assert os.path.exists(self.images[-1])
  36. def __getitem__(self, idx):
  37. try:
  38. with open(self.images[idx], 'rb') as f:
  39. img = f.read()
  40. if self._transform_ops:
  41. img = transform(img, self._transform_ops)
  42. img = img.transpose((2, 0, 1))
  43. label = np.array(self.labels[idx]).astype("float32")
  44. return (img, label)
  45. except Exception as ex:
  46. logger.error("Exception occured when parse line: {} with msg: {}".
  47. format(self.images[idx], ex))
  48. rnd_idx = np.random.randint(self.__len__())
  49. return self.__getitem__(rnd_idx)