summaryrefslogtreecommitdiff
path: root/tools/nnpackage_tool/model2nnpkg/model2nnpkg.sh
blob: 87cd7878b7f964a6b4e033eb2af01d0b3ea4f1ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash

set -eu

progname=$(basename "${BASH_SOURCE[0]}")
outdir="."

usage() {
  echo "Usage: $progname [options] modelfile"
  echo "Convert modelfile (either tflite or circle) to nnpackage."
  echo ""
  echo "Options:"
  echo "    -h   show this help"
  echo "    -o   set nnpackage output directory (default=$outdir)"
  echo ""
  echo "Examples:"
  echo "    $progname add.tflite        => create nnpackage in $outdir/"
  echo "    $progname -o out add.tflite => create nnpackage in out/"
  exit 1
}

if [ $# -eq 0 ]; then
  echo "For help, type $progname -h"
  exit 1
fi

while getopts "ho:" OPTION; do
case "${OPTION}" in
    h) usage;;
    o) outdir=$OPTARG;;
    ?) exit 1;;
esac
done

shift $((OPTIND-1))

if [ $# -ne 1 ]; then
  echo "error: wrong argument (no argument or too many arguments)."
  echo "For help, type $progname -h"
  exit 1
fi

modelfile=$(basename "$1")

if [[ "$modelfile" != *.* ]]; then
  echo "error: modelfile does not have extension."
  echo "Please provide extension so that $progname can identify what type of model you use."
  exit 1
fi

if [ ! -e $1 ]; then
  echo "error: "$1" does not exist."
  exit 1
fi

name=${modelfile%.*}
extension=${modelfile##*.}

echo "Generating nnpackage "$name" in "$outdir""
mkdir -p "$outdir"/"$name"/metadata
cat > "$outdir"/"$name"/metadata/MANIFEST <<-EOF
{
  "major-version" : "1",
  "minor-version" : "0",
  "patch-version" : "0",
  "models"      : [ "$name.$extension" ],
  "model-types" : [ "$extension" ]
}
EOF
cp "$1" "$outdir"/"$name"