Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add throttled warning message about aspet ration in SR models
  • Loading branch information
sivanov-work committed Nov 3, 2023
commit d4e7e5fd7780e88814bb7fb7f0026c6f3e53e36d
2 changes: 2 additions & 0 deletions demos/common/cpp/models/include/models/image_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class ImageModel : public ModelBase {

size_t netInputHeight = 0;
size_t netInputWidth = 0;
size_t netOutputHeight = 0;
size_t netOutputWidth = 0;
cv::InterpolationFlags interpolationMode = cv::INTER_LINEAR;
RESIZE_MODE resizeMode = RESIZE_FILL;
};
34 changes: 26 additions & 8 deletions demos/common/cpp/models/src/super_resolution_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include "models/internal_model_data.h"
#include "models/results.h"

static constexpr unsigned log_throttle_interval_frames_count = 500;

SuperResolutionModel::SuperResolutionModel(const std::string& modelFileName,
const cv::Size& inputImgSize,
const std::string& layout)
Expand Down Expand Up @@ -106,9 +108,10 @@ void SuperResolutionModel::prepareInputsOutputs(std::shared_ptr<ov::Model>& mode
const ov::Shape& outShape = model->output().get_shape();

const ov::Layout outputLayout("NCHW");
const auto outWidth = outShape[ov::layout::width_idx(outputLayout)];
netOutputWidth = outShape[ov::layout::width_idx(outputLayout)];
netOutputHeight = outShape[ov::layout::height_idx(outputLayout)];
const auto inWidth = lrShape[ov::layout::width_idx(outputLayout)];
changeInputSize(model, static_cast<int>(outWidth / inWidth));
changeInputSize(model, static_cast<int>(netOutputWidth / inWidth));
}

void SuperResolutionModel::changeInputSize(std::shared_ptr<ov::Model>& model, int coeff) {
Expand Down Expand Up @@ -155,8 +158,15 @@ std::shared_ptr<InternalModelData> SuperResolutionModel::preprocess(const InputD
cv::cvtColor(img, img, cv::COLOR_BGR2GRAY);
}

if (static_cast<size_t>(img.cols) != netInputWidth || static_cast<size_t>(img.rows) != netInputHeight) {
slog::warn << "\tChosen model aspect ratio doesn't match image aspect ratio" << slog::endl;
if (static_cast<size_t>(img.cols) != netInputWidth || static_cast<size_t>(img.rows) != netInputHeight ||
!is_aspect_ratio_equal(std::make_tuple(img.cols, img.rows),
std::make_tuple(netOutputWidth, netOutputHeight))) {
static unsigned counter = 0;
if (counter++ % log_throttle_interval_frames_count == 0) {
slog::warn << "\tChosen model aspect ratio for resolution: " << netOutputWidth << "x" << netOutputHeight
<< " doesn't match initial image aspect ratio for resolution: " << img.cols << "x" << img.rows
<< ". You may observe video disproportions. To avoid this please use a suitable model" << slog::endl;
}
}
const size_t height = lrInputTensor.get_shape()[ov::layout::height_idx(layout)];
const size_t width = lrInputTensor.get_shape()[ov::layout::width_idx(layout)];
Expand Down Expand Up @@ -237,8 +247,15 @@ std::shared_ptr<InternalModelData> SuperResolutionChannelJoint::preprocess(const
const ov::Tensor lrInputTensor = request.get_tensor(inputsNames[0]);
const ov::Layout layout("NCHW");

if (static_cast<size_t>(img.cols) != netInputWidth || static_cast<size_t>(img.rows) != netInputHeight) {
slog::warn << "\tChosen model aspect ratio doesn't match image aspect ratio" << slog::endl;
if (static_cast<size_t>(img.cols) != netInputWidth || static_cast<size_t>(img.rows) != netInputHeight ||
!is_aspect_ratio_equal(std::make_tuple(img.cols, img.rows),
std::make_tuple(netOutputWidth, netOutputHeight))) {
static unsigned counter = 0;
if (counter++ % log_throttle_interval_frames_count == 0) {
slog::warn << "\tChosen model aspect ratio for resolution: " << netOutputWidth << "x" << netOutputHeight
<< " doesn't match initial image aspect ratio for resolution: " << img.cols << "x" << img.rows
<< ". You may observe video disproportions. To avoid this please use a suitable model" << slog::endl;
}
}

const size_t height = lrInputTensor.get_shape()[ov::layout::height_idx(layout)];
Expand Down Expand Up @@ -297,10 +314,11 @@ void SuperResolutionChannelJoint::prepareInputsOutputs(std::shared_ptr<ov::Model
const ov::Shape& outShape = model->output().get_shape();

const ov::Layout outputLayout("NCHW");
const auto outWidth = outShape[ov::layout::width_idx(outputLayout)];
netOutputWidth = outShape[ov::layout::width_idx(outputLayout)];
netOutputHeight = outShape[ov::layout::height_idx(outputLayout)];
const auto inWidth = lrShape[ov::layout::width_idx(outputLayout)];

changeInputSize(model, static_cast<int>(outWidth / inWidth));
changeInputSize(model, static_cast<int>(netOutputWidth / inWidth));

ov::set_batch(model, 3);
}
Expand Down
4 changes: 4 additions & 0 deletions demos/common/cpp/utils/include/utils/image_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#pragma once
#include <tuple>

#include <opencv2/opencv.hpp>

Expand All @@ -27,3 +28,6 @@ enum RESIZE_MODE {
cv::Mat resizeImageExt(const cv::Mat& mat, int width, int height, RESIZE_MODE resizeMode = RESIZE_FILL,
cv::InterpolationFlags interpolationMode = cv::INTER_LINEAR, cv::Rect* roi = nullptr,
cv::Scalar BorderConstant = cv::Scalar(0, 0, 0));


bool is_aspect_ratio_equal(const std::tuple<int, int> &lhs_res,const std::tuple<int, int> &rhs_res);
6 changes: 6 additions & 0 deletions demos/common/cpp/utils/src/image_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ cv::Mat resizeImageExt(const cv::Mat& mat, int width, int height, RESIZE_MODE re
}
return dst;
}

bool is_aspect_ratio_equal(const std::tuple<int, int> &lhs_res, const std::tuple<int, int> &rhs_res) {
float leftAspectRation = std::get<0>(lhs_res) / static_cast<float>(std::get<1>(lhs_res));
float rightAspectRation = std::get<0>(rhs_res) / static_cast<float>(std::get<1>(rhs_res));
return fabs(leftAspectRation - rightAspectRation) <= std::numeric_limits<float>::epsilon();
}