check.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Copyright (c) 2019 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. import sys
  18. import paddle
  19. import six
  20. import paddle.version as fluid_version
  21. from .logger import setup_logger
  22. logger = setup_logger(__name__)
  23. __all__ = ['check_gpu', 'check_version', 'check_config']
  24. def check_gpu(use_gpu):
  25. """
  26. Log error and exit when set use_gpu=true in paddlepaddle
  27. cpu version.
  28. """
  29. err = "Config use_gpu cannot be set as true while you are " \
  30. "using paddlepaddle cpu version ! \nPlease try: \n" \
  31. "\t1. Install paddlepaddle-gpu to run model on GPU \n" \
  32. "\t2. Set use_gpu as false in config file to run " \
  33. "model on CPU"
  34. try:
  35. if use_gpu and not paddle.is_compiled_with_cuda():
  36. logger.error(err)
  37. sys.exit(1)
  38. except Exception as e:
  39. pass
  40. def check_version(version='2.0'):
  41. """
  42. Log error and exit when the installed version of paddlepaddle is
  43. not satisfied.
  44. """
  45. err = "PaddlePaddle version {} or higher is required, " \
  46. "or a suitable develop version is satisfied as well. \n" \
  47. "Please make sure the version is good with your code.".format(version)
  48. version_installed = [
  49. fluid_version.major, fluid_version.minor, fluid_version.patch,
  50. fluid_version.rc
  51. ]
  52. if version_installed == ['0', '0', '0', '0']:
  53. return
  54. version_split = version.split('.')
  55. length = min(len(version_installed), len(version_split))
  56. for i in six.moves.range(length):
  57. if version_installed[i] > version_split[i]:
  58. return
  59. if version_installed[i] < version_split[i]:
  60. raise Exception(err)
  61. def check_config(cfg):
  62. """
  63. Check the correctness of the configuration file. Log error and exit
  64. when Config is not compliant.
  65. """
  66. err = "'{}' not specified in config file. Please set it in config file."
  67. check_list = ['architecture', 'num_classes']
  68. try:
  69. for var in check_list:
  70. if not var in cfg:
  71. logger.error(err.format(var))
  72. sys.exit(1)
  73. except Exception as e:
  74. pass
  75. if 'log_iter' not in cfg:
  76. cfg.log_iter = 20
  77. return cfg