Joseph Redmon
2015-07-21 8ed0a5538e04002c95ff0757791c52b4ec9a3e96
faster resize?
5 files modified
79 ■■■■■ changed files
Makefile 4 ●●●● patch | view | raw | blame | history
cfg/yolo.cfg 2 ●●● patch | view | raw | blame | history
src/data.c 21 ●●●●● patch | view | raw | blame | history
src/image.c 50 ●●●●● patch | view | raw | blame | history
src/image.h 2 ●●●●● patch | view | raw | blame | history
Makefile
@@ -1,5 +1,5 @@
GPU=0
OPENCV=0
GPU=1
OPENCV=1
DEBUG=0
ARCH= -arch=sm_52
cfg/yolo.cfg
@@ -1,6 +1,6 @@
[net]
batch=64
subdivisions=64
subdivisions=2
height=448
width=448
channels=3
src/data.c
@@ -425,10 +425,18 @@
    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 @@
        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;
}
src/image.c
@@ -584,6 +584,51 @@
    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 @@
    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)
{
src/image.h
@@ -28,6 +28,7 @@
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 @@
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);