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.

README.md 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # Load and Run
  2. Load a model and run, for testing/debugging/profiling.
  3. ## Build
  4. <!--
  5. -->
  6. ### Build with cmake
  7. Build MegEngine from source following [README.md](../../README.md). It will also produce the executable, `load_and_run`, which loads a model and runs the test cases attached to the model.
  8. <!--
  9. -->
  10. ## Dump Model with Test Cases Using [dump_with_testcase_mge.py](dump_with_testcase_mge.py)
  11. ### Step 1
  12. Dump the model by calling the python API `megengine.jit.trace.dump()`.
  13. ### Step 2
  14. Append the test cases to the dumped model using [dump_with_testcase_mge.py](dump_with_testcase_mge.py).
  15. The basic usage of [dump_with_testcase_mge.py](dump_with_testcase_mge.py) is
  16. ```
  17. python3 dump_with_testcase_mge.py model -d input_description -o model_with_testcases
  18. ```
  19. where `model` is the file dumped at step 1, `input_description` describes the input data of the test cases, and `model_with_testcases` is the saved model with test cases.
  20. `input_description` can be provided in the following ways:
  21. 1. In the format `var0:file0;var1:file1...` meaning that `var0` should use
  22. image file `file0`, `var1` should use image `file1` and so on. If there
  23. is only one input var, the var name can be omitted. This can be combined
  24. with `--resize-input` option.
  25. 2. In the format `var0:#rand(min, max, shape...);var1:#rand(min, max)...`
  26. meaning to fill the corresponding input vars with uniform random numbers
  27. in the range `[min, max)`, optionally overriding its shape.
  28. For more usages, run
  29. ```
  30. python3 dump_with_testcase_mge.py --help
  31. ```
  32. ### Example
  33. 1. Obtain the model file by running [xornet.py](../xor-deploy/xornet.py).
  34. 2. Dump the file with test cases attached to the model.
  35. ```
  36. python3 dump_with_testcase_mge.py xornet_deploy.mge -o xornet.mge -d "#rand(0.1, 0.8, 4, 2)"
  37. ```
  38. 3. Verify the correctness by running `load_and_run` at the target platform.
  39. ```
  40. load_and_run xornet.mge
  41. ```
  42. ## `load_and_run --input` the dumped mge file
  43. You can also use `--input` to set mge file's input, this argument support these 4 formats:
  44. 1. PPM/PGM image file.
  45. PPM/PGM is supported by OpenCV and simple to parse, you can easily use `cv::imwrite` to generate one.
  46. ```
  47. load_and_run model.mge --input "data:image.ppm"
  48. ```
  49. `data` is blob name and `image.ppm` is file path, we use `:` to seperate key and value. Please note that `"` is necessary in terminal.
  50. 2. npy file.
  51. npy is `Numpy` file format, here is a Python example
  52. ```
  53. import numpy as np
  54. import cv2
  55. mat = cv2.imread('file.jpg')
  56. np.save('image.npy', mat)
  57. arr = np.array([[[1.1, 1.2],[100, 200.0]]], dtype=np.float32)
  58. np.save('bbox.npy', arr)
  59. ```
  60. then `load_and_run` the model
  61. ```
  62. load_and_run model.mge --input data:image.npy;bbox.npy
  63. ```
  64. 3. json format.
  65. For json format, you have to identify data type and blob shape. Here is a Python example
  66. ```
  67. import numpy as np
  68. import json
  69. import cv2
  70. bbox = np.array([[[1.1, 1.2],[100, 200.0]]], dtype=np.float32)
  71. obj = dict()
  72. obj['shape'] = bbox.shape
  73. obj['raw'] = bbox.flatten().tolist()
  74. obj['type'] = str(bbox.dtype)
  75. json_object = dict()
  76. json_object['bbox'] = obj
  77. json_str = json.dumps(json_object)
  78. with open('bbox.json', 'w') as f:
  79. f.write(json_str)
  80. f.flush()
  81. f.close()
  82. ```
  83. The json loader in `load_and_run` is not fully implement [RFC7159](https://tools.ietf.org/html/rfc7159), it does not support `boolean` and `utf` string format which is useless during inference.
  84. Now let's `load-and-run` the model with json file
  85. ```
  86. load_and_run model.mge --input data:image.npy:bbox:bbox.json
  87. ```
  88. Mutiple key-value pair could be seperated with `;`.
  89. 4. plain string format.
  90. Also, you can give the value directly
  91. ```
  92. load_and_run model.mge --input data:image.ppm --input "bbox:[0,0],[200.0,200.0]" --input "batchid:0"
  93. ```
  94. 1. `bbox` shape is `[1,2,2]` for `[0,0],[200.0,200.0]`. In order to facilitate user experience, the string parser would add an extra axis for input, thus `bbox:0` is correspond to `[1]` and `bbox:[0]` means that the shape is `[1,1]`
  95. 2. Since we can only identify `int32` and `float32` from this format, don't forget `.` for float number.

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