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 #ifndef OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_BATCH_NORM_HPP
6 #define OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_BATCH_NORM_HPP
8 #include "../../op_cuda.hpp"
10 #include "../csl/stream.hpp"
11 #include "../csl/tensor.hpp"
13 #include "../kernels/scale_shift.hpp"
17 namespace cv { namespace dnn { namespace cuda4dnn {
20 class BatchNormOp final : public CUDABackendNode {
22 using wrapper_type = GetCUDABackendWrapperType<T>;
24 BatchNormOp(csl::Stream stream_, const cv::Mat& weights, const cv::Mat& bias)
25 : stream(std::move(stream_))
27 biasTensor = csl::makeTensorHeader<T>(bias);
28 csl::copyMatToTensor<T>(bias, biasTensor, stream);
30 weightsTensor = csl::makeTensorHeader<T>(weights);
31 csl::copyMatToTensor<T>(weights, weightsTensor, stream);
35 const std::vector<cv::Ptr<BackendWrapper>>& inputs,
36 const std::vector<cv::Ptr<BackendWrapper>>& outputs,
37 csl::Workspace& workspace) override
39 CV_Assert(inputs.size() == 1 && outputs.size() == 1);
41 auto input_wrapper = inputs[0].dynamicCast<wrapper_type>();
42 auto input = input_wrapper->getView();
44 auto output_wrapper = outputs[0].dynamicCast<wrapper_type>();
45 auto output = output_wrapper->getSpan();
47 std::size_t inner_size = input.size_range(2, input.rank());
48 kernels::scaleN_with_biasN<T>(stream, output, input, inner_size, weightsTensor, biasTensor);
53 csl::Tensor<T> weightsTensor, biasTensor;
56 }}} /* namespace cv::dnn::cuda4dnn */
58 #endif /* OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_BATCH_NORM_HPP */