|
| 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/style_transfer_model.h" |
| 18 | + |
| 19 | +#include "utils/ocv_common.hpp" |
| 20 | +#include <utils/slog.hpp> |
| 21 | + |
| 22 | +#include <string> |
| 23 | +#include <vector> |
| 24 | +#include <memory> |
| 25 | + |
| 26 | +using namespace InferenceEngine; |
| 27 | + |
| 28 | +StyleTransferModel::StyleTransferModel(const std::string& modelFileName) : |
| 29 | + ImageModel(modelFileName, false) { |
| 30 | +} |
| 31 | + |
| 32 | +void StyleTransferModel::prepareInputsOutputs(InferenceEngine::CNNNetwork& cnnNetwork) { |
| 33 | + // --------------------------- Configure input & output --------------------------------------------- |
| 34 | + // --------------------------- Prepare input blobs -------------------------------------------------- |
| 35 | + |
| 36 | + ICNNNetwork::InputShapes inputShapes = cnnNetwork.getInputShapes(); |
| 37 | + if (inputShapes.size() != 1) |
| 38 | + throw std::runtime_error("Demo supports topologies only with 1 input"); |
| 39 | + inputsNames.push_back(inputShapes.begin()->first); |
| 40 | + SizeVector& inSizeVector = inputShapes.begin()->second; |
| 41 | + if (inSizeVector.size() != 4 || inSizeVector[0] != 1 || inSizeVector[1] != 3) |
| 42 | + throw std::runtime_error("3-channel 4-dimensional model's input is expected"); |
| 43 | + InputInfo& inputInfo = *cnnNetwork.getInputsInfo().begin()->second; |
| 44 | + inputInfo.setPrecision(Precision::FP32); |
| 45 | + |
| 46 | + // --------------------------- Prepare output blobs ----------------------------------------------------- |
| 47 | + const OutputsDataMap& outputInfo = cnnNetwork.getOutputsInfo(); |
| 48 | + if (outputInfo.size() != 1) |
| 49 | + throw std::runtime_error("Demo supports topologies only with 1 output"); |
| 50 | + |
| 51 | + outputsNames.push_back(outputInfo.begin()->first); |
| 52 | + Data& data = *outputInfo.begin()->second; |
| 53 | + data.setPrecision(Precision::FP32); |
| 54 | + const SizeVector& outSizeVector = data.getTensorDesc().getDims(); |
| 55 | + if (outSizeVector.size() != 4 || outSizeVector[0] != 1 || outSizeVector[1] != 3) |
| 56 | + throw std::runtime_error("3-channel 4-dimensional model's output is expected"); |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +std::shared_ptr<InternalModelData> StyleTransferModel::preprocess(const InputData& inputData, InferenceEngine::InferRequest::Ptr& request) { |
| 61 | + auto imgData = inputData.asRef<ImageInputData>(); |
| 62 | + auto& img = imgData.inputImage; |
| 63 | + |
| 64 | + Blob::Ptr minput = request->GetBlob(inputsNames[0]); |
| 65 | + matToBlob(img, minput); |
| 66 | + return std::make_shared<InternalImageModelData>(img.cols, img.rows); |
| 67 | +} |
| 68 | + |
| 69 | +std::unique_ptr<ResultBase> StyleTransferModel::postprocess(InferenceResult& infResult) { |
| 70 | + |
| 71 | + ImageResult* result = new ImageResult; |
| 72 | + *static_cast<ResultBase*>(result) = static_cast<ResultBase&>(infResult); |
| 73 | + |
| 74 | + const auto& inputImgSize = infResult.internalModelData->asRef<InternalImageModelData>(); |
| 75 | + |
| 76 | + LockedMemory<const void> outMapped = infResult.getFirstOutputBlob()->rmap(); |
| 77 | + const auto outputData = outMapped.as<float*>(); |
| 78 | + |
| 79 | + const SizeVector& outSizeVector = infResult.getFirstOutputBlob()->getTensorDesc().getDims(); |
| 80 | + size_t outHeight = (int)(outSizeVector[2]); |
| 81 | + size_t outWidth = (int)(outSizeVector[3]); |
| 82 | + size_t numOfPixels = outWidth * outHeight; |
| 83 | + |
| 84 | + std::vector<cv::Mat> imgPlanes; |
| 85 | + imgPlanes = std::vector<cv::Mat>{ |
| 86 | + cv::Mat(outHeight, outWidth, CV_32FC1, &(outputData[numOfPixels * 2])), |
| 87 | + cv::Mat(outHeight, outWidth, CV_32FC1, &(outputData[numOfPixels])), |
| 88 | + cv::Mat(outHeight, outWidth, CV_32FC1, &(outputData[0]))}; |
| 89 | + cv::Mat resultImg; |
| 90 | + cv::merge(imgPlanes, resultImg); |
| 91 | + cv::resize(resultImg, result->resultImage, cv::Size(inputImgSize.inputImgWidth, inputImgSize.inputImgHeight)); |
| 92 | + |
| 93 | + result->resultImage.convertTo(result->resultImage, CV_8UC3); |
| 94 | + |
| 95 | + return std::unique_ptr<ResultBase>(result); |
| 96 | +} |
0 commit comments