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 [ -n ${INPUT_SHAPES} ] && [ ${TF_INTERFACE} = "--v2" ]; then
87 echo "Warning: if --v2 option is used, shape will be ignored"
90 if [ -z ${INPUT_PATH} ] || [ ! -e ${INPUT_PATH} ]; then
91 echo "Error: input model not found"
97 FILE_BASE=$(basename ${OUTPUT_PATH})
98 MODEL_NAME="${FILE_BASE%.*}"
101 trap "{ rm -rf $TMPDIR; }" EXIT
103 # activate python virtual environment
104 VIRTUALENV_LINUX="${DRIVER_PATH}/venv/bin/activate"
105 VIRTUALENV_WINDOWS="${DRIVER_PATH}/venv/Scripts/activate"
107 if [ -e ${VIRTUALENV_LINUX} ]; then
108 source ${VIRTUALENV_LINUX}
109 elif [ -e ${VIRTUALENV_WINDOWS} ]; then
110 source ${VIRTUALENV_WINDOWS}
113 # remove previous log
114 rm -rf "${OUTPUT_PATH}.log"
118 cat "${OUTPUT_PATH}.log"
121 trap show_err_onexit ERR
123 # generate temporary tflite file
124 CONVERT_SCRIPT="python ${DRIVER_PATH}/tf2tfliteV2.py ${TF_INTERFACE} "
125 CONVERT_SCRIPT+="--input_path ${INPUT_PATH} "
126 CONVERT_SCRIPT+="--input_arrays ${INPUT_ARRAYS} "
127 CONVERT_SCRIPT+="--output_path ${TMPDIR}/${MODEL_NAME}.tflite "
128 CONVERT_SCRIPT+="--output_arrays ${OUTPUT_ARRAYS} "
129 if [ ! -z ${INPUT_SHAPES} ]; then
130 CONVERT_SCRIPT+="--input_shapes ${INPUT_SHAPES} "
133 echo ${CONVERT_SCRIPT} > "${OUTPUT_PATH}.log"
134 echo "" >> "${OUTPUT_PATH}.log"
135 $CONVERT_SCRIPT >> "${OUTPUT_PATH}.log" 2>&1
137 # convert .tflite to .circle
138 echo " " >> "${OUTPUT_PATH}.log"
139 echo "${DRIVER_PATH}/tflite2circle" "${TMPDIR}/${MODEL_NAME}.tflite" \
140 "${OUTPUT_PATH}" >> "${OUTPUT_PATH}.log"
141 echo " " >> "${OUTPUT_PATH}.log"
143 "${DRIVER_PATH}/tflite2circle" "${TMPDIR}/${MODEL_NAME}.tflite" \
144 "${OUTPUT_PATH}" >> "${OUTPUT_PATH}.log" 2>&1