tflite 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. if [ -z "$(which cmake)" ]; then
  42. sudo apt install cmake
  43. fi
  44. ;;
  45. "Darwin")
  46. if [ -z "$(which cmake)" ]; then
  47. brew install cmake
  48. fi
  49. ;;
  50. esac
  51. pushd "${third_party}/flatbuffers" > /dev/null
  52. cmake -G "Unix Makefiles" .
  53. make
  54. popd > /dev/null
  55. if [ ! -d "${virtualenv}" ]; then
  56. virtualenv --quiet -p ${python} ${virtualenv}
  57. fi
  58. }
  59. schema() {
  60. bold "tflite schema"
  61. sed 's/namespace tflite;/namespace tflite_schema;/g' <${third_party}/tensorflow/tensorflow/lite/schema/schema.fbs >${tools}/tflite.schema.fbs
  62. ${third_party}/flatbuffers/flatc --no-js-exports --js ${tools}/tflite.schema.fbs
  63. rm ${tools}/tflite.schema.fbs
  64. rm ${src}/tflite-schema.js
  65. cat <<EOT >> ${src}/tflite-schema.js
  66. /* eslint 'indent': [ 'error', 2 ] */
  67. EOT
  68. cat ./tflite.schema_generated.js >> ${src}/tflite-schema.js
  69. cat <<EOT >> ${src}/tflite-schema.js
  70. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  71. module.exports = tflite_schema;
  72. }
  73. EOT
  74. rm ./tflite.schema_generated.js
  75. }
  76. visualize() {
  77. bold "tflite visualize"
  78. source ${virtualenv}/bin/activate
  79. ${python} -m pip install --quiet tensorflow
  80. ${python} ${third_party}/tensorflow/tensorflow/lite/tools/visualize.py $@
  81. deactivate
  82. }
  83. while [ "$#" != 0 ]; do
  84. command="$1" && shift
  85. case "${command}" in
  86. "clean") clean;;
  87. "sync") sync;;
  88. "install") install;;
  89. "schema") schema;;
  90. "visualize") visualize ${1} ${2} && shift && shift;;
  91. esac
  92. done