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.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2021 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(url: str, filename: str, target_md5: str, raw_data_dir: str):
  23. cached_file = os.path.join(raw_data_dir, filename)
  24. logger.debug(
  25. "load_raw_data_from_url: downloading to or using cached %s ...", cached_file
  26. )
  27. if not os.path.exists(cached_file):
  28. if is_distributed():
  29. logger.warning(
  30. "Downloading raw data in DISTRIBUTED mode\n"
  31. " File may be downloaded multiple times. We recommend\n"
  32. " users to download in single process first."
  33. )
  34. md5 = download_from_url(url, cached_file)
  35. else:
  36. md5 = calculate_md5(cached_file)
  37. if target_md5 == md5:
  38. logger.debug("%s exists with correct md5: %s", filename, target_md5)
  39. else:
  40. os.remove(cached_file)
  41. raise RuntimeError("{} exists but fail to match md5".format(filename))
  42. def calculate_md5(filename):
  43. m = hashlib.md5()
  44. with open(filename, "rb") as f:
  45. while True:
  46. data = f.read(4096)
  47. if not data:
  48. break
  49. m.update(data)
  50. return m.hexdigest()
  51. def is_img(filename):
  52. return filename.lower().endswith(IMG_EXT)
  53. def untar(path, to=None, remove=False):
  54. if to is None:
  55. to = os.path.dirname(path)
  56. with tarfile.open(path, "r") as tar:
  57. tar.extractall(path=to)
  58. if remove:
  59. os.remove(path)
  60. def untargz(path, to=None, remove=False):
  61. if path.endswith(".tar.gz"):
  62. if to is None:
  63. to = os.path.dirname(path)
  64. with tarfile.open(path, "r:gz") as tar:
  65. tar.extractall(path=to)
  66. else:
  67. raise ValueError("path %s does not end with .tar" % path)
  68. if remove:
  69. os.remove(path)

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