Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OVC] Fixed error in --output_model param. #20969

Merged
merged 4 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions tests/layer_tests/ovc_python_api_tests/test_ovc_cli_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from common.mo_convert_test_class import CommonMOConvertTest
from common.tf_layer_test_class import save_to_pb
from common.utils.common_utils import shell
from unittest.mock import patch
import unittest


def generate_ir_ovc(coverage=False, **kwargs):
Expand Down Expand Up @@ -155,3 +157,16 @@ def test_ovc_tool_saved_model_dir_with_sep_at_path_end(self, ie_device, precisio
ov_model = core.read_model(os.path.join(temp_dir, "test_model.xml"))
flag, msg = compare_functions(ov_model, ref_model, False)
assert flag, msg

def test_ovc_tool_non_existng_output_dir(self, ie_device, precision, ir_version, temp_dir, use_new_frontend, use_old_api):
popovaan marked this conversation as resolved.
Show resolved Hide resolved
from openvino.runtime import Core
core = Core()

model_dir, ref_model = self.create_tf_saved_model_dir(temp_dir)

exit_code, stderr = generate_ir_ovc(coverage=False, **{"input_model": model_dir + os.sep, "output_model": temp_dir + os.sep + "dir" + os.sep})
assert not exit_code

ov_model = core.read_model(os.path.join(temp_dir, "dir", "test_model.xml"))
flag, msg = compare_functions(ov_model, ref_model, False)
assert flag, msg
15 changes: 10 additions & 5 deletions tools/ovc/openvino/tools/ovc/cli_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,16 +559,21 @@ def get_model_name_from_args(argv: argparse.Namespace):
if hasattr(argv, 'output_model') and argv.output_model:
model_name = argv.output_model

if not os.path.isdir(argv.output_model):
if not os.path.isdir(argv.output_model) and not argv.output_model.endswith(os.sep):
# In this branch we assume that model name is set in 'output_model'.
if not model_name.endswith('.xml'):
model_name += '.xml'
# Logic of creating and checking directory is covered in save_model() method.
rkazants marked this conversation as resolved.
Show resolved Hide resolved
return model_name
else:
if not os.access(argv.output_model, os.W_OK):
# In this branch 'output_model' has directory without name of model.
# The directory may not exist.
if os.path.isdir(argv.output_model) and not os.access(argv.output_model, os.W_OK):
# If the provided path is existing directory, but not writable, then raise error
raise Error('The directory "{}" is not writable'.format(argv.output_model))
output_dir = argv.output_model

input_model = argv.input_model
input_model = os.path.abspath(argv.input_model)
if isinstance(input_model, (tuple, list)) and len(input_model) > 0:
input_model = input_model[0]

Expand All @@ -583,8 +588,8 @@ def get_model_name_from_args(argv: argparse.Namespace):
input_model_name = os.path.splitext(input_model_name)[0]

# if no valid name exists in input path set name to 'model'
if input_model_name == '' or input_model_name == '.':
input_model_name = "model"
if input_model_name == '':
raise Exception("Could not derive model name from input model. Please provide 'output_model' parameter.")

# add .xml extension
return os.path.join(output_dir, input_model_name + ".xml")
Expand Down
85 changes: 77 additions & 8 deletions tools/ovc/unit_tests/ovc/utils/cli_parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@
# SPDX-License-Identifier: Apache-2.0

import argparse
import numpy
import os
import shutil
import sys
import tempfile
import unittest
from unittest.mock import patch

import numpy as np
import openvino.runtime as ov
from openvino.runtime import PartialShape

from openvino.tools.ovc.cli_parser import _InputCutInfo
from openvino.tools.ovc.cli_parser import input_to_input_cut_info, \
readable_file_or_object, get_all_cli_parser, get_mo_convert_params, parse_inputs
readable_file_or_object, get_all_cli_parser, get_mo_convert_params, parse_inputs, get_model_name_from_args
from openvino.tools.ovc.convert_impl import pack_params_to_args_namespace, arguments_post_parsing, args_to_argv
from openvino.tools.ovc.error import Error
from unit_tests.ovc.unit_test_with_mocked_telemetry import UnitTestWithMockedTelemetry
from openvino.runtime import PartialShape, Dimension, Layout
from openvino.tools.ovc.cli_parser import _InputCutInfo
import openvino.runtime as ov


class TestShapesParsing(UnitTestWithMockedTelemetry):
Expand Down Expand Up @@ -518,3 +514,76 @@ def test_mo_convert_params_parsing(self):
assert param_name not in cli_parser._option_string_actions
else:
assert param_name in cli_parser._option_string_actions



class GetModelNameTest(unittest.TestCase):
def test_case1(self):
current_dir = os.getcwd()
dir = os.path.basename(current_dir)
argv = argparse.Namespace(input_model="."+ os.sep)
assert get_model_name_from_args(argv) == current_dir + os.sep + dir + ".xml"

def test_case2(self):
current_dir = os.getcwd()
argv = argparse.Namespace(input_model="."+ os.sep +"test_model")
assert get_model_name_from_args(argv) == current_dir + os.sep + "test_model.xml"


def test_case3(self):
current_dir = os.getcwd()
argv = argparse.Namespace(input_model="."+ os.sep +"test_model.pb")
assert get_model_name_from_args(argv) == current_dir + os.sep + "test_model.xml"


def test_case4(self):
current_dir = os.getcwd()
dir = os.path.basename(current_dir)
argv = argparse.Namespace(input_model="."+ os.sep,
output_model="."+ os.sep)
assert get_model_name_from_args(argv) == "." + os.sep + dir + ".xml"

def test_case5(self):
argv = argparse.Namespace(input_model="test_model.pb",
output_model="."+ os.sep)
assert get_model_name_from_args(argv) == "." + os.sep + "test_model.xml"


def test_case6(self):
argv = argparse.Namespace(input_model="test_model",
output_model="."+ os.sep)
assert get_model_name_from_args(argv) == "." + os.sep + "test_model.xml"



def test_case7(self):
argv = argparse.Namespace(input_model="test_dir" + os.sep,
output_model="."+ os.sep)
assert get_model_name_from_args(argv) == "." + os.sep + "test_dir.xml"

def test_case8(self):
argv = argparse.Namespace(input_model="test_model.pb",
output_model="new_model")
assert get_model_name_from_args(argv) == "new_model.xml"

def test_case9(self):
argv = argparse.Namespace(input_model="test_model",
output_model="new_dir" + os.sep)
assert get_model_name_from_args(argv) == "new_dir" + os.sep + "test_model.xml"

def test_case10(self):
argv = argparse.Namespace(input_model="test_dir" + os.sep,
output_model="new_model.xml")
assert get_model_name_from_args(argv) == "new_model.xml"

def test_case11(self):
argv = argparse.Namespace(input_model="/",
output_model="new_model")
assert get_model_name_from_args(argv) == "new_model.xml"


def test_negative(self):

argv = argparse.Namespace(input_model="/",)
with self.assertRaisesRegex(Exception, ".*Could not derive model name from input model. Please provide 'output_model' parameter.*"):
get_model_name_from_args(argv)
Loading