Skip to content

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

[core] ROI tensor allow set new shape with different rank (#29968)

Pawel Raaszd13563f5d9b4
diff --git a/src/core/tests/ov_tensor_test.cpp b/src/core/tests/ov_tensor_test.cpp
index 2a5c747a983..6b318428f0f 100644
--- a/src/core/tests/ov_tensor_test.cpp
+++ b/src/core/tests/ov_tensor_test.cpp
@@ -2,10 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
//
-#include <gmock/gmock-spec-builders.h>
#include <gmock/gmock.h>
-#include <gtest/gtest-param-test.h>
-#include <gtest/gtest.h>
#include <cstdint>
@@ -671,6 +668,28 @@ TEST_F(OVTensorTest, cannotSetShapeOnRoiTensor) {
ASSERT_THROW(roi_tensor.set_shape(newShape), ov::Exception);
}
+TEST_F(OVTensorTest, setShapeExpandRoiRank) {
+ ov::Tensor t{ov::element::i32, {16, 16}};
+ std::iota(t.data<int32_t>(), t.data<int32_t>() + t.get_size(), 1);
+ ov::Tensor roi_tensor{t, {0, 0}, {3, 3}};
+
+ EXPECT_NO_THROW(roi_tensor.set_shape({1, 1, 2}));
+ EXPECT_EQ(roi_tensor.get_shape(), ov::Shape({1, 1, 2}));
+ EXPECT_NO_THROW(roi_tensor.set_shape({1, 1, 2, 2}));
+ EXPECT_EQ(roi_tensor.get_shape(), ov::Shape({1, 1, 2, 2}));
+}
+
+TEST_F(OVTensorTest, setShapeInvalidExpandRoiRank) {
+ ov::Tensor t{ov::element::i32, {16, 16}};
+ std::iota(t.data<int32_t>(), t.data<int32_t>() + t.get_size(), 1);
+ ov::Tensor roi_tensor{t, {0, 0}, {2, 2}};
+
+ OV_EXPECT_THROW(roi_tensor.set_shape({1, 2, 1, 2, 2}), ov::Exception, _);
+ OV_EXPECT_THROW(roi_tensor.set_shape({2, 1, 1}), ov::Exception, _);
+ OV_EXPECT_THROW(roi_tensor.set_shape({2, 1, 1, 1}), ov::Exception, _);
+ OV_EXPECT_THROW(roi_tensor.set_shape({2}), ov::Exception, _);
+}
+
TEST_F(OVTensorTest, cannotSetShapeOnRoiStringTensor) {
ov::Tensor t{ov::element::string, {1, 3, 6, 5}}; // RGBp picture of size (WxH) = 5x6
ov::Tensor roi_tensor{t, {0, 0, 1, 2}, {1, 3, 5, 4}};
diff --git a/src/inference/src/dev/make_tensor.cpp b/src/inference/src/dev/make_tensor.cpp
index 4110f3c1510..5d5cb00fc76 100644
--- a/src/inference/src/dev/make_tensor.cpp
+++ b/src/inference/src/dev/make_tensor.cpp
@@ -359,16 +359,27 @@ public:
}
void set_shape(ov::Shape new_shape) {
- OPENVINO_ASSERT(new_shape.size() == m_shape.size());
- for (auto new_dim = new_shape.cbegin(), max_dim = m_capacity.cbegin(); new_dim != new_shape.cend();
+ OPENVINO_ASSERT(new_shape.size() >= m_shape.size());
+ const auto last_new_dim = new_shape.crend();
+ auto new_dim = new_shape.crbegin();
+ for (auto max_dim = m_capacity.crbegin(); new_dim != last_new_dim && max_dim != m_capacity.crend();
++max_dim, ++new_dim) {
OPENVINO_ASSERT(*new_dim <= *max_dim,
"Cannot set new shape: ",
new_shape,
- " for ROI tensor! Dimension: ",
- std::distance(new_shape.cbegin(), new_dim),
+ " for ROI tensor! New dimension at index: ",
+ std::distance(new_shape.cbegin(), new_dim.base()) - 1,
" is not compatible.");
}
+ new_dim = std::find_if(new_dim, last_new_dim, [](auto&& dim) {
+ return dim != 1;
+ });
+ OPENVINO_ASSERT(
+ new_dim == last_new_dim,
+ "Cannot set new shape: ",
+ new_shape,
+ " for ROI tensor! The expanding rank dimension(s) of ROI must be ones, but it is not at index: ",
+ std::distance(new_shape.cbegin(), new_dim.base()) - 1);
m_shape = std::move(new_shape);
}