onnx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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) ] && [ $(which pip3) ]; then
  12. python="python3"
  13. pip="pip3"
  14. else
  15. python="python"
  16. pip="pip"
  17. fi
  18. bold() {
  19. echo "$(tty -s && tput bold)$1$(tty -s && tput sgr0)"
  20. }
  21. git_sync() {
  22. mkdir -p "${third_party}"
  23. if [ -d "${third_party}/${1}" ]; then
  24. git -C "${third_party}/${1}" fetch -p --quiet
  25. git -C "${third_party}/${1}" reset --quiet --hard origin/master
  26. else
  27. echo "Clone ${2}..."
  28. git -C "${third_party}" clone --recursive ${2}
  29. fi
  30. git -C "${third_party}" submodule update --init
  31. }
  32. clean() {
  33. bold "onnx clean"
  34. rm -rf ${virtualenv}
  35. rm -rf ${third_party}/${identifier}
  36. rm -rf ${third_party}/onnxmltools
  37. }
  38. sync() {
  39. bold "onnx sync"
  40. git_sync onnx https://github.com/onnx/onnx.git
  41. git_sync onnxmltools https://github.com/onnx/onnxmltools.git
  42. }
  43. install() {
  44. bold "onnx install"
  45. case "$(uname)" in
  46. "Linux")
  47. if [ -z "$(which cmake)" ]; then
  48. sudo apt install cmake
  49. fi
  50. if [ -z "$(which protoc)" ]; then
  51. sudo apt install protobuf-compiler libprotoc-dev
  52. fi
  53. ;;
  54. "Darwin")
  55. if [ -z "$(which cmake)" ]; then
  56. brew install cmake
  57. fi
  58. if [ -z "$(which protoc)" ]; then
  59. brew install protobuf
  60. fi
  61. ;;
  62. esac
  63. virtualenv --quiet -p ${python} ${virtualenv}
  64. source ${virtualenv}/bin/activate
  65. ${pip} install --quiet protobuf
  66. export ONNX_ML=1
  67. export ONNX_NAMESPACE=onnx
  68. ${pip} install --quiet ${third_party}/onnx
  69. deactivate
  70. }
  71. schema() {
  72. bold "onnx schema"
  73. source ${virtualenv}/bin/activate
  74. ${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
  75. node ${tools}/update_pbjs.js array ${src}/onnx-proto.js float_data float 1
  76. node ${tools}/update_pbjs.js array ${src}/onnx-proto.js double_data double 1
  77. deactivate
  78. }
  79. metadata() {
  80. bold "onnx metadata"
  81. source ${virtualenv}/bin/activate
  82. pushd ${tools} > /dev/null
  83. ${python} onnx-script.py metadata
  84. popd > /dev/null
  85. deactivate
  86. }
  87. convert() {
  88. bold "onnx convert"
  89. source ${virtualenv}/bin/activate
  90. ${pip} install --quiet ${third_party}/onnxmltools
  91. ${python} ${tools}/onnx-script.py convert ${1}
  92. deactivate
  93. }
  94. infer() {
  95. bold "onnx infer"
  96. source ${virtualenv}/bin/activate
  97. ${python} ${tools}/onnx-script.py infer ${1}
  98. deactivate
  99. }
  100. optimize() {
  101. bold "onnx optimize"
  102. source ${virtualenv}/bin/activate
  103. ${python} ${tools}/onnx-script.py optimize ${1}
  104. deactivate
  105. }
  106. while [ "$#" != 0 ]; do
  107. command="$1" && shift
  108. case "${command}" in
  109. "clean") clean;;
  110. "sync") sync;;
  111. "install") install;;
  112. "schema") schema;;
  113. "metadata") metadata;;
  114. "convert") convert ${1} && shift;;
  115. "infer") infer ${1} && shift;;
  116. "optimize") optimize ${1} && shift;;
  117. esac
  118. done