Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / tools / nnpackage_tool / model2nnpkg / model2nnpkg.sh
1 #!/bin/bash
2
3 set -eu
4
5 progname=$(basename "${BASH_SOURCE[0]}")
6 outdir="."
7 name=""
8 config=""
9 config_src=""
10
11 usage() {
12   echo "Usage: $progname [options] modelfile"
13   echo "Convert modelfile (either tflite or circle) to nnpackage."
14   echo ""
15   echo "Options:"
16   echo "    -h   show this help"
17   echo "    -o   set nnpackage output directory (default=$outdir)"
18   echo "    -p   set nnpackage output name (default=[modelfile name])"
19   echo "    -c   provide configuration file"
20   echo ""
21   echo "Examples:"
22   echo "    $progname add.tflite                  => create nnpackage 'add' in $outdir/"
23   echo "    $progname -o out add.tflite           => create nnpackage 'add' in out/"
24   echo "    $progname -o out -p addpkg add.tflite => create nnpackage 'addpkg' in out/"
25   echo "    $progname -c add.cfg add.tflite       => create nnpackage 'add' with add.cfg"
26   exit 1
27 }
28
29 if [ $# -eq 0 ]; then
30   echo "For help, type $progname -h"
31   exit 1
32 fi
33
34 while getopts "ho:p:c:" OPTION; do
35 case "${OPTION}" in
36     h) usage;;
37     o) outdir=$OPTARG;;
38     p) name=$OPTARG;;
39     c) config_src=$OPTARG;;
40     ?) exit 1;;
41 esac
42 done
43
44 shift $((OPTIND-1))
45
46 if [ $# -ne 1 ]; then
47   echo "error: wrong argument (no argument or too many arguments)."
48   echo "For help, type $progname -h"
49   exit 1
50 fi
51
52 modelfile=$(basename "$1")
53
54 if [[ "$modelfile" != *.* ]]; then
55   echo "error: modelfile does not have extension."
56   echo "Please provide extension so that $progname can identify what type of model you use."
57   exit 1
58 fi
59
60 if [ ! -e $1 ]; then
61   echo "error: "$1" does not exist."
62   exit 1
63 fi
64
65 if [ -z "$name" ]; then
66   name=${modelfile%.*}
67 fi
68 extension=${modelfile##*.}
69
70 echo "Generating nnpackage "$name" in "$outdir""
71 mkdir -p "$outdir"/"$name"/metadata
72
73 if [ -s "$config_src" ]; then
74   config=$(basename "$config_src")
75   cp "$config_src" "$outdir/$name/metadata/$config"
76 fi
77
78 cat > "$outdir"/"$name"/metadata/MANIFEST <<-EOF
79 {
80   "major-version" : "1",
81   "minor-version" : "1",
82   "patch-version" : "0",
83   "configs"     : [ "$config" ],
84   "models"      : [ "$modelfile" ],
85   "model-types" : [ "$extension" ]
86 }
87 EOF
88 cp "$1" "$outdir"/"$name"