tflite 2.5 KB

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