00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <stdlib.h>
00004
00005 #include "activation_layer.h"
00006 #include "activations.h"
00007 #include "assert.h"
00008 #include "avgpool_layer.h"
00009 #include "batchnorm_layer.h"
00010 #include "blas.h"
00011 #include "connected_layer.h"
00012 #include "convolutional_layer.h"
00013 #include "cost_layer.h"
00014 #include "crnn_layer.h"
00015 #include "crop_layer.h"
00016 #include "detection_layer.h"
00017 #include "dropout_layer.h"
00018 #include "gru_layer.h"
00019 #include "list.h"
00020 #include "local_layer.h"
00021 #include "maxpool_layer.h"
00022 #include "normalization_layer.h"
00023 #include "option_list.h"
00024 #include "parser.h"
00025 #include "region_layer.h"
00026 #include "reorg_layer.h"
00027 #include "rnn_layer.h"
00028 #include "route_layer.h"
00029 #include "shortcut_layer.h"
00030 #include "softmax_layer.h"
00031 #include "utils.h"
00032
00033 typedef struct{
00034 char *type;
00035 list *options;
00036 }section;
00037
00038 list *read_cfg(char *filename);
00039
00040 LAYER_TYPE string_to_layer_type(char * type)
00041 {
00042
00043 if (strcmp(type, "[shortcut]")==0) return SHORTCUT;
00044 if (strcmp(type, "[crop]")==0) return CROP;
00045 if (strcmp(type, "[cost]")==0) return COST;
00046 if (strcmp(type, "[detection]")==0) return DETECTION;
00047 if (strcmp(type, "[region]")==0) return REGION;
00048 if (strcmp(type, "[local]")==0) return LOCAL;
00049 if (strcmp(type, "[conv]")==0
00050 || strcmp(type, "[convolutional]")==0) return CONVOLUTIONAL;
00051 if (strcmp(type, "[activation]")==0) return ACTIVE;
00052 if (strcmp(type, "[net]")==0
00053 || strcmp(type, "[network]")==0) return NETWORK;
00054 if (strcmp(type, "[crnn]")==0) return CRNN;
00055 if (strcmp(type, "[gru]")==0) return GRU;
00056 if (strcmp(type, "[rnn]")==0) return RNN;
00057 if (strcmp(type, "[conn]")==0
00058 || strcmp(type, "[connected]")==0) return CONNECTED;
00059 if (strcmp(type, "[max]")==0
00060 || strcmp(type, "[maxpool]")==0) return MAXPOOL;
00061 if (strcmp(type, "[reorg]")==0) return REORG;
00062 if (strcmp(type, "[avg]")==0
00063 || strcmp(type, "[avgpool]")==0) return AVGPOOL;
00064 if (strcmp(type, "[dropout]")==0) return DROPOUT;
00065 if (strcmp(type, "[lrn]")==0
00066 || strcmp(type, "[normalization]")==0) return NORMALIZATION;
00067 if (strcmp(type, "[batchnorm]")==0) return BATCHNORM;
00068 if (strcmp(type, "[soft]")==0
00069 || strcmp(type, "[softmax]")==0) return SOFTMAX;
00070 if (strcmp(type, "[route]")==0) return ROUTE;
00071 return BLANK;
00072 }
00073
00074 void free_section(section *s)
00075 {
00076 free(s->type);
00077 node *n = s->options->front;
00078 while(n){
00079 kvp *pair = (kvp *)n->val;
00080 free(pair->key);
00081 free(pair);
00082 node *next = n->next;
00083 free(n);
00084 n = next;
00085 }
00086 free(s->options);
00087 free(s);
00088 }
00089
00090 void parse_data(char *data, float *a, int n)
00091 {
00092 int i;
00093 if(!data) return;
00094 char *curr = data;
00095 char *next = data;
00096 int done = 0;
00097 for(i = 0; i < n && !done; ++i){
00098 while(*++next !='\0' && *next != ',');
00099 if(*next == '\0') done = 1;
00100 *next = '\0';
00101 sscanf(curr, "%g", &a[i]);
00102 curr = next+1;
00103 }
00104 }
00105
00106 typedef struct size_params{
00107 int batch;
00108 int inputs;
00109 int h;
00110 int w;
00111 int c;
00112 int index;
00113 int time_steps;
00114 network net;
00115 } size_params;
00116
00117 local_layer parse_local(list *options, size_params params)
00118 {
00119 int n = option_find_int(options, "filters",1);
00120 int size = option_find_int(options, "size",1);
00121 int stride = option_find_int(options, "stride",1);
00122 int pad = option_find_int(options, "pad",0);
00123 char *activation_s = option_find_str(options, "activation", "logistic");
00124 ACTIVATION activation = get_activation(activation_s);
00125
00126 int batch,h,w,c;
00127 h = params.h;
00128 w = params.w;
00129 c = params.c;
00130 batch=params.batch;
00131 if(!(h && w && c)) error("Layer before local layer must output image.");
00132
00133 local_layer layer = make_local_layer(batch,h,w,c,n,size,stride,pad,activation);
00134
00135 return layer;
00136 }
00137
00138 convolutional_layer parse_convolutional(list *options, size_params params)
00139 {
00140 int n = option_find_int(options, "filters",1);
00141 int size = option_find_int(options, "size",1);
00142 int stride = option_find_int(options, "stride",1);
00143 int pad = option_find_int_quiet(options, "pad",0);
00144 int padding = option_find_int_quiet(options, "padding",0);
00145 if(pad) padding = size/2;
00146
00147 char *activation_s = option_find_str(options, "activation", "logistic");
00148 ACTIVATION activation = get_activation(activation_s);
00149
00150 int batch,h,w,c;
00151 h = params.h;
00152 w = params.w;
00153 c = params.c;
00154 batch=params.batch;
00155 if(!(h && w && c)) error("Layer before convolutional layer must output image.");
00156 int batch_normalize = option_find_int_quiet(options, "batch_normalize", 0);
00157 int binary = option_find_int_quiet(options, "binary", 0);
00158 int xnor = option_find_int_quiet(options, "xnor", 0);
00159
00160 convolutional_layer layer = make_convolutional_layer(batch,h,w,c,n,size,stride,padding,activation, batch_normalize, binary, xnor, params.net.adam);
00161 layer.flipped = option_find_int_quiet(options, "flipped", 0);
00162 layer.dot = option_find_float_quiet(options, "dot", 0);
00163 if(params.net.adam){
00164 layer.B1 = params.net.B1;
00165 layer.B2 = params.net.B2;
00166 layer.eps = params.net.eps;
00167 }
00168
00169 return layer;
00170 }
00171
00172 layer parse_crnn(list *options, size_params params)
00173 {
00174 int output_filters = option_find_int(options, "output_filters",1);
00175 int hidden_filters = option_find_int(options, "hidden_filters",1);
00176 char *activation_s = option_find_str(options, "activation", "logistic");
00177 ACTIVATION activation = get_activation(activation_s);
00178 int batch_normalize = option_find_int_quiet(options, "batch_normalize", 0);
00179
00180 layer l = make_crnn_layer(params.batch, params.w, params.h, params.c, hidden_filters, output_filters, params.time_steps, activation, batch_normalize);
00181
00182 l.shortcut = option_find_int_quiet(options, "shortcut", 0);
00183
00184 return l;
00185 }
00186
00187 layer parse_rnn(list *options, size_params params)
00188 {
00189 int output = option_find_int(options, "output",1);
00190 int hidden = option_find_int(options, "hidden",1);
00191 char *activation_s = option_find_str(options, "activation", "logistic");
00192 ACTIVATION activation = get_activation(activation_s);
00193 int batch_normalize = option_find_int_quiet(options, "batch_normalize", 0);
00194 int logistic = option_find_int_quiet(options, "logistic", 0);
00195
00196 layer l = make_rnn_layer(params.batch, params.inputs, hidden, output, params.time_steps, activation, batch_normalize, logistic);
00197
00198 l.shortcut = option_find_int_quiet(options, "shortcut", 0);
00199
00200 return l;
00201 }
00202
00203 layer parse_gru(list *options, size_params params)
00204 {
00205 int output = option_find_int(options, "output",1);
00206 int batch_normalize = option_find_int_quiet(options, "batch_normalize", 0);
00207
00208 layer l = make_gru_layer(params.batch, params.inputs, output, params.time_steps, batch_normalize);
00209
00210 return l;
00211 }
00212
00213 connected_layer parse_connected(list *options, size_params params)
00214 {
00215 int output = option_find_int(options, "output",1);
00216 char *activation_s = option_find_str(options, "activation", "logistic");
00217 ACTIVATION activation = get_activation(activation_s);
00218 int batch_normalize = option_find_int_quiet(options, "batch_normalize", 0);
00219
00220 connected_layer layer = make_connected_layer(params.batch, params.inputs, output, activation, batch_normalize);
00221
00222 return layer;
00223 }
00224
00225 softmax_layer parse_softmax(list *options, size_params params)
00226 {
00227 int groups = option_find_int_quiet(options, "groups",1);
00228 softmax_layer layer = make_softmax_layer(params.batch, params.inputs, groups);
00229 layer.temperature = option_find_float_quiet(options, "temperature", 1);
00230 char *tree_file = option_find_str(options, "tree", 0);
00231 if (tree_file) layer.softmax_tree = read_tree(tree_file);
00232 return layer;
00233 }
00234
00235 layer parse_region(list *options, size_params params)
00236 {
00237 int coords = option_find_int(options, "coords", 4);
00238 int classes = option_find_int(options, "classes", 20);
00239 int num = option_find_int(options, "num", 1);
00240
00241 layer l = make_region_layer(params.batch, params.w, params.h, num, classes, coords);
00242 assert(l.outputs == params.inputs);
00243
00244 l.log = option_find_int_quiet(options, "log", 0);
00245 l.sqrt = option_find_int_quiet(options, "sqrt", 0);
00246
00247 l.softmax = option_find_int(options, "softmax", 0);
00248 l.max_boxes = option_find_int_quiet(options, "max",30);
00249 l.jitter = option_find_float(options, "jitter", .2);
00250 l.rescore = option_find_int_quiet(options, "rescore",0);
00251
00252 l.thresh = option_find_float(options, "thresh", .5);
00253 l.classfix = option_find_int_quiet(options, "classfix", 0);
00254 l.absolute = option_find_int_quiet(options, "absolute", 0);
00255 l.random = option_find_int_quiet(options, "random", 0);
00256
00257 l.coord_scale = option_find_float(options, "coord_scale", 1);
00258 l.object_scale = option_find_float(options, "object_scale", 1);
00259 l.noobject_scale = option_find_float(options, "noobject_scale", 1);
00260 l.class_scale = option_find_float(options, "class_scale", 1);
00261 l.bias_match = option_find_int_quiet(options, "bias_match",0);
00262
00263 char *tree_file = option_find_str(options, "tree", 0);
00264 if (tree_file) l.softmax_tree = read_tree(tree_file);
00265 char *map_file = option_find_str(options, "map", 0);
00266 if (map_file) l.map = read_map(map_file);
00267
00268 char *a = option_find_str(options, "anchors", 0);
00269 if(a){
00270 int len = strlen(a);
00271 int n = 1;
00272 int i;
00273 for(i = 0; i < len; ++i){
00274 if (a[i] == ',') ++n;
00275 }
00276 for(i = 0; i < n; ++i){
00277 float bias = atof(a);
00278 l.biases[i] = bias;
00279 a = strchr(a, ',')+1;
00280 }
00281 }
00282 return l;
00283 }
00284 detection_layer parse_detection(list *options, size_params params)
00285 {
00286 int coords = option_find_int(options, "coords", 1);
00287 int classes = option_find_int(options, "classes", 1);
00288 int rescore = option_find_int(options, "rescore", 0);
00289 int num = option_find_int(options, "num", 1);
00290 int side = option_find_int(options, "side", 7);
00291 detection_layer layer = make_detection_layer(params.batch, params.inputs, num, side, classes, coords, rescore);
00292
00293 layer.softmax = option_find_int(options, "softmax", 0);
00294 layer.sqrt = option_find_int(options, "sqrt", 0);
00295
00296 layer.max_boxes = option_find_int_quiet(options, "max",30);
00297 layer.coord_scale = option_find_float(options, "coord_scale", 1);
00298 layer.forced = option_find_int(options, "forced", 0);
00299 layer.object_scale = option_find_float(options, "object_scale", 1);
00300 layer.noobject_scale = option_find_float(options, "noobject_scale", 1);
00301 layer.class_scale = option_find_float(options, "class_scale", 1);
00302 layer.jitter = option_find_float(options, "jitter", .2);
00303 layer.random = option_find_int_quiet(options, "random", 0);
00304 layer.reorg = option_find_int_quiet(options, "reorg", 0);
00305 return layer;
00306 }
00307
00308 cost_layer parse_cost(list *options, size_params params)
00309 {
00310 char *type_s = option_find_str(options, "type", "sse");
00311 COST_TYPE type = get_cost_type(type_s);
00312 float scale = option_find_float_quiet(options, "scale",1);
00313 cost_layer layer = make_cost_layer(params.batch, params.inputs, type, scale);
00314 layer.ratio = option_find_float_quiet(options, "ratio",0);
00315 return layer;
00316 }
00317
00318 crop_layer parse_crop(list *options, size_params params)
00319 {
00320 int crop_height = option_find_int(options, "crop_height",1);
00321 int crop_width = option_find_int(options, "crop_width",1);
00322 int flip = option_find_int(options, "flip",0);
00323 float angle = option_find_float(options, "angle",0);
00324 float saturation = option_find_float(options, "saturation",1);
00325 float exposure = option_find_float(options, "exposure",1);
00326
00327 int batch,h,w,c;
00328 h = params.h;
00329 w = params.w;
00330 c = params.c;
00331 batch=params.batch;
00332 if(!(h && w && c)) error("Layer before crop layer must output image.");
00333
00334 int noadjust = option_find_int_quiet(options, "noadjust",0);
00335
00336 crop_layer l = make_crop_layer(batch,h,w,c,crop_height,crop_width,flip, angle, saturation, exposure);
00337 l.shift = option_find_float(options, "shift", 0);
00338 l.noadjust = noadjust;
00339 return l;
00340 }
00341
00342 layer parse_reorg(list *options, size_params params)
00343 {
00344 int stride = option_find_int(options, "stride",1);
00345 int reverse = option_find_int_quiet(options, "reverse",0);
00346
00347 int batch,h,w,c;
00348 h = params.h;
00349 w = params.w;
00350 c = params.c;
00351 batch=params.batch;
00352 if(!(h && w && c)) error("Layer before reorg layer must output image.");
00353
00354 layer layer = make_reorg_layer(batch,w,h,c,stride,reverse);
00355 return layer;
00356 }
00357
00358 maxpool_layer parse_maxpool(list *options, size_params params)
00359 {
00360 int stride = option_find_int(options, "stride",1);
00361 int size = option_find_int(options, "size",stride);
00362 int padding = option_find_int_quiet(options, "padding", (size-1)/2);
00363
00364 int batch,h,w,c;
00365 h = params.h;
00366 w = params.w;
00367 c = params.c;
00368 batch=params.batch;
00369 if(!(h && w && c)) error("Layer before maxpool layer must output image.");
00370
00371 maxpool_layer layer = make_maxpool_layer(batch,h,w,c,size,stride,padding);
00372 return layer;
00373 }
00374
00375 avgpool_layer parse_avgpool(list *options, size_params params)
00376 {
00377 int batch,w,h,c;
00378 w = params.w;
00379 h = params.h;
00380 c = params.c;
00381 batch=params.batch;
00382 if(!(h && w && c)) error("Layer before avgpool layer must output image.");
00383
00384 avgpool_layer layer = make_avgpool_layer(batch,w,h,c);
00385 return layer;
00386 }
00387
00388 dropout_layer parse_dropout(list *options, size_params params)
00389 {
00390 float probability = option_find_float(options, "probability", .5);
00391 dropout_layer layer = make_dropout_layer(params.batch, params.inputs, probability);
00392 layer.out_w = params.w;
00393 layer.out_h = params.h;
00394 layer.out_c = params.c;
00395 return layer;
00396 }
00397
00398 layer parse_normalization(list *options, size_params params)
00399 {
00400 float alpha = option_find_float(options, "alpha", .0001);
00401 float beta = option_find_float(options, "beta" , .75);
00402 float kappa = option_find_float(options, "kappa", 1);
00403 int size = option_find_int(options, "size", 5);
00404 layer l = make_normalization_layer(params.batch, params.w, params.h, params.c, size, alpha, beta, kappa);
00405 return l;
00406 }
00407
00408 layer parse_batchnorm(list *options, size_params params)
00409 {
00410 layer l = make_batchnorm_layer(params.batch, params.w, params.h, params.c);
00411 return l;
00412 }
00413
00414 layer parse_shortcut(list *options, size_params params, network net)
00415 {
00416 char *l = option_find(options, "from");
00417 int index = atoi(l);
00418 if(index < 0) index = params.index + index;
00419
00420 int batch = params.batch;
00421 layer from = net.layers[index];
00422
00423 layer s = make_shortcut_layer(batch, index, params.w, params.h, params.c, from.out_w, from.out_h, from.out_c);
00424
00425 char *activation_s = option_find_str(options, "activation", "linear");
00426 ACTIVATION activation = get_activation(activation_s);
00427 s.activation = activation;
00428 return s;
00429 }
00430
00431
00432 layer parse_activation(list *options, size_params params)
00433 {
00434 char *activation_s = option_find_str(options, "activation", "linear");
00435 ACTIVATION activation = get_activation(activation_s);
00436
00437 layer l = make_activation_layer(params.batch, params.inputs, activation);
00438
00439 l.out_h = params.h;
00440 l.out_w = params.w;
00441 l.out_c = params.c;
00442 l.h = params.h;
00443 l.w = params.w;
00444 l.c = params.c;
00445
00446 return l;
00447 }
00448
00449 route_layer parse_route(list *options, size_params params, network net)
00450 {
00451 char *l = option_find(options, "layers");
00452 int len = strlen(l);
00453 if(!l) error("Route Layer must specify input layers");
00454 int n = 1;
00455 int i;
00456 for(i = 0; i < len; ++i){
00457 if (l[i] == ',') ++n;
00458 }
00459
00460 int *layers = calloc(n, sizeof(int));
00461 int *sizes = calloc(n, sizeof(int));
00462 for(i = 0; i < n; ++i){
00463 int index = atoi(l);
00464 l = strchr(l, ',')+1;
00465 if(index < 0) index = params.index + index;
00466 layers[i] = index;
00467 sizes[i] = net.layers[index].outputs;
00468 }
00469 int batch = params.batch;
00470
00471 route_layer layer = make_route_layer(batch, n, layers, sizes);
00472
00473 convolutional_layer first = net.layers[layers[0]];
00474 layer.out_w = first.out_w;
00475 layer.out_h = first.out_h;
00476 layer.out_c = first.out_c;
00477 for(i = 1; i < n; ++i){
00478 int index = layers[i];
00479 convolutional_layer next = net.layers[index];
00480 if(next.out_w == first.out_w && next.out_h == first.out_h){
00481 layer.out_c += next.out_c;
00482 }else{
00483 layer.out_h = layer.out_w = layer.out_c = 0;
00484 }
00485 }
00486
00487 return layer;
00488 }
00489
00490 learning_rate_policy get_policy(char *s)
00491 {
00492 if (strcmp(s, "random")==0) return RANDOM;
00493 if (strcmp(s, "poly")==0) return POLY;
00494 if (strcmp(s, "constant")==0) return CONSTANT;
00495 if (strcmp(s, "step")==0) return STEP;
00496 if (strcmp(s, "exp")==0) return EXP;
00497 if (strcmp(s, "sigmoid")==0) return SIG;
00498 if (strcmp(s, "steps")==0) return STEPS;
00499 fprintf(stderr, "Couldn't find policy %s, going with constant\n", s);
00500 return CONSTANT;
00501 }
00502
00503 void parse_net_options(list *options, network *net)
00504 {
00505 net->batch = option_find_int(options, "batch",1);
00506 net->learning_rate = option_find_float(options, "learning_rate", .001);
00507 net->momentum = option_find_float(options, "momentum", .9);
00508 net->decay = option_find_float(options, "decay", .0001);
00509 int subdivs = option_find_int(options, "subdivisions",1);
00510 net->time_steps = option_find_int_quiet(options, "time_steps",1);
00511 net->batch /= subdivs;
00512 net->batch *= net->time_steps;
00513 net->subdivisions = subdivs;
00514
00515 net->adam = option_find_int_quiet(options, "adam", 0);
00516 if(net->adam){
00517 net->B1 = option_find_float(options, "B1", .9);
00518 net->B2 = option_find_float(options, "B2", .999);
00519 net->eps = option_find_float(options, "eps", .000001);
00520 }
00521
00522 net->h = option_find_int_quiet(options, "height",0);
00523 net->w = option_find_int_quiet(options, "width",0);
00524 net->c = option_find_int_quiet(options, "channels",0);
00525 net->inputs = option_find_int_quiet(options, "inputs", net->h * net->w * net->c);
00526 net->max_crop = option_find_int_quiet(options, "max_crop",net->w*2);
00527 net->min_crop = option_find_int_quiet(options, "min_crop",net->w);
00528
00529 net->angle = option_find_float_quiet(options, "angle", 0);
00530 net->aspect = option_find_float_quiet(options, "aspect", 1);
00531 net->saturation = option_find_float_quiet(options, "saturation", 1);
00532 net->exposure = option_find_float_quiet(options, "exposure", 1);
00533 net->hue = option_find_float_quiet(options, "hue", 0);
00534
00535 if(!net->inputs && !(net->h && net->w && net->c)) error("No input parameters supplied");
00536
00537 char *policy_s = option_find_str(options, "policy", "constant");
00538 net->policy = get_policy(policy_s);
00539 net->burn_in = option_find_int_quiet(options, "burn_in", 0);
00540 if(net->policy == STEP){
00541 net->step = option_find_int(options, "step", 1);
00542 net->scale = option_find_float(options, "scale", 1);
00543 } else if (net->policy == STEPS){
00544 char *l = option_find(options, "steps");
00545 char *p = option_find(options, "scales");
00546 if(!l || !p) error("STEPS policy must have steps and scales in cfg file");
00547
00548 int len = strlen(l);
00549 int n = 1;
00550 int i;
00551 for(i = 0; i < len; ++i){
00552 if (l[i] == ',') ++n;
00553 }
00554 int *steps = calloc(n, sizeof(int));
00555 float *scales = calloc(n, sizeof(float));
00556 for(i = 0; i < n; ++i){
00557 int step = atoi(l);
00558 float scale = atof(p);
00559 l = strchr(l, ',')+1;
00560 p = strchr(p, ',')+1;
00561 steps[i] = step;
00562 scales[i] = scale;
00563 }
00564 net->scales = scales;
00565 net->steps = steps;
00566 net->num_steps = n;
00567 } else if (net->policy == EXP){
00568 net->gamma = option_find_float(options, "gamma", 1);
00569 } else if (net->policy == SIG){
00570 net->gamma = option_find_float(options, "gamma", 1);
00571 net->step = option_find_int(options, "step", 1);
00572 } else if (net->policy == POLY || net->policy == RANDOM){
00573 net->power = option_find_float(options, "power", 1);
00574 }
00575 net->max_batches = option_find_int(options, "max_batches", 0);
00576 }
00577
00578 int is_network(section *s)
00579 {
00580 return (strcmp(s->type, "[net]")==0
00581 || strcmp(s->type, "[network]")==0);
00582 }
00583
00584 network parse_network_cfg(char *filename)
00585 {
00586 list *sections = read_cfg(filename);
00587 node *n = sections->front;
00588 if(!n) error("Config file has no sections");
00589 network net = make_network(sections->size - 1);
00590 net.gpu_index = gpu_index;
00591 size_params params;
00592
00593 section *s = (section *)n->val;
00594 list *options = s->options;
00595 if(!is_network(s)) error("First section must be [net] or [network]");
00596 parse_net_options(options, &net);
00597
00598 params.h = net.h;
00599 params.w = net.w;
00600 params.c = net.c;
00601 params.inputs = net.inputs;
00602 params.batch = net.batch;
00603 params.time_steps = net.time_steps;
00604 params.net = net;
00605
00606 size_t workspace_size = 0;
00607 n = n->next;
00608 int count = 0;
00609 free_section(s);
00610 fprintf(stderr, "layer filters size input output\n");
00611 while(n){
00612 params.index = count;
00613 fprintf(stderr, "%5d ", count);
00614 s = (section *)n->val;
00615 options = s->options;
00616 layer l = {0};
00617 LAYER_TYPE lt = string_to_layer_type(s->type);
00618 if(lt == CONVOLUTIONAL){
00619 l = parse_convolutional(options, params);
00620 }else if(lt == LOCAL){
00621 l = parse_local(options, params);
00622 }else if(lt == ACTIVE){
00623 l = parse_activation(options, params);
00624 }else if(lt == RNN){
00625 l = parse_rnn(options, params);
00626 }else if(lt == GRU){
00627 l = parse_gru(options, params);
00628 }else if(lt == CRNN){
00629 l = parse_crnn(options, params);
00630 }else if(lt == CONNECTED){
00631 l = parse_connected(options, params);
00632 }else if(lt == CROP){
00633 l = parse_crop(options, params);
00634 }else if(lt == COST){
00635 l = parse_cost(options, params);
00636 }else if(lt == REGION){
00637 l = parse_region(options, params);
00638 }else if(lt == DETECTION){
00639 l = parse_detection(options, params);
00640 }else if(lt == SOFTMAX){
00641 l = parse_softmax(options, params);
00642 net.hierarchy = l.softmax_tree;
00643 }else if(lt == NORMALIZATION){
00644 l = parse_normalization(options, params);
00645 }else if(lt == BATCHNORM){
00646 l = parse_batchnorm(options, params);
00647 }else if(lt == MAXPOOL){
00648 l = parse_maxpool(options, params);
00649 }else if(lt == REORG){
00650 l = parse_reorg(options, params);
00651 }else if(lt == AVGPOOL){
00652 l = parse_avgpool(options, params);
00653 }else if(lt == ROUTE){
00654 l = parse_route(options, params, net);
00655 }else if(lt == SHORTCUT){
00656 l = parse_shortcut(options, params, net);
00657 }else if(lt == DROPOUT){
00658 l = parse_dropout(options, params);
00659 l.output = net.layers[count-1].output;
00660 l.delta = net.layers[count-1].delta;
00661 #ifdef GPU
00662 l.output_gpu = net.layers[count-1].output_gpu;
00663 l.delta_gpu = net.layers[count-1].delta_gpu;
00664 #endif
00665 }else{
00666 fprintf(stderr, "Type not recognized: %s\n", s->type);
00667 }
00668 l.dontload = option_find_int_quiet(options, "dontload", 0);
00669 l.dontloadscales = option_find_int_quiet(options, "dontloadscales", 0);
00670 option_unused(options);
00671 net.layers[count] = l;
00672 if (l.workspace_size > workspace_size) workspace_size = l.workspace_size;
00673 free_section(s);
00674 n = n->next;
00675 ++count;
00676 if(n){
00677 params.h = l.out_h;
00678 params.w = l.out_w;
00679 params.c = l.out_c;
00680 params.inputs = l.outputs;
00681 }
00682 }
00683 free_list(sections);
00684 net.outputs = get_network_output_size(net);
00685 net.output = get_network_output(net);
00686 if(workspace_size){
00687
00688 #ifdef GPU
00689 if(gpu_index >= 0){
00690 net.workspace = cuda_make_array(0, (workspace_size-1)/sizeof(float)+1);
00691 }else {
00692 net.workspace = calloc(1, workspace_size);
00693 }
00694 #else
00695 net.workspace = calloc(1, workspace_size);
00696 #endif
00697 }
00698 return net;
00699 }
00700
00701 list *read_cfg(char *filename)
00702 {
00703 FILE *file = fopen(filename, "r");
00704 if(file == 0) file_error(filename);
00705 char *line;
00706 int nu = 0;
00707 list *sections = make_list();
00708 section *current = 0;
00709 while((line=fgetl(file)) != 0){
00710 ++ nu;
00711 strip(line);
00712 switch(line[0]){
00713 case '[':
00714 current = malloc(sizeof(section));
00715 list_insert(sections, current);
00716 current->options = make_list();
00717 current->type = line;
00718 break;
00719 case '\0':
00720 case '#':
00721 case ';':
00722 free(line);
00723 break;
00724 default:
00725 if(!read_option(line, current->options)){
00726 fprintf(stderr, "Config file error line %d, could parse: %s\n", nu, line);
00727 free(line);
00728 }
00729 break;
00730 }
00731 }
00732 fclose(file);
00733 return sections;
00734 }
00735
00736 void save_convolutional_weights_binary(layer l, FILE *fp)
00737 {
00738 #ifdef GPU
00739 if(gpu_index >= 0){
00740 pull_convolutional_layer(l);
00741 }
00742 #endif
00743 binarize_weights(l.weights, l.n, l.c*l.size*l.size, l.binary_weights);
00744 int size = l.c*l.size*l.size;
00745 int i, j, k;
00746 fwrite(l.biases, sizeof(float), l.n, fp);
00747 if (l.batch_normalize){
00748 fwrite(l.scales, sizeof(float), l.n, fp);
00749 fwrite(l.rolling_mean, sizeof(float), l.n, fp);
00750 fwrite(l.rolling_variance, sizeof(float), l.n, fp);
00751 }
00752 for(i = 0; i < l.n; ++i){
00753 float mean = l.binary_weights[i*size];
00754 if(mean < 0) mean = -mean;
00755 fwrite(&mean, sizeof(float), 1, fp);
00756 for(j = 0; j < size/8; ++j){
00757 int index = i*size + j*8;
00758 unsigned char c = 0;
00759 for(k = 0; k < 8; ++k){
00760 if (j*8 + k >= size) break;
00761 if (l.binary_weights[index + k] > 0) c = (c | 1<<k);
00762 }
00763 fwrite(&c, sizeof(char), 1, fp);
00764 }
00765 }
00766 }
00767
00768 void save_convolutional_weights(layer l, FILE *fp)
00769 {
00770 if(l.binary){
00771
00772
00773 }
00774 #ifdef GPU
00775 if(gpu_index >= 0){
00776 pull_convolutional_layer(l);
00777 }
00778 #endif
00779 int num = l.n*l.c*l.size*l.size;
00780 fwrite(l.biases, sizeof(float), l.n, fp);
00781 if (l.batch_normalize){
00782 fwrite(l.scales, sizeof(float), l.n, fp);
00783 fwrite(l.rolling_mean, sizeof(float), l.n, fp);
00784 fwrite(l.rolling_variance, sizeof(float), l.n, fp);
00785 }
00786 fwrite(l.weights, sizeof(float), num, fp);
00787 if(l.adam){
00788 fwrite(l.m, sizeof(float), num, fp);
00789 fwrite(l.v, sizeof(float), num, fp);
00790 }
00791 }
00792
00793 void save_batchnorm_weights(layer l, FILE *fp)
00794 {
00795 #ifdef GPU
00796 if(gpu_index >= 0){
00797 pull_batchnorm_layer(l);
00798 }
00799 #endif
00800 fwrite(l.scales, sizeof(float), l.c, fp);
00801 fwrite(l.rolling_mean, sizeof(float), l.c, fp);
00802 fwrite(l.rolling_variance, sizeof(float), l.c, fp);
00803 }
00804
00805 void save_connected_weights(layer l, FILE *fp)
00806 {
00807 #ifdef GPU
00808 if(gpu_index >= 0){
00809 pull_connected_layer(l);
00810 }
00811 #endif
00812 fwrite(l.biases, sizeof(float), l.outputs, fp);
00813 fwrite(l.weights, sizeof(float), l.outputs*l.inputs, fp);
00814 if (l.batch_normalize){
00815 fwrite(l.scales, sizeof(float), l.outputs, fp);
00816 fwrite(l.rolling_mean, sizeof(float), l.outputs, fp);
00817 fwrite(l.rolling_variance, sizeof(float), l.outputs, fp);
00818 }
00819 }
00820
00821 void save_weights_upto(network net, char *filename, int cutoff)
00822 {
00823 #ifdef GPU
00824 if(net.gpu_index >= 0){
00825 cuda_set_device(net.gpu_index);
00826 }
00827 #endif
00828 fprintf(stderr, "Saving weights to %s\n", filename);
00829 FILE *fp = fopen(filename, "w");
00830 if(!fp) file_error(filename);
00831
00832 int major = 0;
00833 int minor = 1;
00834 int revision = 0;
00835 fwrite(&major, sizeof(int), 1, fp);
00836 fwrite(&minor, sizeof(int), 1, fp);
00837 fwrite(&revision, sizeof(int), 1, fp);
00838 fwrite(net.seen, sizeof(int), 1, fp);
00839
00840 int i;
00841 for(i = 0; i < net.n && i < cutoff; ++i){
00842 layer l = net.layers[i];
00843 if(l.type == CONVOLUTIONAL){
00844 save_convolutional_weights(l, fp);
00845 } if(l.type == CONNECTED){
00846 save_connected_weights(l, fp);
00847 } if(l.type == BATCHNORM){
00848 save_batchnorm_weights(l, fp);
00849 } if(l.type == RNN){
00850 save_connected_weights(*(l.input_layer), fp);
00851 save_connected_weights(*(l.self_layer), fp);
00852 save_connected_weights(*(l.output_layer), fp);
00853 } if(l.type == GRU){
00854 save_connected_weights(*(l.input_z_layer), fp);
00855 save_connected_weights(*(l.input_r_layer), fp);
00856 save_connected_weights(*(l.input_h_layer), fp);
00857 save_connected_weights(*(l.state_z_layer), fp);
00858 save_connected_weights(*(l.state_r_layer), fp);
00859 save_connected_weights(*(l.state_h_layer), fp);
00860 } if(l.type == CRNN){
00861 save_convolutional_weights(*(l.input_layer), fp);
00862 save_convolutional_weights(*(l.self_layer), fp);
00863 save_convolutional_weights(*(l.output_layer), fp);
00864 } if(l.type == LOCAL){
00865 #ifdef GPU
00866 if(gpu_index >= 0){
00867 pull_local_layer(l);
00868 }
00869 #endif
00870 int locations = l.out_w*l.out_h;
00871 int size = l.size*l.size*l.c*l.n*locations;
00872 fwrite(l.biases, sizeof(float), l.outputs, fp);
00873 fwrite(l.weights, sizeof(float), size, fp);
00874 }
00875 }
00876 fclose(fp);
00877 }
00878 void save_weights(network net, char *filename)
00879 {
00880 save_weights_upto(net, filename, net.n);
00881 }
00882
00883 void transpose_matrix(float *a, int rows, int cols)
00884 {
00885 float *transpose = calloc(rows*cols, sizeof(float));
00886 int x, y;
00887 for(x = 0; x < rows; ++x){
00888 for(y = 0; y < cols; ++y){
00889 transpose[y*rows + x] = a[x*cols + y];
00890 }
00891 }
00892 memcpy(a, transpose, rows*cols*sizeof(float));
00893 free(transpose);
00894 }
00895
00896 void load_connected_weights(layer l, FILE *fp, int transpose)
00897 {
00898 fread(l.biases, sizeof(float), l.outputs, fp);
00899 fread(l.weights, sizeof(float), l.outputs*l.inputs, fp);
00900 if(transpose){
00901 transpose_matrix(l.weights, l.inputs, l.outputs);
00902 }
00903
00904
00905 if (l.batch_normalize && (!l.dontloadscales)){
00906 fread(l.scales, sizeof(float), l.outputs, fp);
00907 fread(l.rolling_mean, sizeof(float), l.outputs, fp);
00908 fread(l.rolling_variance, sizeof(float), l.outputs, fp);
00909
00910
00911
00912 }
00913 #ifdef GPU
00914 if(gpu_index >= 0){
00915 push_connected_layer(l);
00916 }
00917 #endif
00918 }
00919
00920 void load_batchnorm_weights(layer l, FILE *fp)
00921 {
00922 fread(l.scales, sizeof(float), l.c, fp);
00923 fread(l.rolling_mean, sizeof(float), l.c, fp);
00924 fread(l.rolling_variance, sizeof(float), l.c, fp);
00925 #ifdef GPU
00926 if(gpu_index >= 0){
00927 push_batchnorm_layer(l);
00928 }
00929 #endif
00930 }
00931
00932 void load_convolutional_weights_binary(layer l, FILE *fp)
00933 {
00934 fread(l.biases, sizeof(float), l.n, fp);
00935 if (l.batch_normalize && (!l.dontloadscales)){
00936 fread(l.scales, sizeof(float), l.n, fp);
00937 fread(l.rolling_mean, sizeof(float), l.n, fp);
00938 fread(l.rolling_variance, sizeof(float), l.n, fp);
00939 }
00940 int size = l.c*l.size*l.size;
00941 int i, j, k;
00942 for(i = 0; i < l.n; ++i){
00943 float mean = 0;
00944 fread(&mean, sizeof(float), 1, fp);
00945 for(j = 0; j < size/8; ++j){
00946 int index = i*size + j*8;
00947 unsigned char c = 0;
00948 fread(&c, sizeof(char), 1, fp);
00949 for(k = 0; k < 8; ++k){
00950 if (j*8 + k >= size) break;
00951 l.weights[index + k] = (c & 1<<k) ? mean : -mean;
00952 }
00953 }
00954 }
00955 #ifdef GPU
00956 if(gpu_index >= 0){
00957 push_convolutional_layer(l);
00958 }
00959 #endif
00960 }
00961
00962 void load_convolutional_weights(layer l, FILE *fp)
00963 {
00964 if(l.binary){
00965
00966
00967 }
00968 int num = l.n*l.c*l.size*l.size;
00969 if(0){
00970 fread(l.biases + ((l.n != 1374)?0:5), sizeof(float), l.n, fp);
00971 if (l.batch_normalize && (!l.dontloadscales)){
00972 fread(l.scales + ((l.n != 1374)?0:5), sizeof(float), l.n, fp);
00973 fread(l.rolling_mean + ((l.n != 1374)?0:5), sizeof(float), l.n, fp);
00974 fread(l.rolling_variance + ((l.n != 1374)?0:5), sizeof(float), l.n, fp);
00975 }
00976 fread(l.weights + ((l.n != 1374)?0:5*l.c*l.size*l.size), sizeof(float), num, fp);
00977 }else{
00978 fread(l.biases, sizeof(float), l.n, fp);
00979 if (l.batch_normalize && (!l.dontloadscales)){
00980 fread(l.scales, sizeof(float), l.n, fp);
00981 fread(l.rolling_mean, sizeof(float), l.n, fp);
00982 fread(l.rolling_variance, sizeof(float), l.n, fp);
00983 }
00984 fread(l.weights, sizeof(float), num, fp);
00985 }
00986 if(l.adam){
00987 fread(l.m, sizeof(float), num, fp);
00988 fread(l.v, sizeof(float), num, fp);
00989 }
00990
00991 if (l.flipped) {
00992 transpose_matrix(l.weights, l.c*l.size*l.size, l.n);
00993 }
00994
00995 #ifdef GPU
00996 if(gpu_index >= 0){
00997 push_convolutional_layer(l);
00998 }
00999 #endif
01000 }
01001
01002
01003 void load_weights_upto(network *net, char *filename, int cutoff)
01004 {
01005 #ifdef GPU
01006 if(net->gpu_index >= 0){
01007 cuda_set_device(net->gpu_index);
01008 }
01009 #endif
01010
01011 fflush(stdout);
01012 FILE *fp = fopen(filename, "rb");
01013 if(!fp) file_error(filename);
01014
01015 int major;
01016 int minor;
01017 int revision;
01018 fread(&major, sizeof(int), 1, fp);
01019 fread(&minor, sizeof(int), 1, fp);
01020 fread(&revision, sizeof(int), 1, fp);
01021 fread(net->seen, sizeof(int), 1, fp);
01022 int transpose = (major > 1000) || (minor > 1000);
01023
01024 int i;
01025 for(i = 0; i < net->n && i < cutoff; ++i){
01026 layer l = net->layers[i];
01027 if (l.dontload) continue;
01028 if(l.type == CONVOLUTIONAL){
01029 load_convolutional_weights(l, fp);
01030 }
01031 if(l.type == CONNECTED){
01032 load_connected_weights(l, fp, transpose);
01033 }
01034 if(l.type == BATCHNORM){
01035 load_batchnorm_weights(l, fp);
01036 }
01037 if(l.type == CRNN){
01038 load_convolutional_weights(*(l.input_layer), fp);
01039 load_convolutional_weights(*(l.self_layer), fp);
01040 load_convolutional_weights(*(l.output_layer), fp);
01041 }
01042 if(l.type == RNN){
01043 load_connected_weights(*(l.input_layer), fp, transpose);
01044 load_connected_weights(*(l.self_layer), fp, transpose);
01045 load_connected_weights(*(l.output_layer), fp, transpose);
01046 }
01047 if(l.type == GRU){
01048 load_connected_weights(*(l.input_z_layer), fp, transpose);
01049 load_connected_weights(*(l.input_r_layer), fp, transpose);
01050 load_connected_weights(*(l.input_h_layer), fp, transpose);
01051 load_connected_weights(*(l.state_z_layer), fp, transpose);
01052 load_connected_weights(*(l.state_r_layer), fp, transpose);
01053 load_connected_weights(*(l.state_h_layer), fp, transpose);
01054 }
01055 if(l.type == LOCAL){
01056 int locations = l.out_w*l.out_h;
01057 int size = l.size*l.size*l.c*l.n*locations;
01058 fread(l.biases, sizeof(float), l.outputs, fp);
01059 fread(l.weights, sizeof(float), size, fp);
01060 #ifdef GPU
01061 if(gpu_index >= 0){
01062 push_local_layer(l);
01063 }
01064 #endif
01065 }
01066 }
01067
01068 fclose(fp);
01069 }
01070
01071 void load_weights(network *net, char *filename)
01072 {
01073 load_weights_upto(net, filename, net->n);
01074 }
01075