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

296 lines
6.7 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "blas.h"
  2. #include <math.h>
  3. #include <assert.h>
  4. #include <float.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. namespace DN{
  9. void reorg_cpu(float *x, int w, int h, int c, int batch, int stride, int forward, float *out)
  10. {
  11. int b, i, j, k;
  12. int out_c = c / (stride*stride);
  13. for (b = 0; b < batch; ++b){
  14. for (k = 0; k < c; ++k){
  15. for (j = 0; j < h; ++j){
  16. for (i = 0; i < w; ++i){
  17. int in_index = i + w*(j + h*(k + c*b));
  18. int c2 = k % out_c;
  19. int offset = k / out_c;
  20. int w2 = i*stride + offset % stride;
  21. int h2 = j*stride + offset / stride;
  22. int out_index = w2 + w*stride*(h2 + h*stride*(c2 + out_c*b));
  23. if (forward) out[out_index] = x[in_index];
  24. else out[in_index] = x[out_index];
  25. }
  26. }
  27. }
  28. }
  29. }
  30. void flatten(float *x, int size, int layers, int batch, int forward)
  31. {
  32. float *swap = (float*)calloc(size*layers*batch, sizeof(float));
  33. int i, c, b;
  34. for (b = 0; b < batch; ++b){
  35. for (c = 0; c < layers; ++c){
  36. for (i = 0; i < size; ++i){
  37. int i1 = b*layers*size + c*size + i;
  38. int i2 = b*layers*size + i*layers + c;
  39. if (forward) swap[i2] = x[i1];
  40. else swap[i1] = x[i2];
  41. }
  42. }
  43. }
  44. memcpy(x, swap, size*layers*batch*sizeof(float));
  45. free(swap);
  46. }
  47. void weighted_sum_cpu(float *a, float *b, float *s, int n, float *c)
  48. {
  49. int i;
  50. for (i = 0; i < n; ++i){
  51. c[i] = s[i] * a[i] + (1 - s[i])*(b ? b[i] : 0);
  52. }
  53. }
  54. void weighted_delta_cpu(float *a, float *b, float *s, float *da, float *db, float *ds, int n, float *dc)
  55. {
  56. int i;
  57. for (i = 0; i < n; ++i){
  58. if (da) da[i] += dc[i] * s[i];
  59. if (db) db[i] += dc[i] * (1 - s[i]);
  60. ds[i] += dc[i] * (a[i] - b[i]);
  61. }
  62. }
  63. void shortcut_cpu(int batch, int w1, int h1, int c1, float *add, int w2, int h2, int c2, float *out)
  64. {
  65. int stride = w1 / w2;
  66. int sample = w2 / w1;
  67. assert(stride == h1 / h2);
  68. assert(sample == h2 / h1);
  69. if (stride < 1) stride = 1;
  70. if (sample < 1) sample = 1;
  71. int minw = (w1 < w2) ? w1 : w2;
  72. int minh = (h1 < h2) ? h1 : h2;
  73. int minc = (c1 < c2) ? c1 : c2;
  74. int i, j, k, b;
  75. for (b = 0; b < batch; ++b){
  76. for (k = 0; k < minc; ++k){
  77. for (j = 0; j < minh; ++j){
  78. for (i = 0; i < minw; ++i){
  79. int out_index = i*sample + w2*(j*sample + h2*(k + c2*b));
  80. int add_index = i*stride + w1*(j*stride + h1*(k + c1*b));
  81. out[out_index] += add[add_index];
  82. }
  83. }
  84. }
  85. }
  86. }
  87. void mean_cpu(float *x, int batch, int filters, int spatial, float *mean)
  88. {
  89. float scale = (float)(1. / (batch * spatial));
  90. int i, j, k;
  91. for (i = 0; i < filters; ++i){
  92. mean[i] = 0;
  93. for (j = 0; j < batch; ++j){
  94. for (k = 0; k < spatial; ++k){
  95. int index = j*filters*spatial + i*spatial + k;
  96. mean[i] += x[index];
  97. }
  98. }
  99. mean[i] *= scale;
  100. }
  101. }
  102. void variance_cpu(float *x, float *mean, int batch, int filters, int spatial, float *variance)
  103. {
  104. float scale = (float)(1. / (batch * spatial - 1));
  105. int i, j, k;
  106. for (i = 0; i < filters; ++i){
  107. variance[i] = 0;
  108. for (j = 0; j < batch; ++j){
  109. for (k = 0; k < spatial; ++k){
  110. int index = j*filters*spatial + i*spatial + k;
  111. variance[i] += pow((x[index] - mean[i]), 2);
  112. }
  113. }
  114. variance[i] *= scale;
  115. }
  116. }
  117. void normalize_cpu(float *x, float *mean, float *variance, int batch, int filters, int spatial)
  118. {
  119. int b, f, i;
  120. for (b = 0; b < batch; ++b){
  121. for (f = 0; f < filters; ++f){
  122. for (i = 0; i < spatial; ++i){
  123. int index = b*filters*spatial + f*spatial + i;
  124. x[index] = (x[index] - mean[f]) / (sqrt(variance[f]) + .000001f);
  125. }
  126. }
  127. }
  128. }
  129. void const_cpu(int N, float ALPHA, float *X, int INCX)
  130. {
  131. int i;
  132. for (i = 0; i < N; ++i) X[i*INCX] = ALPHA;
  133. }
  134. void mul_cpu(int N, float *X, int INCX, float *Y, int INCY)
  135. {
  136. int i;
  137. for (i = 0; i < N; ++i) Y[i*INCY] *= X[i*INCX];
  138. }
  139. void pow_cpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY)
  140. {
  141. int i;
  142. for (i = 0; i < N; ++i) Y[i*INCY] = pow(X[i*INCX], ALPHA);
  143. }
  144. void axpy_cpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY)
  145. {
  146. int i;
  147. for (i = 0; i < N; ++i) Y[i*INCY] += ALPHA*X[i*INCX];
  148. }
  149. void scal_cpu(int N, float ALPHA, float *X, int INCX)
  150. {
  151. int i;
  152. for (i = 0; i < N; ++i) X[i*INCX] *= ALPHA;
  153. }
  154. void fill_cpu(int N, float ALPHA, float *X, int INCX)
  155. {
  156. int i;
  157. for (i = 0; i < N; ++i) X[i*INCX] = ALPHA;
  158. }
  159. void deinter_cpu(int NX, float *X, int NY, float *Y, int B, float *OT)
  160. {
  161. int i, j;
  162. int index = 0;
  163. for (j = 0; j < B; ++j) {
  164. for (i = 0; i < NX; ++i){
  165. if (X) X[j*NX + i] += OT[index];
  166. ++index;
  167. }
  168. for (i = 0; i < NY; ++i){
  169. if (Y) Y[j*NY + i] += OT[index];
  170. ++index;
  171. }
  172. }
  173. }
  174. void inter_cpu(int NX, float *X, int NY, float *Y, int B, float *OT)
  175. {
  176. int i, j;
  177. int index = 0;
  178. for (j = 0; j < B; ++j) {
  179. for (i = 0; i < NX; ++i){
  180. OT[index++] = X[j*NX + i];
  181. }
  182. for (i = 0; i < NY; ++i){
  183. OT[index++] = Y[j*NY + i];
  184. }
  185. }
  186. }
  187. void copy_cpu(int N, float *X, int INCX, float *Y, int INCY)
  188. {
  189. int i;
  190. for (i = 0; i < N; ++i) Y[i*INCY] = X[i*INCX];
  191. }
  192. void mult_add_into_cpu(int N, float *X, float *Y, float *Z)
  193. {
  194. int i;
  195. for (i = 0; i < N; ++i) Z[i] += X[i] * Y[i];
  196. }
  197. void smooth_l1_cpu(int n, float *pred, float *truth, float *delta, float *error)
  198. {
  199. int i;
  200. for (i = 0; i < n; ++i){
  201. float diff = truth[i] - pred[i];
  202. float abs_val = fabs(diff);
  203. if (abs_val < 1) {
  204. error[i] = diff * diff;
  205. delta[i] = diff;
  206. }
  207. else {
  208. error[i] = 2.f*abs_val - 1.f;
  209. delta[i] = (diff < 0) ? 1.f : -1.f;
  210. }
  211. }
  212. }
  213. void l1_cpu(int n, float *pred, float *truth, float *delta, float *error)
  214. {
  215. int i;
  216. for (i = 0; i < n; ++i){
  217. float diff = truth[i] - pred[i];
  218. error[i] = fabs(diff);
  219. delta[i] = diff > 0 ? 1.f : -1.f;
  220. }
  221. }
  222. void l2_cpu(int n, float *pred, float *truth, float *delta, float *error)
  223. {
  224. int i;
  225. for (i = 0; i < n; ++i){
  226. float diff = truth[i] - pred[i];
  227. error[i] = diff * diff;
  228. delta[i] = diff;
  229. }
  230. }
  231. float dot_cpu(int N, float *X, int INCX, float *Y, int INCY)
  232. {
  233. int i;
  234. float dot = 0;
  235. for (i = 0; i < N; ++i) dot += X[i*INCX] * Y[i*INCY];
  236. return dot;
  237. }
  238. void softmax(float *input, int n, float temp, int stride, float *output)
  239. {
  240. int i;
  241. float sum = 0;
  242. float largest = -FLT_MAX;
  243. for (i = 0; i < n; ++i){
  244. if (input[i*stride] > largest) largest = input[i*stride];
  245. }
  246. for (i = 0; i < n; ++i){
  247. float e = exp(input[i*stride] / temp - largest / temp);
  248. sum += e;
  249. output[i*stride] = e;
  250. }
  251. for (i = 0; i < n; ++i){
  252. output[i*stride] /= sum;
  253. }
  254. }
  255. void softmax_cpu(float *input, int n, int batch, int batch_offset, int groups, int group_offset, int stride, float temp, float *output)
  256. {
  257. int g, b;
  258. for (b = 0; b < batch; ++b){
  259. for (g = 0; g < groups; ++g){
  260. softmax(input + b*batch_offset + g*group_offset, n, temp, stride, output + b*batch_offset + g*group_offset);
  261. }
  262. }
  263. }
  264. };