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.

dump_model.py 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 argparse
  10. import numpy as np
  11. import yaml
  12. from megengine import jit
  13. from megengine.module.external import ExternOprSubgraph
  14. # "1,3,224,224" -> (1,3,224,224)
  15. def str2tuple(x):
  16. x = x.split(",")
  17. x = [int(a) for a in x]
  18. x = tuple(x)
  19. return x
  20. def main():
  21. parser = argparse.ArgumentParser(
  22. description="load a .pb model and convert to corresponding "
  23. "load-and-run model"
  24. )
  25. parser.add_argument("input", help="mace model file")
  26. parser.add_argument("param", help="mace param file")
  27. parser.add_argument(
  28. "output", help="converted model that can be fed to dump_with_testcase_mge.py"
  29. )
  30. parser.add_argument("config", help="config file with yaml format")
  31. args = parser.parse_args()
  32. with open(args.config, "r") as f:
  33. configs = yaml.load(f)
  34. for model_name in configs["models"]:
  35. # ignore several sub models currently
  36. sub_model = configs["models"][model_name]["subgraphs"][0]
  37. # input/output shapes
  38. isizes = [str2tuple(x) for x in sub_model["input_shapes"]]
  39. # input/output names
  40. input_names = sub_model["input_tensors"]
  41. if "check_tensors" in sub_model:
  42. output_names = sub_model["check_tensors"]
  43. osizes = [str2tuple(x) for x in sub_model["check_shapes"]]
  44. else:
  45. output_names = sub_model["output_tensors"]
  46. osizes = [str2tuple(x) for x in sub_model["output_shapes"]]
  47. with open(args.input, "rb") as fin:
  48. raw_model = fin.read()
  49. with open(args.param, "rb") as fin:
  50. raw_param = fin.read()
  51. model_size = (len(raw_model)).to_bytes(4, byteorder="little")
  52. param_size = (len(raw_param)).to_bytes(4, byteorder="little")
  53. n_inputs = (len(input_names)).to_bytes(4, byteorder="little")
  54. n_outputs = (len(output_names)).to_bytes(4, byteorder="little")
  55. names_buffer = n_inputs + n_outputs
  56. for iname in input_names:
  57. names_buffer += (len(iname)).to_bytes(4, byteorder="little")
  58. names_buffer += str.encode(iname)
  59. for oname in output_names:
  60. names_buffer += (len(oname)).to_bytes(4, byteorder="little")
  61. names_buffer += str.encode(oname)
  62. shapes_buffer = n_outputs
  63. for oshape in osizes:
  64. shapes_buffer += (len(oshape)).to_bytes(4, byteorder="little")
  65. for oi in oshape:
  66. shapes_buffer += oi.to_bytes(4, byteorder="little")
  67. # raw content contains:
  68. # input/output names + output shapes + model buffer + param buffer
  69. wk_raw_content = (
  70. names_buffer
  71. + shapes_buffer
  72. + model_size
  73. + raw_model
  74. + param_size
  75. + raw_param
  76. )
  77. net = ExternOprSubgraph(wk_raw_content, "mace", osizes)
  78. net.eval()
  79. @jit.trace(symbolic=True)
  80. def inference(inputs):
  81. return net(inputs)
  82. inputs = [
  83. np.random.random(isizes[i]).astype(np.float32) for i in range(len(isizes))
  84. ]
  85. inference.trace(*inputs)
  86. inference.dump(args.output)
  87. if __name__ == "__main__":
  88. main()

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