|
| 1 | +// This file is part of OpenCV project. |
| 2 | +// It is subject to the license terms in the LICENSE file found in the top-level directory |
| 3 | +// of this distribution and at http://opencv.org/license.html. |
| 4 | + |
| 5 | +// Copyright (C) 2020, Intel Corporation, all rights reserved. |
| 6 | +// Third party copyrights are property of their respective owners. |
| 7 | + |
| 8 | +#include "../precomp.hpp" |
| 9 | +#include "layers_common.hpp" |
| 10 | + |
| 11 | + |
| 12 | +namespace cv { namespace dnn { |
| 13 | + |
| 14 | +class AccumLayerImpl CV_FINAL : public AccumLayer |
| 15 | +{ |
| 16 | +public: |
| 17 | + AccumLayerImpl(const LayerParams& params) |
| 18 | + { |
| 19 | + setParamsFrom(params); |
| 20 | + top_height = params.get<int>("top_height", 0); |
| 21 | + top_width = params.get<int>("top_width", 0); |
| 22 | + divisor = params.get<int>("size_divisible_by", 0); |
| 23 | + have_reference = params.get<String>("have_reference", "false") == "true"; |
| 24 | + } |
| 25 | + |
| 26 | + virtual bool getMemoryShapes(const std::vector<MatShape> &inputs, |
| 27 | + const int requiredOutputs, |
| 28 | + std::vector<MatShape> &outputs, |
| 29 | + std::vector<MatShape> &internals) const CV_OVERRIDE |
| 30 | + { |
| 31 | + std::vector<int> outShape; |
| 32 | + int batch = inputs[0][0]; |
| 33 | + outShape.push_back(batch); |
| 34 | + |
| 35 | + if (have_reference) |
| 36 | + { |
| 37 | + CV_Assert(inputs.size() >= 2); |
| 38 | + int totalchannels = 0; |
| 39 | + for (int i = 0; i < inputs.size() - 1; i++) { |
| 40 | + CV_Assert(inputs[i][0] == batch); |
| 41 | + totalchannels += inputs[i][1]; |
| 42 | + } |
| 43 | + outShape.push_back(totalchannels); |
| 44 | + |
| 45 | + int height = inputs.back()[2]; |
| 46 | + int width = inputs.back()[3]; |
| 47 | + |
| 48 | + outShape.push_back(height); |
| 49 | + outShape.push_back(width); |
| 50 | + } |
| 51 | + else |
| 52 | + { |
| 53 | + int maxwidth = -1; |
| 54 | + int maxheight = -1; |
| 55 | + int totalchannels = 0; |
| 56 | + |
| 57 | + // Find largest blob size and count total channels |
| 58 | + for (int i = 0; i < inputs.size(); ++i) |
| 59 | + { |
| 60 | + totalchannels += inputs[i][1]; |
| 61 | + maxheight = std::max(maxheight, inputs[i][2]); |
| 62 | + maxwidth = std::max(maxwidth, inputs[i][3]); |
| 63 | + CV_Assert(inputs[i][0] == batch); |
| 64 | + } |
| 65 | + outShape.push_back(totalchannels); |
| 66 | + |
| 67 | + int out_h = divisor ? static_cast<int>(ceil(maxheight / divisor) * divisor) : top_height; |
| 68 | + int out_w = divisor ? static_cast<int>(ceil(maxwidth / divisor) * divisor) : top_width; |
| 69 | + |
| 70 | + // Layer can specify custom top size which is larger than default |
| 71 | + if (out_h <= maxheight || out_w <= maxwidth) |
| 72 | + { |
| 73 | + out_h = maxheight; |
| 74 | + out_w = maxwidth; |
| 75 | + } |
| 76 | + |
| 77 | + outShape.push_back(out_h); |
| 78 | + outShape.push_back(out_w); |
| 79 | + } |
| 80 | + |
| 81 | + outputs.assign(1, outShape); |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + virtual void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr) CV_OVERRIDE |
| 86 | + { |
| 87 | + LayerParams resizeParams; |
| 88 | + resizeParams.set("interpolation", "bilinear"); |
| 89 | + resizeParams.set("align_corners", true); |
| 90 | + resize = ResizeLayer::create(resizeParams); |
| 91 | + } |
| 92 | + |
| 93 | + void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE |
| 94 | + { |
| 95 | + CV_TRACE_FUNCTION(); |
| 96 | + CV_TRACE_ARG_VALUE(name, "name", name.c_str()); |
| 97 | + |
| 98 | + std::vector<Mat> inputs, outputs; |
| 99 | + inputs_arr.getMatVector(inputs); |
| 100 | + outputs_arr.getMatVector(outputs); |
| 101 | + |
| 102 | + const int out_h = outputs[0].size[2]; |
| 103 | + const int out_w = outputs[0].size[3]; |
| 104 | + float* out_data = outputs[0].ptr<float>(); |
| 105 | + std::vector<int> sizes(&outputs[0].size[0], &outputs[0].size[0] + outputs[0].size.dims()); |
| 106 | + for (int i = 0; i < inputs.size() - have_reference; i++) |
| 107 | + { |
| 108 | + sizes[1] = inputs[i].size[1]; |
| 109 | + Mat outSlice(sizes, CV_32F, out_data); |
| 110 | + |
| 111 | + if (out_h == inputs[i].size[2] && out_w == inputs[i].size[3]) |
| 112 | + { |
| 113 | + inputs[i].copyTo(outSlice); |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + std::vector<Mat> inp_slices, out_slices; |
| 118 | + inp_slices.push_back(inputs[i]); |
| 119 | + out_slices.push_back(outSlice); |
| 120 | + |
| 121 | + resize->finalize(inp_slices, out_slices); |
| 122 | + resize->forward(inp_slices, out_slices, internals_arr); |
| 123 | + } |
| 124 | + out_data += outSlice.total(1); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | +private: |
| 129 | + int top_height; |
| 130 | + int top_width; |
| 131 | + int divisor; |
| 132 | + bool have_reference; |
| 133 | + Ptr<ResizeLayer> resize; |
| 134 | +}; |
| 135 | + |
| 136 | +Ptr<AccumLayer> AccumLayer::create(const LayerParams& params) |
| 137 | +{ |
| 138 | + return Ptr<AccumLayer>(new AccumLayerImpl(params)); |
| 139 | +} |
| 140 | + |
| 141 | +}} // namespace cv::dnn |
0 commit comments