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.

283 lines
6.9 KiB

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