easydata_cls.py 3.8 KB

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