__init__.py 3.1 KB

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