Yolov3源码解析:reorg_cpu

函数原型如下,

void reorg_cpu(float *x, int out_w, int out_h, int out_c, int batch, int stride, int forward, float *out)
{
    int b,i,j,k;
    int in_c = out_c/(stride*stride); 

    //printf("\n out_c = %d, out_w = %d, out_h = %d, stride = %d, forward = %d \n", out_c, out_w, out_h, stride, forward);
    //printf("  in_c = %d,  in_w = %d,  in_h = %d \n", in_c, out_w*stride, out_h*stride);

    for(b = 0; b < batch; ++b){
        for(k = 0; k < out_c; ++k){
            for(j = 0; j < out_h; ++j){
                for(i = 0; i < out_w; ++i){
                    int in_index  = i + out_w*(j + out_h*(k + out_c*b));
                    int c2 = k % in_c;     // 分成4个后属于哪个,求具体位置,e.g. 125%64 = 61
                    int offset = k / in_c; // 分成4个后属于哪个 e.g. 125/64=1
                    int w2 = i*stride + offset % stride; 
                    int h2 = j*stride + offset / stride;
                    int out_index = w2 + out_w*stride*(h2 + out_h*stride*(c2 + in_c*b));
                    if(forward) out[out_index] = x[in_index];    // used by default for forward (i.e. forward = 0)
                    else out[in_index] = x[out_index];
                }
            }
        }
    }
}

考虑 ! forward 的情况,相当于一个大channel拆分成stride*stride个子channel
考虑  forward 的情况,相当于stride*stride个子channel合成一个大channel
例如stride=2时,每个子channel中矩阵的大小为8*8=64,则大channel矩阵的大小为16*16=256

输入矩阵为8x256x8x8:
in_index = i(col) + width*j(row) + width*heigth*c + width*hight*out_channel*b
表示:
第b个batch, 第c个channel, 第j个行, 第i个列,

输出矩阵为8x64x16x16:(注意此矩阵宽为width*stride, 高为height*stride)
out_index = w2 + width*stride*h2 + width*stride*height*stride*c2 + width*stride*height*stride*in_channel*b
表示:第b个batch, 第c2个channel, 第h2个行, 第w2个列,

根据源码中的计算,举个例子,如图,

k = channel = 5
-> i j k b = 3,4,5,6 ==> c2 = k%64 = 5 offset = 5/64=0 w2 = 6 h2 = 8
-> i j k b = 3,5,5,6 ==> c2 = k%64 = 5 offset = 5/64=0 w2 = 6 h2 = 10
-> i j k b = 4,4,5,6 ==> c2 = k%64 = 5 offset = 5/64=0 w2 = 8 h2 = 8
-> i j k b = 4,5,5,6 ==> c2 = k%64 = 5 offset = 5/64=0 w2 = 8 h2 = 10

k = chanel = 64+5 = 69
-> i j k b = 3,4,68,6 ==> c2 = k%64 = 5 offset = 69/64=1 w2 = 7 h2 = 8
-> i j k b = 3,5,68,6 ==> c2 = k%64 = 5 offset = 69/64=1 w2 = 7 h2 = 10
-> i j k b = 4,4,68,6 ==> c2 = k%64 = 5 offset = 69/64=1 w2 = 9 h2 = 8
-> i j k b = 4,5,68,6 ==> c2 = k%64 = 5 offset = 69/64=1 w2 = 9 h2 = 10

 

k = chanel = 64 + 64 + 5 = 133
-> i j k b = 3,4,68,6 ==> c2 = k%64 = 5 offset = 133/64=2 w2 = 6 h2 = 9
-> i j k b = 3,5,68,6 ==> c2 = k%64 = 5 offset = 133/64=2 w2 = 6 h2 = 11
-> i j k b = 4,4,68,6 ==> c2 = k%64 = 5 offset = 133/64=2 w2 = 8 h2 = 9
-> i j k b = 4,5,68,6 ==> c2 = k%64 = 5 offset = 133/64=2 w2 = 8 h2 = 11

 

k = chanel = 64 + 64 + 64 + 5 = 192+5 = 197
-> i j k b = 3,4,68,6 ==> c2 = k%64 = 5 offset = 197/64=3 w2 = 7 h2 = 9
-> i j k b = 3,5,68,6 ==> c2 = k%64 = 5 offset = 197/64=3 w2 = 7 h2 = 11
-> i j k b = 4,4,68,6 ==> c2 = k%64 = 5 offset = 197/64=3 w2 = 9 h2 = 9
-> i j k b = 4,5,68,6 ==> c2 = k%64 = 5 offset = 197/64=3 w2 = 9 h2 = 11

posted @ 2018-09-30 15:18  SpaceVision  阅读(53)  评论(0编辑  收藏  举报