You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

innerproduct.cpp 2.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include "innerproduct.h"
  15. namespace ncnn {
  16. DEFINE_LAYER_CREATOR(InnerProduct)
  17. InnerProduct::InnerProduct()
  18. {
  19. one_blob_only = true;
  20. support_inplace = false;
  21. }
  22. int InnerProduct::load_param(const ParamDict& pd)
  23. {
  24. num_output = pd.get(0, 0);
  25. bias_term = pd.get(1, 0);
  26. weight_data_size = pd.get(2, 0);
  27. return 0;
  28. }
  29. int InnerProduct::load_model(const ModelBin& mb)
  30. {
  31. weight_data = mb.load(weight_data_size, 0);
  32. if (weight_data.empty())
  33. return -100;
  34. if (bias_term)
  35. {
  36. bias_data = mb.load(num_output, 1);
  37. if (bias_data.empty())
  38. return -100;
  39. }
  40. return 0;
  41. }
  42. int InnerProduct::forward(const Mat& bottom_blob, Mat& top_blob) const
  43. {
  44. int w = bottom_blob.w;
  45. int h = bottom_blob.h;
  46. int channels = bottom_blob.c;
  47. int size = w * h;
  48. top_blob.create(num_output);
  49. if (top_blob.empty())
  50. return -100;
  51. // num_output
  52. #pragma omp parallel for
  53. for (int p=0; p<num_output; p++)
  54. {
  55. float sum = 0.f;
  56. if (bias_term)
  57. sum = bias_data[p];
  58. // channels
  59. for (int q=0; q<channels; q++)
  60. {
  61. const float* w = (const float*)weight_data + size * channels * p + size * q;
  62. const float* m = bottom_blob.channel(q);
  63. for (int i = 0; i < size; i++)
  64. {
  65. sum += m[i] * w[i];
  66. }
  67. }
  68. top_blob[p] = sum;
  69. }
  70. return 0;
  71. }
  72. } // namespace ncnn