From 2baa7bde542ed490f8ab35c82dd3174fddea63f3 Mon Sep 17 00:00:00 2001 From: AlexeyAB Date: Fri, 15 Sep 2017 20:31:48 +0300 Subject: [PATCH] Fixed classifier for AlexNet and Resnet50 --- src/classifier.c | 5 +++-- src/image.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/image.h | 3 +++ src/network.c | 1 + 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/classifier.c b/src/classifier.c index 5e718c51..37f02d51 100644 --- a/src/classifier.c +++ b/src/classifier.c @@ -706,8 +706,9 @@ void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *fi strtok(input, "\n"); } image im = load_image_color(input, 0, 0); - image r = resize_min(im, size); - resize_network(&net, r.w, r.h); + image r = letterbox_image(im, net.w, net.h); + //image r = resize_min(im, size); + //resize_network(&net, r.w, r.h); printf("%d %d\n", r.w, r.h); float *X = r.data; diff --git a/src/image.c b/src/image.c index d44bb776..75364e1c 100644 --- a/src/image.c +++ b/src/image.c @@ -883,6 +883,51 @@ void composite_3d(char *f1, char *f2, char *out, int delta) #endif } +void fill_image(image m, float s) +{ + int i; + for (i = 0; i < m.h*m.w*m.c; ++i) m.data[i] = s; +} + +void letterbox_image_into(image im, int w, int h, image boxed) +{ + int new_w = im.w; + int new_h = im.h; + if (((float)w / im.w) < ((float)h / im.h)) { + new_w = w; + new_h = (im.h * w) / im.w; + } + else { + new_h = h; + new_w = (im.w * h) / im.h; + } + image resized = resize_image(im, new_w, new_h); + embed_image(resized, boxed, (w - new_w) / 2, (h - new_h) / 2); + free_image(resized); +} + +image letterbox_image(image im, int w, int h) +{ + int new_w = im.w; + int new_h = im.h; + if (((float)w / im.w) < ((float)h / im.h)) { + new_w = w; + new_h = (im.h * w) / im.w; + } + else { + new_h = h; + new_w = (im.w * h) / im.h; + } + image resized = resize_image(im, new_w, new_h); + image boxed = make_image(w, h, im.c); + fill_image(boxed, .5); + //int i; + //for(i = 0; i < boxed.w*boxed.h*boxed.c; ++i) boxed.data[i] = 0; + embed_image(resized, boxed, (w - new_w) / 2, (h - new_h) / 2); + free_image(resized); + return boxed; +} + image resize_max(image im, int max) { int w = im.w; @@ -1312,6 +1357,7 @@ image load_image(char *filename, int w, int h, int c) #ifndef CV_VERSION_EPOCH image out = load_image_stb(filename, c); // OpenCV 3.x + //image out = load_image_cv(filename, c); #else image out = load_image_cv(filename, c); // OpenCV 2.4.x #endif diff --git a/src/image.h b/src/image.h index 39c3962d..165a6241 100644 --- a/src/image.h +++ b/src/image.h @@ -30,6 +30,9 @@ image random_crop_image(image im, int w, int h); image random_augment_image(image im, float angle, float aspect, int low, int high, int size); void random_distort_image(image im, float hue, float saturation, float exposure); image resize_image(image im, int w, int h); +void fill_image(image m, float s); +void letterbox_image_into(image im, int w, int h, image boxed); +image letterbox_image(image im, int w, int h); image resize_min(image im, int min); image resize_max(image im, int max); void translate_image(image m, float s); diff --git a/src/network.c b/src/network.c index 79940e65..2d755ae3 100644 --- a/src/network.c +++ b/src/network.c @@ -356,6 +356,7 @@ int resize_network(network *net, int w, int h) }else if(l.type == COST){ resize_cost_layer(&l, inputs); }else{ + fprintf(stderr, "Resizing type %d \n", (int)l.type); error("Cannot resize this type of layer"); } if(l.workspace_size > workspace_size) workspace_size = l.workspace_size;