3 # Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
19 DRIVER_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
23 echo "Convert TensorFlow model to circle."
24 echo "Usage: one-import-tf"
25 echo " --version Show version information and exit"
26 echo " --input_path <path/to/tfmodel>"
27 echo " --output_path <path/to/circle>"
28 echo " --input_arrays <names of the input arrays, comma-separated>"
29 echo " --input_shapes <input shapes, colon-separated>"
30 echo " --output_arrays <names of the output arrays, comma-separated>"
31 echo " --v2 Use TensorFlow 2.x interface (default is 1.x interface)"
37 $DRIVER_PATH/one-version one-import-tf
43 # Parse command-line arguments
45 while [ "$#" -ne 0 ]; do
56 export INPUT_PATH="$2"
60 export OUTPUT_PATH="$2"
64 export INPUT_ARRAYS="$2"
68 export INPUT_SHAPES="$2"
72 export OUTPUT_ARRAYS="$2"
80 echo "Unknown parameter: ${CUR}"
86 if [ -z ${INPUT_PATH} ] || [ ! -e ${INPUT_PATH} ]; then
87 echo "Error: input model not found"
93 FILE_BASE=$(basename ${OUTPUT_PATH})
94 MODEL_NAME="${FILE_BASE%.*}"
97 trap "{ rm -rf $TMPDIR; }" EXIT
99 # activate python virtual environment
100 VIRTUALENV_LINUX="${DRIVER_PATH}/venv/bin/activate"
101 VIRTUALENV_WINDOWS="${DRIVER_PATH}/venv/Scripts/activate"
103 if [ -e ${VIRTUALENV_LINUX} ]; then
104 source ${VIRTUALENV_LINUX}
105 elif [ -e ${VIRTUALENV_WINDOWS} ]; then
106 source ${VIRTUALENV_WINDOWS}
109 # remove previous log
110 rm -rf "${OUTPUT_PATH}.log"
114 cat "${OUTPUT_PATH}.log"
117 trap show_err_onexit ERR
119 # generate temporary tflite file
120 echo "python" "${DRIVER_PATH}/tf2tfliteV2.py" ${TF_INTERFACE} --input_path ${INPUT_PATH} \
121 --input_arrays ${INPUT_ARRAYS} --input_shapes ${INPUT_SHAPES} \
122 --output_path "${TMPDIR}/${MODEL_NAME}.tflite" \
123 --output_arrays ${OUTPUT_ARRAYS} > "${OUTPUT_PATH}.log"
124 echo " " >> "${OUTPUT_PATH}.log"
126 python "${DRIVER_PATH}/tf2tfliteV2.py" ${TF_INTERFACE} --input_path ${INPUT_PATH} \
127 --input_arrays ${INPUT_ARRAYS} --input_shapes ${INPUT_SHAPES} \
128 --output_path "${TMPDIR}/${MODEL_NAME}.tflite" \
129 --output_arrays ${OUTPUT_ARRAYS} >> "${OUTPUT_PATH}.log" 2>&1
131 # convert .tflite to .circle
132 echo " " >> "${OUTPUT_PATH}.log"
133 echo "${DRIVER_PATH}/tflite2circle" "${TMPDIR}/${MODEL_NAME}.tflite" \
134 "${OUTPUT_PATH}" >> "${OUTPUT_PATH}.log"
135 echo " " >> "${OUTPUT_PATH}.log"
137 "${DRIVER_PATH}/tflite2circle" "${TMPDIR}/${MODEL_NAME}.tflite" \
138 "${OUTPUT_PATH}" >> "${OUTPUT_PATH}.log" 2>&1