|
| 1 | +/* |
| 2 | +// Copyright (C) 2021 Intel Corporation |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +#include "models/deblurring_model.h" |
| 18 | +#include "utils/ocv_common.hpp" |
| 19 | +#include <utils/slog.hpp> |
| 20 | + |
| 21 | +using namespace InferenceEngine; |
| 22 | + |
| 23 | +DeblurringModel::DeblurringModel(const std::string& modelFileName, const cv::Size& inputImgSize) : |
| 24 | + ImageModel(modelFileName, false) { |
| 25 | + netInputHeight = inputImgSize.height; |
| 26 | + netInputWidth = inputImgSize.width; |
| 27 | +} |
| 28 | + |
| 29 | +void DeblurringModel::prepareInputsOutputs(CNNNetwork& cnnNetwork) { |
| 30 | + // --------------------------- Configure input & output ------------------------------------------------- |
| 31 | + // --------------------------- Prepare input blobs ------------------------------------------------------ |
| 32 | + changeInputSize(cnnNetwork); |
| 33 | + |
| 34 | + ICNNNetwork::InputShapes inputShapes = cnnNetwork.getInputShapes(); |
| 35 | + if (inputShapes.size() != 1) |
| 36 | + throw std::runtime_error("Demo supports topologies only with 1 input"); |
| 37 | + inputsNames.push_back(inputShapes.begin()->first); |
| 38 | + SizeVector& inSizeVector = inputShapes.begin()->second; |
| 39 | + if (inSizeVector.size() != 4 || inSizeVector[0] != 1 || inSizeVector[1] != 3) |
| 40 | + throw std::runtime_error("3-channel 4-dimensional model's input is expected"); |
| 41 | + InputInfo& inputInfo = *cnnNetwork.getInputsInfo().begin()->second; |
| 42 | + inputInfo.setPrecision(Precision::U8); |
| 43 | + |
| 44 | + // --------------------------- Prepare output blobs ----------------------------------------------------- |
| 45 | + const OutputsDataMap& outputInfo = cnnNetwork.getOutputsInfo(); |
| 46 | + if (outputInfo.size() != 1) |
| 47 | + throw std::runtime_error("Demo supports topologies only with 1 output"); |
| 48 | + |
| 49 | + outputsNames.push_back(outputInfo.begin()->first); |
| 50 | + Data& data = *outputInfo.begin()->second; |
| 51 | + data.setPrecision(Precision::FP32); |
| 52 | + const SizeVector& outSizeVector = data.getTensorDesc().getDims(); |
| 53 | + if (outSizeVector.size() != 4 || outSizeVector[0] != 1 || outSizeVector[1] != 3) |
| 54 | + throw std::runtime_error("3-channel 4-dimensional model's output is expected"); |
| 55 | +} |
| 56 | + |
| 57 | +void DeblurringModel::changeInputSize(CNNNetwork& cnnNetwork) { |
| 58 | + ICNNNetwork::InputShapes inputShapes = cnnNetwork.getInputShapes(); |
| 59 | + SizeVector& inputDims = inputShapes.begin()->second; |
| 60 | + |
| 61 | + if (inputDims[2] % stride || inputDims[3] % stride) |
| 62 | + throw std::runtime_error("The shape of the model input must be divisible by stride"); |
| 63 | + |
| 64 | + netInputHeight = static_cast<int>((netInputHeight + stride - 1) / stride) * stride; |
| 65 | + netInputWidth = static_cast<int>((netInputWidth + stride - 1) / stride) * stride; |
| 66 | + |
| 67 | + inputDims[0] = 1; |
| 68 | + inputDims[2] = netInputHeight; |
| 69 | + inputDims[3] = netInputWidth; |
| 70 | + |
| 71 | + cnnNetwork.reshape(inputShapes); |
| 72 | +} |
| 73 | + |
| 74 | +std::shared_ptr<InternalModelData> DeblurringModel::preprocess(const InputData& inputData, InferRequest::Ptr& request) { |
| 75 | + auto& image = inputData.asRef<ImageInputData>().inputImage; |
| 76 | + size_t h = image.rows; |
| 77 | + size_t w = image.cols; |
| 78 | + cv::Mat resizedImage; |
| 79 | + |
| 80 | + if (netInputHeight - stride < h && h <= netInputHeight |
| 81 | + && netInputWidth - stride < w && w <= netInputWidth) { |
| 82 | + int bottom = netInputHeight - h; |
| 83 | + int right = netInputWidth - w; |
| 84 | + cv::copyMakeBorder(image, resizedImage, 0, bottom, 0, right, |
| 85 | + cv::BORDER_CONSTANT, 0); |
| 86 | + } else { |
| 87 | + slog::warn << "Chosen model aspect ratio doesn't match image aspect ratio\n"; |
| 88 | + cv::resize(image, resizedImage, cv::Size(netInputWidth, netInputHeight)); |
| 89 | + } |
| 90 | + Blob::Ptr frameBlob = request->GetBlob(inputsNames[0]); |
| 91 | + matU8ToBlob<uint8_t>(resizedImage, frameBlob); |
| 92 | + |
| 93 | + return std::make_shared<InternalImageModelData>(image.cols, image.rows); |
| 94 | +} |
| 95 | + |
| 96 | +std::unique_ptr<ResultBase> DeblurringModel::postprocess(InferenceResult& infResult) { |
| 97 | + ImageResult* result = new ImageResult; |
| 98 | + *static_cast<ResultBase*>(result) = static_cast<ResultBase&>(infResult); |
| 99 | + |
| 100 | + const auto& inputImgSize = infResult.internalModelData->asRef<InternalImageModelData>(); |
| 101 | + |
| 102 | + LockedMemory<const void> outMapped = infResult.getFirstOutputBlob()->rmap(); |
| 103 | + const auto outputData = outMapped.as<float*>(); |
| 104 | + |
| 105 | + std::vector<cv::Mat> imgPlanes; |
| 106 | + const SizeVector& outSizeVector = infResult.getFirstOutputBlob()->getTensorDesc().getDims(); |
| 107 | + size_t outHeight = (int)(outSizeVector[2]); |
| 108 | + size_t outWidth = (int)(outSizeVector[3]); |
| 109 | + size_t numOfPixels = outWidth * outHeight; |
| 110 | + imgPlanes = std::vector<cv::Mat>{ |
| 111 | + cv::Mat(outHeight, outWidth, CV_32FC1, &(outputData[0])), |
| 112 | + cv::Mat(outHeight, outWidth, CV_32FC1, &(outputData[numOfPixels])), |
| 113 | + cv::Mat(outHeight, outWidth, CV_32FC1, &(outputData[numOfPixels * 2]))}; |
| 114 | + cv::Mat resultImg; |
| 115 | + cv::merge(imgPlanes, resultImg); |
| 116 | + |
| 117 | + if (netInputHeight - stride < static_cast<size_t>(inputImgSize.inputImgHeight) && static_cast<size_t>(inputImgSize.inputImgHeight) <= netInputHeight |
| 118 | + && netInputWidth - stride < static_cast<size_t>(inputImgSize.inputImgWidth) && static_cast<size_t>(inputImgSize.inputImgWidth) <= netInputWidth) { |
| 119 | + result->resultImage = resultImg(cv::Rect(0, 0, inputImgSize.inputImgWidth, inputImgSize.inputImgHeight)); |
| 120 | + } else { |
| 121 | + cv::resize(resultImg, result->resultImage, cv::Size(inputImgSize.inputImgWidth, inputImgSize.inputImgHeight)); |
| 122 | + } |
| 123 | + |
| 124 | + result->resultImage.convertTo(result->resultImage, CV_8UC3, 255); |
| 125 | + |
| 126 | + return std::unique_ptr<ResultBase>(result); |
| 127 | +} |
0 commit comments