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 #include "perf_precomp.hpp"
6 #include <opencv2/dnn/shape_utils.hpp>
8 namespace opencv_test {
10 struct Conv3DParam_t {
12 struct BlobShape { int dims[5]; } shapeIn;
20 double declared_flops;
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.}
47 CONV_LAST = sizeof(testConvolution3DConfigs) / sizeof(testConvolution3DConfigs[0])
50 Conv3DParamID(int val = 0) : val_(val) {}
51 operator int() const { return val_; }
52 static ::testing::internal::ParamGenerator<Conv3DParamID> all()
55 enum { NUM = (int)CONV_LAST };
57 enum { NUM = (int)CONV_100 };
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);
63 static inline void PrintTo(const Conv3DParamID& v, std::ostream* os)
65 CV_Assert((int)v >= 0); CV_Assert((int)v < Conv3DParamID::CONV_LAST);
66 const Conv3DParam_t& p = testConvolution3DConfigs[(int)v];
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;
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);
90 typedef tuple<Conv3DParamID, tuple<Backend, Target> > Conv3DTestParam_t;
91 typedef TestBaseWithParam<Conv3DTestParam_t> Conv3D;
93 PERF_TEST_P_(Conv3D, conv3d)
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;
100 DictValue kernel = DictValue::arrayInt(¶ms.kernel[0], 3);
101 DictValue stride = DictValue::arrayInt(¶ms.stride[0], 3);
102 DictValue pad = DictValue::arrayInt(¶ms.pad[0], 6);
103 DictValue dilation = DictValue::arrayInt(¶ms.dilation[0], 3);
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);
110 bool hasBias = params.hasBias;
111 Backend backendId = get<0>(get<1>(GetParam()));
112 Target targetId = get<1>(get<1>(GetParam()));
114 if (targetId != DNN_TARGET_CPU)
115 throw SkipTestException("Only CPU is supported");
117 int inChannels = inputShape[1];
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);
124 lp.set("kernel_size", kernel);
126 if (!padMode.empty())
127 lp.set("pad_mode", padMode);
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);
140 Mat bias(1, outChannels, CV_32F);
141 randu(bias, -1.0f, 1.0f);
142 lp.blobs.push_back(bias);
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);
149 net.addLayerToPrev(lp.name, lp.type, lp);
152 net.setPreferableBackend(backendId);
153 net.setPreferableTarget(targetId);
155 Mat output = net.forward();
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);
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;
171 Mat res = net.forward();
173 EXPECT_NEAR(flops, declared_flops, declared_flops * 1e-6);
174 SANITY_CHECK_NOTHING();
177 INSTANTIATE_TEST_CASE_P(/**/, Conv3D, Combine(
178 Conv3DParamID::all(),
179 dnnBackendsAndTargets(false, false) // defined in ../test/test_common.hpp