Joseph Redmon
2016-06-06 8a767f106677b78a389e1ceffc066501015ec51a
src/convolutional_layer.c
@@ -8,6 +8,10 @@
#include <stdio.h>
#include <time.h>
#ifndef AI2
#define AI2 0
#endif
void swap_binary(convolutional_layer *l)
{
    float *swap = l->filters;
@@ -21,24 +25,6 @@
    #endif
}
void binarize_filters2(float *filters, int n, int size, char *binary, float *scales)
{
    int i, k, f;
    for(f = 0; f < n; ++f){
        float mean = 0;
        for(i = 0; i < size; ++i){
            mean += fabs(filters[f*size + i]);
        }
        mean = mean / size;
        scales[f] = mean;
        for(i = 0; i < size/8; ++i){
            binary[f*size + i] = (filters[f*size + i] > 0) ? 1 : 0;
            for(k = 0; k < 8; ++k){
            }
        }
    }
}
void binarize_filters(float *filters, int n, int size, float *binary)
{
    int i, f;
@@ -54,6 +40,21 @@
    }
}
void binarize_input(float *input, int n, int size, float *binary)
{
    int i, s;
    for(s = 0; s < size; ++s){
        float mean = 0;
        for(i = 0; i < n; ++i){
            mean += fabs(input[i*size + s]);
        }
        mean = mean / n;
        for(i = 0; i < n; ++i){
            binary[i*size + s] = (input[i*size + s] > 0) ? mean : -mean;
        }
    }
}
int convolutional_out_height(convolutional_layer l)
{
    int h = l.h;
@@ -133,6 +134,7 @@
    l.c = c;
    l.n = n;
    l.binary = binary;
    l.xnor = xnor;
    l.batch = batch;
    l.stride = stride;
    l.size = size;
@@ -164,6 +166,10 @@
        l.cfilters = calloc(c*n*size*size, sizeof(char));
        l.scales = calloc(n, sizeof(float));
    }
    if(xnor){
        l.binary_filters = calloc(c*n*size*size, sizeof(float));
        l.binary_input = calloc(l.inputs*l.batch, sizeof(float));
    }
    if(batch_normalize){
        l.scales = calloc(n, sizeof(float));
@@ -199,7 +205,6 @@
        l.binary_filters_gpu = cuda_make_array(l.filters, c*n*size*size);
        l.binary_input_gpu = cuda_make_array(0, l.inputs*l.batch);
    }
    l.xnor = xnor;
    if(batch_normalize){
        l.mean_gpu = cuda_make_array(l.mean, n);
@@ -404,7 +409,9 @@
    int out_w = convolutional_out_width(l);
    int i;
    fill_cpu(l.outputs*l.batch, 0, l.output, 1);
    /*
       if(l.binary){
       binarize_filters(l.filters, l.n, l.c*l.size*l.size, l.binary_filters);
@@ -437,10 +444,24 @@
    }
    */
    if(l.xnor && (l.c%32 != 0 || !AI2)){
        binarize_filters(l.filters, l.n, l.c*l.size*l.size, l.binary_filters);
        swap_binary(&l);
        for(i = 0; i < l.batch; ++i){
            binarize_input(state.input + i*l.inputs, l.c, l.h*l.w, l.binary_input + i*l.inputs);
        }
        state.input = l.binary_input;
    }
    int m = l.n;
    int k = l.size*l.size*l.c;
    int n = out_h*out_w;
    if (l.xnor && l.c%32 == 0 && AI2) {
        forward_xnor_layer(l, state);
        printf("xnor\n");
    } else {
    float *a = l.filters;
    float *b = state.workspace;
    float *c = l.output;
@@ -452,6 +473,7 @@
        c += n*m;
        state.input += l.c*l.h*l.w;
    }
    }
    if(l.batch_normalize){
        forward_batchnorm_layer(l, state);
@@ -459,6 +481,7 @@
    add_bias(l.output, l.biases, l.batch, l.n, out_h*out_w);
    activate_array(l.output, m*n*l.batch, l.activation);
    if(l.binary || l.xnor) swap_binary(&l);
}
void backward_convolutional_layer(convolutional_layer l, network_state state)