Joseph Redmon
2014-12-03 ff67f0347653c35c67ddbafad8dc76bbd868047e
src/cnn.c
@@ -312,7 +312,8 @@
    network net = parse_network_cfg("cfg/detnet.cfg");
    printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
    int imgs = 1000/net.batch+1;
    srand(time(0));
    //srand(time(0));
    srand(23410);
    int i = 0;
    list *plist = get_paths("/home/pjreddie/data/imagenet/horse.txt");
    char **paths = (char **)list_to_array(plist);
@@ -323,6 +324,31 @@
        time=clock();
        data train = load_data_detection_random(imgs*net.batch, paths, plist->size, 256, 256, 8, 8, 256);
        //translate_data_rows(train, -144);
        /*
        image im = float_to_image(256, 256, 3, train.X.vals[0]);
        float *truth = train.y.vals[0];
        int j;
        int r, c;
        for(r = 0; r < 8; ++r){
            for(c = 0; c < 8; ++c){
                j = (r*8 + c) * 5;
                if(truth[j]){
                    int d = 256/8;
                    int y = r*d+truth[j+1]*d;
                    int x = c*d+truth[j+2]*d;
                    int h = truth[j+3]*256;
                    int w = truth[j+4]*256;
                    printf("%f %f %f %f\n", truth[j+1], truth[j+2], truth[j+3], truth[j+4]);
                    printf("%d %d %d %d\n", x, y, w, h);
                    printf("%d %d %d %d\n", x-w/2, y-h/2, x+w/2, y+h/2);
                    draw_box(im, x-w/2, y-h/2, x+w/2, y+h/2);
                }
            }
        }
        show_image(im, "box");
        cvWaitKey(0);
        */
        normalize_data_rows(train);
        printf("Loaded: %lf seconds\n", sec(clock()-time));
        time=clock();
@@ -334,7 +360,7 @@
        free_data(train);
        if(i%10==0){
            char buff[256];
            sprintf(buff, "/home/pjreddie/imagenet_backup/imagenet_%d.cfg", i);
            sprintf(buff, "/home/pjreddie/imagenet_backup/detnet_%d.cfg", i);
            save_network(net, buff);
        }
    }
@@ -345,7 +371,7 @@
{
    float avg_loss = 1;
    //network net = parse_network_cfg("/home/pjreddie/imagenet_backup/alexnet_1270.cfg");
    network net = parse_network_cfg("cfg/trained_alexnet.cfg");
    network net = parse_network_cfg("cfg/alexnet.part");
    printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
    int imgs = 1000/net.batch+1;
    srand(time(0));
@@ -371,7 +397,7 @@
        free_data(train);
        if(i%10==0){
            char buff[256];
            sprintf(buff, "/home/pjreddie/imagenet_backup/imagenet_%d.cfg", i);
            sprintf(buff, "/home/pjreddie/imagenet_backup/alexnet_%d.cfg", i);
            save_network(net, buff);
        }
    }
@@ -399,6 +425,7 @@
        char **part = paths+(i*m/splits);
        int num = (i+1)*m/splits - i*m/splits;
        data val = load_data(part, num, labels, 1000, 256, 256);
        normalize_data_rows(val);
        printf("Loaded: %d images in %lf seconds\n", val.X.rows, sec(clock()-time));
        time=clock();
@@ -411,25 +438,48 @@
    }
}
void draw_detection(image im, float *box)
{
    int j;
    int r, c;
    for(r = 0; r < 8; ++r){
        for(c = 0; c < 8; ++c){
            j = (r*8 + c) * 5;
            printf("Prob: %f\n", box[j]);
            if(box[j] > .999){
                int d = 256/8;
                int y = r*d+box[j+1]*d;
                int x = c*d+box[j+2]*d;
                int h = box[j+3]*256;
                int w = box[j+4]*256;
                printf("%f %f %f %f\n", box[j+1], box[j+2], box[j+3], box[j+4]);
                printf("%d %d %d %d\n", x, y, w, h);
                printf("%d %d %d %d\n", x-w/2, y-h/2, x+w/2, y+h/2);
                draw_box(im, x-w/2, y-h/2, x+w/2, y+h/2);
            }
        }
    }
    show_image(im, "box");
    cvWaitKey(0);
}
void test_detection()
{
    network net = parse_network_cfg("cfg/detnet_test.cfg");
    //imgs=1;
    srand(2222222);
    int i = 0;
    clock_t time;
    char filename[256];
    int indexes[10];
    while(1){
        fgets(filename, 256, stdin);
        strtok(filename, "\n");
        image im = load_image_color(filename, 256, 256);
        z_normalize_image(im);
        printf("%d %d %d\n", im.h, im.w, im.c);
        float *X = im.data;
        time=clock();
        float *predictions = network_predict(net, X);
        top_predictions(net, 10, indexes);
        printf("%s: Predicted in %f seconds.\n", filename, sec(clock()-time));
        draw_detection(im, predictions);
        free_image(im);
    }
}
@@ -446,6 +496,7 @@
    int indexes[10];
    while(1){
        fgets(filename, 256, stdin);
        strtok(filename, "\n");
        image im = load_image_color(filename, 256, 256);
        z_normalize_image(im);
        printf("%d %d %d\n", im.h, im.w, im.c);
@@ -731,6 +782,14 @@
#endif
}
void test_server()
{
    server_update();
}
void test_client()
{
    client_update();
}
int main(int argc, char *argv[])
{
@@ -744,6 +803,9 @@
    else if(0==strcmp(argv[1], "nist")) train_nist();
    else if(0==strcmp(argv[1], "test_correct")) test_gpu_net();
    else if(0==strcmp(argv[1], "test")) test_imagenet();
    else if(0==strcmp(argv[1], "server")) test_server();
    else if(0==strcmp(argv[1], "client")) test_client();
    else if(0==strcmp(argv[1], "detect")) test_detection();
    else if(0==strcmp(argv[1], "visualize")) test_visualize(argv[2]);
    else if(0==strcmp(argv[1], "valid")) validate_imagenet(argv[2]);
#ifdef GPU