onnx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 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. virtualenv --quiet -p ${python} ${virtualenv}
  46. source ${virtualenv}/bin/activate
  47. export ONNX_ML=1
  48. export ONNX_NAMESPACE=onnx
  49. ${pip} install --quiet ${third_party}/onnx
  50. deactivate
  51. }
  52. schema() {
  53. bold "onnx schema"
  54. source ${virtualenv}/bin/activate
  55. ${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
  56. node ${tools}/update_pbjs.js array ${src}/onnx-proto.js float_data float 1
  57. node ${tools}/update_pbjs.js array ${src}/onnx-proto.js double_data double 1
  58. deactivate
  59. }
  60. metadata() {
  61. bold "onnx metadata"
  62. source ${virtualenv}/bin/activate
  63. pushd ${tools} > /dev/null
  64. ${python} onnx-script.py metadata
  65. popd > /dev/null
  66. deactivate
  67. }
  68. convert() {
  69. bold "onnx convert"
  70. source ${virtualenv}/bin/activate
  71. ${pip} install --quiet sklearn
  72. ${pip} install --quiet lightgbm
  73. ${pip} install --quiet ${third_party}/onnxmltools
  74. ${python} ${tools}/onnx-script.py convert ${1}
  75. deactivate
  76. }
  77. infer() {
  78. bold "onnx infer"
  79. source ${virtualenv}/bin/activate
  80. ${python} ${tools}/onnx-script.py infer ${1}
  81. deactivate
  82. }
  83. optimize() {
  84. bold "onnx optimize"
  85. source ${virtualenv}/bin/activate
  86. ${python} ${tools}/onnx-script.py optimize ${1}
  87. deactivate
  88. }
  89. while [ "$#" != 0 ]; do
  90. command="$1" && shift
  91. case "${command}" in
  92. "clean") clean;;
  93. "sync") sync;;
  94. "install") install;;
  95. "schema") schema;;
  96. "metadata") metadata;;
  97. "convert") convert ${1} && shift;;
  98. "infer") infer ${1} && shift;;
  99. "optimize") optimize ${1} && shift;;
  100. esac
  101. done