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

demos/tests: enable retinaface-resnet50-pytorch.onnx #3823

Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions demos/common/cpp/models/src/detection_model_retinaface_pt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void ModelRetinaFacePT::prepareInputsOutputs(std::shared_ptr<ov::Model>& model)
throw std::logic_error("RetinaFacePT model wrapper expects models that have only 1 input");
}

const ov::Shape& inputShape = model->input().get_shape();
const ov::Shape& inputShape = model->input().get_partial_shape().get_max_shape();
const ov::Layout& inputLayout = getInputLayout(model->input());

if (inputShape[ov::layout::channels_idx(inputLayout)] != 3) {
Expand Down Expand Up @@ -94,7 +94,7 @@ void ModelRetinaFacePT::prepareInputsOutputs(std::shared_ptr<ov::Model>& model)
ppp.output(outTensorName)
.tensor()
.set_element_type(ov::element::f32)
.set_layout(output.get_shape().size() == 4 ? nchw : chw);
.set_layout(output.get_partial_shape().size() == 4 ? nchw : chw);

if (outTensorName.find("bbox") != std::string::npos) {
outputsNames[OUT_BOXES] = outTensorName;
Expand All @@ -106,7 +106,7 @@ void ModelRetinaFacePT::prepareInputsOutputs(std::shared_ptr<ov::Model>& model)
outputsNames.resize(std::max(outputsNames.size(), (size_t)OUT_LANDMARKS + 1));
outputsNames[OUT_LANDMARKS] = outTensorName;
landmarksNum =
output.get_shape()[ov::layout::width_idx(chw)] / 2; // Each landmark consist of 2 variables (x and y)
output.get_partial_shape()[ov::layout::width_idx(chw)].get_length() / 2; // Each landmark consist of 2 variables (x and y)
} else {
continue;
}
Expand Down
3 changes: 1 addition & 2 deletions demos/common/cpp/models/src/model_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ ov::CompiledModel ModelBase::compileModel(const ModelConfig& config, ov::Core& c
}

ov::Layout ModelBase::getInputLayout(const ov::Output<ov::Node>& input) {
const ov::Shape& inputShape = input.get_shape();
ov::Layout layout = ov::layout::get_layout(input);
if (layout.empty()) {
if (inputsLayouts.empty()) {
layout = getLayoutFromShape(inputShape);
layout = getLayoutFromShape(input.get_partial_shape());
slog::warn << "Automatically detected layout '" << layout.to_string() << "' for input '"
<< input.get_any_name() << "' will be used." << slog::endl;
} else if (inputsLayouts.size() == 1) {
Expand Down
3 changes: 1 addition & 2 deletions demos/common/cpp/models/src/segmentation_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ void SegmentationModel::prepareInputsOutputs(std::shared_ptr<ov::Model>& model)

const ov::Shape& outputShape = output.get_partial_shape().get_max_shape();

constexpr bool gues_layout = true;
ov::Layout outputLayout = getLayoutFromShape(outputShape, gues_layout);
ov::Layout outputLayout = getLayoutFromShape(outputShape);
outHeight = static_cast<int>(outputShape[ov::layout::height_idx(outputLayout)]);
outWidth = static_cast<int>(outputShape[ov::layout::width_idx(outputLayout)]);

Expand Down
31 changes: 14 additions & 17 deletions demos/common/cpp/utils/include/utils/ocv_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ static inline void resize2tensor(const cv::Mat& mat, const ov::Tensor& tensor) {
struct IntervalCondition {
using DimType = size_t;
using IndexType = size_t;
using ConditionChecker = std::function<bool(IndexType, const ov::Shape&)>;
using ConditionChecker = std::function<bool(IndexType, const ov::PartialShape&)>;

template<class Cond>
constexpr IntervalCondition(IndexType i1, IndexType i2, Cond c) :
impl([=](IndexType i0, const ov::Shape& shape) {
return c(shape[i0], shape[i1]) && c(shape[i0], shape[i2]);})
impl([=](IndexType i0, const ov::PartialShape& shape) {
return c(shape[i0].get_max_length(), shape[i1].get_max_length()) && c(shape[i0].get_max_length(), shape[i2].get_max_length());})
{}
bool operator() (IndexType i0, const ov::Shape& shape) const { return impl(i0, shape); }
bool operator() (IndexType i0, const ov::PartialShape& shape) const { return impl(i0, shape); }
private:
ConditionChecker impl;
};
Expand All @@ -132,7 +132,7 @@ IntervalCondition makeCond(Args&&...args) {
}
using LayoutCondition = std::tuple<size_t/*dim index*/, IntervalCondition, std::string>;

static inline std::tuple<bool, ov::Layout> makeGuesLayoutFrom4DShape(const ov::Shape& shape) {
static inline std::tuple<bool, ov::Layout> makeGuesLayoutFrom4DShape(const ov::PartialShape& shape) {
// at the moment we make assumption about NCHW & NHCW only
// if hypothetical C value is less than hypothetical H and W - then
// out assumption is correct and we pick a corresponding layout
Expand All @@ -151,7 +151,7 @@ static inline std::tuple<bool, ov::Layout> makeGuesLayoutFrom4DShape(const ov::S
return {false, ov::Layout{}};
}

static inline ov::Layout getLayoutFromShape(const ov::Shape& shape, bool gues_layout = false) {
static inline ov::Layout getLayoutFromShape(const ov::PartialShape& shape) {
if (shape.size() == 2) {
return "NC";
}
Expand All @@ -165,10 +165,10 @@ static inline ov::Layout getLayoutFromShape(const ov::Shape& shape, bool gues_la
throw std::runtime_error("Can't guess layout for " + shape.to_string());
}
if (shape.size() == 4) {
if (shape[1] >= 1 && shape[1] <= 4) {
if (ov::Interval{1, 4}.contains(shape[1].get_interval())) {
return "NCHW";
}
if (shape[3] >= 1 && shape[3] <= 4) {
if (ov::Interval{1, 4}.contains(shape[3].get_interval())) {
return "NHWC";
}
if (shape[1] == shape[2]) {
Expand All @@ -177,17 +177,14 @@ static inline ov::Layout getLayoutFromShape(const ov::Shape& shape, bool gues_la
if (shape[2] == shape[3]) {
return "NCHW";
}
if (gues_layout) {
bool guesResult = false;
ov::Layout guessedLayout;
std::tie(guesResult, guessedLayout) = makeGuesLayoutFrom4DShape(shape);
if (guesResult) {
return guessedLayout;
}
bool guesResult = false;
ov::Layout guessedLayout;
std::tie(guesResult, guessedLayout) = makeGuesLayoutFrom4DShape(shape);
if (guesResult) {
return guessedLayout;
}
}
throw std::runtime_error(std::string("Usupported " + std::to_string(shape.size()) + "D shape: ") +
shape.to_string());
throw std::runtime_error("Usupported " + std::to_string(shape.size()) + "D shape");
}

/**
Expand Down
6 changes: 4 additions & 2 deletions demos/tests/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,10 @@ def single_option_cases(key, *args):
TestCase(options={'-at': 'retinaface-pytorch'}),
[
TestCase(options={'-m': ModelArg('retinaface-resnet50-pytorch')}),
# TestCase(options={'-m': ModelFileArg('retinaface-resnet50-pytorch', 'retinaface-resnet50-pytorch.onnx'),
# '-mean_values': "104.0 117.0 123.0"}), dynamic bathc: get_shape was called on a descriptor::Tensor with dynamic shape
TestCase({
'-m': ModelFileArg('retinaface-resnet50-pytorch', 'retinaface-resnet50-pytorch.onnx'),
'-mean_values': "104.0 117.0 123.0",
}),
]
),
*combine_cases(
Expand Down