1f512b2a155e1e3a6e17602b32b12f59422e1bfa
[platform/upstream/opencv.git] / modules / dnn / perf / perf_convolution3d.cpp
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 #include "perf_precomp.hpp"
6 #include <opencv2/dnn/shape_utils.hpp>
7
8 namespace opencv_test {
9
10 struct Conv3DParam_t {
11     int kernel[3];
12     struct BlobShape { int dims[5]; } shapeIn;
13     int outCN;
14     int groups;
15     int stride[3];
16     int dilation[3];
17     int pad[6];
18     const char* padMode;
19     bool hasBias;
20     double declared_flops;
21 };
22 // Details: #12142
23 static const Conv3DParam_t testConvolution3DConfigs[] = {
24     {{3, 3, 3}, {{1, 6, 10, 38, 50}}, 6, 1, {1, 1, 1}, {1, 1, 1}, {0, 0, 0, 0, 0, 0}, "VALID", true, 26956800.},
25     {{3, 3, 3}, {{1, 2, 19, 19, 19}}, 2, 2, {2, 2, 2}, {1, 1, 1}, {1, 1, 1, 1, 1, 1}, "", true, 218000.},
26     {{3, 3, 3}, {{1, 2, 25, 19, 19}}, 2, 2, {1, 2, 2}, {1, 1, 1}, {2, 2, 2, 2, 2, 2}, "SAME", false, 545000.},
27     {{3, 3, 3}, {{1, 11, 9, 150, 200}}, 11, 1, {1, 1, 1}, {1, 1, 1}, {0, 0, 0, 0, 0, 0}, "VALID", true, 1342562760.},
28     {{3, 3, 3}, {{1, 10, 98, 10, 10}}, 10, 1, {1, 1, 1}, {1, 1, 1}, {1, 0, 1, 1, 0,1}, "SAME", false, 53018000.},
29     {{5, 5, 5}, {{1, 6, 19, 19, 19}}, 6, 2, {1, 1, 1}, {1, 1, 1}, {0, 0, 0, 0, 0, 0}, "", false, 30395250.},
30     {{5, 5, 5}, {{1, 4, 50, 19, 19}}, 4, 1, {2, 2, 2}, {1, 1, 1}, {1, 1, 1, 1, 1, 1}, "VALID", false, 5893888.},
31     {{5, 5, 5}, {{1, 3, 75, 75, 100}}, 3, 1, {1, 1, 1}, {1, 1, 1}, {0, 0, 0, 0, 0, 0}, "SAME", true, 1267312500.},
32     {{5, 5, 5}, {{1, 2, 21, 75, 100}}, 2, 1, {1, 1, 1}, {1, 1, 1}, {0, 0, 0, 0, 0, 0}, "", true, 116103744.},
33     {{5, 5, 5}, {{1, 4, 40, 75, 75}}, 4, 1, {2, 2, 2}, {1, 1, 1}, {0, 0, 0, 0, 0, 0}, "", false, 93405312.},
34     {{7, 7, 7}, {{1, 6, 15, 19, 19}}, 6, 1, {2, 1, 1}, {1, 1, 1}, {3, 3, 3, 3, 3, 3}, "SAME", true, 71339376.},
35     {{7, 7, 7}, {{1, 2, 38, 38, 38}}, 2, 1, {1, 2, 1}, {1, 1, 1}, {0, 0, 0, 0, 0, 0}, "", false, 44990464.},
36     {{1, 1, 1}, {{1, 4, 9, 10, 10}}, 4, 1, {1, 1, 2}, {1, 1, 1}, {1, 1, 1, 1, 1, 1}, "VALID", false, 16200.},
37     {{3, 1, 4}, {{1, 14, 5, 10, 10}}, 14, 1, {1, 1, 1}, {1, 1, 1}, {0, 0, 0, 0, 0, 0}, "SAME", false, 2359000.},
38     {{1, 1, 1}, {{1, 8, 1, 10, 10}}, 8, 8, {1, 1, 1}, {1, 1, 1}, {1, 1, 1, 1, 1, 1}, "", true, 58752.},
39     {{3, 4, 2}, {{1, 4, 8, 10, 10}}, 4, 4, {1, 2, 1}, {1, 1, 1}, {0, 0, 0, 0, 0, 0}, "", true, 166752.}
40 };
41
42 struct Conv3DParamID
43 {
44     enum {
45         CONV_0 = 0,
46         CONV_100 = 16,
47         CONV_LAST = sizeof(testConvolution3DConfigs) / sizeof(testConvolution3DConfigs[0])
48     };
49     int val_;                                                                  \
50     Conv3DParamID(int val = 0) : val_(val) {}
51     operator int() const { return val_; }
52     static ::testing::internal::ParamGenerator<Conv3DParamID> all()
53     {
54 #if 0
55         enum { NUM = (int)CONV_LAST };
56 #else
57         enum { NUM = (int)CONV_100 };
58 #endif
59         Conv3DParamID v_[NUM]; for (int i = 0; i < NUM; ++i) { v_[i] = Conv3DParamID(i); } // reduce generated code size
60         return ::testing::ValuesIn(v_, v_ + NUM);
61     }
62 };                                                                                  \
63 static inline void PrintTo(const Conv3DParamID& v, std::ostream* os)
64 {
65     CV_Assert((int)v >= 0); CV_Assert((int)v < Conv3DParamID::CONV_LAST);
66     const Conv3DParam_t& p = testConvolution3DConfigs[(int)v];
67
68     *os << "GFLOPS=" << cv::format("%.3f", p.declared_flops * 1e-9)
69         << ", K=[" << p.kernel[0] << " x " << p.kernel[1]  << " x " << p.kernel[2] << "]"
70         << ", IN={" << p.shapeIn.dims[0] << ", " << p.shapeIn.dims[1] << ", " << p.shapeIn.dims[2] << ", " << p.shapeIn.dims[3] << ", " << p.shapeIn.dims[4] << "}"
71         << ", OCN=" << p.outCN;
72     if (p.groups > 1)
73        *os << ", G=" << p.groups;
74     if (p.stride[0] * p.stride[1] * p.stride[2] != 1)
75         *os << ", S=[" << p.stride[0] << " x " << p.stride[1]  << " x " << p.stride[2] << "]";
76     if (p.dilation[0] * p.dilation[1] * p.dilation[2] != 1)
77         *os << ", D=["  << p.dilation[0] << " x " << p.dilation[1]  << " x " << p.dilation[2] << "]";
78     if (p.pad[0] != 0 && p.pad[1] != 0 && p.pad[2] != 0 &&
79         p.pad[3] != 0 && p.pad[4] != 0 && p.pad[5] != 0)
80         *os << ", P=(" << p.pad[0] << ", " << p.pad[3] << ") x ("
81                        << p.pad[1] << ", " << p.pad[4] << ") x ("
82                        << p.pad[2] << ", " << p.pad[5] << ")";
83     if (!((std::string)p.padMode).empty())
84         *os << ", PM=" << ((std::string)p.padMode);
85     if (p.hasBias)
86         *os << ", BIAS";
87 }
88
89
90 typedef tuple<Conv3DParamID, tuple<Backend, Target> > Conv3DTestParam_t;
91 typedef TestBaseWithParam<Conv3DTestParam_t> Conv3D;
92
93 PERF_TEST_P_(Conv3D, conv3d)
94 {
95     int test_id = (int)get<0>(GetParam());
96     ASSERT_GE(test_id, 0); ASSERT_LT(test_id, Conv3DParamID::CONV_LAST);
97     const Conv3DParam_t& params = testConvolution3DConfigs[test_id];
98     double declared_flops = params.declared_flops;
99
100     DictValue kernel   = DictValue::arrayInt(&params.kernel[0], 3);
101     DictValue stride   = DictValue::arrayInt(&params.stride[0], 3);
102     DictValue pad      = DictValue::arrayInt(&params.pad[0], 6);
103     DictValue dilation = DictValue::arrayInt(&params.dilation[0], 3);
104
105     MatShape inputShape = MatShape(params.shapeIn.dims, params.shapeIn.dims + 5);
106     int outChannels = params.outCN;
107     int groups = params.groups;
108     std::string padMode(params.padMode);
109
110     bool hasBias = params.hasBias;
111     Backend backendId = get<0>(get<1>(GetParam()));
112     Target targetId = get<1>(get<1>(GetParam()));
113
114     if (targetId != DNN_TARGET_CPU)
115         throw SkipTestException("Only CPU is supported");
116
117     int inChannels = inputShape[1];
118
119     int sz[] = {outChannels, inChannels / groups, params.kernel[0], params.kernel[1], params.kernel[2]};
120     Mat weights(5, &sz[0], CV_32F);
121     randu(weights, -1.0f, 1.0f);
122
123     LayerParams lp;
124     lp.set("kernel_size", kernel);
125     lp.set("pad", pad);
126     if (!padMode.empty())
127         lp.set("pad_mode", padMode);
128
129     lp.set("stride", stride);
130     lp.set("dilation", dilation);
131     lp.set("num_output", outChannels);
132     lp.set("group", groups);
133     lp.set("bias_term", hasBias);
134     lp.type = "Convolution";
135     lp.name = "testLayer";
136     lp.blobs.push_back(weights);
137
138     if (hasBias)
139     {
140         Mat bias(1, outChannels, CV_32F);
141         randu(bias, -1.0f, 1.0f);
142         lp.blobs.push_back(bias);
143     }
144     int inpSz[] = {1, inChannels, inputShape[2], inputShape[3], inputShape[4]};
145     Mat input(5, &inpSz[0], CV_32F);
146     randu(input, -1.0f, 1.0f);
147
148     Net net;
149     net.addLayerToPrev(lp.name, lp.type, lp);
150
151     net.setInput(input);
152     net.setPreferableBackend(backendId);
153     net.setPreferableTarget(targetId);
154
155     Mat output = net.forward();
156
157     MatShape netInputShape = shape(input);
158     size_t weightsMemory = 0, blobsMemory = 0;
159     net.getMemoryConsumption(netInputShape, weightsMemory, blobsMemory);
160     int64 flops = net.getFLOPS(netInputShape);
161     CV_Assert(flops > 0);
162
163     std::cout
164         << "IN=" << divUp(input.total() * input.elemSize(), 1u<<10) << " Kb " << netInputShape
165         << "    OUT=" << divUp(output.total() * output.elemSize(), 1u<<10) << " Kb " << shape(output)
166         << "    Weights(parameters): " << divUp(weightsMemory, 1u<<10) << " Kb"
167         << "    MFLOPS=" << flops * 1e-6 << std::endl;
168
169     TEST_CYCLE()
170     {
171         Mat res = net.forward();
172     }
173     EXPECT_NEAR(flops, declared_flops, declared_flops * 1e-6);
174     SANITY_CHECK_NOTHING();
175 }
176
177 INSTANTIATE_TEST_CASE_P(/**/, Conv3D, Combine(
178     Conv3DParamID::all(),
179     dnnBackendsAndTargets(false, false)  // defined in ../test/test_common.hpp
180 ));
181
182 } // namespace