| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2017 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 "lrn.h" |
| 16 | |
| 17 | namespace ncnn { |
| 18 | |
| 19 | LRN::LRN() |
| 20 | { |
| 21 | one_blob_only = true; |
| 22 | support_inplace = true; |
| 23 | } |
| 24 | |
| 25 | int LRN::load_param(const ParamDict& pd) |
| 26 | { |
| 27 | region_type = pd.get(0, 0); |
| 28 | local_size = pd.get(1, 5); |
| 29 | alpha = pd.get(2, 1.f); |
| 30 | beta = pd.get(3, 0.75f); |
| 31 | bias = pd.get(4, 1.f); |
| 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | int LRN::forward_inplace(Mat& bottom_top_blob, const Option& opt) const |
| 37 | { |
| 38 | int w = bottom_top_blob.w; |
| 39 | int h = bottom_top_blob.h; |
| 40 | int channels = bottom_top_blob.c; |
| 41 | size_t elemsize = bottom_top_blob.elemsize; |
| 42 | int size = w * h; |
| 43 | |
| 44 | // squared values with local_size padding |
| 45 | Mat square_blob; |
| 46 | square_blob.create(w, h, channels, elemsize, opt.workspace_allocator); |
| 47 | if (square_blob.empty()) |
| 48 | return -100; |
| 49 | |
| 50 | #pragma omp parallel for num_threads(opt.num_threads) |
| 51 | for (int q = 0; q < channels; q++) |
| 52 | { |
| 53 | const float* ptr = bottom_top_blob.channel(q); |
| 54 | float* outptr = square_blob.channel(q); |
| 55 | |
| 56 | for (int i = 0; i < size; i++) |
| 57 | { |
| 58 | outptr[i] = ptr[i] * ptr[i]; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if (region_type == NormRegion_ACROSS_CHANNELS) |
| 63 | { |
| 64 | Mat square_sum; |
| 65 | square_sum.create(w, h, channels, elemsize, opt.workspace_allocator); |
| 66 | if (square_sum.empty()) |
| 67 | return -100; |
| 68 | square_sum.fill(0.f); |
| 69 | |
| 70 | const float alpha_div_size = alpha / local_size; |
| 71 | |
| 72 | #pragma omp parallel for num_threads(opt.num_threads) |
| 73 | for (int q = 0; q < channels; q++) |
| 74 | { |
| 75 | // square sum |
| 76 | float* ssptr = square_sum.channel(q); |
| 77 | for (int p = q - local_size / 2; p <= q + local_size / 2; p++) |
| 78 | { |
| 79 | if (p < 0 || p >= channels) |
| 80 | continue; |
| 81 | |
| 82 | const float* sptr = square_blob.channel(p); |
| 83 | for (int i = 0; i < size; i++) |
| 84 | { |
| 85 | ssptr[i] += sptr[i]; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | float* ptr = bottom_top_blob.channel(q); |
| 90 | for (int i = 0; i < size; i++) |
| 91 | { |
| 92 | ptr[i] = ptr[i] * powf(bias + alpha_div_size * ssptr[i], -beta); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | else if (region_type == NormRegion_WITHIN_CHANNEL) |
| 97 | { |
| 98 | int outw = w; |
| 99 | int outh = h; |
| 100 | |
| 101 | Mat square_blob_bordered = square_blob; |
| 102 | int pad = local_size / 2; |
| 103 | if (pad > 0) |
| 104 | { |
| 105 | Option opt_b = opt; |
| 106 | opt_b.blob_allocator = opt.workspace_allocator; |
| 107 | opt_b.use_packing_layout = false; |
| 108 | copy_make_border(square_blob, square_blob_bordered, pad, local_size - pad - 1, pad, local_size - pad - 1, BORDER_CONSTANT, 0.f, opt_b); |
| 109 | if (square_blob_bordered.empty()) |
| 110 | return -100; |
| 111 | |
| 112 | w = square_blob_bordered.w; |
| 113 | h = square_blob_bordered.h; |
| 114 | } |
| 115 | |
| 116 | const int maxk = local_size * local_size; |
| 117 | |
| 118 | const float alpha_div_size = alpha / maxk; |
| 119 | |
| 120 | // norm window offsets |
| 121 | std::vector<int> _space_ofs(maxk); |
| 122 | int* space_ofs = &_space_ofs[0]; |
| 123 | { |
| 124 | int p1 = 0; |
| 125 | int p2 = 0; |
| 126 | int gap = w - local_size; |
| 127 | for (int i = 0; i < local_size; i++) |
| 128 | { |
| 129 | for (int j = 0; j < local_size; j++) |
| 130 | { |
| 131 | space_ofs[p1] = p2; |
| 132 | p1++; |
| 133 | p2++; |
| 134 | } |
| 135 | p2 += gap; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | #pragma omp parallel for num_threads(opt.num_threads) |
| 140 | for (int q = 0; q < channels; q++) |
| 141 | { |
| 142 | float* ptr = bottom_top_blob.channel(q); |
| 143 | const Mat m = square_blob_bordered.channel(q); |
| 144 | |
| 145 | for (int i = 0; i < outh; i++) |
| 146 | { |
| 147 | for (int j = 0; j < outw; j++) |
| 148 | { |
| 149 | const float* sptr = m.row(i) + j; |
| 150 | |
| 151 | float ss = 0.f; |
| 152 | |
| 153 | for (int k = 0; k < maxk; k++) |
| 154 | { |
| 155 | float val = sptr[space_ofs[k]]; |
| 156 | ss += val; |
| 157 | } |
| 158 | |
| 159 | ptr[j] = ptr[j] * powf(bias + alpha_div_size * ss, -beta); |
| 160 | } |
| 161 | |
| 162 | ptr += outw; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | } // namespace ncnn |