seg_env.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. """
  15. This module is used to store environmental parameters in PaddleSeg.
  16. SEG_HOME : Root directory for storing PaddleSeg related data. Default to ~/.paddleseg.
  17. Users can change the default value through the SEG_HOME environment variable.
  18. DATA_HOME : The directory to store the automatically downloaded dataset, e.g ADE20K.
  19. PRETRAINED_MODEL_HOME : The directory to store the automatically downloaded pretrained model.
  20. """
  21. import os
  22. from paddlex.paddleseg.utils import logger
  23. def _get_user_home():
  24. return os.path.expanduser('~')
  25. def _get_seg_home():
  26. if 'SEG_HOME' in os.environ:
  27. home_path = os.environ['SEG_HOME']
  28. if os.path.exists(home_path):
  29. if os.path.isdir(home_path):
  30. return home_path
  31. else:
  32. logger.warning('SEG_HOME {} is a file!'.format(home_path))
  33. else:
  34. return home_path
  35. return os.path.join(_get_user_home(), '.paddleseg')
  36. def _get_sub_home(directory):
  37. home = os.path.join(_get_seg_home(), directory)
  38. if not os.path.exists(home):
  39. os.makedirs(home, exist_ok=True)
  40. return home
  41. USER_HOME = _get_user_home()
  42. SEG_HOME = _get_seg_home()
  43. DATA_HOME = _get_sub_home('dataset')
  44. TMP_HOME = _get_sub_home('tmp')
  45. PRETRAINED_MODEL_HOME = _get_sub_home('pretrained_model')