Skip to content

Commit

Permalink
Fix ONNX compatibility and numpy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
p-wysocki committed Aug 31, 2022
1 parent e71d6c6 commit cd8a0e6
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/bindings/python/requirements_test.txt
Expand Up @@ -23,7 +23,7 @@ flake8_pep3101
flake8_quotes
import-order
mypy
onnx<1.12.0
onnx<=1.12.0
Pep8-naming
pydocstyle
pytest-xdist
Expand Down
20 changes: 10 additions & 10 deletions src/bindings/python/tests/test_frontend/test_frontend_onnx.py
Expand Up @@ -75,15 +75,15 @@ def create_onnx_model_with_custom_attributes():
attribute_i32=np.int32(10),
attribute_i64=np.int64(10),
attribute_str="string",
attribute_f32=np.float(10),
attribute_f32=float(10),
attribute_f64=np.float64(10),
attribute_bool=np.bool(True),
attribute_bool=True,
attribute_type=onnx.TensorProto.INT32,

attribute_list_i32=np.array([1, 2, 3], dtype=np.int32),
attribute_list_i64=np.array([1, 2, 3], dtype=np.int64),
attribute_list_str=np.array(["a", "b", "c"], dtype=np.str),
attribute_list_f32=np.array([1, 2, 3], dtype=np.float),
attribute_list_str=np.array(["a", "b", "c"], dtype=str),
attribute_list_f32=np.array([1, 2, 3], dtype=float),
attribute_list_f64=np.array([1, 2, 3], dtype=np.float64),
attribute_list_bool=[True, False, True],
attribute_list_type=np.array([onnx.TensorProto.INT32,
Expand Down Expand Up @@ -340,15 +340,15 @@ def check_attribute(context, name, default_value):
check_attribute(node, "attribute_str", "abc")
check_attribute(node, "attribute_f32", np.float32(5))
check_attribute(node, "attribute_f64", np.float64(5))
check_attribute(node, "attribute_bool", np.bool(False))
check_attribute(node, "attribute_bool", False)
check_attribute(node, "attribute_type", onnx.TensorProto.FLOAT)

check_attribute(node, "attribute_list_i32", np.array([4, 5, 6], dtype=np.int32))
check_attribute(node, "attribute_list_i64", np.array([4, 5, 6], dtype=np.int64))
check_attribute(node, "attribute_list_str", np.array(["d", "e", "f"], dtype=np.str))
check_attribute(node, "attribute_list_f32", np.array([4, 5, 6], dtype=np.float))
check_attribute(node, "attribute_list_str", np.array(["d", "e", "f"], dtype=str))
check_attribute(node, "attribute_list_f32", np.array([4, 5, 6], dtype=float))
check_attribute(node, "attribute_list_f64", np.array([4, 5, 6], dtype=np.float64))
check_attribute(node, "attribute_list_bool", np.array([True, False, True], dtype=np.bool))
check_attribute(node, "attribute_list_bool", np.array([True, False, True], dtype=bool))
check_attribute(node, "attribute_list_type", np.array([onnx.TensorProto.INT32,
onnx.TensorProto.FLOAT]))

Expand Down Expand Up @@ -395,15 +395,15 @@ def check_attribute(context, name, expected_value, dtype):

check_attribute(node, "attribute_i32", 10, float)
check_attribute(node, "attribute_i64", 10, float)
check_attribute(node, "attribute_str", "string", np.str)
check_attribute(node, "attribute_str", "string", str)
check_attribute(node, "attribute_f32", 10, int)
check_attribute(node, "attribute_f64", 10, int)
check_attribute(node, "attribute_bool", True, bool)
check_attribute(node, "attribute_type", Type.i32, Type)

check_attribute(node, "attribute_list_i32", [1., 2., 3.], float)
check_attribute(node, "attribute_list_i64", [1., 2., 3.], float)
check_attribute(node, "attribute_list_str", ["a", "b", "c"], np.str)
check_attribute(node, "attribute_list_str", ["a", "b", "c"], str)
check_attribute(node, "attribute_list_f32", [1, 2, 3], int)
check_attribute(node, "attribute_list_f64", [1, 2, 3], int)
check_attribute(node, "attribute_list_bool", [True, False, True], bool)
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/python/tests/test_graph/test_basic.py
Expand Up @@ -249,7 +249,7 @@ def test_bad_data_shape():

def test_constant_get_data_bool():
input_data = np.array([True, False, False, True])
node = ops.constant(input_data, dtype=np.bool)
node = ops.constant(input_data, dtype=bool)
retrieved_data = node.get_data()
assert np.allclose(input_data, retrieved_data)

Expand Down
16 changes: 8 additions & 8 deletions src/bindings/python/tests/test_graph/test_create_op.py
Expand Up @@ -792,7 +792,7 @@ def test_rnn_sequence():


def test_loop():
bool_val = [True] # np.array([1], dtype=np.bool)
bool_val = [True] # np.array([1], dtype=bool)
condition = ov.constant(bool_val)
trip_count = ov.constant(16, dtype=np.int32)
# Body parameters
Expand Down Expand Up @@ -1816,11 +1816,11 @@ def test_multiclass_nms():
0.0, -0.1, 1.0, 0.9, 0.0, 10.0, 1.0, 11.0,
0.0, 10.1, 1.0, 11.1, 0.0, 100.0, 1.0, 101.0], dtype="float32")
boxes_data = boxes_data.reshape([1, 6, 4])
box = ov.constant(boxes_data, dtype=np.float)
box = ov.constant(boxes_data, dtype=float)
scores_data = np.array([0.9, 0.75, 0.6, 0.95, 0.5, 0.3,
0.95, 0.75, 0.6, 0.80, 0.5, 0.3], dtype="float32")
scores_data = scores_data.reshape([1, 2, 6])
score = ov.constant(scores_data, dtype=np.float)
score = ov.constant(scores_data, dtype=float)

nms_node = ov.multiclass_nms(box, score, None, output_type="i32", nms_top_k=3,
iou_threshold=0.5, score_threshold=0.0, sort_result_type="classid",
Expand All @@ -1841,13 +1841,13 @@ def test_multiclass_nms():
[9.66, 3.36, 18.57, 13.26]],
[[6.50, 7.00, 13.33, 17.63],
[0.73, 5.34, 19.97, 19.97]]]).astype("float32")
box = ov.constant(boxes_data, dtype=np.float)
box = ov.constant(boxes_data, dtype=float)
scores_data = np.array([[0.34, 0.66],
[0.45, 0.61],
[0.39, 0.59]]).astype("float32")
score = ov.constant(scores_data, dtype=np.float)
score = ov.constant(scores_data, dtype=float)
rois_num_data = np.array([3]).astype("int32")
roisnum = ov.constant(rois_num_data, dtype=np.int)
roisnum = ov.constant(rois_num_data, dtype=int)
nms_node = ov.multiclass_nms(box, score, roisnum, output_type="i32", nms_top_k=3,
iou_threshold=0.5, score_threshold=0.0, sort_result_type="classid",
nms_eta=1.0)
Expand All @@ -1867,11 +1867,11 @@ def test_matrix_nms():
0.0, -0.1, 1.0, 0.9, 0.0, 10.0, 1.0, 11.0,
0.0, 10.1, 1.0, 11.1, 0.0, 100.0, 1.0, 101.0], dtype="float32")
boxes_data = boxes_data.reshape([1, 6, 4])
box = ov.constant(boxes_data, dtype=np.float)
box = ov.constant(boxes_data, dtype=float)
scores_data = np.array([0.9, 0.75, 0.6, 0.95, 0.5, 0.3,
0.95, 0.75, 0.6, 0.80, 0.5, 0.3], dtype="float32")
scores_data = scores_data.reshape([1, 2, 6])
score = ov.constant(scores_data, dtype=np.float)
score = ov.constant(scores_data, dtype=float)

nms_node = ov.matrix_nms(box, score, output_type="i32", nms_top_k=3,
score_threshold=0.0, sort_result_type="score", background_class=0,
Expand Down
14 changes: 7 additions & 7 deletions src/bindings/python/tests/test_graph/test_if.py
Expand Up @@ -12,7 +12,7 @@


def create_simple_if_with_two_outputs(condition_val):
condition = ov.constant(condition_val, dtype=np.bool)
condition = ov.constant(condition_val, dtype=bool)

# then_body
x_t = ov.parameter([], np.float32, "X")
Expand Down Expand Up @@ -54,7 +54,7 @@ def create_simple_if_with_two_outputs(condition_val):


def create_diff_if_with_two_outputs(condition_val):
condition = ov.constant(condition_val, dtype=np.bool)
condition = ov.constant(condition_val, dtype=bool)

# then_body
x_t = ov.parameter([2], np.float32, "X")
Expand Down Expand Up @@ -90,7 +90,7 @@ def create_diff_if_with_two_outputs(condition_val):


def simple_if(condition_val):
condition = ov.constant(condition_val, dtype=np.bool)
condition = ov.constant(condition_val, dtype=bool)
# then_body
x_t = ov.parameter([2], np.float32, "X")
y_t = ov.parameter([2], np.float32, "Y")
Expand Down Expand Up @@ -121,15 +121,15 @@ def simple_if(condition_val):


def simple_if_without_parameters(condition_val):
condition = ov.constant(condition_val, dtype=np.bool)
condition = ov.constant(condition_val, dtype=bool)

# then_body
then_constant = ov.constant(0.7, dtype=np.float)
then_constant = ov.constant(0.7, dtype=float)
then_body_res_1 = ov.result(then_constant)
then_body = Model([then_body_res_1], [])

# else_body
else_const = ov.constant(9.0, dtype=np.float)
else_const = ov.constant(9.0, dtype=float)
else_body_res_1 = ov.result(else_const)
else_body = Model([else_body_res_1], [])

Expand Down Expand Up @@ -180,7 +180,7 @@ def test_simple_if_without_body_parameters():


def test_simple_if_basic():
condition = ov.constant(True, dtype=np.bool)
condition = ov.constant(True, dtype=bool)
# then_body
x_t = ov.parameter([2], np.float32, "X")
y_t = ov.parameter([2], np.float32, "Y")
Expand Down
6 changes: 3 additions & 3 deletions src/bindings/python/tests/test_graph/test_loop.py
Expand Up @@ -26,11 +26,11 @@ def test_simple_loop():
x_i = ov.parameter(input_shape, np.float32)
y_i = ov.parameter(input_shape, np.float32)
m_body = ov.parameter(input_shape, np.float32)
bool_val = np.array([1], dtype=np.bool)
bool_val = np.array([1], dtype=bool)
bool_val[0] = True
body_condition = ov.constant(bool_val)
trip_count = ov.constant(10, dtype=np.int64)
exec_condition = ov.constant(True, dtype=np.bool)
exec_condition = ov.constant(True, dtype=bool)

add = ov.add(x_i, y_i)
zo = ov.multiply(add, m_body)
Expand Down Expand Up @@ -66,7 +66,7 @@ def test_simple_loop():


def test_loop_basic():
bool_val = np.array([1], dtype=np.bool)
bool_val = np.array([1], dtype=bool)
bool_val[0] = True
condition = ov.constant(bool_val)
trip_count = ov.constant(16, dtype=np.int32)
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/python/tests/test_graph/test_ops.py
Expand Up @@ -556,7 +556,7 @@ def test_select():
runtime = get_runtime()
computation = runtime.computation(function, *parameter_list)
result = computation(
np.array([[True, False]], dtype=np.bool),
np.array([[True, False]], dtype=bool),
np.array([[5, 6]], dtype=np.float32),
np.array([[7, 8]], dtype=np.float32),
)[0]
Expand Down
14 changes: 7 additions & 7 deletions src/bindings/python/tests/test_graph/test_ops_binary.py
Expand Up @@ -91,14 +91,14 @@ def test_binary_logical_op(graph_api_helper, numpy_function):
runtime = get_runtime()

shape = [2, 2]
parameter_a = ov.parameter(shape, name="A", dtype=np.bool)
parameter_b = ov.parameter(shape, name="B", dtype=np.bool)
parameter_a = ov.parameter(shape, name="A", dtype=bool)
parameter_b = ov.parameter(shape, name="B", dtype=bool)

model = graph_api_helper(parameter_a, parameter_b)
computation = runtime.computation(model, parameter_a, parameter_b)

value_a = np.array([[True, False], [False, True]], dtype=np.bool)
value_b = np.array([[False, True], [False, True]], dtype=np.bool)
value_a = np.array([[True, False], [False, True]], dtype=bool)
value_b = np.array([[False, True], [False, True]], dtype=bool)

result = computation(value_a, value_b)
expected = numpy_function(value_a, value_b)
Expand All @@ -112,11 +112,11 @@ def test_binary_logical_op(graph_api_helper, numpy_function):
def test_binary_logical_op_with_scalar(graph_api_helper, numpy_function):
runtime = get_runtime()

value_a = np.array([[True, False], [False, True]], dtype=np.bool)
value_b = np.array([[False, True], [False, True]], dtype=np.bool)
value_a = np.array([[True, False], [False, True]], dtype=bool)
value_b = np.array([[False, True], [False, True]], dtype=bool)

shape = [2, 2]
parameter_a = ov.parameter(shape, name="A", dtype=np.bool)
parameter_a = ov.parameter(shape, name="A", dtype=bool)

model = graph_api_helper(parameter_a, value_b)
computation = runtime.computation(model, parameter_a)
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/python/tests/test_graph/test_reduction.py
Expand Up @@ -53,7 +53,7 @@ def test_reduction_ops(graph_api_helper, numpy_function, reduction_axes):
def test_reduction_logical_ops(graph_api_helper, numpy_function, reduction_axes):
shape = [2, 4, 3, 2]
np.random.seed(133391)
input_data = np.random.randn(*shape).astype(np.bool)
input_data = np.random.randn(*shape).astype(bool)

expected = numpy_function(input_data, axis=tuple(reduction_axes))
result = run_op_node([input_data], graph_api_helper, reduction_axes)
Expand Down
10 changes: 4 additions & 6 deletions src/bindings/python/tests/test_graph/test_utils.py
Expand Up @@ -8,9 +8,8 @@


def test_get_constant_from_source_success():
dtype = np.int
input1 = ov.opset8.parameter(Shape([5, 5]), dtype=dtype, name="input_1")
input2 = ov.opset8.parameter(Shape([25]), dtype=dtype, name="input_2")
input1 = ov.opset8.parameter(Shape([5, 5]), dtype=int, name="input_1")
input2 = ov.opset8.parameter(Shape([25]), dtype=int, name="input_2")
shape_of = ov.opset8.shape_of(input2, name="shape_of")
reshape = ov.opset8.reshape(input1, shape_of, special_zero=True)
folded_const = ov.utils.get_constant_from_source(reshape.input(1).get_source_output())
Expand All @@ -20,9 +19,8 @@ def test_get_constant_from_source_success():


def test_get_constant_from_source_failed():
dtype = np.int
input1 = ov.opset8.parameter(Shape([5, 5]), dtype=dtype, name="input_1")
input2 = ov.opset8.parameter(Shape([1]), dtype=dtype, name="input_2")
input1 = ov.opset8.parameter(Shape([5, 5]), dtype=int, name="input_1")
input2 = ov.opset8.parameter(Shape([1]), dtype=int, name="input_2")
reshape = ov.opset8.reshape(input1, input2, special_zero=True)
folded_const = ov.utils.get_constant_from_source(reshape.input(1).get_source_output())

Expand Down
6 changes: 3 additions & 3 deletions src/bindings/python/tests/test_onnx/test_ops_logical.py
Expand Up @@ -12,9 +12,9 @@
@pytest.mark.parametrize(
("onnx_op", "numpy_func", "data_type"),
[
pytest.param("And", np.logical_and, np.bool_),
pytest.param("Or", np.logical_or, np.bool_),
pytest.param("Xor", np.logical_xor, np.bool_),
pytest.param("And", np.logical_and, bool),
pytest.param("Or", np.logical_or, bool),
pytest.param("Xor", np.logical_xor, bool),
pytest.param("Equal", np.equal, np.int32),
pytest.param("Greater", np.greater, np.int32),
pytest.param("Less", np.less, np.int32),
Expand Down
4 changes: 0 additions & 4 deletions src/bindings/python/tests/test_onnx/utils/model_importer.py
Expand Up @@ -34,10 +34,6 @@
else: # for ONNX >= 1.12
ExtOnnxTestCase = tuple((field.name for field in dataclasses.fields(OnnxTestCase))) + ("post_processing",)

#ExtOnnxTestCase = namedtuple("TestCaseExt", OnnxTestCase_fields + ("post_processing",))
#ExtOnnxTestCase = namedtuple("TestCaseExt", OnnxTestCase_fields + ("post_processing",))


class ModelImportRunner(onnx.backend.test.BackendTest):
def __init__(
self,
Expand Down
4 changes: 2 additions & 2 deletions src/bindings/python/tests/test_runtime/test_infer_request.py
Expand Up @@ -376,7 +376,7 @@ def test_infer_mixed_keys(device):
(Type.u16, np.uint16),
(Type.i64, np.int64),
(Type.u64, np.uint64),
(Type.boolean, np.bool_),
(Type.boolean, bool),
])
def test_infer_mixed_values(device, ov_type, numpy_dtype):
request, tensor1, array1 = concat_model_with_data(device, ov_type, numpy_dtype)
Expand All @@ -399,7 +399,7 @@ def test_infer_mixed_values(device, ov_type, numpy_dtype):
(Type.u16, np.uint16),
(Type.i64, np.int64),
(Type.u64, np.uint64),
(Type.boolean, np.bool_),
(Type.boolean, bool),
])
def test_async_mixed_values(device, ov_type, numpy_dtype):
request, tensor1, array1 = concat_model_with_data(device, ov_type, numpy_dtype)
Expand Down
12 changes: 6 additions & 6 deletions src/bindings/python/tests/test_runtime/test_tensor.py
Expand Up @@ -30,7 +30,7 @@
(ov.Type.u16, np.uint16),
(ov.Type.i64, np.int64),
(ov.Type.u64, np.uint64),
(ov.Type.boolean, np.bool_),
(ov.Type.boolean, bool),
(ov.Type.u1, np.uint8),
(ov.Type.u4, np.uint8),
(ov.Type.i4, np.int8),
Expand Down Expand Up @@ -64,7 +64,7 @@ def test_subprocess():
(ov.Type.u16, np.uint16),
(ov.Type.i64, np.int64),
(ov.Type.u64, np.uint64),
(ov.Type.boolean, np.bool_),
(ov.Type.boolean, bool),
])
def test_init_with_numpy_dtype(ov_type, numpy_dtype):
shape = (1, 3, 127, 127)
Expand Down Expand Up @@ -94,7 +94,7 @@ def test_init_with_numpy_dtype(ov_type, numpy_dtype):
(ov.Type.u16, np.uint16),
(ov.Type.i64, np.int64),
(ov.Type.u64, np.uint64),
(ov.Type.boolean, np.bool_),
(ov.Type.boolean, bool),
])
def test_init_with_numpy_shared_memory(ov_type, numpy_dtype):
arr = generate_image().astype(numpy_dtype)
Expand Down Expand Up @@ -131,7 +131,7 @@ def test_init_with_numpy_shared_memory(ov_type, numpy_dtype):
(ov.Type.u16, np.uint16),
(ov.Type.i64, np.int64),
(ov.Type.u64, np.uint64),
(ov.Type.boolean, np.bool_),
(ov.Type.boolean, bool),
])
def test_init_with_numpy_copy_memory(ov_type, numpy_dtype):
arr = generate_image().astype(numpy_dtype)
Expand Down Expand Up @@ -178,7 +178,7 @@ def test_init_with_roi_tensor():
(ov.Type.u16, np.uint16),
(ov.Type.i64, np.int64),
(ov.Type.u64, np.uint64),
(ov.Type.boolean, np.bool_),
(ov.Type.boolean, bool),
])
def test_write_to_buffer(ov_type, numpy_dtype):
ov_tensor = Tensor(ov_type, ov.Shape([1, 3, 32, 32]))
Expand All @@ -200,7 +200,7 @@ def test_write_to_buffer(ov_type, numpy_dtype):
(ov.Type.u16, np.uint16),
(ov.Type.i64, np.int64),
(ov.Type.u64, np.uint64),
(ov.Type.boolean, np.bool_),
(ov.Type.boolean, bool),
])
def test_set_shape(ov_type, numpy_dtype):
shape = ov.Shape([1, 3, 32, 32])
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/python/tests/test_runtime/test_type.py
Expand Up @@ -20,7 +20,7 @@
("uint16", np.uint16, Type.u16),
("uint32", np.uint32, Type.u32),
("uint64", np.uint64, Type.u64),
("bool", np.bool_, Type.boolean),
("bool", bool, Type.boolean),
])
def test_dtype_ovtype_conversion(dtype_string, dtype, ovtype):
assert ovtype.to_dtype() == dtype
Expand Down

0 comments on commit cd8a0e6

Please sign in to comment.