From 1edcf73a73d2007afc61289245763f5cf0c29e10 Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Thu, 04 Dec 2014 07:20:29 +0000
Subject: [PATCH] Detection good, split up col images

---
 src/cnn.c |  306 ++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 220 insertions(+), 86 deletions(-)

diff --git a/src/cnn.c b/src/cnn.c
index 29f9565..46248ed 100644
--- a/src/cnn.c
+++ b/src/cnn.c
@@ -36,6 +36,7 @@
 
 void test_convolutional_layer()
 {
+/*
     int i;
     image dog = load_image("data/dog.jpg",224,224);
     network net = parse_network_cfg("cfg/convolutional.cfg");
@@ -72,6 +73,7 @@
 
     float *gpu_del = calloc(del_size, sizeof(float));
     memcpy(gpu_del, get_network_delta_layer(net, 0), del_size*sizeof(float));
+    */
 
     /*
        start = clock();
@@ -97,6 +99,7 @@
      */
 }
 
+/*
 void test_col2im()
 {
     float col[] =  {1,2,1,2,
@@ -116,13 +119,12 @@
     int ksize = 3;
     int stride = 1;
     int pad = 0;
-    col2im_gpu(col, batch,
-            channels,  height,  width,
-            ksize,  stride, pad, im);
+    //col2im_gpu(col, batch,
+    //        channels,  height,  width,
+    //        ksize,  stride, pad, im);
     int i;
     for(i = 0; i < 16; ++i)printf("%f,", im[i]);
     printf("\n");
-    /*
        float data_im[] = {
        1,2,3,4,
        5,6,7,8,
@@ -134,8 +136,8 @@
        ksize,   stride,  pad, data_col) ;
        for(i = 0; i < 18; ++i)printf("%f,", data_col[i]);
        printf("\n");
-     */
 }
+*/
 
 #endif
 
@@ -158,7 +160,7 @@
     int i;
     clock_t start = clock(), end;
     for(i = 0; i < 1000; ++i){
-        im2col_cpu(dog.data,1, dog.c,  dog.h,  dog.w,  size,  stride, 0, matrix);
+        //im2col_cpu(dog.data,1, dog.c,  dog.h,  dog.w,  size,  stride, 0, matrix);
         gemm(0,0,n,mw,mh,1,filters,mh,matrix,mw,1,edge.data,mw);
     }
     end = clock();
@@ -175,6 +177,7 @@
 
 void verify_convolutional_layer()
 {
+/*
     srand(0);
     int i;
     int n = 1;
@@ -225,6 +228,7 @@
     printf("%f %f\n", avg_image_layer(mj1,0), avg_image_layer(mj2,0));
     show_image(mj1, "forward jacobian");
     show_image(mj2, "backward jacobian");
+    */
 }
 
 void test_load()
@@ -312,18 +316,43 @@
     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;
-    char **labels = get_labels("/home/pjreddie/data/imagenet/cls.labels.list");
-    list *plist = get_paths("/data/imagenet/cls.train.list");
+    list *plist = get_paths("/home/pjreddie/data/imagenet/horse.txt");
     char **paths = (char **)list_to_array(plist);
     printf("%d\n", plist->size);
     clock_t time;
     while(1){
         i += 1;
         time=clock();
-        data train = load_data_random(imgs*net.batch, paths, plist->size, labels, 1000, 256, 256);
+        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();
@@ -335,7 +364,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);
         }
     }
@@ -346,7 +375,7 @@
 {
     float avg_loss = 1;
     //network net = parse_network_cfg("/home/pjreddie/imagenet_backup/alexnet_1270.cfg");
-    network net = parse_network_cfg("cfg/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));
@@ -372,7 +401,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);
         }
     }
@@ -400,6 +429,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();
@@ -412,6 +442,52 @@
     }
 }
 
+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] > .05){
+                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");
+    srand(2222222);
+    clock_t time;
+    char filename[256];
+    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);
+        printf("%s: Predicted in %f seconds.\n", filename, sec(clock()-time));
+        draw_detection(im, predictions);
+        free_image(im);
+    }
+}
+
 void test_imagenet()
 {
     network net = parse_network_cfg("cfg/imagenet_test.cfg");
@@ -424,6 +500,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);
@@ -653,7 +730,7 @@
     float *matrix = calloc(msize, sizeof(float));
     int i;
     for(i = 0; i < 1000; ++i){
-        im2col_cpu(test.data,1,  c,  h,  w,  size,  stride, 0, matrix);
+        //im2col_cpu(test.data,1,  c,  h,  w,  size,  stride, 0, matrix);
         //image render = float_to_image(mh, mw, mc, matrix);
     }
 }
@@ -709,6 +786,60 @@
 #endif
 }
 
+void test_correct_alexnet()
+{
+    char **labels = get_labels("/home/pjreddie/data/imagenet/cls.labels.list");
+    list *plist = get_paths("/data/imagenet/cls.train.list");
+    char **paths = (char **)list_to_array(plist);
+    printf("%d\n", plist->size);
+    clock_t time;
+    int count = 0;
+
+    srand(222222);
+    network net = parse_network_cfg("cfg/alexnet.test");
+    printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
+    int imgs = 1000/net.batch+1;
+    imgs = 1;
+
+    while(++count <= 5){
+        time=clock();
+        data train = load_data_random(imgs*net.batch, paths, plist->size, labels, 1000, 256, 256);
+        //translate_data_rows(train, -144);
+        normalize_data_rows(train);
+        printf("Loaded: %lf seconds\n", sec(clock()-time));
+        time=clock();
+        float loss = train_network_data_cpu(net, train, imgs);
+        printf("%d: %f, %lf seconds, %d images\n", count, loss, sec(clock()-time), imgs*net.batch);
+        free_data(train);
+    }
+#ifdef GPU
+    count = 0;
+    srand(222222);
+    net = parse_network_cfg("cfg/alexnet.test");
+    while(++count <= 5){
+        time=clock();
+        data train = load_data_random(imgs*net.batch, paths, plist->size, labels, 1000, 256, 256);
+        //translate_data_rows(train, -144);
+        normalize_data_rows(train);
+        printf("Loaded: %lf seconds\n", sec(clock()-time));
+        time=clock();
+        float loss = train_network_data_gpu(net, train, imgs);
+        printf("%d: %f, %lf seconds, %d images\n", count, loss, sec(clock()-time), imgs*net.batch);
+        free_data(train);
+    }
+#endif
+}
+
+void test_server()
+{
+    network net = parse_network_cfg("cfg/alexnet.test");
+    server_update(net);
+}
+void test_client()
+{
+    network net = parse_network_cfg("cfg/alexnet.test");
+    client_update(net);
+}
 
 int main(int argc, char *argv[])
 {
@@ -717,94 +848,97 @@
         return 0;
     }
     if(0==strcmp(argv[1], "train")) train_imagenet();
+    else if(0==strcmp(argv[1], "detection")) train_detection_net();
     else if(0==strcmp(argv[1], "asirra")) train_asirra();
     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_correct")) test_correct_alexnet();
     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
     else if(0==strcmp(argv[1], "test_gpu")) test_gpu_blas();
 #endif
-    test_parser();
     fprintf(stderr, "Success!\n");
     return 0;
 }
 
 /*
-void visualize_imagenet_topk(char *filename)
-{
-    int i,j,k,l;
-    int topk = 10;
-    network net = parse_network_cfg("cfg/voc_imagenet.cfg");
-    list *plist = get_paths(filename);
-    node *n = plist->front;
-    int h = voc_size(1), w = voc_size(1);
-    int num = get_network_image(net).c;
-    image **vizs = calloc(num, sizeof(image*));
-    float **score = calloc(num, sizeof(float *));
-    for(i = 0; i < num; ++i){
-        vizs[i] = calloc(topk, sizeof(image));
-        for(j = 0; j < topk; ++j) vizs[i][j] = make_image(h,w,3);
-        score[i] = calloc(topk, sizeof(float));
-    }
+   void visualize_imagenet_topk(char *filename)
+   {
+   int i,j,k,l;
+   int topk = 10;
+   network net = parse_network_cfg("cfg/voc_imagenet.cfg");
+   list *plist = get_paths(filename);
+   node *n = plist->front;
+   int h = voc_size(1), w = voc_size(1);
+   int num = get_network_image(net).c;
+   image **vizs = calloc(num, sizeof(image*));
+   float **score = calloc(num, sizeof(float *));
+   for(i = 0; i < num; ++i){
+   vizs[i] = calloc(topk, sizeof(image));
+   for(j = 0; j < topk; ++j) vizs[i][j] = make_image(h,w,3);
+   score[i] = calloc(topk, sizeof(float));
+   }
 
-    int count = 0;
-    while(n){
-        ++count;
-        char *image_path = (char *)n->val;
-        image im = load_image(image_path, 0, 0);
-        n = n->next;
-        if(im.h < 200 || im.w < 200) continue;
-        printf("Processing %dx%d image\n", im.h, im.w);
-        resize_network(net, im.h, im.w, im.c);
-        //scale_image(im, 1./255);
-        translate_image(im, -144);
-        forward_network(net, im.data, 0, 0);
-        image out = get_network_image(net);
+   int count = 0;
+   while(n){
+   ++count;
+   char *image_path = (char *)n->val;
+   image im = load_image(image_path, 0, 0);
+   n = n->next;
+   if(im.h < 200 || im.w < 200) continue;
+   printf("Processing %dx%d image\n", im.h, im.w);
+   resize_network(net, im.h, im.w, im.c);
+//scale_image(im, 1./255);
+translate_image(im, -144);
+forward_network(net, im.data, 0, 0);
+image out = get_network_image(net);
 
-        int dh = (im.h - h)/(out.h-1);
-        int dw = (im.w - w)/(out.w-1);
-        //printf("%d %d\n", dh, dw);
-        for(k = 0; k < out.c; ++k){
-            float topv = 0;
-            int topi = -1;
-            int topj = -1;
-            for(i = 0; i < out.h; ++i){
-                for(j = 0; j < out.w; ++j){
-                    float val = get_pixel(out, i, j, k);
-                    if(val > topv){
-                        topv = val;
-                        topi = i;
-                        topj = j;
-                    }
-                }
-            }
-            if(topv){
-                image sub = get_sub_image(im, dh*topi, dw*topj, h, w);
-                for(l = 0; l < topk; ++l){
-                    if(topv > score[k][l]){
-                        float swap = score[k][l];
-                        score[k][l] = topv;
-                        topv = swap;
+int dh = (im.h - h)/(out.h-1);
+int dw = (im.w - w)/(out.w-1);
+//printf("%d %d\n", dh, dw);
+for(k = 0; k < out.c; ++k){
+float topv = 0;
+int topi = -1;
+int topj = -1;
+for(i = 0; i < out.h; ++i){
+for(j = 0; j < out.w; ++j){
+float val = get_pixel(out, i, j, k);
+if(val > topv){
+topv = val;
+topi = i;
+topj = j;
+}
+}
+}
+if(topv){
+image sub = get_sub_image(im, dh*topi, dw*topj, h, w);
+for(l = 0; l < topk; ++l){
+if(topv > score[k][l]){
+float swap = score[k][l];
+score[k][l] = topv;
+topv = swap;
 
-                        image swapi = vizs[k][l];
-                        vizs[k][l] = sub;
-                        sub = swapi;
-                    }
-                }
-                free_image(sub);
-            }
-        }
-        free_image(im);
-        if(count%50 == 0){
-            image grid = grid_images(vizs, num, topk);
-            //show_image(grid, "IMAGENET Visualization");
-            save_image(grid, "IMAGENET Grid Single Nonorm");
-            free_image(grid);
-        }
-    }
-    //cvWaitKey(0);
+image swapi = vizs[k][l];
+vizs[k][l] = sub;
+sub = swapi;
+}
+}
+free_image(sub);
+}
+}
+free_image(im);
+if(count%50 == 0){
+image grid = grid_images(vizs, num, topk);
+//show_image(grid, "IMAGENET Visualization");
+save_image(grid, "IMAGENET Grid Single Nonorm");
+free_image(grid);
+}
+}
+//cvWaitKey(0);
 }
 
 void visualize_imagenet_features(char *filename)

--
Gitblit v1.10.0