utils.py 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. # The DAMO-YOLO implementation is also open-sourced by the authors, and available
  3. # at https://github.com/tinyvision/damo-yolo.
  4. import importlib
  5. import os
  6. import shutil
  7. import sys
  8. import tempfile
  9. from os.path import dirname, join
  10. from easydict import EasyDict
  11. def parse_config(filename):
  12. filename = str(filename)
  13. if filename.endswith('.py'):
  14. with tempfile.TemporaryDirectory() as temp_config_dir:
  15. shutil.copyfile(filename, join(temp_config_dir, '_tempconfig.py'))
  16. sys.path.insert(0, temp_config_dir)
  17. mod = importlib.import_module('_tempconfig')
  18. sys.path.pop(0)
  19. cfg_dict = EasyDict({
  20. name: value
  21. for name, value in mod.__dict__.items()
  22. if not name.startswith('__')
  23. })
  24. # delete imported module
  25. del sys.modules['_tempconfig']
  26. else:
  27. raise IOError('Only .py type are supported now!')
  28. return cfg_dict