darknet源码解读-im2col_cpu

  关于im2col_cpu网上已经有不少优秀的解读博文,我不再复述,可以参考以下几篇文章:

https://blog.csdn.net/mrhiuser/article/details/52672824

https://blog.csdn.net/dwyane12138/article/details/78449898

我这里只是将这段代码单独摘出来做了一个小测试,给定一个输入,看看输出究竟是什么,以佐证自己的理解。代码主要参数如下:

输入:3通道3x3矩阵,使用一维数组表示,如图1。

卷积核尺寸:2x2

填充:0

步长:1

                                                                   图1:输入矩阵的一维数组表示

示例代码:

#include <stdio.h>
#include <stdlib.h>

int conv_out_height(h, pad, size, stride) {
        return (h + 2*pad - size) / stride + 1;
}

int conv_out_width(w, pad, size, stride) {
        return (w + 2*pad - size) / stride + 1;
}

int im2col_get_pixel(int *im, int height, int width, int channels,
                        int row, int col, int channel, int pad)
{
    row -= pad;
    col -= pad;

    if (row < 0 || col < 0 ||
        row >= height || col >= width) return 0;
    return im[col + width*(row + height*channel)];
}

//From Berkeley Vision's Caffe!
//https://github.com/BVLC/caffe/blob/master/LICENSE
void im2col_cpu(int* data_im,
     int channels,  int height,  int width,
     int ksize,  int stride, int pad, int* data_col) 
{
    int c,h,w;
    int height_col = (height + 2*pad - ksize) / stride + 1;
    int width_col = (width + 2*pad - ksize) / stride + 1;

    int channels_col = channels * ksize * ksize;
    for (c = 0; c < channels_col; ++c) { //卷积核参数个数
        int w_offset = c % ksize;
        int h_offset = (c / ksize) % ksize;
        int c_im = c / ksize / ksize;
        for (h = 0; h < height_col; ++h) {
            for (w = 0; w < width_col; ++w) {
                int im_row = h_offset + h * stride;
                int im_col = w_offset + w * stride;
                int col_index = (c * height_col + h) * width_col + w;
                data_col[col_index] = im2col_get_pixel(data_im, height, width, channels,
                        im_row, im_col, c_im, pad);
            }
        }
    }
}

int main(int argc, char* argv[]) {
        int *data_im=NULL;
        int *data_col=NULL;
        int channels=3,height=3,width=3;
        int ksize=2,stride=1,pad=0;
        int out_w,out_h;
        int workspace_size;

        int inputs = height * width * channels;
        data_im = (int*)malloc(inputs * sizeof(int));
        if (!data_im) {
                printf("malloc error\n");
                exit(EXIT_FAILURE);
        }

        out_w = conv_out_width(width, pad, ksize, stride);
        out_h = conv_out_width(height, pad, ksize, stride);
        workspace_size = out_h * out_w * ksize * ksize * channels;

        data_col = (int*)malloc(workspace_size * sizeof(int));
        if (!data_col) {
                printf("malloc error\n");
                exit(EXIT_FAILURE);
        }

        //init image
        for (int i=0; i<inputs; i++) data_im[i] = i;

        im2col_cpu(data_im, channels, height, width, ksize, stride, pad, data_col);

        printf("data_im:\n");
        for (int i=0; i<inputs; i++) {
                printf("%-3d", data_im[i]);
                //if( (i+1) % 4 == 0) printf("\n");
        }

        printf("\ndata_col:\n");
        for (int i=0; i<workspace_size; i++) {
                printf("%-3d", data_col[i]);
                //if( (i+1) % 4 == 0) printf("\n");
        }
        printf("\n");

        free(data_im);
        free(data_col);

        exit(EXIT_SUCCESS);
}

运行程序,观察结果:

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值