tf 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/bin/bash
  2. set -e
  3. pushd $(cd $(dirname ${0})/..; pwd) > /dev/null
  4. case "${OSTYPE}" in
  5. msys*) python="winpty python";;
  6. *) python="python";;
  7. esac
  8. venv() {
  9. env_dir=./third_party/env/tensorflow
  10. [ -d "${env_dir}" ] || ${python} -m venv ${env_dir}
  11. case "${OSTYPE}" in
  12. msys*) source ${env_dir}/Scripts/activate;;
  13. *) source ${env_dir}/bin/activate;;
  14. esac
  15. ${python} -m pip install --quiet --upgrade pip
  16. }
  17. clean() {
  18. echo "tf clean"
  19. rm -rf "./third_party/env/tensorflow"
  20. rm -rf "./third_party/source/tensorflow"
  21. rm -rf "./third_party/source/tflite-support"
  22. }
  23. sync() {
  24. echo "tf sync"
  25. [ -d "./third_party/source/tensorflow" ] || git clone --quiet --recursive https://github.com/tensorflow/tensorflow.git "./third_party/source/tensorflow"
  26. pushd "./third_party/source/tensorflow" > /dev/null
  27. git pull --quiet --prune
  28. git submodule sync --quiet
  29. git submodule update --quiet --init --recursive
  30. popd > /dev/null
  31. [ -d "./third_party/source/tflite-support" ] || git clone --quiet --recursive https://github.com/tensorflow/tflite-support.git "./third_party/source/tflite-support"
  32. pushd "./third_party/source/tflite-support" > /dev/null
  33. git pull --quiet --prune
  34. popd > /dev/null
  35. }
  36. install() {
  37. echo "tf install"
  38. if [ "$(uname -sm)" = "Darwin arm64" ]; then
  39. echo "- arm64 [skip]"
  40. return
  41. fi
  42. venv
  43. ${python} -m pip install --quiet --upgrade wheel
  44. ${python} -m pip install --quiet --upgrade tf-nightly
  45. deactivate
  46. }
  47. schema() {
  48. echo "tf schema"
  49. [[ $(grep -U $'\x0D' ./source/tf-proto.js) ]] && crlf=1
  50. node ./tools/protoc.js --text --root tf --out ./source/tf-proto.js --path ./third_party/source/tensorflow tensorflow/core/protobuf/saved_model.proto tensorflow/core/protobuf/tensor_bundle.proto tensorflow/core/util/saved_tensor_slice.proto tensorflow/core/util/event.proto tensorflow/python/keras/protobuf/saved_metadata.proto
  51. if [[ -n ${crlf} ]]; then
  52. unix2dos --quiet --newfile ./source/tf-proto.js ./source/tf-proto.js
  53. fi
  54. echo "tflite schema"
  55. [[ $(grep -U $'\x0D' ./source/tflite-schema.js) ]] && crlf=1
  56. temp1=$(mktemp -d)
  57. temp2=$(mktemp -d)
  58. node ./tools/flatc.js --text --root tflite --out ./source/tflite-schema.js ./third_party/source/tensorflow/tensorflow/lite/schema/schema.fbs
  59. node ./tools/flatc.js --root tflite --out ${temp1}/tflite-metadata-schema.js ./third_party/source/tflite-support/tensorflow_lite_support/metadata/metadata_schema.fbs
  60. sed "s/var \$root = flatbuffers.get('tflite');//g" < ${temp1}/tflite-metadata-schema.js >> ${temp2}/tflite-metadata-schema.js
  61. cat ${temp2}/tflite-metadata-schema.js >> ./source/tflite-schema.js
  62. rm -rf ${temp1} ${temp2}
  63. if [[ -n ${crlf} ]]; then
  64. unix2dos --quiet --newfile ./source/tflite-schema.js ./source/tflite-schema.js
  65. fi
  66. }
  67. metadata() {
  68. echo "tflite metadata"
  69. if [[ $(grep -U $'\x0D' ./source/tflite-metadata.json) ]]; then crlf=1; else crlf=; fi
  70. node ./tools/tf-script.js
  71. if [[ -n ${crlf} ]]; then
  72. unix2dos --quiet --newfile ./source/tflite-metadata.json ./source/tflite-metadata.json
  73. fi
  74. echo "tf metadata"
  75. if [ "$(uname -sm)" = "Darwin arm64" ]; then
  76. echo "- arm64 [skip]"
  77. return
  78. fi
  79. venv
  80. if [[ $(grep -U $'\x0D' ./source/tf-metadata.json) ]]; then crlf=1; else crlf=; fi
  81. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  82. ${python} ./tools/tf-script.py metadata
  83. if [[ -n ${crlf} ]]; then
  84. unix2dos --quiet --newfile ./source/tf-metadata.json ./source/tf-metadata.json
  85. fi
  86. deactivate
  87. echo "keras metadata"
  88. venv
  89. if [[ $(grep -U $'\x0D' ./source/keras-metadata.json) ]]; then crlf=1; else crlf=; fi
  90. ${python} ./tools/keras-script.py metadata
  91. if [[ -n ${crlf} ]]; then
  92. unix2dos --quiet --newfile ./source/keras-metadata.json ./source/keras-metadata.json
  93. fi
  94. deactivate
  95. }
  96. while [ "$#" != 0 ]; do
  97. command="$1" && shift
  98. case "${command}" in
  99. "clean") clean;;
  100. "sync") sync;;
  101. "install") install;;
  102. "schema") schema;;
  103. "metadata") metadata;;
  104. esac
  105. done