Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / tools / nnpackage_tool / nncc-tc-to-nnpkg-tc / nncc-tc-to-nnpkg-tc.sh
1 #!/bin/bash
2
3 set -eu
4
5 progname=$(basename "${BASH_SOURCE[0]}")
6 script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
7 model2nnpkg=${model2nnpkg:-"$script_dir"/../model2nnpkg/model2nnpkg.sh}
8 # Need to install nncc package & set path to tf2nnpkg
9 tf2nnpkg=$(which tf2nnpkg)
10
11 indir="."
12 outdir="."
13
14 usage() {
15   echo "Usage: $progname [options] nncc_tc_name"
16   echo "Convert nncc testcase to nnpackage testcase."
17   echo ""
18   echo "Options:"
19   echo "    -h   show this help"
20   echo "    -i   set input directory (default=$indir)"
21   echo "    -o   set nnpackage testcase output directory (default=$outdir)"
22   echo ""
23   echo "Env:"
24   echo "   model2nnpkg    path to model2nnpkg tool (default={this_script_home}/../model2nnpkg)"
25   echo ""
26   echo "Examples:"
27   echo "    $progname -i build/compiler/tf2tflite UNIT_Add_000"
28   echo "      => create nnpackage testcase in $outdir/ from build/compiler/tf2tflite/UNIT_Add_000.*"
29   echo "    $progname -o out UNIT_Add_000"
30   echo "      => create nnpackage testcase in out/ using $indir/UNIT_Add_000.*"
31   exit 1
32 }
33
34 if [ $# -eq 0 ]; then
35   echo "For help, type $progname -h"
36   exit 1
37 fi
38
39 while getopts "hi:o:" OPTION; do
40 case "${OPTION}" in
41     h) usage;;
42     i) indir=$OPTARG;;
43     o) outdir=$OPTARG;;
44     ?) exit 1;;
45 esac
46 done
47
48 shift $((OPTIND-1))
49
50 if [ $# -ne 1 ]; then
51   echo "error: wrong argument (no argument or too many arguments)."
52   echo "For help, type $progname -h"
53   exit 1
54 fi
55
56 tcname=$1
57
58 supported_model_types="
59 pb
60 circle
61 tflite
62 "
63
64 model_type=""
65 tf_intf_version=""
66
67 for ext in $supported_model_types; do
68   [ -e "$indir/$tcname"."$ext" ] && model_type=$ext
69 done;
70
71 if [[ "$model_type" == "" ]]; then
72   echo "error: No modelfile is found in $indir/$tcname*"
73   exit 1
74 fi
75
76 if [[ "$model_type" == "pb" ]]; then
77   [ -f "$indir/$tcname"."v2" ] && tf_intf_version="--v2"
78   $tf2nnpkg --info "$indir/$tcname".info --graphdef "$indir/$tcname"."$model_type" \
79   "$tf_intf_version" -o "$outdir"
80 else
81   $model2nnpkg -o "$outdir" "$indir/$tcname"."$model_type"
82 fi
83
84 extensions="
85 expected.h5
86 input.h5
87 "
88
89 destdir="$outdir/$tcname/metadata/tc"
90 mkdir -p "$destdir"
91 for ext in $extensions; do
92   cp "$indir/$tcname.$ext" "$destdir/$ext"
93 done;
94