Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[PyOV] small improvements (#29404)

Anastasia Kuporosovab1a07c7682fd
diff --git a/src/bindings/python/src/pyopenvino/core/tensor.cpp b/src/bindings/python/src/pyopenvino/core/tensor.cpp
index 782de265ea0..3393062c19c 100644
--- a/src/bindings/python/src/pyopenvino/core/tensor.cpp
+++ b/src/bindings/python/src/pyopenvino/core/tensor.cpp
@@ -216,7 +216,8 @@ void regclass_Tensor(py::module m) {
cls.def(py::init([](py::object& image) {
if (!py::isinstance(image, py::module::import("PIL.Image").attr("Image"))) {
- throw py::type_error("Input must be a PIL.Image.Image object");
+ throw py::type_error(
+ "Input argument must be a PIL.Image.Image/numpy.array/List[int, float, str] object");
}
auto numpy = py::module::import("numpy");
py::array np_array = numpy.attr("array")(image);
@@ -225,19 +226,19 @@ void regclass_Tensor(py::module m) {
}),
py::arg("image"),
R"(
- Constructs Tensor from a Pillow Image.
+ Constructs Tensor from a Pillow Image.
- :param image: Pillow Image to create the tensor from.
- :type image: PIL.Image.Image
- :Example:
- .. code-block:: python
+ :param image: Pillow Image to create the tensor from.
+ :type image: PIL.Image.Image
+ :Example:
+ .. code-block:: python
- from PIL import Image
- import openvino as ov
+ from PIL import Image
+ import openvino as ov
- img = Image.open("example.jpg")
- tensor = ov.Tensor(img)
- )");
+ img = Image.open("example.jpg")
+ tensor = ov.Tensor(img)
+ )");
cls.def("get_element_type",
&ov::Tensor::get_element_type,
diff --git a/src/bindings/python/src/pyopenvino/graph/op.cpp b/src/bindings/python/src/pyopenvino/graph/op.cpp
index f648b42018d..2068eb0ee16 100644
--- a/src/bindings/python/src/pyopenvino/graph/op.cpp
+++ b/src/bindings/python/src/pyopenvino/graph/op.cpp
@@ -53,7 +53,17 @@ bool PyOp::evaluate(ov::TensorVector& output_values, const ov::TensorVector& inp
}
bool PyOp::has_evaluate() const {
- PYBIND11_OVERRIDE(bool, ov::op::Op, has_evaluate);
+ py::gil_scoped_acquire gil; // Acquire the GIL while in this scope.
+ // Try to look up the overridden method on the Python side.
+ py::function overrided_py_method = pybind11::get_override(this, "has_evaluate");
+ if (overrided_py_method) { // method is found
+ return static_cast<py::bool_>(overrided_py_method()); // Call the Python function.
+ }
+ py::function overrided_evaluate_method = pybind11::get_override(this, "evaluate");
+ if (overrided_evaluate_method) {
+ return true;
+ }
+ return false;
}
void PyOp::update_type_info() {
diff --git a/src/bindings/python/tests/test_graph/test_custom_op.py b/src/bindings/python/tests/test_graph/test_custom_op.py
index a662c374956..3f40f49b05b 100644
--- a/src/bindings/python/tests/test_graph/test_custom_op.py
+++ b/src/bindings/python/tests/test_graph/test_custom_op.py
@@ -106,6 +106,10 @@ class CustomOpWithAttribute(Op):
visitor.on_attributes(self._attrs)
return True
+ def evaluate(self, outputs, inputs):
+ inputs[0].copy_to(outputs[0])
+ return True
+
# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#request
@pytest.fixture
@@ -156,6 +160,14 @@ def test_visit_attributes_custom_op(prepared_paths, attributes, expectation, rai
if e is not None:
assert raise_msg in str(e.value)
+ input_data = np.ones([2, 1], dtype=np.float32)
+ expected_output = np.maximum(0.0, input_data)
+
+ compiled_model = compile_model(model_with_op_attr)
+ input_tensor = Tensor(input_data)
+ results = compiled_model({"data1": input_tensor})
+ assert np.allclose(results[list(results)[0]], expected_output, 1e-4, 1e-4)
+
def test_custom_add_op():
data1 = np.array([1, 2, 3])