| 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 "instancenorm.h" |
| 16 | |
| 17 | namespace ncnn { |
| 18 | |
| 19 | InstanceNorm::InstanceNorm() |
| 20 | { |
| 21 | one_blob_only = true; |
| 22 | support_inplace = true; |
| 23 | } |
| 24 | |
| 25 | int InstanceNorm::load_param(const ParamDict& pd) |
| 26 | { |
| 27 | channels = pd.get(0, 0); |
| 28 | eps = pd.get(1, 0.001f); |
| 29 | affine = pd.get(2, 1); |
| 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | int InstanceNorm::load_model(const ModelBin& mb) |
| 35 | { |
| 36 | if (affine == 0) |
| 37 | return 0; |
| 38 | |
| 39 | gamma_data = mb.load(channels, 1); |
| 40 | if (gamma_data.empty()) |
| 41 | return -100; |
| 42 | |
| 43 | beta_data = mb.load(channels, 1); |
| 44 | if (beta_data.empty()) |
| 45 | return -100; |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | int InstanceNorm::forward_inplace(Mat& bottom_top_blob, const Option& opt) const |
| 51 | { |
| 52 | // x = (x - mean) / (sqrt(var + eps)) * gamma + beta |
| 53 | |
| 54 | int w = bottom_top_blob.w; |
| 55 | int h = bottom_top_blob.h; |
| 56 | int c = bottom_top_blob.c; |
| 57 | int size = w * h; |
| 58 | |
| 59 | #pragma omp parallel for num_threads(opt.num_threads) |
| 60 | for (int q = 0; q < c; q++) |
| 61 | { |
| 62 | float* ptr = bottom_top_blob.channel(q); |
| 63 | |
| 64 | // mean and var |
| 65 | float sum = 0.f; |
| 66 | float sqsum = 0.f; |
| 67 | for (int i = 0; i < size; i++) |
| 68 | { |
| 69 | sum += ptr[i]; |
| 70 | //sqsum += ptr[i] * ptr[i]; |
| 71 | } |
| 72 | float mean = sum / size; |
| 73 | float tmp = 0.f; |
| 74 | for (int i = 0; i < size; i++) |
| 75 | { |
| 76 | tmp = ptr[i] - mean; |
| 77 | sqsum += tmp * tmp; |
| 78 | } |
| 79 | float var = sqsum / size; |
| 80 | // the var maybe minus due to accuracy |
| 81 | //float var = sqsum / size - mean * mean; |
| 82 | |
| 83 | float a; |
| 84 | float b; |
| 85 | if (affine) |
| 86 | { |
| 87 | float gamma = gamma_data[q]; |
| 88 | float beta = beta_data[q]; |
| 89 | |
| 90 | a = gamma / (sqrtf(var + eps)); |
| 91 | b = -mean * a + beta; |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | a = 1.f / (sqrtf(var + eps)); |
| 96 | b = -mean * a; |
| 97 | } |
| 98 | |
| 99 | for (int i = 0; i < size; i++) |
| 100 | { |
| 101 | ptr[i] = ptr[i] * a + b; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | } // namespace ncnn |