onnx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/bin/bash
  2. set -e
  3. root=$(cd $(dirname ${0})/..; pwd)
  4. build=${root}/build
  5. node_modules=${root}/node_modules
  6. src=${root}/src
  7. third_party=${root}/third_party
  8. tools=${root}/tools
  9. identifier=onnx
  10. virtualenv=${build}/virtualenv/${identifier}
  11. if [ $(which python3) ]; then
  12. python="python3"
  13. else
  14. python="python"
  15. fi
  16. bold() {
  17. echo "$(tty -s && tput bold)$1$(tty -s && tput sgr0)"
  18. }
  19. git_sync() {
  20. mkdir -p "${third_party}"
  21. if [ -d "${third_party}/${1}" ]; then
  22. git -C "${third_party}/${1}" fetch --quiet -p
  23. git -C "${third_party}/${1}" reset --quiet --hard origin/master
  24. else
  25. git -C "${third_party}" clone --quiet --recursive ${2}
  26. fi
  27. git -C "${third_party}" submodule update --quiet --init
  28. }
  29. clean() {
  30. bold "onnx clean"
  31. rm -rf ${virtualenv}
  32. rm -rf ${third_party}/${identifier}
  33. rm -rf ${third_party}/onnxmltools
  34. }
  35. sync() {
  36. bold "onnx sync"
  37. git_sync onnx https://github.com/onnx/onnx.git
  38. git_sync onnxmltools https://github.com/onnx/onnxmltools.git
  39. }
  40. install() {
  41. bold "onnx install"
  42. case "$(uname)" in
  43. "Linux")
  44. [ -n "$(which cmake)" ] || sudo apt install -y cmake
  45. [ -n "$(which protoc)" ] || sudo apt install -y protobuf-compiler libprotoc-dev
  46. ;;
  47. "Darwin")
  48. brew list cmake > /dev/null 2>&1 || brew install cmake > /dev/null
  49. brew list protobuf > /dev/null 2>&1 || brew install protobuf > /dev/null
  50. ;;
  51. esac
  52. [ -n "$(python3 -m pip list --disable-pip-version-check | grep -w virtualenv)" ] || ${python} -m pip install --force-reinstall --user --quiet virtualenv
  53. [ -d "${virtualenv}" ] || virtualenv --quiet -p ${python} ${virtualenv}
  54. source ${virtualenv}/bin/activate
  55. ${python} -m pip install --quiet protobuf
  56. export ONNX_ML=1
  57. export ONNX_NAMESPACE=onnx
  58. ${python} -m pip install --quiet ${third_party}/onnx
  59. deactivate
  60. }
  61. schema() {
  62. bold "onnx schema"
  63. source ${virtualenv}/bin/activate
  64. ${node_modules}/protobufjs/bin/pbjs -t static-module -w closure --no-encode --no-delimited --no-comments --keep-case --decode-text -r onnx -o ${src}/onnx-proto.js ${third_party}/${identifier}/onnx/onnx-ml.proto ${third_party}/${identifier}/onnx/onnx-operators-ml.proto
  65. node ${tools}/update_pbjs.js array ${src}/onnx-proto.js float_data float 1
  66. node ${tools}/update_pbjs.js array ${src}/onnx-proto.js double_data double 1
  67. deactivate
  68. }
  69. metadata() {
  70. bold "onnx metadata"
  71. source ${virtualenv}/bin/activate
  72. pushd ${tools} > /dev/null
  73. ${python} onnx-script.py metadata
  74. popd > /dev/null
  75. deactivate
  76. }
  77. convert() {
  78. bold "onnx convert"
  79. source ${virtualenv}/bin/activate
  80. ${python} -m pip install --quiet ${third_party}/onnxmltools
  81. ${python} ${tools}/onnx-script.py convert ${1}
  82. deactivate
  83. }
  84. infer() {
  85. bold "onnx infer"
  86. source ${virtualenv}/bin/activate
  87. ${python} ${tools}/onnx-script.py infer ${1}
  88. deactivate
  89. }
  90. optimize() {
  91. bold "onnx optimize"
  92. source ${virtualenv}/bin/activate
  93. ${python} ${tools}/onnx-script.py optimize ${1}
  94. deactivate
  95. }
  96. while [ "$#" != 0 ]; do
  97. command="$1" && shift
  98. case "${command}" in
  99. "clean") clean;;
  100. "sync") sync;;
  101. "install") install;;
  102. "schema") schema;;
  103. "metadata") metadata;;
  104. "convert") convert ${1} && shift;;
  105. "infer") infer ${1} && shift;;
  106. "optimize") optimize ${1} && shift;;
  107. esac
  108. done