From 8ed0a5538e04002c95ff0757791c52b4ec9a3e96 Mon Sep 17 00:00:00 2001 From: Joseph Redmon Date: Tue, 21 Jul 2015 11:38:36 -0700 Subject: [PATCH] faster resize? --- Makefile | 4 ++-- cfg/yolo.cfg | 2 +- src/data.c | 21 +++++++++++++++++++-- src/image.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/image.h | 2 ++ 5 files changed, 74 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 32ff4c0a..1b422777 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -GPU=0 -OPENCV=0 +GPU=1 +OPENCV=1 DEBUG=0 ARCH= -arch=sm_52 diff --git a/cfg/yolo.cfg b/cfg/yolo.cfg index eef0b695..ba44c4dc 100644 --- a/cfg/yolo.cfg +++ b/cfg/yolo.cfg @@ -1,6 +1,6 @@ [net] batch=64 -subdivisions=64 +subdivisions=2 height=448 width=448 channels=3 diff --git a/src/data.c b/src/data.c index 982ef216..2b618530 100644 --- a/src/data.c +++ b/src/data.c @@ -425,10 +425,18 @@ data load_data_detection_jitter_random(int n, char **paths, int m, int classes, d.X.vals = calloc(d.X.rows, sizeof(float*)); d.X.cols = h*w*3; + clock_t time; + clock_t load = 0; + clock_t resize = 0; + clock_t crop = 0; + int k = num_boxes*num_boxes*(4+classes+background); d.y = make_matrix(n, k); for(i = 0; i < n; ++i){ + time=clock(); image orig = load_image_color(random_paths[i], 0, 0); + load += clock() - time; + time = clock(); int oh = orig.h; int ow = orig.w; @@ -456,17 +464,26 @@ data load_data_detection_jitter_random(int n, char **paths, int m, int classes, int flip = rand_r(&data_seed)%2; image cropped = crop_image(orig, pleft, ptop, swidth, sheight); + + crop += clock() - time; + time = clock(); + float dx = ((float)pleft/ow)/sx; float dy = ((float)ptop /oh)/sy; - free_image(orig); image sized = resize_image(cropped, w, h); - free_image(cropped); if(flip) flip_image(sized); d.X.vals[i] = sized.data; + resize += clock() - time; + time = clock(); + fill_truth_detection(random_paths[i], d.y.vals[i], classes, num_boxes, flip, background, dx, dy, 1./sx, 1./sy); + + free_image(orig); + free_image(cropped); } + printf("load: %f, crop: %f, resize: %f\n", sec(load), sec(crop), sec(resize)); free(random_paths); return d; } diff --git a/src/image.c b/src/image.c index 046de0c9..c499c799 100644 --- a/src/image.c +++ b/src/image.c @@ -584,6 +584,51 @@ image resize_image(image im, int w, int h) return resized; } +image resize_image2(image im, int w, int h) +{ + image resized = make_image(w, h, im.c); + image part = make_image(w, im.h, im.c); + int r, c, k; + float w_scale = (float)(im.w - 1) / (w - 1); + float h_scale = (float)(im.h - 1) / (h - 1); + for(k = 0; k < im.c; ++k){ + for(r = 0; r < im.h; ++r){ + for(c = 0; c < w; ++c){ + float val = 0; + if(c == w-1){ + val = get_pixel(im, im.w-1, r, k); + } else { + float sx = c*w_scale; + int ix = (int) sx; + float dx = sx - ix; + val = (1 - dx) * get_pixel(im, ix, r, k) + dx * get_pixel(im, ix+1, r, k); + } + set_pixel(part, c, r, k, val); + } + } + } + + for(k = 0; k < im.c; ++k){ + for(r = 0; r < h; ++r){ + float sy = r*h_scale; + int iy = (int) sy; + float dy = sy - iy; + for(c = 0; c < w; ++c){ + float val = (1-dy) * get_pixel(part, c, iy, k); + set_pixel(resized, c, r, k, val); + } + if(r == h-1) continue; + for(c = 0; c < w; ++c){ + float val = dy * get_pixel(part, c, iy+1, k); + add_pixel(resized, c, r, k, val); + } + } + } + + free_image(part); + return resized; +} + void test_resize(char *filename) { image im = load_image(filename, 0,0, 3); @@ -728,6 +773,11 @@ void set_pixel(image m, int x, int y, int c, float val) assert(x < m.w && y < m.h && c < m.c); m.data[c*m.h*m.w + y*m.w + x] = val; } +void add_pixel(image m, int x, int y, int c, float val) +{ + assert(x < m.w && y < m.h && c < m.c); + m.data[c*m.h*m.w + y*m.w + x] += val; +} void print_image(image m) { diff --git a/src/image.h b/src/image.h index 578d6900..1506077a 100644 --- a/src/image.h +++ b/src/image.h @@ -28,6 +28,7 @@ image image_distance(image a, image b); void scale_image(image m, float s); image crop_image(image im, int dx, int dy, int w, int h); image resize_image(image im, int w, int h); +image resize_image2(image im, int w, int h); void translate_image(image m, float s); void normalize_image(image p); image rotate_image(image m, float rad); @@ -62,6 +63,7 @@ image load_image_color(char *filename, int w, int h); float get_pixel(image m, int x, int y, int c); float get_pixel_extend(image m, int x, int y, int c); void set_pixel(image m, int x, int y, int c, float val); +void add_pixel(image m, int x, int y, int c, float val); float billinear_interpolate(image im, float x, float y, int c); image get_image_layer(image m, int l);