Introduce model2nnpackage (#6976)
author이상규/On-Device Lab(SR)/Principal Engineer/삼성전자 <sg5.lee@samsung.com>
Thu, 29 Aug 2019 04:52:16 +0000 (13:52 +0900)
committer오형석/On-Device Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Thu, 29 Aug 2019 04:52:16 +0000 (13:52 +0900)
This patch introduces model2nnpackage.
It will take model file (either circle or tflite) and generate
containing nnpackage.

Signed-off-by: Sanggyu Lee <sg5.lee@samsung.com>
tools/nnpackage_tool/model2nnpkg/README.md [new file with mode: 0644]
tools/nnpackage_tool/model2nnpkg/model2nnpkg.sh [new file with mode: 0755]

diff --git a/tools/nnpackage_tool/model2nnpkg/README.md b/tools/nnpackage_tool/model2nnpkg/README.md
new file mode 100644 (file)
index 0000000..716f4f8
--- /dev/null
@@ -0,0 +1,21 @@
+# model2nnpkg
+
+`model2nnpkg` is a tool to convert model (either `tflite` or `circle`) to `nnpackage`.
+
+It takes `modelfile` as input and generates `nnpackage`.
+
+## Usage
+
+```
+Usage: model2nnpkg.sh [options] modelfile
+Convert modelfile (either tflite or circle) to nnpackage.
+
+Options:
+    -h   show this help
+    -o   set nnpackage output directory (default=.)
+
+Examples:
+    model2nnpkg.sh add.tflite        => create nnpackage in ./
+    model2nnpkg.sh -o out add.tflite => create nnpackage in out/
+
+```
diff --git a/tools/nnpackage_tool/model2nnpkg/model2nnpkg.sh b/tools/nnpackage_tool/model2nnpkg/model2nnpkg.sh
new file mode 100755 (executable)
index 0000000..b66e534
--- /dev/null
@@ -0,0 +1,71 @@
+#!/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 -n "Generating nnpackage ... "
+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"
+echo "done; created in $outdir/$name."