2
0

tf 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 https://github.com/tensorflow/tensorflow.git "./third_party/source/tensorflow"
  26. git -C "./third_party/source/tensorflow" pull --quiet --prune
  27. mkdir -p "./third_party/source/tflite-support/tensorflow_lite_support/metadata"
  28. curl --silent --location --output "./third_party/source/tflite-support/tensorflow_lite_support/metadata/metadata_schema.fbs" "https://github.com/tensorflow/tflite-support/blob/master/tensorflow_lite_support/metadata/metadata_schema.fbs?raw=true"
  29. }
  30. install() {
  31. echo "tf install"
  32. if [ "$(uname -sm)" = "Darwin arm64" ]; then
  33. echo "- arm64 [skip]"
  34. return
  35. fi
  36. venv
  37. ${python} -m pip install --quiet --upgrade wheel
  38. ${python} -m pip install --quiet --upgrade tf-nightly
  39. deactivate
  40. }
  41. schema() {
  42. echo "tf schema"
  43. [[ $(grep -U $'\x0D' ./source/tf-proto.js) ]] && crlf=1
  44. 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/core/protobuf/config.proto tensorflow/python/keras/protobuf/saved_metadata.proto tensorflow/core/util/memmapped_file_system.proto
  45. if [[ -n ${crlf} ]]; then
  46. unix2dos --quiet --newfile ./source/tf-proto.js ./source/tf-proto.js
  47. fi
  48. echo "tflite schema"
  49. [[ $(grep -U $'\x0D' ./source/tflite-schema.js) ]] && crlf=1
  50. temp1=$(mktemp -d)
  51. temp2=$(mktemp -d)
  52. node ./tools/flatc.js --text --root tflite --out ./source/tflite-schema.js ./third_party/source/tensorflow/tensorflow/lite/schema/schema.fbs
  53. node ./tools/flatc.js --root tflite --out ${temp1}/tflite-metadata-schema.js ./third_party/source/tflite-support/tensorflow_lite_support/metadata/metadata_schema.fbs
  54. sed "s/var \$root = flatbuffers.get('tflite');//g" < ${temp1}/tflite-metadata-schema.js >> ${temp2}/tflite-metadata-schema.js
  55. cat ${temp2}/tflite-metadata-schema.js >> ./source/tflite-schema.js
  56. rm -rf ${temp1} ${temp2}
  57. if [[ -n ${crlf} ]]; then
  58. unix2dos --quiet --newfile ./source/tflite-schema.js ./source/tflite-schema.js
  59. fi
  60. }
  61. metadata() {
  62. echo "tflite metadata"
  63. export TF_CPP_MIN_LOG_LEVEL=2
  64. if [[ $(grep -U $'\x0D' ./source/tflite-metadata.json) ]]; then crlf=1; else crlf=; fi
  65. node ./tools/tflite_script.js
  66. if [[ -n ${crlf} ]]; then
  67. unix2dos --quiet --newfile ./source/tflite-metadata.json ./source/tflite-metadata.json
  68. fi
  69. echo "tf metadata"
  70. if [ "$(uname -sm)" = "Darwin arm64" ]; then
  71. echo "- arm64 [skip]"
  72. return
  73. fi
  74. venv
  75. if [[ $(grep -U $'\x0D' ./source/tf-metadata.json) ]]; then crlf=1; else crlf=; fi
  76. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  77. ${python} ./tools/tf_script.py metadata
  78. if [[ -n ${crlf} ]]; then
  79. unix2dos --quiet --newfile ./source/tf-metadata.json ./source/tf-metadata.json
  80. fi
  81. deactivate
  82. echo "keras metadata"
  83. venv
  84. if [[ $(grep -U $'\x0D' ./source/keras-metadata.json) ]]; then crlf=1; else crlf=; fi
  85. ${python} ./tools/keras_script.py metadata
  86. if [[ -n ${crlf} ]]; then
  87. unix2dos --quiet --newfile ./source/keras-metadata.json ./source/keras-metadata.json
  88. fi
  89. deactivate
  90. }
  91. while [ "$#" != 0 ]; do
  92. command="$1" && shift
  93. case "${command}" in
  94. "clean") clean;;
  95. "sync") sync;;
  96. "install") install;;
  97. "schema") schema;;
  98. "metadata") metadata;;
  99. esac
  100. done