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.

1055 lines
35 KiB

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