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.
5 // Copyright (C) 2016, Intel Corporation, all rights reserved.
6 // Third party copyrights are property of their respective owners.
9 Implementation of Batch Normalization layer.
12 #include "../precomp.hpp"
13 #include "layers_common.hpp"
14 #include "../op_halide.hpp"
15 #include <opencv2/dnn/shape_utils.hpp>
24 class MaxUnpoolLayerImpl CV_FINAL : public MaxUnpoolLayer
27 MaxUnpoolLayerImpl(const LayerParams& params)
29 setParamsFrom(params);
30 poolKernel = Size(params.get<int>("pool_k_w"), params.get<int>("pool_k_h"));
31 poolPad = Size(params.get<int>("pool_pad_w"), params.get<int>("pool_pad_h"));
32 poolStride = Size(params.get<int>("pool_stride_w"), params.get<int>("pool_stride_h"));
35 virtual bool supportBackend(int backendId) CV_OVERRIDE
37 return backendId == DNN_BACKEND_OPENCV ||
38 (backendId == DNN_BACKEND_HALIDE && haveHalide() && !poolPad.width && !poolPad.height);
41 bool getMemoryShapes(const std::vector<MatShape> &inputs,
42 const int requiredOutputs,
43 std::vector<MatShape> &outputs,
44 std::vector<MatShape> &internals) const CV_OVERRIDE
46 CV_Assert(inputs.size() == 2);
47 CV_Assert(total(inputs[0]) == total(inputs[1]));
49 MatShape outShape = inputs[0];
50 outShape[2] = (outShape[2] - 1) * poolStride.height + poolKernel.height - 2 * poolPad.height;
51 outShape[3] = (outShape[3] - 1) * poolStride.width + poolKernel.width - 2 * poolPad.width;
54 outputs.push_back(outShape);
59 void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
62 CV_TRACE_ARG_VALUE(name, "name", name.c_str());
64 if (inputs_arr.depth() == CV_16S)
66 forward_fallback(inputs_arr, outputs_arr, internals_arr);
70 std::vector<Mat> inputs, outputs;
71 inputs_arr.getMatVector(inputs);
72 outputs_arr.getMatVector(outputs);
74 CV_Assert(inputs.size() == 2);
75 Mat& input = inputs[0];
76 Mat& indices = inputs[1];
78 CV_Assert(input.total() == indices.total());
79 CV_Assert(input.size[0] == 1);
80 CV_Assert(input.isContinuous());
82 for(int i_n = 0; i_n < outputs.size(); i_n++)
84 Mat& outBlob = outputs[i_n];
86 CV_Assert(input.size[1] == outBlob.size[1]);
87 int outPlaneTotal = outBlob.size[2]*outBlob.size[3];
89 for (int i_c = 0; i_c < input.size[1]; i_c++)
91 Mat outPlane = getPlane(outBlob, 0, i_c);
92 int wh_area = input.size[2]*input.size[3];
93 const float* inptr = input.ptr<float>(0, i_c);
94 const float* idxptr = indices.ptr<float>(0, i_c);
95 float* outptr = outPlane.ptr<float>();
97 for(int i_wh = 0; i_wh < wh_area; i_wh++)
99 int index = idxptr[i_wh];
100 if (!(0 <= index && index < outPlaneTotal))
103 << "i_n=" << i_n << std::endl
104 << "i_c=" << i_c << std::endl
105 << "i_wh=" << i_wh << std::endl
106 << "index=" << index << std::endl
107 << "maxval=" << inptr[i_wh] << std::endl
108 << "outPlaneTotal=" << outPlaneTotal << std::endl
109 << "input.size=" << input.size << std::endl
110 << "indices.size=" << indices.size << std::endl
111 << "outBlob=" << outBlob.size << std::endl
113 CV_Assert(0 <= index && index < outPlaneTotal);
115 outptr[index] = inptr[i_wh];
121 virtual Ptr<BackendNode> initHalide(const std::vector<Ptr<BackendWrapper> > &input) CV_OVERRIDE
124 // Meaningless operation if false because if kernel > stride
125 // it is not deterministic and if kernel < stride we just
126 // skip a part of input data (you'd better change your model).
127 if (poolKernel.width != poolStride.width ||
128 poolKernel.height != poolStride.height)
129 CV_Error(cv::Error::StsNotImplemented,
130 "Halide backend for maximum unpooling "
131 "is not support cases when kernel != stride");
133 Halide::Var x("x"), y("y"), c("c"), n("n");
134 Halide::Func top = (name.empty() ? Halide::Func() : Halide::Func(name));
135 Halide::Buffer<float> inputBuffer = halideBuffer(input[0]);
136 Halide::Buffer<float> indices = halideBuffer(input[1]);
138 Halide::Expr pooledX = x / poolKernel.width;
139 Halide::Expr pooledY = y / poolKernel.height;
141 const int outW = inputBuffer.width() * poolKernel.width;
142 top(x, y, c, n) = select(y * outW + x == indices(pooledX, pooledY, c, n),
143 inputBuffer(pooledX, pooledY, c, n), 0.0f);
144 return Ptr<BackendNode>(new HalideBackendNode(top));
145 #endif // HAVE_HALIDE
146 return Ptr<BackendNode>();
150 Ptr<MaxUnpoolLayer> MaxUnpoolLayer::create(const LayerParams& params)
152 return Ptr<MaxUnpoolLayer>(new MaxUnpoolLayerImpl(params));