__init__.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Copyright (c) 2020 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 absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. from __future__ import unicode_literals
  18. import os
  19. import sys
  20. import numpy as np
  21. import skimage
  22. import paddle
  23. import signal
  24. import random
  25. __dir__ = os.path.dirname(os.path.abspath(__file__))
  26. sys.path.append(os.path.abspath(os.path.join(__dir__, '../..')))
  27. import copy
  28. from paddle.io import Dataset, DataLoader, BatchSampler, DistributedBatchSampler
  29. import paddle.distributed as dist
  30. from ppocr.data.imaug import transform, create_operators
  31. from ppocr.data.simple_dataset import SimpleDataSet
  32. from ppocr.data.lmdb_dataset import LMDBDataSet, LMDBDataSetSR
  33. from ppocr.data.pgnet_dataset import PGDataSet
  34. from ppocr.data.pubtab_dataset import PubTabDataSet
  35. __all__ = ['build_dataloader', 'transform', 'create_operators']
  36. def term_mp(sig_num, frame):
  37. """ kill all child processes
  38. """
  39. pid = os.getpid()
  40. pgid = os.getpgid(os.getpid())
  41. print("main proc {} exit, kill process group " "{}".format(pid, pgid))
  42. os.killpg(pgid, signal.SIGKILL)
  43. def build_dataloader(config, mode, device, logger, seed=None):
  44. config = copy.deepcopy(config)
  45. support_dict = [
  46. 'SimpleDataSet', 'LMDBDataSet', 'PGDataSet', 'PubTabDataSet',
  47. 'LMDBDataSetSR'
  48. ]
  49. module_name = config[mode]['dataset']['name']
  50. assert module_name in support_dict, Exception(
  51. 'DataSet only support {}'.format(support_dict))
  52. assert mode in ['Train', 'Eval', 'Test'
  53. ], "Mode should be Train, Eval or Test."
  54. dataset = eval(module_name)(config, mode, logger, seed)
  55. loader_config = config[mode]['loader']
  56. batch_size = loader_config['batch_size_per_card']
  57. drop_last = loader_config['drop_last']
  58. shuffle = loader_config['shuffle']
  59. num_workers = loader_config['num_workers']
  60. if 'use_shared_memory' in loader_config.keys():
  61. use_shared_memory = loader_config['use_shared_memory']
  62. else:
  63. use_shared_memory = True
  64. if mode == "Train":
  65. # Distribute data to multiple cards
  66. batch_sampler = DistributedBatchSampler(
  67. dataset=dataset,
  68. batch_size=batch_size,
  69. shuffle=shuffle,
  70. drop_last=drop_last)
  71. else:
  72. # Distribute data to single card
  73. batch_sampler = BatchSampler(
  74. dataset=dataset,
  75. batch_size=batch_size,
  76. shuffle=shuffle,
  77. drop_last=drop_last)
  78. if 'collate_fn' in loader_config:
  79. from . import collate_fn
  80. collate_fn = getattr(collate_fn, loader_config['collate_fn'])()
  81. else:
  82. collate_fn = None
  83. data_loader = DataLoader(
  84. dataset=dataset,
  85. batch_sampler=batch_sampler,
  86. places=device,
  87. num_workers=num_workers,
  88. return_list=True,
  89. use_shared_memory=use_shared_memory,
  90. collate_fn=collate_fn)
  91. # support exit using ctrl+c
  92. signal.signal(signal.SIGINT, term_mp)
  93. signal.signal(signal.SIGTERM, term_mp)
  94. return data_loader