Joseph Redmon
2014-02-14 118bdd6f624a81c7b43689943485f8d70cbd944e
src/parser.c
@@ -23,18 +23,25 @@
int is_softmax(section *s);
list *read_cfg(char *filename);
network parse_network_cfg(char *filename)
void free_section(section *s)
{
    list *sections = read_cfg(filename);
    network net = make_network(sections->size);
    node *n = sections->front;
    int count = 0;
    free(s->type);
    node *n = s->options->front;
    while(n){
        section *s = (section *)n->val;
        list *options = s->options;
        if(is_convolutional(s)){
        kvp *pair = (kvp *)n->val;
        free(pair->key);
        free(pair);
        node *next = n->next;
        free(n);
        n = next;
    }
    free(s->options);
    free(s);
}
convolutional_layer *parse_convolutional(list *options, network net, int count)
{
    int i;
            int h,w,c;
            int n = option_find_int(options, "filters",1);
            int size = option_find_int(options, "size",1);
@@ -53,11 +60,30 @@
                if(h == 0) error("Layer before convolutional layer must output image.");
            }
            convolutional_layer *layer = make_convolutional_layer(h,w,c,n,size,stride, activation);
            net.types[count] = CONVOLUTIONAL;
            net.layers[count] = layer;
            option_unused(options);
    char *data = option_find_str(options, "data", 0);
    if(data){
        char *curr = data;
        char *next = data;
        for(i = 0; i < n; ++i){
            while(*++next !='\0' && *next != ',');
            *next = '\0';
            sscanf(curr, "%g", &layer->biases[i]);
            curr = next+1;
        }
        else if(is_connected(s)){
        for(i = 0; i < c*n*size*size; ++i){
            while(*++next !='\0' && *next != ',');
            *next = '\0';
            sscanf(curr, "%g", &layer->filters[i]);
            curr = next+1;
        }
    }
    option_unused(options);
    return layer;
}
connected_layer *parse_connected(list *options, network net, int count)
{
    int i;
            int input;
            int output = option_find_int(options, "output",1);
            char *activation_s = option_find_str(options, "activation", "sigmoid");
@@ -68,10 +94,29 @@
                input =  get_network_output_size_layer(net, count-1);
            }
            connected_layer *layer = make_connected_layer(input, output, activation);
            net.types[count] = CONNECTED;
            net.layers[count] = layer;
    char *data = option_find_str(options, "data", 0);
    if(data){
        char *curr = data;
        char *next = data;
        for(i = 0; i < output; ++i){
            while(*++next !='\0' && *next != ',');
            *next = '\0';
            sscanf(curr, "%g", &layer->biases[i]);
            curr = next+1;
        }
        for(i = 0; i < input*output; ++i){
            while(*++next !='\0' && *next != ',');
            *next = '\0';
            sscanf(curr, "%g", &layer->weights[i]);
            curr = next+1;
        }
    }
            option_unused(options);
        }else if(is_softmax(s)){
    return layer;
}
softmax_layer *parse_softmax(list *options, network net, int count)
{
            int input;
            if(count == 0){
                input = option_find_int(options, "input",1);
@@ -79,13 +124,14 @@
                input =  get_network_output_size_layer(net, count-1);
            }
            softmax_layer *layer = make_softmax_layer(input);
            net.types[count] = SOFTMAX;
            net.layers[count] = layer;
            option_unused(options);
        }else if(is_maxpool(s)){
    return layer;
}
maxpool_layer *parse_maxpool(list *options, network net, int count)
{
            int h,w,c;
            int stride = option_find_int(options, "stride",1);
            //char *activation_s = option_find_str(options, "activation", "sigmoid");
            if(count == 0){
                h = option_find_int(options, "height",1);
                w = option_find_int(options, "width",1);
@@ -98,15 +144,44 @@
                if(h == 0) error("Layer before convolutional layer must output image.");
            }
            maxpool_layer *layer = make_maxpool_layer(h,w,c,stride);
    option_unused(options);
    return layer;
}
network parse_network_cfg(char *filename)
{
    list *sections = read_cfg(filename);
    network net = make_network(sections->size);
    node *n = sections->front;
    int count = 0;
    while(n){
        section *s = (section *)n->val;
        list *options = s->options;
        if(is_convolutional(s)){
            convolutional_layer *layer = parse_convolutional(options, net, count);
            net.types[count] = CONVOLUTIONAL;
            net.layers[count] = layer;
        }else if(is_connected(s)){
            connected_layer *layer = parse_connected(options, net, count);
            net.types[count] = CONNECTED;
            net.layers[count] = layer;
        }else if(is_softmax(s)){
            softmax_layer *layer = parse_softmax(options, net, count);
            net.types[count] = SOFTMAX;
            net.layers[count] = layer;
        }else if(is_maxpool(s)){
            maxpool_layer *layer = parse_maxpool(options, net, count);
            net.types[count] = MAXPOOL;
            net.layers[count] = layer;
            option_unused(options);
        }else{
            fprintf(stderr, "Type not recognized: %s\n", s->type);
        }
        free_section(s);
        ++count;
        n = n->next;
    }   
    free_list(sections);
    net.outputs = get_network_output_size(net);
    net.output = get_network_output(net);
    return net;