darknet im2col_cpu analysis

本文深入探讨了卷积运算的优化方法,通过将N*N的卷积运算转换为N*N个1*1的卷积运算,有效提高了计算效率。文章提供了在VC2017中调试过的代码实例,详细解释了im2col_cpu函数的工作原理,该函数能够为每个卷积核变量做好乘法准备。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本来想走捷径但发现网络上很多理解是错误的,自己调试了下。

从这里就完全可以知道,这个函数其实是为每个卷积核变量需要做乘法做准备,基本上就是把N*N的卷积运算

转换成N*N个的1*1卷积运算

这段代码可以直接拷贝过去使用,我是在VC2017里面调试过。

#include <stdio.h>

#define tchannel 2
#define theight  5
#define twidth   5
#define tksize   3
#define tstride  2	
#define tpad     0	



int im_data[tchannel][theight][twidth];
int data_col[tchannel*theight*twidth];

void data_init(void)
{
	static int i, j, k;
	int m = 0;
	for (size_t i = 0; i < tchannel; i++)
	{
		printf("{");
		for (size_t j = 0; j < theight; j++)
		{
			printf("\t");
			for (size_t k = 0; k < twidth; k++)
			{
                                m++;
				im_data[i][j][k] = m;
				
				printf( "%d,",im_data[i][j][k]);
			}

			printf("\n");
		}
		printf("}");
		printf("\n");
	}
}

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;  //output size height
	int width_col = (width + 2 * pad - ksize) / stride + 1;    //output size height

	printf("------%d--%d----------\n", height_col,width_col);

	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;
		//这两层循环是用卷积核把图像遍历一遍
		printf("{" );
		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);

				printf("%d,", data_col[col_index]);
			}
		}
		printf("}-->x%d\n",c);
	}
}


int main()
{
	data_init();
	im2col_cpu((int*)im_data, tchannel, theight, twidth,
		tksize, tstride, tpad, (int*)data_col);

	while (1);

}

运行结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值