def test_convolution_with_non_zero_padding(): element_type = Type.f32 image_shape = Shape([1, 1, 10, 10]) filter_shape = Shape([1, 1, 3, 3]) data = Parameter(element_type, image_shape) filters = Parameter(element_type, filter_shape) parameter_list = [data, filters] image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10) filter_arr = (np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)) * -1 filter_arr[0][0][1][1] = 1 strides = [1, 1] dilations = [2, 2] pads_begin = [2, 1] pads_end = [1, 2] model = ov.convolution(data, filters, strides, pads_begin, pads_end, dilations) function = Function([model], parameter_list, "test") runtime = get_runtime() computation = runtime.computation(function, *parameter_list) result = computation(image_arr, filter_arr)[0] expected = convolution2d( image_arr[0][0], filter_arr[0][0], strides, dilations, pads_begin, pads_end ).reshape([1, 1, 9, 9]) assert np.allclose(result, expected)
def test_proposal_props(): float_dtype = np.float32 batch_size = 1 post_nms_topn = 20 probs = ov.parameter(Shape([batch_size, 8, 255, 255]), dtype=float_dtype, name="probs") deltas = ov.parameter(Shape([batch_size, 16, 255, 255]), dtype=float_dtype, name="bbox_deltas") im_info = ov.parameter(Shape([4]), dtype=float_dtype, name="im_info") attrs = { "base_size": np.uint32(85), "pre_nms_topn": np.uint32(10), "post_nms_topn": np.uint32(post_nms_topn), "nms_thresh": np.float32(0.34), "feat_stride": np.uint32(16), "min_size": np.uint32(32), "ratio": np.array([0.1, 1.5, 2.0, 2.5], dtype=np.float32), "scale": np.array([2, 3, 3, 4], dtype=np.float32), } node = ov.proposal(probs, deltas, im_info, attrs) assert node.get_type_name() == "Proposal" assert node.get_output_size() == 2 assert list(node.get_output_shape(0)) == [batch_size * post_nms_topn, 5] assert list(node.get_output_shape(1)) == [batch_size * post_nms_topn] assert node.get_output_element_type(0) == Type.f32 assert node.get_output_element_type(1) == Type.f32
def test_convolution_simple(): element_type = Type.f32 image_shape = Shape([1, 1, 16, 16]) filter_shape = Shape([1, 1, 3, 3]) data = Parameter(element_type, image_shape) filters = Parameter(element_type, filter_shape) parameter_list = [data, filters] image_arr = np.arange(-128, 128, 1, dtype=np.float32).reshape(1, 1, 16, 16) filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3) filter_arr[0][0][0][0] = -1 filter_arr[0][0][1][1] = -1 filter_arr[0][0][2][2] = -1 filter_arr[0][0][0][2] = -1 filter_arr[0][0][2][0] = -1 strides = [1, 1] pads_begin = [0, 0] pads_end = [0, 0] dilations = [1, 1] model = ov.convolution(data, filters, strides, pads_begin, pads_end, dilations) function = Function([model], parameter_list, "test") runtime = get_runtime() computation = runtime.computation(function, *parameter_list) result = computation(image_arr, filter_arr)[0] expected = convolution2d(image_arr[0][0], filter_arr[0][0]).reshape(1, 1, 14, 14) assert np.allclose(result, expected)
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") reshape = ov.opset8.reshape(input1, input2, special_zero=True) folded_const = ov.impl.util.get_constant_from_source( reshape.input(1).get_source_output()) assert folded_const is None
def test_swish_props_with_beta(): float_dtype = np.float32 data = ov.parameter(Shape([3, 10]), dtype=float_dtype, name="data") beta = ov.parameter(Shape([]), dtype=float_dtype, name="beta") node = ov.swish(data, beta) assert node.get_type_name() == "Swish" assert node.get_output_size() == 1 assert list(node.get_output_shape(0)) == [3, 10] assert node.get_output_element_type(0) == Type.f32
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") shape_of = ov.opset8.shape_of(input2, name="shape_of") reshape = ov.opset8.reshape(input1, shape_of, special_zero=True) folded_const = ov.impl.util.get_constant_from_source( reshape.input(1).get_source_output()) assert folded_const is not None assert folded_const.get_vector() == [25]
def test_evaluate(): param1 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data1") param2 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data2") add = ops.add(param1, param2) func = Function(add, [param1, param2], "TestFunction") input1 = np.array([2, 1], dtype=np.float32).reshape(2, 1) input2 = np.array([3, 7], dtype=np.float32).reshape(2, 1) out_tensor = Tensor("float32", Shape([2, 1])) assert func.evaluate([out_tensor], [Tensor(input1), Tensor(input2)]) assert np.allclose(out_tensor.data, np.array([5, 8]).reshape(2, 1))
def test_gather_elements(): indices_type = np.int32 data_dtype = np.float32 data = ov.parameter(Shape([2, 5]), dtype=data_dtype, name="data") indices = ov.parameter(Shape([2, 100]), dtype=indices_type, name="indices") axis = 1 expected_shape = [2, 100] node = ov.gather_elements(data, indices, axis) assert node.get_type_name() == "GatherElements" assert node.get_output_size() == 1 assert list(node.get_output_shape(0)) == expected_shape assert node.get_output_element_type(0) == Type.f32
def test_validate_nodes_and_infer_types(): param1 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data1") param2 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data2") add = ops.add(param1, param2) func = Function(add, [param1, param2], "TestFunction") invalid_shape = Shape([3, 7]) param3 = ops.parameter(invalid_shape, dtype=np.float32, name="data3") func.replace_parameter(0, param3) with pytest.raises(RuntimeError) as e: func.validate_nodes_and_infer_types() assert "Argument shapes are inconsistent" in str(e.value)
def test_reshape(): element_type = Type.f32 shape = Shape([2, 3]) A = Parameter(element_type, shape) parameter_list = [A] function = Function([ov.reshape(A, Shape([3, 2]), special_zero=False)], parameter_list, "test") runtime = get_runtime() computation = runtime.computation(function, *parameter_list) result = computation(np.array(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32), dtype=np.float32))[0] expected = np.reshape(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32), (3, 2)) assert np.allclose(result, expected)
def test_evaluate_invalid_input_shape(): param1 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data1") param2 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data2") add = ops.add(param1, param2) func = Function(add, [param1, param2], "TestFunction") with pytest.raises(RuntimeError) as e: assert func.evaluate( [Tensor("float32", Shape([2, 1]))], [ Tensor("float32", Shape([3, 1])), Tensor("float32", Shape([3, 1])) ], ) assert "must be compatible with the partial shape: {2,1}" in str(e.value)
def test_node_input(): shape = [2, 2] parameter_a = ops.parameter(shape, dtype=np.float32, name="A") parameter_b = ops.parameter(shape, dtype=np.float32, name="B") model = parameter_a + parameter_b model_inputs = model.inputs() assert len(model_inputs) == 2 assert [input_node.get_index() for input_node in model_inputs] == [0, 1] assert np.equal( [input_node.get_element_type() for input_node in model_inputs], model.get_element_type(), ).all() assert np.equal( [input_node.get_shape() for input_node in model_inputs], Shape(shape) ).all() assert np.equal( [input_node.get_partial_shape() for input_node in model_inputs], PartialShape(shape), ).all() input0 = model.input(0) input1 = model.input(1) assert [input0.get_index(), input1.get_index()] == [0, 1]
def test_output_shape(device): core = Core() func = core.read_model(model=test_net_xml, weights=test_net_bin) exec_net = core.compile_model(func, device) output = exec_net.output(0) expected_shape = Shape([1, 10]) assert str(output.get_shape()) == str(expected_shape)
def test_const_output_get_shape(device): core = Core() func = core.read_model(model=test_net_xml, weights=test_net_bin) exec_net = core.compile_model(func, device) node = exec_net.input("data") expected_shape = Shape([1, 3, 32, 32]) assert str(node.get_shape()) == str(expected_shape)
def test_node_output(): input_array = np.array([0, 1, 2, 3, 4, 5]) splits = 3 expected_shape = len(input_array) // splits input_tensor = ops.constant(input_array, dtype=np.int32) axis = ops.constant(0, dtype=np.int64) split_node = ops.split(input_tensor, axis, splits) split_node_outputs = split_node.outputs() assert len(split_node_outputs) == splits assert [output_node.get_index() for output_node in split_node_outputs] == [0, 1, 2] assert np.equal( [output_node.get_element_type() for output_node in split_node_outputs], input_tensor.get_element_type(), ).all() assert np.equal( [output_node.get_shape() for output_node in split_node_outputs], Shape([expected_shape]), ).all() assert np.equal( [output_node.get_partial_shape() for output_node in split_node_outputs], PartialShape([expected_shape]), ).all() output0 = split_node.output(0) output1 = split_node.output(1) output2 = split_node.output(2) assert [output0.get_index(), output1.get_index(), output2.get_index()] == [0, 1, 2]
def test_hsigmoid(): float_dtype = np.float32 data = ov.parameter(Shape([3, 10]), dtype=float_dtype, name="data") node = ov.hsigmoid(data) assert node.get_type_name() == "HSigmoid" assert node.get_output_size() == 1 assert list(node.get_output_shape(0)) == [3, 10] assert node.get_output_element_type(0) == Type.f32
def test_log_softmax(): float_dtype = np.float32 data = ov.parameter(Shape([3, 10]), dtype=float_dtype, name="data") node = ov.log_softmax(data, 1) assert node.get_type_name() == "LogSoftmax" assert node.get_output_size() == 1 assert list(node.get_output_shape(0)) == [3, 10] assert node.get_output_element_type(0) == Type.f32
def test_select(): element_type = Type.f32 A = Parameter(Type.boolean, Shape([1, 2])) B = Parameter(element_type, Shape([1, 2])) C = Parameter(element_type, Shape([1, 2])) parameter_list = [A, B, C] function = Function([ov.select(A, B, C)], parameter_list, "test") runtime = get_runtime() computation = runtime.computation(function, *parameter_list) result = computation( np.array([[True, False]], dtype=np.bool), np.array([[5, 6]], dtype=np.float32), np.array([[7, 8]], dtype=np.float32), )[0] expected = np.array([[5, 8]]) assert np.allclose(result, expected)
def test_create_IENetwork_from_nGraph(): element_type = Type.f32 param = Parameter(element_type, Shape([1, 3, 22, 22])) relu = ov.relu(param) func = Function([relu], [param], "test") cnnNetwork = IENetwork(func) assert cnnNetwork is not None func2 = cnnNetwork.get_function() assert func2 is not None assert len(func2.get_ops()) == 3
def test_constant(): element_type = Type.f32 parameter_list = [] function = Function([Constant(element_type, Shape([3, 3]), list(range(9)))], parameter_list, "test") runtime = get_runtime() computation = runtime.computation(function, *parameter_list) result = computation()[0] expected = np.arange(9).reshape(3, 3) assert np.allclose(result, expected)
def test_concat(): element_type = Type.f32 A = Parameter(element_type, Shape([1, 2])) B = Parameter(element_type, Shape([1, 2])) C = Parameter(element_type, Shape([1, 2])) parameter_list = [A, B, C] axis = 0 function = Function([ov.concat([A, B, C], axis)], parameter_list, "test") a_arr = np.array([[1, 2]], dtype=np.float32) b_arr = np.array([[5, 6]], dtype=np.float32) c_arr = np.array([[7, 8]], dtype=np.float32) runtime = get_runtime() computation = runtime.computation(function, *parameter_list) result = computation(a_arr, b_arr, c_arr)[0] expected = np.concatenate((a_arr, b_arr, c_arr), axis) assert np.allclose(result, expected)
def make_constant_node(value: NumericData, dtype: NumericType = None) -> Constant: """Return an ngraph Constant node with the specified value.""" ndarray = get_ndarray(value) if dtype: element_type = get_element_type(dtype) else: element_type = get_element_type(ndarray.dtype) return Constant(element_type, Shape(ndarray.shape), ndarray.flatten().tolist())
def test_partial_shape_equals(): ps1 = PartialShape.dynamic() ps2 = PartialShape.dynamic() assert ps1 == ps2 ps1 = PartialShape([1, 2, 3]) ps2 = PartialShape([1, 2, 3]) assert ps1 == ps2 shape = Shape([1, 2, 3]) ps = PartialShape([1, 2, 3]) assert shape == ps
def test_broadcast(): element_type = Type.f32 A = Parameter(element_type, Shape([3])) parameter_list = [A] function = Function([ov.broadcast(A, [3, 3])], parameter_list, "test") runtime = get_runtime() computation = runtime.computation(function, *parameter_list) result = computation(np.array([1, 2, 3], dtype=np.float32))[0] a_arr = np.array([[0], [0], [0]], dtype=np.float32) b_arr = np.array([[1, 2, 3]], dtype=np.float32) expected = np.add(a_arr, b_arr) assert np.allclose(result, expected)
def test_round_away(): float_dtype = np.float32 data = ov.parameter(Shape([3, 10]), dtype=float_dtype, name="data") node = ov.round(data, "HALF_AWAY_FROM_ZERO") assert node.get_type_name() == "Round" assert node.get_output_size() == 1 assert list(node.get_output_shape(0)) == [3, 10] assert node.get_output_element_type(0) == Type.f32 input_tensor = np.array([-2.5, -1.5, -0.5, 0.5, 0.9, 1.5, 2.3, 2.5, 3.5], dtype=np.float32) expected = [-3.0, -2.0, -1.0, 1.0, 1.0, 2.0, 2.0, 3.0, 4.0] result = run_op_node([input_tensor], ov.round, "HALF_AWAY_FROM_ZERO") assert np.allclose(result, expected)
def unary_op_exec(op_str, input_list): """ input_list needs to have deep length of 4 """ element_type = Type.f32 shape = Shape(np.array(input_list).shape) A = Parameter(element_type, shape) parameter_list = [A] function = Function([unary_op(op_str, A)], parameter_list, "test") runtime = get_runtime() computation = runtime.computation(function, *parameter_list) result = computation(np.array(input_list, dtype=np.float32))[0] expected = unary_op_ref(op_str, np.array(input_list, dtype=np.float32)) assert np.allclose(result, expected)
def binary_op_comparison(op_str): element_type = Type.f32 shape = Shape([2, 2]) A = Parameter(element_type, shape) B = Parameter(element_type, shape) parameter_list = [A, B] function = Function([binary_op(op_str, A, B)], parameter_list, "test") a_arr = np.array([[1, 5], [3, 2]], dtype=np.float32) b_arr = np.array([[2, 4], [3, 1]], dtype=np.float32) runtime = get_runtime() computation = runtime.computation(function, A, B) result = computation(a_arr, b_arr)[0] expected = binary_op_ref(op_str, a_arr, b_arr) assert np.allclose(result, expected)
def roi_pooling( input: NodeInput, coords: NodeInput, output_size: TensorShape, spatial_scale: NumericData, method: str, name: Optional[str] = None, ) -> Node: """Return a node which produces an ROIPooling operation. @param input: Input feature map {N, C, ...} @param coords: Coordinates of bounding boxes @param output_size: Height/Width of ROI output features (shape) @param spatial_scale: Ratio of input feature map over input image size (float) @param method: Method of pooling - string: "max" or "bilinear" @return ROIPooling node """ method = method.lower() return _get_node_factory_opset2().create( "ROIPooling", as_nodes(input, coords), {"output_size": Shape(output_size), "spatial_scale": spatial_scale, "method": method}, )
def test_add_with_mul(): element_type = Type.f32 shape = Shape([4]) A = Parameter(element_type, shape) B = Parameter(element_type, shape) C = Parameter(element_type, shape) parameter_list = [A, B, C] function = Function([ov.multiply(ov.add(A, B), C)], parameter_list, "test") runtime = get_runtime() computation = runtime.computation(function, A, B, C) result = computation( np.array([1, 2, 3, 4], dtype=np.float32), np.array([5, 6, 7, 8], dtype=np.float32), np.array([9, 10, 11, 12], dtype=np.float32), )[0] a_arr = np.array([1, 2, 3, 4], dtype=np.float32) b_arr = np.array([5, 6, 7, 8], dtype=np.float32) c_arr = np.array([9, 10, 11, 12], dtype=np.float32) result_arr_ref = (a_arr + b_arr) * c_arr assert np.allclose(result, result_arr_ref)
def test_partial_shape(): ps = PartialShape([1, 2, 3, 4]) assert ps.is_static assert not ps.is_dynamic assert ps.rank == 4 assert repr(ps) == "<PartialShape: {1,2,3,4}>" assert ps.get_dimension(0) == Dimension(1) assert ps.get_dimension(1) == Dimension(2) assert ps.get_dimension(2) == Dimension(3) assert ps.get_dimension(3) == Dimension(4) shape = Shape([1, 2, 3]) ps = PartialShape(shape) assert ps.is_static assert not ps.is_dynamic assert ps.all_non_negative assert ps.rank == 3 assert list(ps.get_shape()) == [1, 2, 3] assert list(ps.get_max_shape()) == [1, 2, 3] assert list(ps.get_min_shape()) == [1, 2, 3] assert list(ps.to_shape()) == [1, 2, 3] assert repr(shape) == "<Shape: {1, 2, 3}>" assert repr(ps) == "<PartialShape: {1,2,3}>" ps = PartialShape( [Dimension(1), Dimension(2), Dimension(3), Dimension.dynamic()]) assert not ps.is_static assert ps.is_dynamic assert ps.all_non_negative assert ps.rank == 4 assert list(ps.get_min_shape()) == [1, 2, 3, 0] assert list(ps.get_max_shape())[3] > 1000000000 assert repr(ps) == "<PartialShape: {1,2,3,?}>" assert ps.get_dimension(0) == Dimension(1) assert ps.get_dimension(1) == Dimension(2) assert ps.get_dimension(2) == Dimension(3) assert ps.get_dimension(3) == Dimension.dynamic() ps = PartialShape([1, 2, 3, -1]) assert not ps.is_static assert ps.is_dynamic assert ps.all_non_negative assert ps.rank == 4 assert list(ps.get_min_shape()) == [1, 2, 3, 0] assert list(ps.get_max_shape())[3] > 1000000000 assert repr(ps) == "<PartialShape: {1,2,3,?}>" ps = PartialShape.dynamic() assert not ps.is_static assert ps.is_dynamic assert ps.rank == Dimension.dynamic() assert list(ps.get_min_shape()) == [] assert list(ps.get_max_shape()) == [] assert repr(ps) == "<PartialShape: ?>" ps = PartialShape.dynamic(r=Dimension(2)) assert not ps.is_static assert ps.is_dynamic assert ps.rank == 2 assert 2 == ps.rank assert list(ps.get_min_shape()) == [0, 0] assert list(ps.get_max_shape())[0] > 1000000000 assert repr(ps) == "<PartialShape: {?,?}>"