sys_funcs.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. import sys
  15. import os
  16. import errno
  17. import paddle
  18. def get_check_global_params(mode):
  19. check_params = [
  20. 'use_gpu', 'max_text_length', 'image_shape', 'image_shape',
  21. 'character_type', 'loss_type'
  22. ]
  23. if mode == "train_eval":
  24. check_params = check_params + [
  25. 'train_batch_size_per_card', 'test_batch_size_per_card'
  26. ]
  27. elif mode == "test":
  28. check_params = check_params + ['test_batch_size_per_card']
  29. return check_params
  30. def check_gpu(use_gpu):
  31. """
  32. Log error and exit when set use_gpu=true in paddlepaddle
  33. cpu version.
  34. """
  35. err = "Config use_gpu cannot be set as true while you are " \
  36. "using paddlepaddle cpu version ! \nPlease try: \n" \
  37. "\t1. Install paddlepaddle-gpu to run model on GPU \n" \
  38. "\t2. Set use_gpu as false in config file to run " \
  39. "model on CPU"
  40. if use_gpu:
  41. try:
  42. if not paddle.is_compiled_with_cuda():
  43. print(err)
  44. sys.exit(1)
  45. except:
  46. print("Fail to check gpu state.")
  47. sys.exit(1)
  48. def _mkdir_if_not_exist(path, logger):
  49. """
  50. mkdir if not exists, ignore the exception when multiprocess mkdir together
  51. """
  52. if not os.path.exists(path):
  53. try:
  54. os.makedirs(path)
  55. except OSError as e:
  56. if e.errno == errno.EEXIST and os.path.isdir(path):
  57. logger.warning(
  58. 'be happy if some process has already created {}'.format(
  59. path))
  60. else:
  61. raise OSError('Failed to mkdir {}'.format(path))