C++卷积计算实现 张一极
如果是全连接的nerual network,输入是img(100*100),其参数,100 x 100 x 3,每个通道含有100x100的参数量,三个通道,第一层的nerual network假设为1000个nerual,第一层的参数量为30000 x 1000,所以引入了卷积,有目的地放弃一些权重,卷积其实可以看作,放弃了一部分权重的dnn,那么它对应的反向传播,只需要在最终的upload的结果上,不更新某一些参数即可,具体的后续实现会进行记录。
卷积作为一种拟合,把图像矩阵每一部分都变换成一个新的值,其核心就是权重的封装:
filter的四个数据,通过不断刷新去获取最合适的四个值:
image-2020041715471115(图片请查看原文)
运算的具体过程是这样的:(stride = 1)
image-20200417155643738(图片请查看原文)
每一次卷积都是使用等同于卷积核大小的尺寸数据进行计算,也就是黄色区域每一个像素乘以卷积核对应位置的每一个参数:
image-20200417161144994(图片请查看原文)
image-20200417162127544(图片请查看原文)
output:
cnn中的卷积方式不止有这一种,另外两种方式分别是:
1.(mode = full)从卷积核的第一个卷积接触到的element进行直接卷积:
image-20200418153936720
2.(mode = same)
image-20200418154257098
第三种就是最普遍的情况:
image-20200418154354652
代码实现,mode = 0:
1
Matrix convelement(Matrix mid1,Matrix kernel,int kernelsize = 2,int stride = 1)
2
{
3
Matrix convresult = CreateMatrix(((mid1.row-kernelsize)/stride)+1,((mid1.col-kernelsize)/stride)+1);
4
for(int x = 0;x<=(mid1.row-kernelsize)/stride;x+=stride)
5
{
6
for(int y = 0;y<=(mid1.col-kernelsize)/stride;y+=stride)
7
{
8
Matrix croppic = iloc(mid1,x,x+kernel.col,y,y+kernel.row);
9
changeva(convresult,x,y,matrixsum(mulsimple(croppic,kernel)));
10
}
11
}
12
// cout<<"row: "<<convresult.row<<" , "<<"col: "<<convresult.col<<endl;
13
// coutmat(convresult);
14
return convresult;
15
}
16
/*
17
parameter:
18
Matrix mid1,
19
int inputdim = 3
20
int outputchannels = 3
21
int stride = 1
22
int kernelsize = 2
23
int mode = 0
24
int padding = 0
25
*/
26
double convtest(Matrix mid1,int inputdim = 3,int outputchannels = 3,int stride = 1,int kernelsize = 2,int mode = 0,int padding = 0)
27
{
28
// coutmat(mid1);
29
Matrix midrgb[inputdim];
30
for(int rgbidx = 0;rgbidx<inputdim;rgbidx++)
31
{
32
midrgb[rgbidx] = CreateRandMat(mid1.row,mid1.col);
33
cout<<"---------rgb: "<<rgbidx<<"---------"<<endl;
34
coutmat(midrgb[rgbidx]);
35
}
36
Matrix filters[outputchannels][inputdim];
37
for(int channelindex = 0;channelindex<inputdim;channelindex++)
38
{
39
for(int filterindex = 0;filterindex<outputchannels;filterindex++)
40
{
41
Matrix kernel = ones(kernelsize,kernelsize);
42
filters[channelindex][filterindex] = kernel;
43
// cout<<"---------"<<endl;
44
// cout<<"channel: "<<channelindex<<", index: "<<filterindex<<endl;
45
// coutmat(filters[channelindex][filterindex]);
46
}
47
}
48
if(mode == 0)
49
{
50
cout<<"inputimg:"<<endl;
51
coutmat(mid1);
52
Matrix convresult = CreateMatrix(((mid1.row-kernelsize)/stride)+1,((mid1.col-kernelsize)/stride)+1);
53
Matrix kernel = ones(kernelsize,kernelsize);
54
cout<<"--------- kernels: 3x3--------"<<endl;
55
coutmat(kernel);
56
cout<<"--------- mid1 ---------"<<endl;
57
cout<<"row: "<<mid1.row<<" , "<<"col: "<<mid1.col<<endl;
58
coutmat(mid1);
59
cout<<"--------- output: ---------"<<endl;
60
Matrix featuremaps[outputchannels];
61
for(int filteridx = 0;filteridx<outputchannels;filteridx++)
62
{
63
Matrix sumrgb = CreateMatrix(((mid1.row-kernelsize)/stride)+1,((mid1.col-kernelsize)/stride)+1);
64
for(int channelidx=0;channelidx<inputdim;channelidx++)
65
{
66
sumrgb = add(sumrgb,convelement(midrgb[channelidx],filters[filteridx][channelidx],kernelsize,stride),0);
67
cout<<"sumrgb"<<"filtersindex: "<<filteridx<<" "<<endl;
68
coutmat(sumrgb);
69
}
70
featuremaps[filteridx]=sumrgb;
71
}
72
for(int i = 0;i < outputchannels;i++)
73
{
74
cout<<"==========filter: "<<i<<"========="<<endl;
75
coutmat(featuremaps[i]);
76
77
}
78
return 0.0;
79
}
80
}
假设rgb:
1 ———rgb: 0——— 2 0.6868,1.358,-0.0419,0.3864,-0.87,0.7874,0.1393, 3 0.7593,0.1114,-0.7965,-0.7005,1.5068,-0.3997,1.1945, 4 -0.6814,-0.6164,1.7819,0.933,1.0805,0.2337,-0.7265, 5 1.6941,0.5209,-0.9397,-0.3,0.9019,-0.5348,-0.9147, 6 0.257,1.2136,1.9702,0.996,-0.394,-0.0892,-0.6112, 7 0.9649,0.2556,-0.6445,-0.3106,1.3732,-0.4526,1.5519, 8 ———rgb: 1——— 9 0.5726,-0.2106,-0.6804,-0.6533,-0.7418,1.4237,-0.0423, 10 0.3982,1.0687,-0.2758,-0.7519,0.9336,-0.4004,0.7577, 11 1.0035,1.8065,0.7624,1.0708,0.0301,-0.0468,-0.3465, 12 1.5645,-0.397,1.6417,1.148,-0.1405,-0.3185,-0.7553, 13 1.5715,1.2818,-0.9162,0.0648,1.2894,0.8675,1.2892, 14 1.9186,1.0597,-0.7541,1.4489,-0.5107,1.0148,0.0982, 15 ———rgb: 2——— 16 1.3683,0.3772,1.4713,1.8638,1.019,-0.9855,0.2794, 17 -0.1709,0.7684,1.7359,1.7779,1.4403,0.0131,1.4975, 18 0.2598,1.6541,1.6398,0.9777,-0.0517,0.0652,-0.0643, 19 1.0337,0.4624,1.9443,1.9263,0.4125,-0.9113,0.2605, 20 -0.656,-0.7297,-0.398,0.2574,-0.157,1.5341,0.8177, 21 -0.7898,1.1526,0.2964,0.1901,1.4331,-0.9055,-0.8993, 卷积核是1x1的3x3,输出如下:
1 --------- output: --------- 2 sumrgbfiltersindex: 0 3 2.5612,2.4154,3.2797,2.9576,2.946, 4 1.8336,-0.00589997,3.4674,2.7209,2.3417, 5 5.2002,5.5595,6.0298,2.8271,-1.0543, 6 5.2921,2.7615,2.6525,1.1899,0.8305, 7 sumrgbfiltersindex: 0 8 7.0063,4.5518,2.9734,3.8216,4.5133, 9 9.4063,6.0675,7.8858,4.2453,2.0551, 10 13.5189,12.0223,10.9803,6.7919,0.8143, 11 12.2626,7.3391,5.9238,6.0536,3.6646, 12 sumrgbfiltersindex: 0 13 16.1102,16.8179,14.8474,9.9414,7.7263, 14 18.7338,18.9543,19.6888,9.8953,4.7169, 15 18.7293,19.7566,17.5316,10.8451,2.72, 16 14.5785,12.4409,11.8289,9.8333,5.2494, 17 sumrgbfiltersindex: 1 18 2.5612,2.4154,3.2797,2.9576,2.946, 19 1.8336,-0.00589997,3.4674,2.7209,2.3417, 20 5.2002,5.5595,6.0298,2.8271,-1.0543, 21 5.2921,2.7615,2.6525,1.1899,0.8305, 22 sumrgbfiltersindex: 1 23 7.0063,4.5518,2.9734,3.8216,4.5133, 24 9.4063,6.0675,7.8858,4.2453,2.0551, 25 13.5189,12.0223,10.9803,6.7919,0.8143, 26 12.2626,7.3391,5.9238,6.0536,3.6646, 27 sumrgbfiltersindex: 1 28 16.1102,16.8179,14.8474,9.9414,7.7263, 29 18.7338,18.9543,19.6888,9.8953,4.7169, 30 18.7293,19.7566,17.5316,10.8451,2.72, 31 14.5785,12.4409,11.8289,9.8333,5.2494, 32 sumrgbfiltersindex: 2 33 2.5612,2.4154,3.2797,2.9576,2.946, 34 1.8336,-0.00589997,3.4674,2.7209,2.3417, 35 5.2002,5.5595,6.0298,2.8271,-1.0543, 36 5.2921,2.7615,2.6525,1.1899,0.8305, 37 sumrgbfiltersindex: 2 38 7.0063,4.5518,2.9734,3.8216,4.5133, 39 9.4063,6.0675,7.8858,4.2453,2.0551, 40 13.5189,12.0223,10.9803,6.7919,0.8143, 41 12.2626,7.3391,5.9238,6.0536,3.6646, 42 sumrgbfiltersindex: 2 43 16.1102,16.8179,14.8474,9.9414,7.7263, 44 18.7338,18.9543,19.6888,9.8953,4.7169, 45 18.7293,19.7566,17.5316,10.8451,2.72, 46 14.5785,12.4409,11.8289,9.8333,5.2494, 47 ==========filter: 0========= 48 16.1102,16.8179,14.8474,9.9414,7.7263, 49 18.7338,18.9543,19.6888,9.8953,4.7169, 50 18.7293,19.7566,17.5316,10.8451,2.72, 51 14.5785,12.4409,11.8289,9.8333,5.2494, 52 ==========filter: 1========= 53 16.1102,16.8179,14.8474,9.9414,7.7263, 54 18.7338,18.9543,19.6888,9.8953,4.7169, 55 18.7293,19.7566,17.5316,10.8451,2.72, 56 14.5785,12.4409,11.8289,9.8333,5.2494, 57 ==========filter: 2========= 58 16.1102,16.8179,14.8474,9.9414,7.7263, 59 18.7338,18.9543,19.6888,9.8953,4.7169, 60 18.7293,19.7566,17.5316,10.8451,2.72, 61 14.5785,12.4409,11.8289,9.8333,5.2494,
?(详细请查看原文)
内容中包含的图片若涉及版权问题,请及时与我们联系删除
评论
沙发等你来抢