1#!/bin/bash 2# Copyright 2020 The TensorFlow Authors. All Rights Reserved. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# ============================================================================== 16# Pip install TensorFlow Lite Support and run basic test on the pip package. 17 18# Important: Use msys shell to run this script on Windows. 19 20set -e 21set -x 22 23function run_smoke_test() { 24 VENV_TMP_DIR="$(mktemp -d)" 25 26 if [[ "$OSTYPE" == "msys" ]]; then 27 VENV_TMP_DIR="$(cygpath -m $VENV_TMP_DIR)" 28 fi 29 30 ${PYTHON_BIN_PATH} -m virtualenv -p ${PYTHON_BIN_PATH} "${VENV_TMP_DIR}" || \ 31 die "FAILED: Unable to create virtualenv" 32 33 if [[ "$OSTYPE" == "msys" ]]; then 34 source "${VENV_TMP_DIR}/Scripts/activate" || \ 35 die "FAILED: Unable to activate virtualenv " 36 else 37 source "${VENV_TMP_DIR}/bin/activate" || \ 38 die "FAILED: Unable to activate virtualenv " 39 fi 40 41 # install tflite-support 42 python -m pip install ${WHL_NAME} || \ 43 die "pip install (forcing to reinstall tflite-support) FAILED" 44 echo "Successfully installed pip package ${WHL_NAME}" 45 46 # Download a test model 47 export TEST_MODEL="$(pwd)/test.tflite" 48 wget https://tfhub.dev/tensorflow/lite-model/mobilenet_v1_0.75_192_quantized/1/metadata/1\?lite-format\=tflite -O "$TEST_MODEL" 49 if [[ "$OSTYPE" == "msys" ]]; then 50 TEST_MODEL=$(cygpath -m $TEST_MODEL) 51 fi 52 53 test_tfls_imports 54 55 test_codegen 56 57 # Deactivate from virtualenv. 58 deactivate || source deactivate || \ 59 die "FAILED: Unable to deactivate from existing virtualenv." 60 61 echo "All smoke test passes!" 62} 63 64function test_tfls_imports() { 65 TMP_DIR=$(mktemp -d) 66 pushd "${TMP_DIR}" 67 68 # test for basic import and metadata display. 69 RET_VAL=$(python -c "from tflite_support import metadata; \ 70md = metadata.MetadataDisplayer.with_model_file(\"$TEST_MODEL\"); \ 71print(md.get_metadata_json())") 72 73 # just check if the model name is there. 74 if ! [[ ${RET_VAL} == *"MobileNetV1 image classifier (quantized)"* ]]; then 75 echo "Unexpected return value: ${RET_VAL}" 76 echo "PIP smoke test on virtualenv FAILED, do not upload ${WHL_NAME}." 77 return 1 78 fi 79 80 RESULT=$? 81 82 popd 83 return $RESULT 84} 85 86function test_codegen() { 87 TMP_DIR=$(mktemp -d) 88 pushd "${TMP_DIR}" 89 90 # test for basic import and metadata display. 91 tflite_codegen --model ${TEST_MODEL} --destination tmp 92 RESULT=$? 93 94 # just check if the model name is there. 95 if [[ ${RESULT} -ne 0 ]]; then 96 echo "Unexpected return value: ${RESULT}" 97 echo "PIP smoke test on virtualenv FAILED, do not upload ${WHL_NAME}." 98 return 1 99 fi 100 101 popd 102 return $RESULT 103} 104 105########################################################################### 106# Main 107########################################################################### 108if [[ -z "${1}" ]]; then 109 echo "TFLite Support WHL path not given, unable to install and test." 110 return 1 111fi 112 113WHL_NAME=${1} 114run_smoke_test 115