onnx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. if [ -z "$(which cmake)" ]; then
  45. sudo apt install -y cmake
  46. fi
  47. if [ -z "$(which protoc)" ]; then
  48. sudo apt install -y protobuf-compiler libprotoc-dev
  49. fi
  50. ;;
  51. "Darwin")
  52. if [ -z "$(which cmake)" ]; then
  53. brew install cmake
  54. fi
  55. if [ -z "$(which protoc)" ]; then
  56. brew install protobuf
  57. fi
  58. ;;
  59. esac
  60. if [ -z "$(which virtualenv)" ]; then
  61. sudo ${python} -m pip install --force-reinstall --quiet virtualenv
  62. fi
  63. if [ ! -d "${virtualenv}" ]; then
  64. virtualenv --quiet -p ${python} ${virtualenv}
  65. fi
  66. source ${virtualenv}/bin/activate
  67. ${python} -m pip install --quiet protobuf
  68. export ONNX_ML=1
  69. export ONNX_NAMESPACE=onnx
  70. ${python} -m pip install --quiet ${third_party}/onnx
  71. deactivate
  72. }
  73. schema() {
  74. bold "onnx schema"
  75. source ${virtualenv}/bin/activate
  76. ${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
  77. node ${tools}/update_pbjs.js array ${src}/onnx-proto.js float_data float 1
  78. node ${tools}/update_pbjs.js array ${src}/onnx-proto.js double_data double 1
  79. deactivate
  80. }
  81. metadata() {
  82. bold "onnx metadata"
  83. source ${virtualenv}/bin/activate
  84. pushd ${tools} > /dev/null
  85. ${python} onnx-script.py metadata
  86. popd > /dev/null
  87. deactivate
  88. }
  89. convert() {
  90. bold "onnx convert"
  91. source ${virtualenv}/bin/activate
  92. ${python} -m pip install --quiet ${third_party}/onnxmltools
  93. ${python} ${tools}/onnx-script.py convert ${1}
  94. deactivate
  95. }
  96. infer() {
  97. bold "onnx infer"
  98. source ${virtualenv}/bin/activate
  99. ${python} ${tools}/onnx-script.py infer ${1}
  100. deactivate
  101. }
  102. optimize() {
  103. bold "onnx optimize"
  104. source ${virtualenv}/bin/activate
  105. ${python} ${tools}/onnx-script.py optimize ${1}
  106. deactivate
  107. }
  108. while [ "$#" != 0 ]; do
  109. command="$1" && shift
  110. case "${command}" in
  111. "clean") clean;;
  112. "sync") sync;;
  113. "install") install;;
  114. "schema") schema;;
  115. "metadata") metadata;;
  116. "convert") convert ${1} && shift;;
  117. "infer") infer ${1} && shift;;
  118. "optimize") optimize ${1} && shift;;
  119. esac
  120. done