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.

603 lines
16 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "convolutional_layer.h"
  2. #include "utils.h"
  3. #include "batchnorm_layer.h"
  4. #include "im2col.h"
  5. #include "col2im.h"
  6. #include "blas.h"
  7. #include "gemm.h"
  8. #include <stdio.h>
  9. #include <time.h>
  10. #ifdef AI2
  11. #include "xnor_layer.h"
  12. #endif
  13. namespace DN{
  14. void swap_binary(convolutional_layer *l)
  15. {
  16. float *swap = l->weights;
  17. l->weights = l->binary_weights;
  18. l->binary_weights = swap;
  19. #ifdef GPU
  20. swap = l->weights_gpu;
  21. l->weights_gpu = l->binary_weights_gpu;
  22. l->binary_weights_gpu = swap;
  23. #endif
  24. }
  25. void binarize_weights(float *weights, int n, int size, float *binary)
  26. {
  27. int i, f;
  28. for (f = 0; f < n; ++f){
  29. float mean = 0;
  30. for (i = 0; i < size; ++i){
  31. mean += fabs(weights[f*size + i]);
  32. }
  33. mean = mean / size;
  34. for (i = 0; i < size; ++i){
  35. binary[f*size + i] = (weights[f*size + i] > 0) ? mean : -mean;
  36. }
  37. }
  38. }
  39. void binarize_cpu(float *input, int n, float *binary)
  40. {
  41. int i;
  42. for (i = 0; i < n; ++i){
  43. binary[i] = (float)((input[i] > 0) ? 1 : -1);
  44. }
  45. }
  46. void binarize_input(float *input, int n, int size, float *binary)
  47. {
  48. int i, s;
  49. for (s = 0; s < size; ++s){
  50. float mean = 0;
  51. for (i = 0; i < n; ++i){
  52. mean += fabs(input[i*size + s]);
  53. }
  54. mean = mean / n;
  55. for (i = 0; i < n; ++i){
  56. binary[i*size + s] = (input[i*size + s] > 0) ? mean : -mean;
  57. }
  58. }
  59. }
  60. int convolutional_out_height(convolutional_layer l)
  61. {
  62. return (l.h + 2 * l.pad - l.size) / l.stride + 1;
  63. }
  64. int convolutional_out_width(convolutional_layer l)
  65. {
  66. return (l.w + 2 * l.pad - l.size) / l.stride + 1;
  67. }
  68. image get_convolutional_image(convolutional_layer l)
  69. {
  70. return float_to_image(l.out_w, l.out_h, l.out_c, l.output);
  71. }
  72. image get_convolutional_delta(convolutional_layer l)
  73. {
  74. return float_to_image(l.out_w, l.out_h, l.out_c, l.delta);
  75. }
  76. static size_t get_workspace_size(layer l){
  77. #ifdef CUDNN
  78. if(gpu_index >= 0){
  79. size_t most = 0;
  80. size_t s = 0;
  81. cudnnGetConvolutionForwardWorkspaceSize(cudnn_handle(),
  82. l.srcTensorDesc,
  83. l.weightDesc,
  84. l.convDesc,
  85. l.dstTensorDesc,
  86. l.fw_algo,
  87. &s);
  88. if (s > most) most = s;
  89. cudnnGetConvolutionBackwardFilterWorkspaceSize(cudnn_handle(),
  90. l.srcTensorDesc,
  91. l.ddstTensorDesc,
  92. l.convDesc,
  93. l.dweightDesc,
  94. l.bf_algo,
  95. &s);
  96. if (s > most) most = s;
  97. cudnnGetConvolutionBackwardDataWorkspaceSize(cudnn_handle(),
  98. l.weightDesc,
  99. l.ddstTensorDesc,
  100. l.convDesc,
  101. l.dsrcTensorDesc,
  102. l.bd_algo,
  103. &s);
  104. if (s > most) most = s;
  105. return most;
  106. }
  107. #endif
  108. return (size_t)l.out_h*l.out_w*l.size*l.size*l.c*sizeof(float);
  109. }
  110. #ifdef GPU
  111. #ifdef CUDNN
  112. void cudnn_convolutional_setup(layer *l)
  113. {
  114. cudnnSetTensor4dDescriptor(l->dsrcTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, l->batch, l->c, l->h, l->w);
  115. cudnnSetTensor4dDescriptor(l->ddstTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, l->batch, l->out_c, l->out_h, l->out_w);
  116. cudnnSetFilter4dDescriptor(l->dweightDesc, CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, l->n, l->c, l->size, l->size);
  117. cudnnSetTensor4dDescriptor(l->srcTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, l->batch, l->c, l->h, l->w);
  118. cudnnSetTensor4dDescriptor(l->dstTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, l->batch, l->out_c, l->out_h, l->out_w);
  119. cudnnSetTensor4dDescriptor(l->normTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, 1, l->out_c, 1, 1);
  120. cudnnSetFilter4dDescriptor(l->weightDesc, CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, l->n, l->c, l->size, l->size);
  121. #if CUDNN_MAJOR >= 6
  122. cudnnSetConvolution2dDescriptor(l->convDesc, l->pad, l->pad, l->stride, l->stride, 1, 1, CUDNN_CROSS_CORRELATION, CUDNN_DATA_FLOAT);
  123. #else
  124. cudnnSetConvolution2dDescriptor(l->convDesc, l->pad, l->pad, l->stride, l->stride, 1, 1, CUDNN_CROSS_CORRELATION);
  125. #endif
  126. cudnnGetConvolutionForwardAlgorithm(cudnn_handle(),
  127. l->srcTensorDesc,
  128. l->weightDesc,
  129. l->convDesc,
  130. l->dstTensorDesc,
  131. CUDNN_CONVOLUTION_FWD_PREFER_FASTEST,
  132. 0,
  133. &l->fw_algo);
  134. cudnnGetConvolutionBackwardDataAlgorithm(cudnn_handle(),
  135. l->weightDesc,
  136. l->ddstTensorDesc,
  137. l->convDesc,
  138. l->dsrcTensorDesc,
  139. CUDNN_CONVOLUTION_BWD_DATA_PREFER_FASTEST,
  140. 0,
  141. &l->bd_algo);
  142. cudnnGetConvolutionBackwardFilterAlgorithm(cudnn_handle(),
  143. l->srcTensorDesc,
  144. l->ddstTensorDesc,
  145. l->convDesc,
  146. l->dweightDesc,
  147. CUDNN_CONVOLUTION_BWD_FILTER_PREFER_FASTEST,
  148. 0,
  149. &l->bf_algo);
  150. }
  151. #endif
  152. #endif
  153. convolutional_layer make_convolutional_layer(int batch, int h, int w, int c, int n, int size, int stride, int padding, ACTIVATION activation, int batch_normalize, int binary, int xnor, int adam)
  154. {
  155. int i;
  156. convolutional_layer l;
  157. memset(&l, 0, sizeof(convolutional_layer));
  158. l.type = CONVOLUTIONAL;
  159. l.h = h;
  160. l.w = w;
  161. l.c = c;
  162. l.n = n;
  163. l.binary = binary;
  164. l.xnor = xnor;
  165. l.batch = batch;
  166. l.stride = stride;
  167. l.size = size;
  168. l.pad = padding;
  169. l.batch_normalize = batch_normalize;
  170. l.weights = (float*)calloc(c*n*size*size, sizeof(float));
  171. l.weight_updates = (float*)calloc(c*n*size*size, sizeof(float));
  172. l.biases = (float*)calloc(n, sizeof(float));
  173. l.bias_updates = (float*)calloc(n, sizeof(float));
  174. l.nweights = c*n*size*size;
  175. l.nbiases = n;
  176. // float scale = 1./sqrt(size*size*c);
  177. float scale = (float)sqrt(2. / (size*size*c));
  178. //scale = .02;
  179. //for(i = 0; i < c*n*size*size; ++i) l.weights[i] = scale*rand_uniform(-1, 1);
  180. for (i = 0; i < c*n*size*size; ++i) l.weights[i] = scale*rand_normal();
  181. int out_w = convolutional_out_width(l);
  182. int out_h = convolutional_out_height(l);
  183. l.out_h = out_h;
  184. l.out_w = out_w;
  185. l.out_c = n;
  186. l.outputs = l.out_h * l.out_w * l.out_c;
  187. l.inputs = l.w * l.h * l.c;
  188. l.output = (float*)calloc(l.batch*l.outputs, sizeof(float));
  189. l.delta = (float*)calloc(l.batch*l.outputs, sizeof(float));
  190. l.forward = forward_convolutional_layer;
  191. l.backward = backward_convolutional_layer;
  192. l.update = update_convolutional_layer;
  193. if (binary){
  194. l.binary_weights = (float*)calloc(c*n*size*size, sizeof(float));
  195. l.cweights = (char*)calloc(c*n*size*size, sizeof(char));
  196. l.scales = (float*)calloc(n, sizeof(float));
  197. }
  198. if (xnor){
  199. l.binary_weights = (float*)calloc(c*n*size*size, sizeof(float));
  200. l.binary_input = (float*)calloc(l.inputs*l.batch, sizeof(float));
  201. }
  202. if (batch_normalize){
  203. l.scales = (float*)calloc(n, sizeof(float));
  204. l.scale_updates = (float*)calloc(n, sizeof(float));
  205. for (i = 0; i < n; ++i){
  206. l.scales[i] = 1;
  207. }
  208. l.mean = (float*)calloc(n, sizeof(float));
  209. l.variance = (float*)calloc(n, sizeof(float));
  210. l.mean_delta = (float*)calloc(n, sizeof(float));
  211. l.variance_delta = (float*)calloc(n, sizeof(float));
  212. l.rolling_mean = (float*)calloc(n, sizeof(float));
  213. l.rolling_variance = (float*)calloc(n, sizeof(float));
  214. l.x = (float*)calloc(l.batch*l.outputs, sizeof(float));
  215. l.x_norm = (float*)calloc(l.batch*l.outputs, sizeof(float));
  216. }
  217. if (adam){
  218. l.m = (float*)calloc(c*n*size*size, sizeof(float));
  219. l.v = (float*)calloc(c*n*size*size, sizeof(float));
  220. l.bias_m = (float*)calloc(n, sizeof(float));
  221. l.scale_m = (float*)calloc(n, sizeof(float));
  222. l.bias_v = (float*)calloc(n, sizeof(float));
  223. l.scale_v = (float*)calloc(n, sizeof(float));
  224. }
  225. #ifdef GPU
  226. l.forward_gpu = forward_convolutional_layer_gpu;
  227. l.backward_gpu = backward_convolutional_layer_gpu;
  228. l.update_gpu = update_convolutional_layer_gpu;
  229. if(gpu_index >= 0){
  230. if (adam) {
  231. l.m_gpu = cuda_make_array(l.m, c*n*size*size);
  232. l.v_gpu = cuda_make_array(l.v, c*n*size*size);
  233. l.bias_m_gpu = cuda_make_array(l.bias_m, n);
  234. l.bias_v_gpu = cuda_make_array(l.bias_v, n);
  235. l.scale_m_gpu = cuda_make_array(l.scale_m, n);
  236. l.scale_v_gpu = cuda_make_array(l.scale_v, n);
  237. }
  238. l.weights_gpu = cuda_make_array(l.weights, c*n*size*size);
  239. l.weight_updates_gpu = cuda_make_array(l.weight_updates, c*n*size*size);
  240. l.biases_gpu = cuda_make_array(l.biases, n);
  241. l.bias_updates_gpu = cuda_make_array(l.bias_updates, n);
  242. l.delta_gpu = cuda_make_array(l.delta, l.batch*out_h*out_w*n);
  243. l.output_gpu = cuda_make_array(l.output, l.batch*out_h*out_w*n);
  244. if(binary){
  245. l.binary_weights_gpu = cuda_make_array(l.weights, c*n*size*size);
  246. }
  247. if(xnor){
  248. l.binary_weights_gpu = cuda_make_array(l.weights, c*n*size*size);
  249. l.binary_input_gpu = cuda_make_array(0, l.inputs*l.batch);
  250. }
  251. if(batch_normalize){
  252. l.mean_gpu = cuda_make_array(l.mean, n);
  253. l.variance_gpu = cuda_make_array(l.variance, n);
  254. l.rolling_mean_gpu = cuda_make_array(l.mean, n);
  255. l.rolling_variance_gpu = cuda_make_array(l.variance, n);
  256. l.mean_delta_gpu = cuda_make_array(l.mean, n);
  257. l.variance_delta_gpu = cuda_make_array(l.variance, n);
  258. l.scales_gpu = cuda_make_array(l.scales, n);
  259. l.scale_updates_gpu = cuda_make_array(l.scale_updates, n);
  260. l.x_gpu = cuda_make_array(l.output, l.batch*out_h*out_w*n);
  261. l.x_norm_gpu = cuda_make_array(l.output, l.batch*out_h*out_w*n);
  262. }
  263. #ifdef CUDNN
  264. cudnnCreateTensorDescriptor(&l.normTensorDesc);
  265. cudnnCreateTensorDescriptor(&l.srcTensorDesc);
  266. cudnnCreateTensorDescriptor(&l.dstTensorDesc);
  267. cudnnCreateFilterDescriptor(&l.weightDesc);
  268. cudnnCreateTensorDescriptor(&l.dsrcTensorDesc);
  269. cudnnCreateTensorDescriptor(&l.ddstTensorDesc);
  270. cudnnCreateFilterDescriptor(&l.dweightDesc);
  271. cudnnCreateConvolutionDescriptor(&l.convDesc);
  272. cudnn_convolutional_setup(&l);
  273. #endif
  274. }
  275. #endif
  276. l.workspace_size = get_workspace_size(l);
  277. l.activation = activation;
  278. fprintf(stderr, "conv %5d %2d x%2d /%2d %4d x%4d x%4d -> %4d x%4d x%4d\n", n, size, size, stride, w, h, c, l.out_w, l.out_h, l.out_c);
  279. return l;
  280. }
  281. void denormalize_convolutional_layer(convolutional_layer l)
  282. {
  283. int i, j;
  284. for (i = 0; i < l.n; ++i){
  285. float scale = (float)(l.scales[i] / sqrt(l.rolling_variance[i] + .00001));
  286. for (j = 0; j < l.c*l.size*l.size; ++j){
  287. l.weights[i*l.c*l.size*l.size + j] *= scale;
  288. }
  289. l.biases[i] -= l.rolling_mean[i] * scale;
  290. l.scales[i] = 1;
  291. l.rolling_mean[i] = 0;
  292. l.rolling_variance[i] = 1;
  293. }
  294. }
  295. /*
  296. void test_convolutional_layer()
  297. {
  298. convolutional_layer l = make_convolutional_layer(1, 5, 5, 3, 2, 5, 2, 1, LEAKY, 1, 0, 0, 0);
  299. l.batch_normalize = 1;
  300. float data[] = {1,1,1,1,1,
  301. 1,1,1,1,1,
  302. 1,1,1,1,1,
  303. 1,1,1,1,1,
  304. 1,1,1,1,1,
  305. 2,2,2,2,2,
  306. 2,2,2,2,2,
  307. 2,2,2,2,2,
  308. 2,2,2,2,2,
  309. 2,2,2,2,2,
  310. 3,3,3,3,3,
  311. 3,3,3,3,3,
  312. 3,3,3,3,3,
  313. 3,3,3,3,3,
  314. 3,3,3,3,3};
  315. //net.input = data;
  316. //forward_convolutional_layer(l);
  317. }
  318. */
  319. void resize_convolutional_layer(convolutional_layer *l, int w, int h)
  320. {
  321. l->w = w;
  322. l->h = h;
  323. int out_w = convolutional_out_width(*l);
  324. int out_h = convolutional_out_height(*l);
  325. l->out_w = out_w;
  326. l->out_h = out_h;
  327. l->outputs = l->out_h * l->out_w * l->out_c;
  328. l->inputs = l->w * l->h * l->c;
  329. l->output = (float*)realloc(l->output, l->batch*l->outputs*sizeof(float));
  330. l->delta = (float*)realloc(l->delta, l->batch*l->outputs*sizeof(float));
  331. if (l->batch_normalize){
  332. l->x = (float*)realloc(l->x, l->batch*l->outputs*sizeof(float));
  333. l->x_norm = (float*)realloc(l->x_norm, l->batch*l->outputs*sizeof(float));
  334. }
  335. #ifdef GPU
  336. cuda_free(l->delta_gpu);
  337. cuda_free(l->output_gpu);
  338. l->delta_gpu = cuda_make_array(l->delta, l->batch*l->outputs);
  339. l->output_gpu = cuda_make_array(l->output, l->batch*l->outputs);
  340. if(l->batch_normalize){
  341. cuda_free(l->x_gpu);
  342. cuda_free(l->x_norm_gpu);
  343. l->x_gpu = cuda_make_array(l->output, l->batch*l->outputs);
  344. l->x_norm_gpu = cuda_make_array(l->output, l->batch*l->outputs);
  345. }
  346. #ifdef CUDNN
  347. cudnn_convolutional_setup(l);
  348. #endif
  349. #endif
  350. l->workspace_size = get_workspace_size(*l);
  351. }
  352. void add_bias(float *output, float *biases, int batch, int n, int size)
  353. {
  354. int i, j, b;
  355. for (b = 0; b < batch; ++b){
  356. for (i = 0; i < n; ++i){
  357. for (j = 0; j < size; ++j){
  358. output[(b*n + i)*size + j] += biases[i];
  359. }
  360. }
  361. }
  362. }
  363. void scale_bias(float *output, float *scales, int batch, int n, int size)
  364. {
  365. int i, j, b;
  366. for (b = 0; b < batch; ++b){
  367. for (i = 0; i < n; ++i){
  368. for (j = 0; j < size; ++j){
  369. output[(b*n + i)*size + j] *= scales[i];
  370. }
  371. }
  372. }
  373. }
  374. void backward_bias(float *bias_updates, float *delta, int batch, int n, int size)
  375. {
  376. int i, b;
  377. for (b = 0; b < batch; ++b){
  378. for (i = 0; i < n; ++i){
  379. bias_updates[i] += sum_array(delta + size*(i + b*n), size);
  380. }
  381. }
  382. }
  383. void forward_convolutional_layer(convolutional_layer l, network net)
  384. {
  385. int out_h = l.out_h;
  386. int out_w = l.out_w;
  387. int i;
  388. fill_cpu(l.outputs*l.batch, 0, l.output, 1);
  389. if (l.xnor){
  390. binarize_weights(l.weights, l.n, l.c*l.size*l.size, l.binary_weights);
  391. swap_binary(&l);
  392. binarize_cpu(net.input, l.c*l.h*l.w*l.batch, l.binary_input);
  393. net.input = l.binary_input;
  394. }
  395. int m = l.n;
  396. int k = l.size*l.size*l.c;
  397. int n = out_h*out_w;
  398. float *a = l.weights;
  399. float *b = net.workspace;
  400. float *c = l.output;
  401. for (i = 0; i < l.batch; ++i){
  402. im2col_cpu(net.input, l.c, l.h, l.w,
  403. l.size, l.stride, l.pad, b);
  404. gemm(0, 0, m, n, k, 1, a, k, b, n, 1, c, n);
  405. c += n*m;
  406. net.input += l.c*l.h*l.w;
  407. }
  408. if (l.batch_normalize){
  409. forward_batchnorm_layer(l, net);
  410. }
  411. else {
  412. add_bias(l.output, l.biases, l.batch, l.n, out_h*out_w);
  413. }
  414. activate_array(l.output, m*n*l.batch, l.activation);
  415. if (l.binary || l.xnor) swap_binary(&l);
  416. }
  417. void backward_convolutional_layer(convolutional_layer l, network net)
  418. {
  419. int i;
  420. int m = l.n;
  421. int n = l.size*l.size*l.c;
  422. int k = l.out_w*l.out_h;
  423. gradient_array(l.output, m*k*l.batch, l.activation, l.delta);
  424. if (l.batch_normalize){
  425. backward_batchnorm_layer(l, net);
  426. }
  427. else {
  428. backward_bias(l.bias_updates, l.delta, l.batch, l.n, k);
  429. }
  430. for (i = 0; i < l.batch; ++i){
  431. float *a = l.delta + i*m*k;
  432. float *b = net.workspace;
  433. float *c = l.weight_updates;
  434. float *im = net.input + i*l.c*l.h*l.w;
  435. im2col_cpu(im, l.c, l.h, l.w,
  436. l.size, l.stride, l.pad, b);
  437. gemm(0, 1, m, n, k, 1, a, k, b, k, 1, c, n);
  438. if (net.delta){
  439. a = l.weights;
  440. b = l.delta + i*m*k;
  441. c = net.workspace;
  442. gemm(1, 0, n, k, m, 1, a, n, b, k, 0, c, k);
  443. col2im_cpu(net.workspace, l.c, l.h, l.w, l.size, l.stride, l.pad, net.delta + i*l.c*l.h*l.w);
  444. }
  445. }
  446. }
  447. void update_convolutional_layer(convolutional_layer l, update_args a)
  448. {
  449. float learning_rate = a.learning_rate*l.learning_rate_scale;
  450. float momentum = a.momentum;
  451. float decay = a.decay;
  452. int batch = a.batch;
  453. int size = l.size*l.size*l.c*l.n;
  454. axpy_cpu(l.n, learning_rate / batch, l.bias_updates, 1, l.biases, 1);
  455. scal_cpu(l.n, momentum, l.bias_updates, 1);
  456. if (l.scales){
  457. axpy_cpu(l.n, learning_rate / batch, l.scale_updates, 1, l.scales, 1);
  458. scal_cpu(l.n, momentum, l.scale_updates, 1);
  459. }
  460. axpy_cpu(size, -decay*batch, l.weights, 1, l.weight_updates, 1);
  461. axpy_cpu(size, learning_rate / batch, l.weight_updates, 1, l.weights, 1);
  462. scal_cpu(size, momentum, l.weight_updates, 1);
  463. }
  464. image get_convolutional_weight(convolutional_layer l, int i)
  465. {
  466. int h = l.size;
  467. int w = l.size;
  468. int c = l.c;
  469. return float_to_image(w, h, c, l.weights + i*h*w*c);
  470. }
  471. void rgbgr_weights(convolutional_layer l)
  472. {
  473. int i;
  474. for (i = 0; i < l.n; ++i){
  475. image im = get_convolutional_weight(l, i);
  476. if (im.c == 3) {
  477. rgbgr_image(im);
  478. }
  479. }
  480. }
  481. void rescale_weights(convolutional_layer l, float scale, float trans)
  482. {
  483. int i;
  484. for (i = 0; i < l.n; ++i){
  485. image im = get_convolutional_weight(l, i);
  486. if (im.c == 3) {
  487. scale_image(im, scale);
  488. float sum = sum_array(im.data, im.w*im.h*im.c);
  489. l.biases[i] += sum*trans;
  490. }
  491. }
  492. }
  493. image *get_weights(convolutional_layer l)
  494. {
  495. image *weights = (image*)calloc(l.n, sizeof(image));
  496. int i;
  497. for (i = 0; i < l.n; ++i){
  498. weights[i] = copy_image(get_convolutional_weight(l, i));
  499. normalize_image(weights[i]);
  500. /*
  501. char buff[256];
  502. sprintf(buff, "filter%d", i);
  503. save_image(weights[i], buff);
  504. */
  505. }
  506. //error("hey");
  507. return weights;
  508. }
  509. image *visualize_convolutional_layer(convolutional_layer l, char *window, image *prev_weights)
  510. {
  511. image *single_weights = get_weights(l);
  512. show_images(single_weights, l.n, window);
  513. image delta = get_convolutional_image(l);
  514. image dc = collapse_image_layers(delta, 1);
  515. char buff[256];
  516. sprintf(buff, "%s: Output", window);
  517. //show_image(dc, buff);
  518. //save_image(dc, buff);
  519. free_image(dc);
  520. return single_weights;
  521. }
  522. };