onnx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/bin/bash
  2. set -e
  3. pushd $(cd $(dirname ${0})/..; pwd) > /dev/null
  4. identifier=onnx
  5. case "${OSTYPE}" in
  6. msys*) python="winpty python";;
  7. *) python=python3;;
  8. esac
  9. bold() {
  10. echo "$(tty -s && tput bold)$1$(tty -s && tput sgr0)"
  11. }
  12. venv() {
  13. venv_dir=./third_party/env/onnx
  14. [ -d "${venv_dir}" ] || ${python} -m venv ${venv_dir}
  15. case "${OSTYPE}" in
  16. msys*) source ${venv_dir}/Scripts/activate;;
  17. *) source ${venv_dir}/bin/activate;;
  18. esac
  19. ${python} -m pip install --quiet --upgrade pip
  20. }
  21. git_sync() {
  22. [ -d "./third_party/src/${1}" ] || git clone --quiet --recursive ${2} "./third_party/src/${1}"
  23. pushd "./third_party/src/${1}" > /dev/null
  24. git pull --quiet --prune
  25. git submodule sync --quiet
  26. git submodule update --quiet --init --recursive
  27. popd > /dev/null
  28. }
  29. clean() {
  30. bold "onnx clean"
  31. rm -rf "./third_party/env/onnx"
  32. rm -rf "./third_party/src/onnx"
  33. rm -rf "./third_party/src/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 "${OSTYPE}" 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. protobuf=protobuf
  47. ;;
  48. darwin*)
  49. brew list cmake > /dev/null 2>&1 || brew install cmake > /dev/null
  50. brew list [email protected] > /dev/null 2>&1 || brew install [email protected] > /dev/null
  51. export PATH=/usr/local/opt/[email protected]/bin:${PATH}
  52. protobuf="protobuf==3.7.1"
  53. ;;
  54. msys*)
  55. [ -n "$(which protoc)" ] || choco install --yes protoc --version=3.8.0
  56. protobuf="protobuf==3.8.0"
  57. ;;
  58. esac
  59. venv
  60. ${python} -m pip install --quiet --upgrade ${protobuf}
  61. export ONNX_ML=1
  62. export ONNX_NAMESPACE=onnx
  63. ${python} -m pip install --quiet "./third_party/src/onnx"
  64. deactivate
  65. }
  66. schema() {
  67. bold "onnx schema"
  68. [[ $(grep -U $'\x0D' ./src/onnx-proto.js) ]] && crlf=1
  69. npx pbjs -t static-module -w closure --no-encode --no-delimited --no-comments --no-convert --no-verify --no-create --keep-case --decode-text -r onnx -o ./src/onnx-proto.js ./third_party/src/onnx/onnx/onnx-ml.proto ./third_party/src/onnx/onnx/onnx-operators-ml.proto
  70. node ./tools/update_pbjs.js array ./src/onnx-proto.js float_data float 1
  71. node ./tools/update_pbjs.js array ./src/onnx-proto.js double_data double 1
  72. if [[ -n ${crlf} ]]; then
  73. unix2dos --quiet --newfile ./src/onnx-proto.js ./src/onnx-proto.js
  74. fi
  75. }
  76. metadata() {
  77. bold "onnx metadata"
  78. [[ $(grep -U $'\x0D' ./src/onnx-metadata.json) ]] && crlf=1
  79. venv
  80. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  81. ${python} ./tools/onnx-script.py metadata
  82. deactivate
  83. if [[ -n ${crlf} ]]; then
  84. unix2dos --quiet --newfile ./src/onnx-metadata.json ./src/onnx-metadata.json
  85. fi
  86. }
  87. convert() {
  88. bold "onnx convert"
  89. venv
  90. ${python} -m pip install --quiet ./third_party/src/onnxmltools
  91. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  92. ${python} ./tools/onnx-script.py convert ${1}
  93. deactivate
  94. }
  95. infer() {
  96. bold "onnx infer"
  97. venv
  98. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  99. ${python} ./tools/onnx-script.py infer ${1}
  100. deactivate
  101. }
  102. optimize() {
  103. bold "onnx optimize"
  104. venv
  105. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  106. ${python} ./tools/onnx-script.py optimize ${1}
  107. deactivate
  108. }
  109. while [ "$#" != 0 ]; do
  110. command="$1" && shift
  111. case "${command}" in
  112. "clean") clean;;
  113. "sync") sync;;
  114. "install") install;;
  115. "schema") schema;;
  116. "metadata") metadata;;
  117. "convert") convert ${1} && shift;;
  118. "infer") infer ${1} && shift;;
  119. "optimize") optimize ${1} && shift;;
  120. esac
  121. done