tflite 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/bin/bash
  2. set -e
  3. root=$(cd $(dirname ${0})/..; pwd)
  4. build=${root}/build
  5. src=${root}/src
  6. tools=${root}/tools
  7. third_party=${root}/third_party
  8. identifier=tflite
  9. virtualenv=${build}/virtualenv/${identifier}
  10. if [ $(which python3) ]; then
  11. python="python3"
  12. else
  13. python="python"
  14. fi
  15. bold() {
  16. echo "$(tty -s && tput bold)$1$(tty -s && tput sgr0)"
  17. }
  18. git_sync () {
  19. mkdir -p "${third_party}"
  20. if [ -d "${third_party}/${1}" ]; then
  21. git -C "${third_party}/${1}" fetch --quiet -q
  22. git -C "${third_party}/${1}" reset --quiet --hard origin/master
  23. else
  24. git -C "${third_party}" clone --quiet --recursive ${2}
  25. fi
  26. }
  27. clean() {
  28. bold "tflite clean"
  29. rm -rf ${virtualenv}
  30. rm -rf ${third_party}/tensorflow
  31. }
  32. sync() {
  33. bold "tflite sync"
  34. git_sync flatbuffers https://github.com/google/flatbuffers.git
  35. git_sync tensorflow https://github.com/tensorflow/tensorflow.git
  36. }
  37. install() {
  38. bold "flatbuffers install"
  39. case "$(uname)" in
  40. "Linux")
  41. [ -n "$(which cmake)" ] || sudo apt install -y cmake
  42. ;;
  43. "Darwin")
  44. brew list cmake > /dev/null 2>&1 || brew install cmake > /dev/null
  45. ;;
  46. esac
  47. pushd "${third_party}/flatbuffers" > /dev/null
  48. cmake -G "Unix Makefiles" .
  49. make
  50. popd > /dev/null
  51. [ -n "$(python3 -m pip list --format columns --disable-pip-version-check | grep -w virtualenv)" ] || ${python} -m pip install --force-reinstall --user --quiet virtualenv
  52. [ -d "${virtualenv}" ] || virtualenv --quiet -p ${python} ${virtualenv}
  53. }
  54. schema() {
  55. bold "tflite schema"
  56. sed 's/namespace tflite;/namespace tflite_schema;/g' <${third_party}/tensorflow/tensorflow/lite/schema/schema.fbs >${tools}/tflite.schema.fbs
  57. ${third_party}/flatbuffers/flatc --no-js-exports --js ${tools}/tflite.schema.fbs
  58. rm ${tools}/tflite.schema.fbs
  59. rm ${src}/tflite-schema.js
  60. cat <<EOT >> ${src}/tflite-schema.js
  61. /* eslint 'indent': [ 'error', 2 ] */
  62. EOT
  63. cat ./tflite.schema_generated.js >> ${src}/tflite-schema.js
  64. cat <<EOT >> ${src}/tflite-schema.js
  65. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  66. module.exports = tflite_schema;
  67. }
  68. EOT
  69. rm ./tflite.schema_generated.js
  70. }
  71. visualize() {
  72. bold "tflite visualize"
  73. source ${virtualenv}/bin/activate
  74. ${python} -m pip install --quiet tensorflow
  75. ${python} ${third_party}/tensorflow/tensorflow/lite/tools/visualize.py $@
  76. deactivate
  77. }
  78. while [ "$#" != 0 ]; do
  79. command="$1" && shift
  80. case "${command}" in
  81. "clean") clean;;
  82. "sync") sync;;
  83. "install") install;;
  84. "schema") schema;;
  85. "visualize") visualize ${1} ${2} && shift && shift;;
  86. esac
  87. done