MCPcopy Index your code
hub / github.com/MingchaoZhu/DeepLearning / conv2D

Function conv2D

code/chapter9.py:95–136  ·  view source on GitHub ↗

二维卷积实现过程。 参数说明: X:输入数组,为 (n_samples, in_rows, in_cols, in_ch) W:卷积层的卷积核参数,为 (kernel_rows, kernel_cols, in_ch, out_ch) stride:卷积核的卷积步幅,int型 pad:padding 数目,4-tuple, int, 或 'same','valid'型 在图片的左、右、上、下 (left, right, up, down) 0填充 若为int,表示在左、右、上、下均填充数目为 pad 的 0,

(X, W, stride, pad, dilation=1)

Source from the content-addressed store, hash-verified

93
94####### conv2D ##################
95def conv2D(X, W, stride, pad, dilation=1):
96 """
97 二维卷积实现过程。
98
99 参数说明:
100 X:输入数组,为 (n_samples, in_rows, in_cols, in_ch)
101 W:卷积层的卷积核参数,为 (kernel_rows, kernel_cols, in_ch, out_ch)
102 stride:卷积核的卷积步幅,int型
103 pad:padding 数目,4-tuple, int, 或 'same''valid'
104 在图片的左、右、上、下 (left, right, up, down) 0填充
105 若为int,表示在左、右、上、下均填充数目为 pad 的 0,
106 若为same,表示填充后为相同 (same) 卷积,
107 若为valid,表示填充后为有效 (valid) 卷积
108 dilation:扩张率,int 型,default=1
109
110 输出说明:
111 Z:卷积结果,为 (n_samples, out_rows, out_cols, out_ch)
112 """
113 s, d = stride, dilation
114 X_pad, p = pad2D(X, pad, W.shape[:2], stride=s, dilation=d)
115
116 pr1, pr2, pc1, pc2 = p
117 fr, fc, in_ch, out_ch = W.shape
118 n_samp, in_rows, in_cols, in_ch = X.shape
119
120 # 考虑扩张率
121 _fr, _fc = fr + (fr-1) * (d-1), fc + (fc-1) * (d-1)
122
123 out_rows = int((in_rows + pr1 + pr2 - _fr) / s + 1)
124 out_cols = int((in_cols + pc1 + pc2 - _fc) / s + 1)
125
126 Z = np.zeros((n_samp, out_rows, out_cols, out_ch))
127 for m in range(n_samp):
128 for c in range(out_ch):
129 for i in range(out_rows):
130 for j in range(out_cols):
131 i0, i1 = i * s, (i * s) + fr + (fr-1) * (d-1)
132 j0, j1 = j * s, (j * s) + fc + (fc-1) * (d-1)
133
134 window = X_pad[m, i0 : i1 : d, j0 : j1 : d, :]
135 Z[m, i, j, c] = np.sum(window * W[:, :, :, c])
136 return Z
137
138
139####### conv2D GEMM ############

Callers 1

forwardMethod · 0.85

Calls 1

pad2DFunction · 0.85

Tested by

no test coverage detected