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.

1191 lines
37 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <assert.h>
  5. #include "activation_layer.h"
  6. #include "activations.h"
  7. #include "avgpool_layer.h"
  8. #include "batchnorm_layer.h"
  9. #include "blas.h"
  10. #include "connected_layer.h"
  11. #include "deconvolutional_layer.h"
  12. #include "convolutional_layer.h"
  13. #include "cost_layer.h"
  14. #include "crnn_layer.h"
  15. #include "crop_layer.h"
  16. #include "detection_layer.h"
  17. #include "dropout_layer.h"
  18. #include "gru_layer.h"
  19. #include "list.h"
  20. #include "local_layer.h"
  21. #include "maxpool_layer.h"
  22. #include "normalization_layer.h"
  23. #include "option_list.h"
  24. #include "parser.h"
  25. #include "region_layer.h"
  26. #include "reorg_layer.h"
  27. #include "rnn_layer.h"
  28. #include "route_layer.h"
  29. #include "shortcut_layer.h"
  30. #include "softmax_layer.h"
  31. #include "lstm_layer.h"
  32. #include "utils.h"
  33. namespace DN{
  34. typedef struct{
  35. char *type;
  36. list *options;
  37. }section;
  38. list *read_cfg(char *filename);
  39. LAYER_TYPE string_to_layer_type(char * type)
  40. {
  41. if (strcmp(type, "[shortcut]")==0) return SHORTCUT;
  42. if (strcmp(type, "[crop]")==0) return CROP;
  43. if (strcmp(type, "[cost]")==0) return COST;
  44. if (strcmp(type, "[detection]")==0) return DETECTION;
  45. if (strcmp(type, "[region]")==0) return REGION;
  46. if (strcmp(type, "[local]")==0) return LOCAL;
  47. if (strcmp(type, "[conv]")==0
  48. || strcmp(type, "[convolutional]")==0) return CONVOLUTIONAL;
  49. if (strcmp(type, "[deconv]")==0
  50. || strcmp(type, "[deconvolutional]")==0) return DECONVOLUTIONAL;
  51. if (strcmp(type, "[activation]")==0) return ACTIVE;
  52. if (strcmp(type, "[net]")==0
  53. || strcmp(type, "[network]")==0) return NETWORK;
  54. if (strcmp(type, "[crnn]")==0) return CRNN;
  55. if (strcmp(type, "[gru]")==0) return GRU;
  56. if (strcmp(type, "[lstm]") == 0) return LSTM;
  57. if (strcmp(type, "[rnn]")==0) return RNN;
  58. if (strcmp(type, "[conn]")==0
  59. || strcmp(type, "[connected]")==0) return CONNECTED;
  60. if (strcmp(type, "[max]")==0
  61. || strcmp(type, "[maxpool]")==0) return MAXPOOL;
  62. if (strcmp(type, "[reorg]")==0) return REORG;
  63. if (strcmp(type, "[avg]")==0
  64. || strcmp(type, "[avgpool]")==0) return AVGPOOL;
  65. if (strcmp(type, "[dropout]")==0) return DROPOUT;
  66. if (strcmp(type, "[lrn]")==0
  67. || strcmp(type, "[normalization]")==0) return NORMALIZATION;
  68. if (strcmp(type, "[batchnorm]")==0) return BATCHNORM;
  69. if (strcmp(type, "[soft]")==0
  70. || strcmp(type, "[softmax]")==0) return SOFTMAX;
  71. if (strcmp(type, "[route]")==0) return ROUTE;
  72. return BLANK;
  73. }
  74. void free_section(section *s)
  75. {
  76. free(s->type);
  77. node *n = s->options->front;
  78. while(n){
  79. kvp *pair = (kvp *)n->val;
  80. free(pair->key);
  81. free(pair);
  82. node *next = n->next;
  83. free(n);
  84. n = next;
  85. }
  86. free(s->options);
  87. free(s);
  88. }
  89. void parse_data(char *data, float *a, int n)
  90. {
  91. int i;
  92. if(!data) return;
  93. char *curr = data;
  94. char *next = data;
  95. int done = 0;
  96. for(i = 0; i < n && !done; ++i){
  97. while(*++next !='\0' && *next != ',');
  98. if(*next == '\0') done = 1;
  99. *next = '\0';
  100. sscanf(curr, "%g", &a[i]);
  101. curr = next+1;
  102. }
  103. }
  104. typedef struct size_params{
  105. int batch;
  106. int inputs;
  107. int h;
  108. int w;
  109. int c;
  110. int index;
  111. int time_steps;
  112. network net;
  113. } size_params;
  114. local_layer parse_local(list *options, size_params params)
  115. {
  116. int n = option_find_int(options, "filters",1);
  117. int size = option_find_int(options, "size",1);
  118. int stride = option_find_int(options, "stride",1);
  119. int pad = option_find_int(options, "pad",0);
  120. char *activation_s = option_find_str(options, "activation", "logistic");
  121. ACTIVATION activation = get_activation(activation_s);
  122. int batch,h,w,c;
  123. h = params.h;
  124. w = params.w;
  125. c = params.c;
  126. batch=params.batch;
  127. if(!(h && w && c)) error("Layer before local layer must output image.");
  128. local_layer _layer;
  129. _layer = make_local_layer(batch, h, w, c, n, size, stride, pad, activation);
  130. return _layer;
  131. }
  132. layer parse_deconvolutional(list *options, size_params params)
  133. {
  134. int n = option_find_int(options, "filters",1);
  135. int size = option_find_int(options, "size",1);
  136. int stride = option_find_int(options, "stride",1);
  137. char *activation_s = option_find_str(options, "activation", "logistic");
  138. ACTIVATION activation = get_activation(activation_s);
  139. int batch,h,w,c;
  140. h = params.h;
  141. w = params.w;
  142. c = params.c;
  143. batch=params.batch;
  144. if(!(h && w && c)) error("Layer before deconvolutional layer must output image.");
  145. int batch_normalize = option_find_int_quiet(options, "batch_normalize", 0);
  146. int pad = option_find_int_quiet(options, "pad",0);
  147. int padding = option_find_int_quiet(options, "padding",0);
  148. if(pad) padding = size/2;
  149. layer l = make_deconvolutional_layer(batch,h,w,c,n,size,stride,padding, activation, batch_normalize, params.net.adam);
  150. return l;
  151. }
  152. convolutional_layer parse_convolutional(list *options, size_params params)
  153. {
  154. int n = option_find_int(options, "filters",1);
  155. int size = option_find_int(options, "size",1);
  156. int stride = option_find_int(options, "stride",1);
  157. int pad = option_find_int_quiet(options, "pad",0);
  158. int padding = option_find_int_quiet(options, "padding",0);
  159. if(pad) padding = size/2;
  160. char *activation_s = option_find_str(options, "activation", "logistic");
  161. ACTIVATION activation = get_activation(activation_s);
  162. int batch,h,w,c;
  163. h = params.h;
  164. w = params.w;
  165. c = params.c;
  166. batch=params.batch;
  167. if(!(h && w && c)) error("Layer before convolutional layer must output image.");
  168. int batch_normalize = option_find_int_quiet(options, "batch_normalize", 0);
  169. int binary = option_find_int_quiet(options, "binary", 0);
  170. int xnor = option_find_int_quiet(options, "xnor", 0);
  171. convolutional_layer layer = make_convolutional_layer(batch,h,w,c,n,size,stride,padding,activation, batch_normalize, binary, xnor, params.net.adam);
  172. layer.flipped = option_find_int_quiet(options, "flipped", 0);
  173. layer.dot = option_find_float_quiet(options, "dot", 0);
  174. return layer;
  175. }
  176. layer parse_crnn(list *options, size_params params)
  177. {
  178. int output_filters = option_find_int(options, "output_filters",1);
  179. int hidden_filters = option_find_int(options, "hidden_filters",1);
  180. char *activation_s = option_find_str(options, "activation", "logistic");
  181. ACTIVATION activation = get_activation(activation_s);
  182. int batch_normalize = option_find_int_quiet(options, "batch_normalize", 0);
  183. layer l = make_crnn_layer(params.batch, params.w, params.h, params.c, hidden_filters, output_filters, params.time_steps, activation, batch_normalize);
  184. l.shortcut = option_find_int_quiet(options, "shortcut", 0);
  185. return l;
  186. }
  187. layer parse_rnn(list *options, size_params params)
  188. {
  189. int output = option_find_int(options, "output",1);
  190. char *activation_s = option_find_str(options, "activation", "logistic");
  191. ACTIVATION activation = get_activation(activation_s);
  192. int batch_normalize = option_find_int_quiet(options, "batch_normalize", 0);
  193. layer l = make_rnn_layer(params.batch, params.inputs, output, params.time_steps, activation, batch_normalize, params.net.adam);
  194. l.shortcut = option_find_int_quiet(options, "shortcut", 0);
  195. return l;
  196. }
  197. layer parse_gru(list *options, size_params params)
  198. {
  199. int output = option_find_int(options, "output",1);
  200. int batch_normalize = option_find_int_quiet(options, "batch_normalize", 0);
  201. layer l = make_gru_layer(params.batch, params.inputs, output, params.time_steps, batch_normalize, params.net.adam);
  202. l.tanh = option_find_int_quiet(options, "tanh", 0);
  203. return l;
  204. }
  205. layer parse_lstm(list *options, size_params params)
  206. {
  207. int output = option_find_int(options, "output", 1);
  208. int batch_normalize = option_find_int_quiet(options, "batch_normalize", 0);
  209. layer l = make_lstm_layer(params.batch, params.inputs, output, params.time_steps, batch_normalize, params.net.adam);
  210. return l;
  211. }
  212. layer parse_connected(list *options, size_params params)
  213. {
  214. int output = option_find_int(options, "output",1);
  215. char *activation_s = option_find_str(options, "activation", "logistic");
  216. ACTIVATION activation = get_activation(activation_s);
  217. int batch_normalize = option_find_int_quiet(options, "batch_normalize", 0);
  218. layer l = make_connected_layer(params.batch, params.inputs, output, activation, batch_normalize, params.net.adam);
  219. return l;
  220. }
  221. softmax_layer parse_softmax(list *options, size_params params)
  222. {
  223. int groups = option_find_int_quiet(options, "groups",1);
  224. softmax_layer layer = make_softmax_layer(params.batch, params.inputs, groups);
  225. layer.temperature = option_find_float_quiet(options, "temperature", 1);
  226. char *tree_file = option_find_str(options, "tree", 0);
  227. if (tree_file) layer.softmax_tree = read_tree(tree_file);
  228. layer.w = params.w;
  229. layer.h = params.h;
  230. layer.c = params.c;
  231. layer.spatial = (int)option_find_float_quiet(options, "spatial", 0);
  232. return layer;
  233. }
  234. layer parse_region(list *options, size_params params)
  235. {
  236. int coords = option_find_int(options, "coords", 4);
  237. int classes = option_find_int(options, "classes", 20);
  238. int num = option_find_int(options, "num", 1);
  239. layer l = make_region_layer(params.batch, params.w, params.h, num, classes, coords);
  240. assert(l.outputs == params.inputs);
  241. l.log = option_find_int_quiet(options, "log", 0);
  242. l.sqrt = option_find_int_quiet(options, "sqrt", 0);
  243. l.softmax = option_find_int(options, "softmax", 0);
  244. l.background = option_find_int_quiet(options, "background", 0);
  245. l.max_boxes = option_find_int_quiet(options, "max",30);
  246. l.jitter = option_find_float(options, "jitter", (float).2);
  247. l.rescore = option_find_int_quiet(options, "rescore", 0);
  248. l.thresh = option_find_float(options, "thresh", (float).5);
  249. l.classfix = option_find_int_quiet(options, "classfix", 0);
  250. l.absolute = option_find_int_quiet(options, "absolute", 0);
  251. l.random = option_find_int_quiet(options, "random", 0);
  252. l.coord_scale = option_find_float(options, "coord_scale", 1);
  253. l.object_scale = option_find_float(options, "object_scale", 1);
  254. l.noobject_scale = option_find_float(options, "noobject_scale", 1);
  255. l.mask_scale = option_find_float(options, "mask_scale", 1);
  256. l.class_scale = option_find_float(options, "class_scale", 1);
  257. l.bias_match = option_find_int_quiet(options, "bias_match",0);
  258. char *tree_file = option_find_str(options, "tree", 0);
  259. if (tree_file) l.softmax_tree = read_tree(tree_file);
  260. char *map_file = option_find_str(options, "map", 0);
  261. if (map_file) l.map = read_map(map_file);
  262. char *a = option_find_str(options, "anchors", 0);
  263. if(a){
  264. int len = strlen(a);
  265. int n = 1;
  266. int i;
  267. for(i = 0; i < len; ++i){
  268. if (a[i] == ',') ++n;
  269. }
  270. for(i = 0; i < n; ++i){
  271. float bias = (float)atof(a);
  272. l.biases[i] = bias;
  273. a = strchr(a, ',')+1;
  274. }
  275. }
  276. return l;
  277. }
  278. detection_layer parse_detection(list *options, size_params params)
  279. {
  280. int coords = option_find_int(options, "coords", 1);
  281. int classes = option_find_int(options, "classes", 1);
  282. int rescore = option_find_int(options, "rescore", 0);
  283. int num = option_find_int(options, "num", 1);
  284. int side = option_find_int(options, "side", 7);
  285. detection_layer layer = make_detection_layer(params.batch, params.inputs, num, side, classes, coords, rescore);
  286. layer.softmax = option_find_int(options, "softmax", 0);
  287. layer.sqrt = option_find_int(options, "sqrt", 0);
  288. layer.max_boxes = option_find_int_quiet(options, "max",30);
  289. layer.coord_scale = option_find_float(options, "coord_scale", 1);
  290. layer.forced = option_find_int(options, "forced", 0);
  291. layer.object_scale = option_find_float(options, "object_scale", 1);
  292. layer.noobject_scale = option_find_float(options, "noobject_scale", 1);
  293. layer.class_scale = option_find_float(options, "class_scale", 1);
  294. layer.jitter = option_find_float(options, "jitter", (float).2);
  295. layer.random = option_find_int_quiet(options, "random", 0);
  296. layer.reorg = option_find_int_quiet(options, "reorg", 0);
  297. return layer;
  298. }
  299. cost_layer parse_cost(list *options, size_params params)
  300. {
  301. char *type_s = option_find_str(options, "type", "sse");
  302. COST_TYPE type = get_cost_type(type_s);
  303. float scale = option_find_float_quiet(options, "scale",1);
  304. cost_layer layer = make_cost_layer(params.batch, params.inputs, type, scale);
  305. layer.ratio = option_find_float_quiet(options, "ratio",0);
  306. layer.noobject_scale = option_find_float_quiet(options, "noobj", 1);
  307. layer.thresh = option_find_float_quiet(options, "thresh",0);
  308. return layer;
  309. }
  310. crop_layer parse_crop(list *options, size_params params)
  311. {
  312. int crop_height = option_find_int(options, "crop_height",1);
  313. int crop_width = option_find_int(options, "crop_width",1);
  314. int flip = option_find_int(options, "flip",0);
  315. float angle = option_find_float(options, "angle",0);
  316. float saturation = option_find_float(options, "saturation",1);
  317. float exposure = option_find_float(options, "exposure",1);
  318. int batch,h,w,c;
  319. h = params.h;
  320. w = params.w;
  321. c = params.c;
  322. batch=params.batch;
  323. if(!(h && w && c)) error("Layer before crop layer must output image.");
  324. int noadjust = option_find_int_quiet(options, "noadjust",0);
  325. crop_layer l = make_crop_layer(batch,h,w,c,crop_height,crop_width,flip, angle, saturation, exposure);
  326. l.shift = option_find_float(options, "shift", 0);
  327. l.noadjust = noadjust;
  328. return l;
  329. }
  330. layer parse_reorg(list *options, size_params params)
  331. {
  332. int stride = option_find_int(options, "stride",1);
  333. int reverse = option_find_int_quiet(options, "reverse",0);
  334. int flatten = option_find_int_quiet(options, "flatten",0);
  335. int extra = option_find_int_quiet(options, "extra",0);
  336. int batch,h,w,c;
  337. h = params.h;
  338. w = params.w;
  339. c = params.c;
  340. batch=params.batch;
  341. if(!(h && w && c)) error("Layer before reorg layer must output image.");
  342. layer layer = make_reorg_layer(batch,w,h,c,stride,reverse, flatten, extra);
  343. return layer;
  344. }
  345. maxpool_layer parse_maxpool(list *options, size_params params)
  346. {
  347. int stride = option_find_int(options, "stride",1);
  348. int size = option_find_int(options, "size",stride);
  349. int padding = option_find_int_quiet(options, "padding", (size-1)/2);
  350. int batch,h,w,c;
  351. h = params.h;
  352. w = params.w;
  353. c = params.c;
  354. batch=params.batch;
  355. if(!(h && w && c)) error("Layer before maxpool layer must output image.");
  356. maxpool_layer layer = make_maxpool_layer(batch,h,w,c,size,stride,padding);
  357. return layer;
  358. }
  359. avgpool_layer parse_avgpool(list *options, size_params params)
  360. {
  361. int batch,w,h,c;
  362. w = params.w;
  363. h = params.h;
  364. c = params.c;
  365. batch=params.batch;
  366. if(!(h && w && c)) error("Layer before avgpool layer must output image.");
  367. avgpool_layer layer = make_avgpool_layer(batch,w,h,c);
  368. return layer;
  369. }
  370. dropout_layer parse_dropout(list *options, size_params params)
  371. {
  372. float probability = option_find_float(options, "probability", (float).5);
  373. dropout_layer layer = make_dropout_layer(params.batch, params.inputs, probability);
  374. layer.out_w = params.w;
  375. layer.out_h = params.h;
  376. layer.out_c = params.c;
  377. return layer;
  378. }
  379. layer parse_normalization(list *options, size_params params)
  380. {
  381. float alpha = option_find_float(options, "alpha", (float).0001);
  382. float beta = option_find_float(options, "beta", (float).75);
  383. float kappa = option_find_float(options, "kappa", 1);
  384. int size = option_find_int(options, "size", 5);
  385. layer l = make_normalization_layer(params.batch, params.w, params.h, params.c, size, alpha, beta, kappa);
  386. return l;
  387. }
  388. layer parse_batchnorm(list *options, size_params params)
  389. {
  390. layer l = make_batchnorm_layer(params.batch, params.w, params.h, params.c);
  391. return l;
  392. }
  393. layer parse_shortcut(list *options, size_params params, network net)
  394. {
  395. char *l = option_find(options, "from");
  396. int index = atoi(l);
  397. if(index < 0) index = params.index + index;
  398. int batch = params.batch;
  399. layer from = net.layers[index];
  400. layer s = make_shortcut_layer(batch, index, params.w, params.h, params.c, from.out_w, from.out_h, from.out_c);
  401. char *activation_s = option_find_str(options, "activation", "linear");
  402. ACTIVATION activation = get_activation(activation_s);
  403. s.activation = activation;
  404. return s;
  405. }
  406. layer parse_activation(list *options, size_params params)
  407. {
  408. char *activation_s = option_find_str(options, "activation", "linear");
  409. ACTIVATION activation = get_activation(activation_s);
  410. layer l = make_activation_layer(params.batch, params.inputs, activation);
  411. l.out_h = params.h;
  412. l.out_w = params.w;
  413. l.out_c = params.c;
  414. l.h = params.h;
  415. l.w = params.w;
  416. l.c = params.c;
  417. return l;
  418. }
  419. route_layer parse_route(list *options, size_params params, network net)
  420. {
  421. char *l = option_find(options, "layers");
  422. int len = strlen(l);
  423. if(!l) error("Route Layer must specify input layers");
  424. int n = 1;
  425. int i;
  426. for(i = 0; i < len; ++i){
  427. if (l[i] == ',') ++n;
  428. }
  429. int *layers = (int*)calloc(n, sizeof(int));
  430. int *sizes = (int*)calloc(n, sizeof(int));
  431. for(i = 0; i < n; ++i){
  432. int index = atoi(l);
  433. l = strchr(l, ',')+1;
  434. if(index < 0) index = params.index + index;
  435. layers[i] = index;
  436. sizes[i] = net.layers[index].outputs;
  437. }
  438. int batch = params.batch;
  439. route_layer layer = make_route_layer(batch, n, layers, sizes);
  440. convolutional_layer first = net.layers[layers[0]];
  441. layer.out_w = first.out_w;
  442. layer.out_h = first.out_h;
  443. layer.out_c = first.out_c;
  444. for(i = 1; i < n; ++i){
  445. int index = layers[i];
  446. convolutional_layer next = net.layers[index];
  447. if(next.out_w == first.out_w && next.out_h == first.out_h){
  448. layer.out_c += next.out_c;
  449. }else{
  450. layer.out_h = layer.out_w = layer.out_c = 0;
  451. }
  452. }
  453. return layer;
  454. }
  455. learning_rate_policy get_policy(char *s)
  456. {
  457. if (strcmp(s, "random")==0) return RANDOM;
  458. if (strcmp(s, "poly")==0) return POLY;
  459. if (strcmp(s, "constant")==0) return CONSTANT;
  460. if (strcmp(s, "step")==0) return STEP;
  461. if (strcmp(s, "exp")==0) return EXP;
  462. if (strcmp(s, "sigmoid")==0) return SIG;
  463. if (strcmp(s, "steps")==0) return STEPS;
  464. fprintf(stderr, "Couldn't find policy %s, going with constant\n", s);
  465. return CONSTANT;
  466. }
  467. void parse_net_options(list *options, network *net)
  468. {
  469. net->batch = option_find_int(options, "batch",1);
  470. net->learning_rate = option_find_float(options, "learning_rate", (float).001);
  471. net->momentum = option_find_float(options, "momentum", (float).9);
  472. net->decay = option_find_float(options, "decay", (float).0001);
  473. int subdivs = option_find_int(options, "subdivisions",1);
  474. net->time_steps = option_find_int_quiet(options, "time_steps",1);
  475. net->notruth = option_find_int_quiet(options, "notruth",0);
  476. net->batch /= subdivs;
  477. net->batch *= net->time_steps;
  478. net->subdivisions = subdivs;
  479. net->adam = option_find_int_quiet(options, "adam", 0);
  480. if(net->adam){
  481. net->B1 = option_find_float(options, "B1", (float).9);
  482. net->B2 = option_find_float(options, "B2", (float).999);
  483. net->eps = option_find_float(options, "eps", (float).0000001);
  484. }
  485. net->h = option_find_int_quiet(options, "height",0);
  486. net->w = option_find_int_quiet(options, "width",0);
  487. net->c = option_find_int_quiet(options, "channels",0);
  488. net->inputs = option_find_int_quiet(options, "inputs", net->h * net->w * net->c);
  489. net->max_crop = option_find_int_quiet(options, "max_crop",net->w*2);
  490. net->min_crop = option_find_int_quiet(options, "min_crop",net->w);
  491. net->center = option_find_int_quiet(options, "center",0);
  492. net->angle = option_find_float_quiet(options, "angle", 0);
  493. net->aspect = option_find_float_quiet(options, "aspect", 1);
  494. net->saturation = option_find_float_quiet(options, "saturation", 1);
  495. net->exposure = option_find_float_quiet(options, "exposure", 1);
  496. net->hue = option_find_float_quiet(options, "hue", 0);
  497. if(!net->inputs && !(net->h && net->w && net->c)) error("No input parameters supplied");
  498. char *policy_s = option_find_str(options, "policy", "constant");
  499. net->policy = get_policy(policy_s);
  500. net->burn_in = option_find_int_quiet(options, "burn_in", 0);
  501. net->power = option_find_float_quiet(options, "power", 4);
  502. if(net->policy == STEP){
  503. net->step = option_find_int(options, "step", 1);
  504. net->scale = option_find_float(options, "scale", 1);
  505. } else if (net->policy == STEPS){
  506. char *l = option_find(options, "steps");
  507. char *p = option_find(options, "scales");
  508. if(!l || !p) error("STEPS policy must have steps and scales in cfg file");
  509. int len = strlen(l);
  510. int n = 1;
  511. int i;
  512. for(i = 0; i < len; ++i){
  513. if (l[i] == ',') ++n;
  514. }
  515. int *steps = (int*)calloc(n, sizeof(int));
  516. float *scales = (float*)calloc(n, sizeof(float));
  517. for(i = 0; i < n; ++i){
  518. int step = atoi(l);
  519. float scale = (float)atof(p);
  520. l = strchr(l, ',')+1;
  521. p = strchr(p, ',')+1;
  522. steps[i] = step;
  523. scales[i] = scale;
  524. }
  525. net->scales = scales;
  526. net->steps = steps;
  527. net->num_steps = n;
  528. } else if (net->policy == EXP){
  529. net->gamma = option_find_float(options, "gamma", 1);
  530. } else if (net->policy == SIG){
  531. net->gamma = option_find_float(options, "gamma", 1);
  532. net->step = option_find_int(options, "step", 1);
  533. } else if (net->policy == POLY || net->policy == RANDOM){
  534. }
  535. net->max_batches = option_find_int(options, "max_batches", 0);
  536. }
  537. int is_network(section *s)
  538. {
  539. return (strcmp(s->type, "[net]")==0
  540. || strcmp(s->type, "[network]")==0);
  541. }
  542. network parse_network_cfg(char *filename)
  543. {
  544. list *sections = read_cfg(filename);
  545. node *n = sections->front;
  546. if(!n) error("Config file has no sections");
  547. network net = make_network(sections->size - 1);
  548. net.gpu_index = gpu_index;
  549. size_params params;
  550. section *s = (section *)n->val;
  551. list *options = s->options;
  552. if(!is_network(s)) error("First section must be [net] or [network]");
  553. parse_net_options(options, &net);
  554. params.h = net.h;
  555. params.w = net.w;
  556. params.c = net.c;
  557. params.inputs = net.inputs;
  558. params.batch = net.batch;
  559. params.time_steps = net.time_steps;
  560. params.net = net;
  561. size_t workspace_size = 0;
  562. n = n->next;
  563. int count = 0;
  564. free_section(s);
  565. fprintf(stderr, "layer filters size input output\n");
  566. while(n){
  567. params.index = count;
  568. fprintf(stderr, "%5d ", count);
  569. s = (section *)n->val;
  570. options = s->options;
  571. layer l;
  572. memset(&l, 0, sizeof(layer));
  573. LAYER_TYPE lt = string_to_layer_type(s->type);
  574. if(lt == CONVOLUTIONAL){
  575. l = parse_convolutional(options, params);
  576. }else if(lt == DECONVOLUTIONAL){
  577. l = parse_deconvolutional(options, params);
  578. }else if(lt == LOCAL){
  579. l = parse_local(options, params);
  580. }else if(lt == ACTIVE){
  581. l = parse_activation(options, params);
  582. }else if(lt == RNN){
  583. l = parse_rnn(options, params);
  584. }else if(lt == GRU){
  585. l = parse_gru(options, params);
  586. }else if (lt == LSTM) {
  587. l = parse_lstm(options, params);
  588. }else if(lt == CRNN){
  589. l = parse_crnn(options, params);
  590. }else if(lt == CONNECTED){
  591. l = parse_connected(options, params);
  592. }else if(lt == CROP){
  593. l = parse_crop(options, params);
  594. }else if(lt == COST){
  595. l = parse_cost(options, params);
  596. }else if(lt == REGION){
  597. l = parse_region(options, params);
  598. }else if(lt == DETECTION){
  599. l = parse_detection(options, params);
  600. }else if(lt == SOFTMAX){
  601. l = parse_softmax(options, params);
  602. net.hierarchy = l.softmax_tree;
  603. }else if(lt == NORMALIZATION){
  604. l = parse_normalization(options, params);
  605. }else if(lt == BATCHNORM){
  606. l = parse_batchnorm(options, params);
  607. }else if(lt == MAXPOOL){
  608. l = parse_maxpool(options, params);
  609. }else if(lt == REORG){
  610. l = parse_reorg(options, params);
  611. }else if(lt == AVGPOOL){
  612. l = parse_avgpool(options, params);
  613. }else if(lt == ROUTE){
  614. l = parse_route(options, params, net);
  615. }else if(lt == SHORTCUT){
  616. l = parse_shortcut(options, params, net);
  617. }else if(lt == DROPOUT){
  618. l = parse_dropout(options, params);
  619. l.output = net.layers[count-1].output;
  620. l.delta = net.layers[count-1].delta;
  621. #ifdef GPU
  622. l.output_gpu = net.layers[count-1].output_gpu;
  623. l.delta_gpu = net.layers[count-1].delta_gpu;
  624. #endif
  625. }else{
  626. fprintf(stderr, "Type not recognized: %s\n", s->type);
  627. }
  628. l.truth = option_find_int_quiet(options, "truth", 0);
  629. l.onlyforward = option_find_int_quiet(options, "onlyforward", 0);
  630. l.stopbackward = option_find_int_quiet(options, "stopbackward", 0);
  631. l.dontload = option_find_int_quiet(options, "dontload", 0);
  632. l.dontloadscales = option_find_int_quiet(options, "dontloadscales", 0);
  633. l.learning_rate_scale = option_find_float_quiet(options, "learning_rate", 1);
  634. l.smooth = option_find_float_quiet(options, "smooth", 0);
  635. option_unused(options);
  636. net.layers[count] = l;
  637. if (l.workspace_size > workspace_size) workspace_size = l.workspace_size;
  638. free_section(s);
  639. n = n->next;
  640. ++count;
  641. if(n){
  642. params.h = l.out_h;
  643. params.w = l.out_w;
  644. params.c = l.out_c;
  645. params.inputs = l.outputs;
  646. }
  647. }
  648. free_list(sections);
  649. layer out = get_network_output_layer(net);
  650. net.outputs = out.outputs;
  651. net.truths = out.outputs;
  652. if(net.layers[net.n-1].truths) net.truths = net.layers[net.n-1].truths;
  653. net.output = out.output;
  654. net.input = (float*)calloc(net.inputs*net.batch, sizeof(float));
  655. net.truth = (float*)calloc(net.truths*net.batch, sizeof(float));
  656. #ifdef GPU
  657. net.output_gpu = out.output_gpu;
  658. net.input_gpu = cuda_make_array(net.input, net.inputs*net.batch);
  659. net.truth_gpu = cuda_make_array(net.truth, net.truths*net.batch);
  660. #endif
  661. if(workspace_size){
  662. //printf("%ld\n", workspace_size);
  663. #ifdef GPU
  664. if(gpu_index >= 0){
  665. net.workspace = cuda_make_array(0, (workspace_size-1)/sizeof(float)+1);
  666. }else {
  667. net.workspace = (float*) calloc(1, workspace_size);
  668. }
  669. #else
  670. net.workspace = (float*)calloc(1, workspace_size);
  671. #endif
  672. }
  673. return net;
  674. }
  675. list *read_cfg(char *filename)
  676. {
  677. FILE *file = fopen(filename, "r");
  678. if(file == 0) file_error(filename);
  679. char *line;
  680. int nu = 0;
  681. list *options = make_list();
  682. section *current = 0;
  683. while((line=fgetl(file)) != 0){
  684. ++ nu;
  685. strip(line);
  686. switch(line[0]){
  687. case '[':
  688. current = (section*)malloc(sizeof(section));
  689. list_insert(options, current);
  690. current->options = make_list();
  691. current->type = line;
  692. break;
  693. case '\0':
  694. case '#':
  695. case ';':
  696. free(line);
  697. break;
  698. default:
  699. if(!read_option(line, current->options)){
  700. fprintf(stderr, "Config file error line %d, could parse: %s\n", nu, line);
  701. free(line);
  702. }
  703. break;
  704. }
  705. }
  706. fclose(file);
  707. return options;
  708. }
  709. void save_convolutional_weights_binary(layer l, FILE *fp)
  710. {
  711. #ifdef GPU
  712. if(gpu_index >= 0){
  713. pull_convolutional_layer(l);
  714. }
  715. #endif
  716. binarize_weights(l.weights, l.n, l.c*l.size*l.size, l.binary_weights);
  717. int size = l.c*l.size*l.size;
  718. int i, j, k;
  719. fwrite(l.biases, sizeof(float), l.n, fp);
  720. if (l.batch_normalize){
  721. fwrite(l.scales, sizeof(float), l.n, fp);
  722. fwrite(l.rolling_mean, sizeof(float), l.n, fp);
  723. fwrite(l.rolling_variance, sizeof(float), l.n, fp);
  724. }
  725. for(i = 0; i < l.n; ++i){
  726. float mean = l.binary_weights[i*size];
  727. if(mean < 0) mean = -mean;
  728. fwrite(&mean, sizeof(float), 1, fp);
  729. for(j = 0; j < size/8; ++j){
  730. int index = i*size + j*8;
  731. unsigned char c = 0;
  732. for(k = 0; k < 8; ++k){
  733. if (j*8 + k >= size) break;
  734. if (l.binary_weights[index + k] > 0) c = (c | 1<<k);
  735. }
  736. fwrite(&c, sizeof(char), 1, fp);
  737. }
  738. }
  739. }
  740. void save_convolutional_weights(layer l, FILE *fp)
  741. {
  742. if(l.binary){
  743. //save_convolutional_weights_binary(l, fp);
  744. //return;
  745. }
  746. #ifdef GPU
  747. if(gpu_index >= 0){
  748. pull_convolutional_layer(l);
  749. }
  750. #endif
  751. int num = l.n*l.c*l.size*l.size;
  752. fwrite(l.biases, sizeof(float), l.n, fp);
  753. if (l.batch_normalize){
  754. fwrite(l.scales, sizeof(float), l.n, fp);
  755. fwrite(l.rolling_mean, sizeof(float), l.n, fp);
  756. fwrite(l.rolling_variance, sizeof(float), l.n, fp);
  757. }
  758. fwrite(l.weights, sizeof(float), num, fp);
  759. }
  760. void save_batchnorm_weights(layer l, FILE *fp)
  761. {
  762. #ifdef GPU
  763. if(gpu_index >= 0){
  764. pull_batchnorm_layer(l);
  765. }
  766. #endif
  767. fwrite(l.scales, sizeof(float), l.c, fp);
  768. fwrite(l.rolling_mean, sizeof(float), l.c, fp);
  769. fwrite(l.rolling_variance, sizeof(float), l.c, fp);
  770. }
  771. void save_connected_weights(layer l, FILE *fp)
  772. {
  773. #ifdef GPU
  774. if(gpu_index >= 0){
  775. pull_connected_layer(l);
  776. }
  777. #endif
  778. fwrite(l.biases, sizeof(float), l.outputs, fp);
  779. fwrite(l.weights, sizeof(float), l.outputs*l.inputs, fp);
  780. if (l.batch_normalize){
  781. fwrite(l.scales, sizeof(float), l.outputs, fp);
  782. fwrite(l.rolling_mean, sizeof(float), l.outputs, fp);
  783. fwrite(l.rolling_variance, sizeof(float), l.outputs, fp);
  784. }
  785. }
  786. void save_weights_upto(network net, char *filename, int cutoff)
  787. {
  788. #ifdef GPU
  789. if(net.gpu_index >= 0){
  790. cuda_set_device(net.gpu_index);
  791. }
  792. #endif
  793. fprintf(stderr, "Saving weights to %s\n", filename);
  794. FILE *fp = fopen(filename, "wb");
  795. if(!fp) file_error(filename);
  796. int major = 0;
  797. int minor = 2;
  798. int revision = 0;
  799. fwrite(&major, sizeof(int), 1, fp);
  800. fwrite(&minor, sizeof(int), 1, fp);
  801. fwrite(&revision, sizeof(int), 1, fp);
  802. fwrite(net.seen, sizeof(size_t), 1, fp);
  803. int i;
  804. for(i = 0; i < net.n && i < cutoff; ++i){
  805. layer l = net.layers[i];
  806. if(l.type == CONVOLUTIONAL || l.type == DECONVOLUTIONAL){
  807. save_convolutional_weights(l, fp);
  808. } if(l.type == CONNECTED){
  809. save_connected_weights(l, fp);
  810. } if(l.type == BATCHNORM){
  811. save_batchnorm_weights(l, fp);
  812. } if(l.type == RNN){
  813. save_connected_weights(*(l.input_layer), fp);
  814. save_connected_weights(*(l.self_layer), fp);
  815. save_connected_weights(*(l.output_layer), fp);
  816. } if (l.type == LSTM) {
  817. save_connected_weights(*(l.wi), fp);
  818. save_connected_weights(*(l.wf), fp);
  819. save_connected_weights(*(l.wo), fp);
  820. save_connected_weights(*(l.wg), fp);
  821. save_connected_weights(*(l.ui), fp);
  822. save_connected_weights(*(l.uf), fp);
  823. save_connected_weights(*(l.uo), fp);
  824. save_connected_weights(*(l.ug), fp);
  825. } if (l.type == GRU) {
  826. if(1){
  827. save_connected_weights(*(l.wz), fp);
  828. save_connected_weights(*(l.wr), fp);
  829. save_connected_weights(*(l.wh), fp);
  830. save_connected_weights(*(l.uz), fp);
  831. save_connected_weights(*(l.ur), fp);
  832. save_connected_weights(*(l.uh), fp);
  833. }else{
  834. save_connected_weights(*(l.reset_layer), fp);
  835. save_connected_weights(*(l.update_layer), fp);
  836. save_connected_weights(*(l.state_layer), fp);
  837. }
  838. } if(l.type == CRNN){
  839. save_convolutional_weights(*(l.input_layer), fp);
  840. save_convolutional_weights(*(l.self_layer), fp);
  841. save_convolutional_weights(*(l.output_layer), fp);
  842. } if(l.type == LOCAL){
  843. #ifdef GPU
  844. if(gpu_index >= 0){
  845. pull_local_layer(l);
  846. }
  847. #endif
  848. int locations = l.out_w*l.out_h;
  849. int size = l.size*l.size*l.c*l.n*locations;
  850. fwrite(l.biases, sizeof(float), l.outputs, fp);
  851. fwrite(l.weights, sizeof(float), size, fp);
  852. }
  853. }
  854. fclose(fp);
  855. }
  856. void save_weights(network net, char *filename)
  857. {
  858. save_weights_upto(net, filename, net.n);
  859. }
  860. void transpose_matrix(float *a, int rows, int cols)
  861. {
  862. float *transpose = (float*)calloc(rows*cols, sizeof(float));
  863. int x, y;
  864. for(x = 0; x < rows; ++x){
  865. for(y = 0; y < cols; ++y){
  866. transpose[y*rows + x] = a[x*cols + y];
  867. }
  868. }
  869. memcpy(a, transpose, rows*cols*sizeof(float));
  870. free(transpose);
  871. }
  872. void load_connected_weights(layer l, FILE *fp, int transpose)
  873. {
  874. fread(l.biases, sizeof(float), l.outputs, fp);
  875. fread(l.weights, sizeof(float), l.outputs*l.inputs, fp);
  876. if(transpose){
  877. transpose_matrix(l.weights, l.inputs, l.outputs);
  878. }
  879. //printf("Biases: %f mean %f variance\n", mean_array(l.biases, l.outputs), variance_array(l.biases, l.outputs));
  880. //printf("Weights: %f mean %f variance\n", mean_array(l.weights, l.outputs*l.inputs), variance_array(l.weights, l.outputs*l.inputs));
  881. if (l.batch_normalize && (!l.dontloadscales)){
  882. fread(l.scales, sizeof(float), l.outputs, fp);
  883. fread(l.rolling_mean, sizeof(float), l.outputs, fp);
  884. fread(l.rolling_variance, sizeof(float), l.outputs, fp);
  885. //printf("Scales: %f mean %f variance\n", mean_array(l.scales, l.outputs), variance_array(l.scales, l.outputs));
  886. //printf("rolling_mean: %f mean %f variance\n", mean_array(l.rolling_mean, l.outputs), variance_array(l.rolling_mean, l.outputs));
  887. //printf("rolling_variance: %f mean %f variance\n", mean_array(l.rolling_variance, l.outputs), variance_array(l.rolling_variance, l.outputs));
  888. }
  889. #ifdef GPU
  890. if(gpu_index >= 0){
  891. push_connected_layer(l);
  892. }
  893. #endif
  894. }
  895. void load_batchnorm_weights(layer l, FILE *fp)
  896. {
  897. fread(l.scales, sizeof(float), l.c, fp);
  898. fread(l.rolling_mean, sizeof(float), l.c, fp);
  899. fread(l.rolling_variance, sizeof(float), l.c, fp);
  900. #ifdef GPU
  901. if(gpu_index >= 0){
  902. push_batchnorm_layer(l);
  903. }
  904. #endif
  905. }
  906. void load_convolutional_weights_binary(layer l, FILE *fp)
  907. {
  908. fread(l.biases, sizeof(float), l.n, fp);
  909. if (l.batch_normalize && (!l.dontloadscales)){
  910. fread(l.scales, sizeof(float), l.n, fp);
  911. fread(l.rolling_mean, sizeof(float), l.n, fp);
  912. fread(l.rolling_variance, sizeof(float), l.n, fp);
  913. }
  914. int size = l.c*l.size*l.size;
  915. int i, j, k;
  916. for(i = 0; i < l.n; ++i){
  917. float mean = 0;
  918. fread(&mean, sizeof(float), 1, fp);
  919. for(j = 0; j < size/8; ++j){
  920. int index = i*size + j*8;
  921. unsigned char c = 0;
  922. fread(&c, sizeof(char), 1, fp);
  923. for(k = 0; k < 8; ++k){
  924. if (j*8 + k >= size) break;
  925. l.weights[index + k] = (c & 1<<k) ? mean : -mean;
  926. }
  927. }
  928. }
  929. #ifdef GPU
  930. if(gpu_index >= 0){
  931. push_convolutional_layer(l);
  932. }
  933. #endif
  934. }
  935. void load_convolutional_weights(layer l, FILE *fp)
  936. {
  937. if(l.binary){
  938. //load_convolutional_weights_binary(l, fp);
  939. //return;
  940. }
  941. int num = l.n*l.c*l.size*l.size;
  942. fread(l.biases, sizeof(float), l.n, fp);
  943. if (l.batch_normalize && (!l.dontloadscales)){
  944. fread(l.scales, sizeof(float), l.n, fp);
  945. fread(l.rolling_mean, sizeof(float), l.n, fp);
  946. fread(l.rolling_variance, sizeof(float), l.n, fp);
  947. if(0){
  948. int i;
  949. for(i = 0; i < l.n; ++i){
  950. printf("%g, ", l.rolling_mean[i]);
  951. }
  952. printf("\n");
  953. for(i = 0; i < l.n; ++i){
  954. printf("%g, ", l.rolling_variance[i]);
  955. }
  956. printf("\n");
  957. }
  958. if(0){
  959. fill_cpu(l.n, 0, l.rolling_mean, 1);
  960. fill_cpu(l.n, 0, l.rolling_variance, 1);
  961. }
  962. if(0){
  963. int i;
  964. for(i = 0; i < l.n; ++i){
  965. printf("%g, ", l.rolling_mean[i]);
  966. }
  967. printf("\n");
  968. for(i = 0; i < l.n; ++i){
  969. printf("%g, ", l.rolling_variance[i]);
  970. }
  971. printf("\n");
  972. }
  973. }
  974. fread(l.weights, sizeof(float), num, fp);
  975. //if(l.c == 3) scal_cpu(num, 1./256, l.weights, 1);
  976. if (l.flipped) {
  977. transpose_matrix(l.weights, l.c*l.size*l.size, l.n);
  978. }
  979. //if (l.binary) binarize_weights(l.weights, l.n, l.c*l.size*l.size, l.weights);
  980. #ifdef GPU
  981. if(gpu_index >= 0){
  982. push_convolutional_layer(l);
  983. }
  984. #endif
  985. }
  986. void load_weights_upto(network *net, char *filename, int start, int cutoff)
  987. {
  988. #ifdef GPU
  989. if(net->gpu_index >= 0){
  990. cuda_set_device(net->gpu_index);
  991. }
  992. #endif
  993. fprintf(stderr, "Loading weights from %s...", filename);
  994. fflush(stdout);
  995. FILE *fp = fopen(filename, "rb");
  996. if(!fp) file_error(filename);
  997. int major;
  998. int minor;
  999. int revision;
  1000. fread(&major, sizeof(int), 1, fp);
  1001. fread(&minor, sizeof(int), 1, fp);
  1002. fread(&revision, sizeof(int), 1, fp);
  1003. if ((major*10 + minor) >= 2){
  1004. fread(net->seen, sizeof(size_t), 1, fp);
  1005. } else {
  1006. int iseen = 0;
  1007. fread(&iseen, sizeof(int), 1, fp);
  1008. *net->seen = iseen;
  1009. }
  1010. int transpose = (major > 1000) || (minor > 1000);
  1011. int i;
  1012. for(i = start; i < net->n && i < cutoff; ++i){
  1013. layer l = net->layers[i];
  1014. if (l.dontload) continue;
  1015. if(l.type == CONVOLUTIONAL || l.type == DECONVOLUTIONAL){
  1016. load_convolutional_weights(l, fp);
  1017. }
  1018. if(l.type == CONNECTED){
  1019. load_connected_weights(l, fp, transpose);
  1020. }
  1021. if(l.type == BATCHNORM){
  1022. load_batchnorm_weights(l, fp);
  1023. }
  1024. if(l.type == CRNN){
  1025. load_convolutional_weights(*(l.input_layer), fp);
  1026. load_convolutional_weights(*(l.self_layer), fp);
  1027. load_convolutional_weights(*(l.output_layer), fp);
  1028. }
  1029. if(l.type == RNN){
  1030. load_connected_weights(*(l.input_layer), fp, transpose);
  1031. load_connected_weights(*(l.self_layer), fp, transpose);
  1032. load_connected_weights(*(l.output_layer), fp, transpose);
  1033. }
  1034. if (l.type == LSTM) {
  1035. load_connected_weights(*(l.wi), fp, transpose);
  1036. load_connected_weights(*(l.wf), fp, transpose);
  1037. load_connected_weights(*(l.wo), fp, transpose);
  1038. load_connected_weights(*(l.wg), fp, transpose);
  1039. load_connected_weights(*(l.ui), fp, transpose);
  1040. load_connected_weights(*(l.uf), fp, transpose);
  1041. load_connected_weights(*(l.uo), fp, transpose);
  1042. load_connected_weights(*(l.ug), fp, transpose);
  1043. }
  1044. if (l.type == GRU) {
  1045. if(1){
  1046. load_connected_weights(*(l.wz), fp, transpose);
  1047. load_connected_weights(*(l.wr), fp, transpose);
  1048. load_connected_weights(*(l.wh), fp, transpose);
  1049. load_connected_weights(*(l.uz), fp, transpose);
  1050. load_connected_weights(*(l.ur), fp, transpose);
  1051. load_connected_weights(*(l.uh), fp, transpose);
  1052. }else{
  1053. load_connected_weights(*(l.reset_layer), fp, transpose);
  1054. load_connected_weights(*(l.update_layer), fp, transpose);
  1055. load_connected_weights(*(l.state_layer), fp, transpose);
  1056. }
  1057. }
  1058. if(l.type == LOCAL){
  1059. int locations = l.out_w*l.out_h;
  1060. int size = l.size*l.size*l.c*l.n*locations;
  1061. fread(l.biases, sizeof(float), l.outputs, fp);
  1062. fread(l.weights, sizeof(float), size, fp);
  1063. #ifdef GPU
  1064. if(gpu_index >= 0){
  1065. push_local_layer(l);
  1066. }
  1067. #endif
  1068. }
  1069. }
  1070. fprintf(stderr, "Done!\n");
  1071. fclose(fp);
  1072. }
  1073. void load_weights(network *net, char *filename)
  1074. {
  1075. load_weights_upto(net, filename, 0, net->n);
  1076. }
  1077. };