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.

727 lines
16 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <assert.h>
  4. #include "network.h"
  5. #include "image.h"
  6. #include "data.h"
  7. #include "utils.h"
  8. #include "blas.h"
  9. #include "crop_layer.h"
  10. #include "connected_layer.h"
  11. #include "gru_layer.h"
  12. #include "rnn_layer.h"
  13. #include "crnn_layer.h"
  14. #include "local_layer.h"
  15. #include "convolutional_layer.h"
  16. #include "activation_layer.h"
  17. #include "detection_layer.h"
  18. #include "region_layer.h"
  19. #include "normalization_layer.h"
  20. #include "batchnorm_layer.h"
  21. #include "maxpool_layer.h"
  22. #include "reorg_layer.h"
  23. #include "avgpool_layer.h"
  24. #include "cost_layer.h"
  25. #include "softmax_layer.h"
  26. #include "dropout_layer.h"
  27. #include "route_layer.h"
  28. #include "shortcut_layer.h"
  29. #include "parser.h"
  30. #include "data.h"
  31. namespace DN{
  32. load_args get_base_args(network net)
  33. {
  34. load_args args = { 0 };
  35. args.w = net.w;
  36. args.h = net.h;
  37. args.size = net.w;
  38. args.min = net.min_crop;
  39. args.max = net.max_crop;
  40. args.angle = net.angle;
  41. args.aspect = net.aspect;
  42. args.exposure = net.exposure;
  43. args.center = net.center;
  44. args.saturation = net.saturation;
  45. args.hue = net.hue;
  46. return args;
  47. }
  48. network load_network(char *cfg, char *weights, int clear)
  49. {
  50. network net = parse_network_cfg(cfg);
  51. if (weights && weights[0] != 0){
  52. load_weights(&net, weights);
  53. }
  54. if (clear) *net.seen = 0;
  55. return net;
  56. }
  57. network *load_network_p(char *cfg, char *weights, int clear)
  58. {
  59. network *net = (network *)calloc(1, sizeof(network));
  60. *net = load_network(cfg, weights, clear);
  61. return net;
  62. }
  63. size_t get_current_batch(network net)
  64. {
  65. size_t batch_num = (*net.seen) / (net.batch*net.subdivisions);
  66. return batch_num;
  67. }
  68. void reset_momentum(network net)
  69. {
  70. if (net.momentum == 0) return;
  71. net.learning_rate = 0;
  72. net.momentum = 0;
  73. net.decay = 0;
  74. #ifdef GPU
  75. //if(net.gpu_index >= 0) update_network_gpu(net);
  76. #endif
  77. }
  78. void reset_network_state(network net, int b)
  79. {
  80. int i;
  81. for (i = 0; i < net.n; ++i) {
  82. #ifdef GPU
  83. layer l = net.layers[i];
  84. if(l.state_gpu){
  85. fill_gpu(l.outputs, 0, l.state_gpu + l.outputs*b, 1);
  86. }
  87. if(l.h_gpu){
  88. fill_gpu(l.outputs, 0, l.h_gpu + l.outputs*b, 1);
  89. }
  90. #endif
  91. }
  92. }
  93. void reset_rnn(network *net)
  94. {
  95. reset_network_state(*net, 0);
  96. }
  97. float get_current_rate(network net)
  98. {
  99. size_t batch_num = get_current_batch(net);
  100. int i;
  101. float rate;
  102. if (((int)batch_num) < net.burn_in) return net.learning_rate * pow((float)batch_num / net.burn_in, net.power);
  103. switch (net.policy) {
  104. case CONSTANT:
  105. return net.learning_rate;
  106. case STEP:
  107. return net.learning_rate * pow(net.scale, (float)(batch_num / net.step));
  108. case STEPS:
  109. rate = net.learning_rate;
  110. for (i = 0; i < net.num_steps; ++i){
  111. if (net.steps[i] >((int)batch_num)) return rate;
  112. rate *= net.scales[i];
  113. //if(net.steps[i] > batch_num - 1 && net.scales[i] > 1) reset_momentum(net);
  114. }
  115. return rate;
  116. case EXP:
  117. return net.learning_rate * pow(net.gamma, (float)batch_num);
  118. case POLY:
  119. return net.learning_rate * pow(1 - (float)batch_num / net.max_batches, net.power);
  120. case RANDOM:
  121. return net.learning_rate * pow(rand_uniform(0, 1), net.power);
  122. case SIG:
  123. return (float)(net.learning_rate * (1. / (1. + exp(net.gamma*(batch_num - net.step)))));
  124. default:
  125. fprintf(stderr, "Policy is weird!\n");
  126. return net.learning_rate;
  127. }
  128. }
  129. char *get_layer_string(LAYER_TYPE a)
  130. {
  131. switch (a){
  132. case CONVOLUTIONAL:
  133. return "convolutional";
  134. case ACTIVE:
  135. return "activation";
  136. case LOCAL:
  137. return "local";
  138. case DECONVOLUTIONAL:
  139. return "deconvolutional";
  140. case CONNECTED:
  141. return "connected";
  142. case RNN:
  143. return "rnn";
  144. case GRU:
  145. return "gru";
  146. case LSTM:
  147. return "lstm";
  148. case CRNN:
  149. return "crnn";
  150. case MAXPOOL:
  151. return "maxpool";
  152. case REORG:
  153. return "reorg";
  154. case AVGPOOL:
  155. return "avgpool";
  156. case SOFTMAX:
  157. return "softmax";
  158. case DETECTION:
  159. return "detection";
  160. case REGION:
  161. return "region";
  162. case DROPOUT:
  163. return "dropout";
  164. case CROP:
  165. return "crop";
  166. case COST:
  167. return "cost";
  168. case ROUTE:
  169. return "route";
  170. case SHORTCUT:
  171. return "shortcut";
  172. case NORMALIZATION:
  173. return "normalization";
  174. case BATCHNORM:
  175. return "batchnorm";
  176. default:
  177. break;
  178. }
  179. return "none";
  180. }
  181. network make_network(int n)
  182. {
  183. network net = { 0 };
  184. net.n = n;
  185. net.layers = (layer*)calloc(net.n, sizeof(layer));
  186. net.seen = (size_t*)calloc(1, sizeof(size_t));
  187. net.t = (int*)calloc(1, sizeof(int));
  188. net.cost = (float*)calloc(1, sizeof(float));
  189. return net;
  190. }
  191. void forward_network(network net)
  192. {
  193. int i;
  194. for (i = 0; i < net.n; ++i){
  195. net.index = i;
  196. layer l = net.layers[i];
  197. if (l.delta){
  198. fill_cpu(l.outputs * l.batch, 0, l.delta, 1);
  199. }
  200. l.forward(l, net);
  201. net.input = l.output;
  202. if (l.truth) {
  203. net.truth = l.output;
  204. }
  205. }
  206. calc_network_cost(net);
  207. }
  208. void update_network(network net)
  209. {
  210. int i;
  211. update_args a = { 0 };
  212. a.batch = net.batch*net.subdivisions;
  213. a.learning_rate = get_current_rate(net);
  214. a.momentum = net.momentum;
  215. a.decay = net.decay;
  216. a.adam = net.adam;
  217. a.B1 = net.B1;
  218. a.B2 = net.B2;
  219. a.eps = net.eps;
  220. ++*net.t;
  221. a.t = *net.t;
  222. for (i = 0; i < net.n; ++i){
  223. layer l = net.layers[i];
  224. if (l.update){
  225. l.update(l, a);
  226. }
  227. }
  228. }
  229. void calc_network_cost(network net)
  230. {
  231. int i;
  232. float sum = 0;
  233. int count = 0;
  234. for (i = 0; i < net.n; ++i){
  235. if (net.layers[i].cost){
  236. sum += net.layers[i].cost[0];
  237. ++count;
  238. }
  239. }
  240. *net.cost = sum / count;
  241. }
  242. int get_predicted_class_network(network net)
  243. {
  244. return max_index(net.output, net.outputs);
  245. }
  246. void backward_network(network net)
  247. {
  248. int i;
  249. network orig = net;
  250. for (i = net.n - 1; i >= 0; --i){
  251. layer l = net.layers[i];
  252. if (l.stopbackward) break;
  253. if (i == 0){
  254. net = orig;
  255. }
  256. else{
  257. layer prev = net.layers[i - 1];
  258. net.input = prev.output;
  259. net.delta = prev.delta;
  260. }
  261. net.index = i;
  262. l.backward(l, net);
  263. }
  264. }
  265. float train_network_datum(network net)
  266. {
  267. #ifdef GPU
  268. if(gpu_index >= 0) return train_network_datum_gpu(net);
  269. #endif
  270. *net.seen += net.batch;
  271. net.train = 1;
  272. forward_network(net);
  273. backward_network(net);
  274. float error = *net.cost;
  275. if (((*net.seen) / net.batch) % net.subdivisions == 0) update_network(net);
  276. return error;
  277. }
  278. float train_network_sgd(network net, data d, int n)
  279. {
  280. int batch = net.batch;
  281. int i;
  282. float sum = 0;
  283. for (i = 0; i < n; ++i){
  284. get_random_batch(d, batch, net.input, net.truth);
  285. float err = train_network_datum(net);
  286. sum += err;
  287. }
  288. return (float)sum / (n*batch);
  289. }
  290. float train_network(network net, data d)
  291. {
  292. assert(d.X.rows % net.batch == 0);
  293. int batch = net.batch;
  294. int n = d.X.rows / batch;
  295. int i;
  296. float sum = 0;
  297. for (i = 0; i < n; ++i){
  298. get_next_batch(d, batch, i*batch, net.input, net.truth);
  299. float err = train_network_datum(net);
  300. sum += err;
  301. }
  302. return (float)sum / (n*batch);
  303. }
  304. void set_temp_network(network net, float t)
  305. {
  306. int i;
  307. for (i = 0; i < net.n; ++i){
  308. net.layers[i].temperature = t;
  309. }
  310. }
  311. void set_batch_network(network *net, int b)
  312. {
  313. net->batch = b;
  314. int i;
  315. for (i = 0; i < net->n; ++i){
  316. net->layers[i].batch = b;
  317. #ifdef CUDNN
  318. if(net->layers[i].type == CONVOLUTIONAL){
  319. cudnn_convolutional_setup(net->layers + i);
  320. }
  321. if(net->layers[i].type == DECONVOLUTIONAL){
  322. layer *l = net->layers + i;
  323. cudnnSetTensor4dDescriptor(l->dstTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, 1, l->out_c, l->out_h, l->out_w);
  324. cudnnSetTensor4dDescriptor(l->normTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, 1, l->out_c, 1, 1);
  325. }
  326. #endif
  327. }
  328. }
  329. int resize_network(network *net, int w, int h)
  330. {
  331. #ifdef GPU
  332. cuda_set_device(net->gpu_index);
  333. cuda_free(net->workspace);
  334. #endif
  335. int i;
  336. //if(w == net->w && h == net->h) return 0;
  337. net->w = w;
  338. net->h = h;
  339. int inputs = 0;
  340. size_t workspace_size = 0;
  341. //fprintf(stderr, "Resizing to %d x %d...\n", w, h);
  342. //fflush(stderr);
  343. for (i = 0; i < net->n; ++i){
  344. layer l = net->layers[i];
  345. if (l.type == CONVOLUTIONAL){
  346. resize_convolutional_layer(&l, w, h);
  347. }
  348. else if (l.type == CROP){
  349. resize_crop_layer(&l, w, h);
  350. }
  351. else if (l.type == MAXPOOL){
  352. resize_maxpool_layer(&l, w, h);
  353. }
  354. else if (l.type == REGION){
  355. resize_region_layer(&l, w, h);
  356. }
  357. else if (l.type == ROUTE){
  358. resize_route_layer(&l, net);
  359. }
  360. else if (l.type == REORG){
  361. resize_reorg_layer(&l, w, h);
  362. }
  363. else if (l.type == AVGPOOL){
  364. resize_avgpool_layer(&l, w, h);
  365. }
  366. else if (l.type == NORMALIZATION){
  367. resize_normalization_layer(&l, w, h);
  368. }
  369. else if (l.type == COST){
  370. resize_cost_layer(&l, inputs);
  371. }
  372. else{
  373. error("Cannot resize this type of layer");
  374. }
  375. if (l.workspace_size > workspace_size) workspace_size = l.workspace_size;
  376. inputs = l.outputs;
  377. net->layers[i] = l;
  378. w = l.out_w;
  379. h = l.out_h;
  380. if (l.type == AVGPOOL) break;
  381. }
  382. layer out = get_network_output_layer(*net);
  383. net->inputs = net->layers[0].inputs;
  384. net->outputs = out.outputs;
  385. net->truths = out.outputs;
  386. if (net->layers[net->n - 1].truths) net->truths = net->layers[net->n - 1].truths;
  387. net->output = out.output;
  388. free(net->input);
  389. free(net->truth);
  390. net->input = (float*)calloc(net->inputs*net->batch, sizeof(float));
  391. net->truth = (float*)calloc(net->truths*net->batch, sizeof(float));
  392. #ifdef GPU
  393. if(gpu_index >= 0){
  394. cuda_free(net->input_gpu);
  395. cuda_free(net->truth_gpu);
  396. net->input_gpu = cuda_make_array(net->input, net->inputs*net->batch);
  397. net->truth_gpu = cuda_make_array(net->truth, net->truths*net->batch);
  398. net->workspace = cuda_make_array(0, (workspace_size-1)/sizeof(float)+1);
  399. }else {
  400. free(net->workspace);
  401. net->workspace = (float*)calloc(1, workspace_size);
  402. }
  403. #else
  404. free(net->workspace);
  405. net->workspace = (float*)calloc(1, workspace_size);
  406. #endif
  407. //fprintf(stderr, " Done!\n");
  408. return 0;
  409. }
  410. detection_layer get_network_detection_layer(network net)
  411. {
  412. int i;
  413. for (i = 0; i < net.n; ++i){
  414. if (net.layers[i].type == DETECTION){
  415. return net.layers[i];
  416. }
  417. }
  418. fprintf(stderr, "Detection layer not found!!\n");
  419. detection_layer l;
  420. memset(&l, 0, sizeof(detection_layer));
  421. return l;
  422. }
  423. image get_network_image_layer(network net, int i)
  424. {
  425. layer l = net.layers[i];
  426. #ifdef GPU
  427. //cuda_pull_array(l.output_gpu, l.output, l.outputs);
  428. #endif
  429. if (l.out_w && l.out_h && l.out_c){
  430. return float_to_image(l.out_w, l.out_h, l.out_c, l.output);
  431. }
  432. image def = { 0 };
  433. return def;
  434. }
  435. image get_network_image(network net)
  436. {
  437. int i;
  438. for (i = net.n - 1; i >= 0; --i){
  439. image m = get_network_image_layer(net, i);
  440. if (m.h != 0) return m;
  441. }
  442. image def = { 0 };
  443. return def;
  444. }
  445. void visualize_network(network net)
  446. {
  447. image *prev = 0;
  448. int i;
  449. char buff[256];
  450. for (i = 0; i < net.n; ++i){
  451. sprintf(buff, "Layer %d", i);
  452. layer l = net.layers[i];
  453. if (l.type == CONVOLUTIONAL){
  454. prev = visualize_convolutional_layer(l, buff, prev);
  455. }
  456. }
  457. }
  458. void top_predictions(network net, int k, int *index)
  459. {
  460. top_k(net.output, net.outputs, k, index);
  461. }
  462. float *network_predict(network net, float *input)
  463. {
  464. #ifdef GPU
  465. if(gpu_index >= 0) return network_predict_gpu(net, input);
  466. #endif
  467. net.input = input;
  468. net.truth = 0;
  469. net.train = 0;
  470. net.delta = 0;
  471. forward_network(net);
  472. return net.output;
  473. }
  474. int num_boxes(network *net)
  475. {
  476. layer l = net->layers[net->n - 1];
  477. return l.w*l.h*l.n;
  478. }
  479. box *make_boxes(network *net)
  480. {
  481. layer l = net->layers[net->n - 1];
  482. box *boxes = (box*)calloc(l.w*l.h*l.n, sizeof(box));
  483. return boxes;
  484. }
  485. float **make_probs(network *net)
  486. {
  487. int j;
  488. layer l = net->layers[net->n - 1];
  489. float **probs = (float**)calloc(l.w*l.h*l.n, sizeof(float *));
  490. for (j = 0; j < l.w*l.h*l.n; ++j) probs[j] = (float*)calloc(l.classes + 1, sizeof(float *));
  491. return probs;
  492. }
  493. void network_detect(network *net, image im, float thresh, float hier_thresh, float nms, box *boxes, float **probs)
  494. {
  495. network_predict_image(net, im);
  496. layer l = net->layers[net->n - 1];
  497. if (l.type == REGION){
  498. get_region_boxes(l, im.w, im.h, net->w, net->h, thresh, probs, boxes, 0, 0, 0, hier_thresh, 0);
  499. if (nms) do_nms_sort(boxes, probs, l.w*l.h*l.n, l.classes, nms);
  500. }
  501. }
  502. float *network_predict_p(network *net, float *input)
  503. {
  504. return network_predict(*net, input);
  505. }
  506. float *network_predict_image(network *net, image im)
  507. {
  508. image imr = letterbox_image(im, net->w, net->h);
  509. set_batch_network(net, 1);
  510. float *p = network_predict(*net, imr.data);
  511. free_image(imr);
  512. return p;
  513. }
  514. int network_width(network *net){ return net->w; }
  515. int network_height(network *net){ return net->h; }
  516. matrix network_predict_data_multi(network net, data test, int n)
  517. {
  518. int i, j, b, m;
  519. int k = net.outputs;
  520. matrix pred = make_matrix(test.X.rows, k);
  521. float *X = (float*)calloc(net.batch*test.X.rows, sizeof(float));
  522. for (i = 0; i < test.X.rows; i += net.batch){
  523. for (b = 0; b < net.batch; ++b){
  524. if (i + b == test.X.rows) break;
  525. memcpy(X + b*test.X.cols, test.X.vals[i + b], test.X.cols*sizeof(float));
  526. }
  527. for (m = 0; m < n; ++m){
  528. float *out = network_predict(net, X);
  529. for (b = 0; b < net.batch; ++b){
  530. if (i + b == test.X.rows) break;
  531. for (j = 0; j < k; ++j){
  532. pred.vals[i + b][j] += out[j + b*k] / n;
  533. }
  534. }
  535. }
  536. }
  537. free(X);
  538. return pred;
  539. }
  540. matrix network_predict_data(network net, data test)
  541. {
  542. int i, j, b;
  543. int k = net.outputs;
  544. matrix pred = make_matrix(test.X.rows, k);
  545. float *X = (float*)calloc(net.batch*test.X.cols, sizeof(float));
  546. for (i = 0; i < test.X.rows; i += net.batch){
  547. for (b = 0; b < net.batch; ++b){
  548. if (i + b == test.X.rows) break;
  549. memcpy(X + b*test.X.cols, test.X.vals[i + b], test.X.cols*sizeof(float));
  550. }
  551. float *out = network_predict(net, X);
  552. for (b = 0; b < net.batch; ++b){
  553. if (i + b == test.X.rows) break;
  554. for (j = 0; j < k; ++j){
  555. pred.vals[i + b][j] = out[j + b*k];
  556. }
  557. }
  558. }
  559. free(X);
  560. return pred;
  561. }
  562. void print_network(network net)
  563. {
  564. int i, j;
  565. for (i = 0; i < net.n; ++i){
  566. layer l = net.layers[i];
  567. float *output = l.output;
  568. int n = l.outputs;
  569. float mean = mean_array(output, n);
  570. float vari = variance_array(output, n);
  571. fprintf(stderr, "Layer %d - Mean: %f, Variance: %f\n", i, mean, vari);
  572. if (n > 100) n = 100;
  573. for (j = 0; j < n; ++j) fprintf(stderr, "%f, ", output[j]);
  574. if (n == 100)fprintf(stderr, ".....\n");
  575. fprintf(stderr, "\n");
  576. }
  577. }
  578. void compare_networks(network n1, network n2, data test)
  579. {
  580. matrix g1 = network_predict_data(n1, test);
  581. matrix g2 = network_predict_data(n2, test);
  582. int i;
  583. int a, b, c, d;
  584. a = b = c = d = 0;
  585. for (i = 0; i < g1.rows; ++i){
  586. int truth = max_index(test.y.vals[i], test.y.cols);
  587. int p1 = max_index(g1.vals[i], g1.cols);
  588. int p2 = max_index(g2.vals[i], g2.cols);
  589. if (p1 == truth){
  590. if (p2 == truth) ++d;
  591. else ++c;
  592. }
  593. else{
  594. if (p2 == truth) ++b;
  595. else ++a;
  596. }
  597. }
  598. printf("%5d %5d\n%5d %5d\n", a, b, c, d);
  599. float num = (float)pow((abs(b - c) - 1.), 2.);
  600. float den = (float)(b + c);
  601. printf("%f\n", num / den);
  602. }
  603. float network_accuracy(network net, data d)
  604. {
  605. matrix guess = network_predict_data(net, d);
  606. float acc = matrix_topk_accuracy(d.y, guess, 1);
  607. free_matrix(guess);
  608. return acc;
  609. }
  610. float *network_accuracies(network net, data d, int n)
  611. {
  612. static float acc[2];
  613. matrix guess = network_predict_data(net, d);
  614. acc[0] = matrix_topk_accuracy(d.y, guess, 1);
  615. acc[1] = matrix_topk_accuracy(d.y, guess, n);
  616. free_matrix(guess);
  617. return acc;
  618. }
  619. layer get_network_output_layer(network net)
  620. {
  621. int i;
  622. for (i = net.n - 1; i >= 0; --i){
  623. if (net.layers[i].type != COST) break;
  624. }
  625. return net.layers[i];
  626. }
  627. float network_accuracy_multi(network net, data d, int n)
  628. {
  629. matrix guess = network_predict_data_multi(net, d, n);
  630. float acc = matrix_topk_accuracy(d.y, guess, 1);
  631. free_matrix(guess);
  632. return acc;
  633. }
  634. void free_network(network net)
  635. {
  636. int i;
  637. for (i = 0; i < net.n; ++i){
  638. free_layer(net.layers[i]);
  639. }
  640. free(net.layers);
  641. if (net.input) free(net.input);
  642. if (net.truth) free(net.truth);
  643. #ifdef GPU
  644. if(net.input_gpu) cuda_free(net.input_gpu);
  645. if(net.truth_gpu) cuda_free(net.truth_gpu);
  646. #endif
  647. }
  648. // Some day...
  649. layer network_output_layer(network net)
  650. {
  651. int i;
  652. for (i = net.n - 1; i >= 0; --i){
  653. if (net.layers[i].type != COST) break;
  654. }
  655. return net.layers[i];
  656. }
  657. int network_inputs(network net)
  658. {
  659. return net.layers[0].inputs;
  660. }
  661. int network_outputs(network net)
  662. {
  663. return network_output_layer(net).outputs;
  664. }
  665. float *network_output(network net)
  666. {
  667. return network_output_layer(net).output;
  668. }
  669. };