Merge pull request #14827 from YashasSamaga:cuda4dnn-csl-low
[platform/upstream/opencv.git] / modules / dnn / src / cuda4dnn / primitives / resize.hpp
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 #ifndef OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_RESIZE_HPP
6 #define OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_RESIZE_HPP
7
8 #include "../../op_cuda.hpp"
9
10 #include "../csl/stream.hpp"
11
12 #include "../kernels/resize.hpp"
13
14 #include <utility>
15
16 namespace cv { namespace dnn { namespace cuda4dnn {
17
18     enum class InterpolationType {
19         NEAREST_NEIGHBOUR,
20         BILINEAR
21     };
22
23     template <class T>
24     class ResizeOp final : public CUDABackendNode {
25     public:
26         using wrapper_type = GetCUDABackendWrapperType<T>;
27
28         ResizeOp(csl::Stream stream_, InterpolationType type_, float scaleHeight_, float scaleWidth_)
29             : stream(std::move(stream_)), type{ type_ }, scaleHeight{ scaleHeight_ }, scaleWidth{ scaleWidth_ }
30         {
31         }
32
33         void forward(
34             const std::vector<cv::Ptr<BackendWrapper>>& inputs,
35             const std::vector<cv::Ptr<BackendWrapper>>& outputs,
36             csl::Workspace& workspace) override
37         {
38             CV_Assert(inputs.size() == 1 && outputs.size() == 1);
39
40             auto input_wrapper = inputs[0].dynamicCast<wrapper_type>();
41             auto input = input_wrapper->getView();
42
43             auto output_wrapper = outputs[0].dynamicCast<wrapper_type>();
44             auto output = output_wrapper->getSpan();
45
46             if (type == InterpolationType::NEAREST_NEIGHBOUR)
47                 kernels::resize_nn<T>(stream, output, input);
48             else if (type == InterpolationType::BILINEAR)
49                 kernels::resize_bilinear<T>(stream, output, input, scaleHeight, scaleWidth);
50         }
51
52     private:
53         csl::Stream stream;
54         InterpolationType type;
55         float scaleHeight, scaleWidth; /* for bilinear interpolation */
56     };
57
58 }}} /* namespace cv::dnn::cuda4dnn */
59
60 #endif /* OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_RESIZE_HPP */