#!/usr/bin/env bash ''''export SCRIPT_PATH="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)" # ''' ''''export PY_PATH=${SCRIPT_PATH}/venv/bin/python # ''' ''''test -f ${PY_PATH} && exec ${PY_PATH} "$0" "$@" # ''' ''''echo "Error: Virtual environment not found. Please run 'one-prepare-venv' command." # ''' ''''exit 255 # ''' # Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os import sys import onelib.utils as oneutils # TODO Find better way to suppress trackback on error sys.tracebacklimit = 0 def _get_parser(): parser = argparse.ArgumentParser( description='command line tool to package circle and metadata into nnpackage') oneutils.add_default_arg(parser) ## model2nnpkg arguments model2nnpkg_group = parser.add_argument_group('arguments for packaging') # input and output path. model2nnpkg_group.add_argument( '-i', '--input_path', type=str, help='full filepath of the input file') model2nnpkg_group.add_argument( '-o', '--output_path', type=str, help='full filepath of the output file') return parser def _verify_arg(parser, args): """verify given arguments""" # check if required arguments is given missing = [] if not oneutils.is_valid_attr(args, 'input_path'): missing.append('-i/--input_path') if not oneutils.is_valid_attr(args, 'output_path'): missing.append('-o/--output_path') if len(missing): parser.error('the following arguments are required: ' + ' '.join(missing)) def _parse_arg(parser): args = parser.parse_args() # print version if args.version: oneutils.print_version_and_exit(__file__) return args def _make_model2nnpkg_cmd(driver_path, input_path, output_path): """make a command for running model2nnpkg""" cmd = [os.path.expanduser(driver_path)] cmd.append('-o') cmd.append(os.path.expanduser(output_path)) cmd.append('-m') cmd.append(os.path.expanduser(input_path)) return cmd def _pack(args): # get file path to log dir_path = os.path.dirname(os.path.realpath(__file__)) logfile_path = os.path.realpath(args.output_path) + '.log' with open(logfile_path, 'wb') as f: # make a command to package circle model and metadata into nnpackage model2nnpkg_path = os.path.join(dir_path, 'model2nnpkg') model2nnpkg_cmd = _make_model2nnpkg_cmd(model2nnpkg_path, getattr(args, 'input_path'), getattr(args, 'output_path')) f.write((' '.join(model2nnpkg_cmd) + '\n').encode()) # convert tflite to circle oneutils.run(model2nnpkg_cmd, err_prefix="model2nnpkg", logfile=f) def main(): # parse arguments parser = _get_parser() args = _parse_arg(parser) # parse configuration file oneutils.parse_cfg(args.config, 'one-pack', args) # verify arguments _verify_arg(parser, args) # package _pack(args) if __name__ == '__main__': oneutils.safemain(main, __file__)