You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 2.7 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. import hashlib
  10. import os
  11. import tarfile
  12. from ....distributed.group import is_distributed
  13. from ....logger import get_logger
  14. from ....utils.http_download import download_from_url
  15. IMG_EXT = (".jpg", ".png", ".jpeg", ".ppm", ".bmp", ".pgm", ".tif", ".tiff", ".webp")
  16. logger = get_logger(__name__)
  17. def _default_dataset_root():
  18. default_dataset_root = os.path.expanduser(
  19. os.path.join(os.getenv("XDG_CACHE_HOME", "~/.cache"), "megengine")
  20. )
  21. return default_dataset_root
  22. def load_raw_data_from_url(
  23. url: str, filename: str, target_md5: str, raw_data_dir: str, timeout: int
  24. ):
  25. cached_file = os.path.join(raw_data_dir, filename)
  26. logger.debug(
  27. "load_raw_data_from_url: downloading to or using cached %s ...", cached_file
  28. )
  29. if not os.path.exists(cached_file):
  30. if is_distributed():
  31. logger.warning(
  32. "Downloading raw data in DISTRIBUTED mode\n"
  33. " File may be downloaded multiple times. We recommend\n"
  34. " users to download in single process first."
  35. )
  36. md5 = download_from_url(url, cached_file, http_read_timeout=timeout)
  37. else:
  38. md5 = calculate_md5(cached_file)
  39. if target_md5 == md5:
  40. logger.debug("%s exists with correct md5: %s", filename, target_md5)
  41. else:
  42. os.remove(cached_file)
  43. raise RuntimeError("{} exists but fail to match md5".format(filename))
  44. def calculate_md5(filename):
  45. m = hashlib.md5()
  46. with open(filename, "rb") as f:
  47. while True:
  48. data = f.read(4096)
  49. if not data:
  50. break
  51. m.update(data)
  52. return m.hexdigest()
  53. def is_img(filename):
  54. return filename.lower().endswith(IMG_EXT)
  55. def untar(path, to=None, remove=False):
  56. if to is None:
  57. to = os.path.dirname(path)
  58. with tarfile.open(path, "r") as tar:
  59. tar.extractall(path=to)
  60. if remove:
  61. os.remove(path)
  62. def untargz(path, to=None, remove=False):
  63. if path.endswith(".tar.gz"):
  64. if to is None:
  65. to = os.path.dirname(path)
  66. with tarfile.open(path, "r:gz") as tar:
  67. tar.extractall(path=to)
  68. else:
  69. raise ValueError("path %s does not end with .tar" % path)
  70. if remove:
  71. os.remove(path)

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台