6ad2ef9e29987dfe1b2e81d5f1bb747e39ed2845
[platform/core/ml/nnfw.git] / tools / nnpackage_tool / tflite2circle / tflite2circle.sh
1 #!/bin/bash
2
3 set -u
4
5 progname=$(basename "${BASH_SOURCE[0]}")
6 script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
7 nnfw_root="$( cd "${script_dir%*/*/*/*}" && pwd )"
8 outdir="."
9 flatc=${flatc:-"$nnfw_root/build/externals/FLATBUFFERS/build/flatc"}
10 tflite_schema=${tflite_schema:-"$nnfw_root/externals/TENSORFLOW-1.12/tensorflow/contrib/lite/schema/schema.fbs"}
11 circle_schema=${circle_schema:-"$nnfw_root/nnpackage/schema/circle_schema.fbs"}
12
13 if ! [ -x "$flatc" ]; then
14   echo "Please make sure `flatc` is in path."
15   exit 2
16 fi
17
18 if ! { [ -e "$tflite_schema" ] && [ -e "$circle_schema" ]; }; then
19   echo "Please make sure that the `*.fbs` paths are set properly."
20   exit 3
21 fi
22
23 usage() {
24   echo "Usage: $progname [options] tflite"
25   echo "Convert tflite to circle"
26   echo ""
27   echo "Returns"
28   echo "     0       success"
29   echo "  non-zero   failure"
30   echo ""
31   echo "Options:"
32   echo "    -h   show this help"
33   echo "    -o   set output directory (default=$outdir)"
34   echo ""
35   echo "Environment variables:"
36   echo "   flatc           path to flatc"
37   echo "                   (default=./build/externals/FLATBUFFERS/build/flatc)"
38   echo "   tflite_schema   path to tflite schema (i.e. schema.fbs)"
39   echo "                   (default=./externals/TENSORFLOW-1.12/tensorflow/contrib/lite/schema/schema.fbs)"
40   echo "   circle_schema   path to circle schema"
41   echo "                   (default=./nnpackage/schema/circle_schema.fbs)"
42   echo ""
43   echo "Examples:"
44   echo "    $progname Add_000.tflite         => convert Add_000.tflite into Add_000.circle"
45   echo "    $progname -o my/circles Add_000  => convert Add_000.tflite into my/circles/Add_000.circle"
46   exit 1
47 }
48
49 if [ $# -eq 0 ]; then
50   echo "For help, type $progname -h"
51   exit 1
52 fi
53
54 while getopts "ho:" OPTION; do
55 case "${OPTION}" in
56     h) usage;;
57     o) outdir=$OPTARG;;
58     ?) exit 1;;
59 esac
60 done
61
62 shift $((OPTIND-1))
63
64 if [ $# -ne 1 ]; then
65   echo "error: wrong argument (no argument or too many arguments)."
66   echo "For help, type $progname -h"
67   exit 1
68 fi
69
70 tflite_base=$(basename "$1")
71 name=${tflite_base%.*}
72
73 # convert
74
75 mkdir -p "${outdir}"
76 ${flatc} -o ${outdir} --defaults-json --strict-json -t ${tflite_schema} -- $1
77 ${script_dir}/tflitejson2circlejson.py "${outdir}/${name}.json" > "${outdir}/${name}.circle"
78 ${flatc} -o ${outdir} -b ${circle_schema} "${outdir}/${name}.circle"
79 rm -f ${outdir}/${name}.json