Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/one-cmds/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ set(ONE_COMMAND_FILES
one-import-tf
one-import-tflite
one-import-onnx
one-resize
one-optimize
one-quantize
one-pack
Expand Down
5 changes: 3 additions & 2 deletions compiler/one-cmds/one-build
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def _get_driver_name(driver_name):
'one-import-tf': 'one-import-tf',
'one-import-tflite': 'one-import-tflite',
'one-import-onnx': 'one-import-onnx',
'one-resize': 'one-resize',
'one-optimize': 'one-optimize',
'one-quantize': 'one-quantize',
'one-partition': 'one-partition',
Expand Down Expand Up @@ -154,8 +155,8 @@ def main():
bin_dir = os.path.dirname(os.path.realpath(__file__))
import_drivers_dict = oneutils.detect_one_import_drivers(bin_dir)
transform_drivers = [
'one-optimize', 'one-quantize', 'one-pack', 'one-codegen', 'one-profile',
'one-partition'
'one-resize', 'one-optimize', 'one-quantize', 'one-pack', 'one-codegen',
'one-profile', 'one-partition'
]
_verify_cfg(import_drivers_dict, config)

Expand Down
1 change: 1 addition & 0 deletions compiler/one-cmds/one-build.template.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ one-import-tf=True
one-import-tflite=False
one-import-bcq=False
one-import-onnx=False
one-resize=False
one-optimize=True
one-quantize=False
one-parition=False
Expand Down
128 changes: 128 additions & 0 deletions compiler/one-cmds/one-resize
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#!/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) 2025 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 configparser
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 change shape of model inputs')

oneutils.add_default_arg(parser)

## model2nnpkg arguments
model2nnpkg_group = parser.add_argument_group('arguments for packaging')

model2nnpkg_group.add_argument('-i',
'--input_path',
type=str,
help='Path to the input model (.circle)')

model2nnpkg_group.add_argument('-o',
'--output_path',
type=str,
help='Path to the resized model (.circle)')

model2nnpkg_group.add_argument(
'-s',
'--input_shapes',
type=str,
help=
'New inputs shapes in in comma separated format. An example for 2 inputs: [1,2],[3,4].'
)

return parser


def _parse_arg(parser):
args = parser.parse_args()
# print version
if args.version:
oneutils.print_version_and_exit(__file__)

return args


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('input_path')
if not oneutils.is_valid_attr(args, 'output_path'):
missing.append('output_path')
if not oneutils.is_valid_attr(args, 'input_shapes'):
missing.append('input_shapes')
if len(missing):
parser.error('The following arguments are required: ' + ' '.join(missing))
return


def _resize(args):
bin_path = os.path.dirname(os.path.realpath(__file__))
cur_path = os.getcwd()
input_path = os.path.join(cur_path, args.input_path)
output_path = os.path.join(cur_path, args.output_path)
log_file_path = os.path.join(cur_path, output_path) + '.log'

with open(log_file_path, 'wb', buffering=0) as f:
circle_resizer_path = os.path.join(bin_path, 'circle-resizer')

cmd = [os.path.expanduser(circle_resizer_path)]

cmd.append('--input_path')
cmd.append(input_path)
cmd.append('--output_path')
cmd.append(output_path)
cmd.append('--input_shapes')
cmd.append(args.input_shapes)

f.write((' '.join(cmd) + '\n').encode())

# run circle-resizer
oneutils.run(cmd, err_prefix='one-resize', logfile=f)


def main():
# parse arguments
parser = _get_parser()
args = _parse_arg(parser)

# parse configuration file
oneutils.parse_cfg(args.config, 'one-resize', args)

# verify arguments
_verify_arg(parser, args)

# resize the model
_resize(args)


if __name__ == '__main__':
oneutils.safemain(main, __file__)
10 changes: 10 additions & 0 deletions compiler/one-cmds/onecc.template.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ one-import-tf=False
one-import-tflite=False
one-import-bcq=False
one-import-onnx=False
one-resize=False
; circle to circle with optimization
one-optimize=False
; circle to circle with quantization
Expand Down Expand Up @@ -90,6 +91,15 @@ unroll_rnn=
; True or False
unroll_lstm=

[one-resize]
# mandatory
; path to the input model
input_path=
; path to the resized model
output_path=
; the new shapes of inputs
input_shapes=

[one-optimize]
# mandatory
; circle file
Expand Down
4 changes: 2 additions & 2 deletions compiler/one-cmds/onelib/CfgRunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def _simple_warning(message, category, filename, lineno, file=None, line=None):

class CfgRunner:
driver_sequence = [
'one-optimize', 'one-quantize', 'one-pack', 'one-codegen', 'one-profile',
'one-partition', 'one-infer'
'one-optimize', 'one-resize', 'one-quantize', 'one-pack', 'one-codegen',
'one-profile', 'one-partition', 'one-infer'
]

def __init__(self, path):
Expand Down
7 changes: 4 additions & 3 deletions compiler/one-cmds/onelib/OptionBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ def _build_quantize(self, commands):
return options

def build(self, commands):
cmd_book = dict.fromkeys(
['one-import-bcq', 'one-import-tflite', 'one-pack', 'one-partition'],
self._build_default)
cmd_book = dict.fromkeys([
'one-import-bcq', 'one-import-tflite', 'one-resize', 'one-pack',
'one-partition'
], self._build_default)
cmd_book['one-codegen'] = self._build_with_unknown_command
cmd_book['one-import-onnx'] = self._build_import
cmd_book['one-import-pytorch'] = self._build_import
Expand Down
43 changes: 43 additions & 0 deletions compiler/one-cmds/tests/one-build_001.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

# Copyright (c) 2025 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.

# positive usage with overriding option

filename_ext="$(basename -- $0)"
filename="${filename_ext%.*}"

trap_err_onexit()
{
echo "${filename_ext} FAILED"
exit 255
}

trap trap_err_onexit ERR

config_file="one-resize_001.cfg"
output_file_cfg="inception_v3.resized.circle"

rm -f ${filename}.log
rm -f ${output_file_cfg}

# run test
one-build -C ${config_file} > ${filename}.log 2>&1

if [[ ! -s "${output_file_cfg}" ]]; then
trap_err_onexit
fi

echo "${filename_ext} SUCCESS"
1 change: 1 addition & 0 deletions compiler/one-cmds/tests/one-import_002.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
one-import-tf=True
one-import-tflite=False
one-import-bcq=False
one-resize=True
one-optimize=False
one-quantize=False
one-pack=False
Expand Down
1 change: 1 addition & 0 deletions compiler/one-cmds/tests/one-import_003.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
one-import-tf=True
one-import-tflite=False
one-import-bcq=False
one-resize=False
one-optimize=False
one-quantize=False
one-pack=False
Expand Down
1 change: 1 addition & 0 deletions compiler/one-cmds/tests/one-import_004.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
one-import-tf=True
one-import-tflite=False
one-import-bcq=False
one-resize=False
one-optimize=False
one-quantize=False
one-pack=False
Expand Down
1 change: 1 addition & 0 deletions compiler/one-cmds/tests/one-import_005.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ one-import-tf=False
one-import-tflite=False
one-import-bcq=False
one-import-onnx=True
one-resize=False
one-optimize=False
one-quantize=False
one-pack=False
Expand Down
14 changes: 14 additions & 0 deletions compiler/one-cmds/tests/one-resize_001.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[one-build]
one-import-tf=False
one-import-tflite=False
one-import-bcq=False
one-resize=True
one-optimize=False
one-quantize=False
one-pack=False
one-codegen=False

[one-resize]
input_path=inception_v3.circle
output_path=inception_v3.resized.circle
input_shapes=[2,299,299,3]
46 changes: 46 additions & 0 deletions compiler/one-cmds/tests/one-resize_001.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

# Copyright (c) 2025 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.

filename_ext="$(basename -- $0)"
filename="${filename_ext%.*}"
test_model_name="Add_000"

trap_err_onexit()
{
echo "${filename_ext} FAILED"
exit 255
}

trap trap_err_onexit ERR

input_path="${test_model_name}.circle"
input_shapes="[1,3,3,1],[1,3,3,1]"
output_path="${test_model_name}.resize.circle"

rm -f ${filename}.log
rm -f ${output_path}

# run test
one-resize \
--input_path ${input_path} \
--input_shapes ${input_shapes} \
--output_path ${output_path} > ${filename}.log 2>&1

if [[ ! -s "${output_path}" ]]; then
trap_err_onexit
fi

echo "${filename_ext} SUCCESS"
43 changes: 43 additions & 0 deletions compiler/one-cmds/tests/one-resize_002.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

# Copyright (c) 2025 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.

# positive usage with overriding option

filename_ext="$(basename -- $0)"
filename="${filename_ext%.*}"

trap_err_onexit()
{
echo "${filename_ext} FAILED"
exit 255
}

trap trap_err_onexit ERR

config_file="one-resize_001.cfg"
output_file_cfg="inception_v3.resized.circle"

rm -f ${filename}.log
rm -f ${output_file_cfg}

# run test
one-resize -C ${config_file} > ${filename}.log 2>&1

if [[ ! -s "${output_file_cfg}" ]]; then
trap_err_onexit
fi

echo "${filename_ext} SUCCESS"
Loading