easydata_cls.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  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 absolute_import
  15. import os.path as osp
  16. import random
  17. import copy
  18. import json
  19. import paddlex.utils.logging as logging
  20. from paddlex.utils import path_normalization
  21. from .imagenet import ImageNet
  22. from .dataset import is_pic
  23. from .dataset import get_encoding
  24. class EasyDataCls(ImageNet):
  25. """读取EasyDataCls格式的分类数据集,并对样本进行相应的处理。
  26. Args:
  27. data_dir (str): 数据集所在的目录路径。
  28. file_list (str): 描述数据集图片文件和对应标注文件的文件路径(文本内每行路径为相对data_dir的相对路)。
  29. label_list (str): 描述数据集包含的类别信息文件路径。
  30. transforms (paddlex.cls.transforms): 数据集中每个样本的预处理/增强算子。
  31. num_workers (int|str): 数据集中样本在预处理过程中的线程或进程数。默认为'auto'。当设为'auto'时,根据
  32. 系统的实际CPU核数设置`num_workers`: 如果CPU核数的一半大于8,则`num_workers`为8,否则为CPU核
  33. 数的一半。
  34. buffer_size (int): 数据集中样本在预处理过程中队列的缓存长度,以样本数为单位。默认为100。
  35. parallel_method (str): 数据集中样本在预处理过程中并行处理的方式,支持'thread'
  36. 线程和'process'进程两种方式。默认为'process'(Windows和Mac下会强制使用thread,该参数无效)。
  37. shuffle (bool): 是否需要对数据集中样本打乱顺序。默认为False。
  38. """
  39. def __init__(self,
  40. data_dir,
  41. file_list,
  42. label_list,
  43. transforms=None,
  44. num_workers='auto',
  45. buffer_size=8,
  46. parallel_method='process',
  47. shuffle=False):
  48. super(ImageNet, self).__init__(
  49. transforms=transforms,
  50. num_workers=num_workers,
  51. buffer_size=buffer_size,
  52. parallel_method=parallel_method,
  53. shuffle=shuffle)
  54. self.file_list = list()
  55. self.labels = list()
  56. self._epoch = 0
  57. with open(label_list, encoding=get_encoding(label_list)) as f:
  58. for line in f:
  59. item = line.strip()
  60. self.labels.append(item)
  61. logging.info("Starting to read file list from dataset...")
  62. with open(file_list, encoding=get_encoding(file_list)) as f:
  63. for line in f:
  64. img_file, json_file = [osp.join(data_dir, x) \
  65. for x in line.strip().split()[:2]]
  66. img_file = path_normalization(img_file)
  67. json_file = path_normalization(json_file)
  68. if not is_pic(img_file):
  69. continue
  70. if not osp.isfile(json_file):
  71. continue
  72. if not osp.exists(img_file):
  73. raise IOError('The image file {} is not exist!'.format(
  74. img_file))
  75. with open(json_file, mode='r', \
  76. encoding=get_encoding(json_file)) as j:
  77. json_info = json.load(j)
  78. label = json_info['labels'][0]['name']
  79. self.file_list.append([img_file, self.labels.index(label)])
  80. self.num_samples = len(self.file_list)
  81. logging.info("{} samples in file {}".format(
  82. len(self.file_list), file_list))