Elffuss BraidHub

kikocis/ncnn public

master · 6.5 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#include "deformableconv2d.h"
16
17#include "fused_activation.h"
18
19namespace ncnn {
20
21DeformableConv2D::DeformableConv2D()
22{
23 one_blob_only = false;
24 support_inplace = false;
25}
26
27int DeformableConv2D::load_param(const ParamDict& pd)
28{
29 num_output = pd.get(0, 0);
30 kernel_w = pd.get(1, 0);
31 kernel_h = pd.get(11, kernel_w);
32 dilation_w = pd.get(2, 1);
33 dilation_h = pd.get(12, dilation_w);
34 stride_w = pd.get(3, 1);
35 stride_h = pd.get(13, stride_w);
36 pad_left = pd.get(4, 0);
37 pad_right = pd.get(15, pad_left);
38 pad_top = pd.get(14, pad_left);
39 pad_bottom = pd.get(16, pad_top);
40 bias_term = pd.get(5, 0);
41 weight_data_size = pd.get(6, 0);
42 activation_type = pd.get(9, 0);
43 activation_params = pd.get(10, Mat());
44 return 0;
45}
46
47int DeformableConv2D::load_model(const ModelBin& mb)
48{
49 weight_data = mb.load(weight_data_size, 0);
50 if (weight_data.empty())
51 return -100;
52
53 if (bias_term)
54 {
55 bias_data = mb.load(num_output, 1);
56 if (bias_data.empty())
57 return -100;
58 }
59 return 0;
60}
61
62int DeformableConv2D::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
63{
64 const Mat& bottom_blob = bottom_blobs[0];
65 const Mat& offset = bottom_blobs[1];
66
67 const bool has_mask = (bottom_blobs.size() == 3);
68
69 const int w = bottom_blob.w;
70 const int h = bottom_blob.h;
71 const int in_c = bottom_blob.c;
72 const size_t elemsize = bottom_blob.elemsize;
73
74 const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
75 const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
76
77 const int out_w = (w + pad_left + pad_right - kernel_extent_w) / stride_w + 1;
78 const int out_h = (h + pad_top + pad_bottom - kernel_extent_h) / stride_h + 1;
79
80 // output.shape is [num_output, out_h, out_w] (in python).
81 Mat& output = top_blobs[0];
82 output.create(out_w, out_h, num_output, elemsize, opt.blob_allocator);
83 if (output.empty())
84 return -100;
85
86 const float* weight_ptr = weight_data;
87 const float* bias_ptr = weight_data;
88 if (bias_term)
89 bias_ptr = bias_data;
90
91 // deformable conv
92 #pragma omp parallel for num_threads(opt.num_threads)
93 for (int h_col = 0; h_col < out_h; h_col++)
94 {
95 for (int w_col = 0; w_col < out_w; w_col++)
96 {
97 int h_in = h_col * stride_h - pad_top;
98 int w_in = w_col * stride_w - pad_left;
99 for (int oc = 0; oc < num_output; oc++)
100 {
101 float sum = 0.f;
102 if (bias_term)
103 sum = bias_ptr[oc];
104 for (int i = 0; i < kernel_h; i++)
105 {
106 for (int j = 0; j < kernel_w; j++)
107 {
108 const float offset_h = offset.channel((i * kernel_w + j) * 2).row(h_col)[w_col];
109 const float offset_w = offset.channel((i * kernel_w + j) * 2 + 1).row(h_col)[w_col];
110 const float mask_ = has_mask ? bottom_blobs[2].channel(i * kernel_w + j).row(h_col)[w_col] : 1.f;
111 const float h_im = h_in + i * dilation_h + offset_h;
112 const float w_im = w_in + j * dilation_w + offset_w;
113
114 // Bilinear
115 const bool cond = h_im > -1 && w_im > -1 && h_im < h && w_im < w;
116 int h_low = 0;
117 int w_low = 0;
118 int h_high = 0;
119 int w_high = 0;
120 float w1 = 0.f;
121 float w2 = 0.f;
122 float w3 = 0.f;
123 float w4 = 0.f;
124 bool v1_cond = false;
125 bool v2_cond = false;
126 bool v3_cond = false;
127 bool v4_cond = false;
128 if (cond)
129 {
130 h_low = (int)floorf(h_im);
131 w_low = (int)floorf(w_im);
132 h_high = h_low + 1;
133 w_high = w_low + 1;
134
135 float lh = h_im - h_low;
136 float lw = w_im - w_low;
137 float hh = 1 - lh;
138 float hw = 1 - lw;
139
140 v1_cond = (h_low >= 0 && w_low >= 0);
141 v2_cond = (h_low >= 0 && w_high <= w - 1);
142 v3_cond = (h_high <= h - 1 && w_low >= 0);
143 v4_cond = (h_high <= h - 1 && w_high <= w - 1);
144
145 w1 = hh * hw;
146 w2 = hh * lw;
147 w3 = lh * hw;
148 w4 = lh * lw;
149 }
150
151 for (int c_im = 0; c_im < in_c; c_im++)
152 {
153 float val = 0.f;
154 if (cond)
155 {
156 float v1 = v1_cond ? bottom_blob.channel(c_im).row(h_low)[w_low] : 0.f;
157 float v2 = v2_cond ? bottom_blob.channel(c_im).row(h_low)[w_high] : 0.f;
158 float v3 = v3_cond ? bottom_blob.channel(c_im).row(h_high)[w_low] : 0.f;
159 float v4 = v4_cond ? bottom_blob.channel(c_im).row(h_high)[w_high] : 0.f;
160 val = w1 * v1 + w2 * v2 + w3 * v3 + w4 * v4;
161 }
162 sum += val * mask_ * weight_ptr[((oc * in_c + c_im) * kernel_h + i) * kernel_w + j];
163 }
164 }
165 }
166 output.channel(oc).row(h_col)[w_col] = activation_ss(sum, activation_type, activation_params);
167 }
168 }
169 }
170 return 0;
171}
172
173} // namespace ncnn