im2col 实现 参数说明: X:输入数组,为 (n_samples, in_rows, in_cols, in_ch),此时还未 0 填充(padding) W_shape:卷积层的卷积核的形状,为 (kernel_rows, kernel_cols, in_ch, out_ch) pad:padding 数目,4-tuple, int, 或 'same','valid'型 在图片的左、右、上、下 (left, right, up, down) 0填充 若为int,表示在左、右、上、下均填充数目为 pad 的 0,
(X, W_shape, pad, stride, dilation=1)
| 175 | |
| 176 | |
| 177 | def im2col(X, W_shape, pad, stride, dilation=1): |
| 178 | """ |
| 179 | im2col 实现 |
| 180 | |
| 181 | 参数说明: |
| 182 | X:输入数组,为 (n_samples, in_rows, in_cols, in_ch),此时还未 0 填充(padding) |
| 183 | W_shape:卷积层的卷积核的形状,为 (kernel_rows, kernel_cols, in_ch, out_ch) |
| 184 | pad:padding 数目,4-tuple, int, 或 'same','valid'型 |
| 185 | 在图片的左、右、上、下 (left, right, up, down) 0填充 |
| 186 | 若为int,表示在左、右、上、下均填充数目为 pad 的 0, |
| 187 | 若为same,表示填充后为相同 (same) 卷积, |
| 188 | 若为valid,表示填充后为有效 (valid) 卷积 |
| 189 | stride:卷积核的卷积步幅,int型 |
| 190 | dilation:扩张率,int 型,default=1 |
| 191 | |
| 192 | 输出说明: |
| 193 | X_col:输出结果,形状为 (kernel_rows*kernel_cols*n_in, n_samples*out_rows*out_cols) |
| 194 | p:填充数,4-tuple |
| 195 | """ |
| 196 | fr, fc, n_in, n_out = W_shape |
| 197 | s, p, d = stride, pad, dilation |
| 198 | n_samp, in_rows, in_cols, n_in = X.shape |
| 199 | |
| 200 | X_pad, p = pad2D(X, p, W_shape[:2], stride=s, dilation=d) |
| 201 | pr1, pr2, pc1, pc2 = p |
| 202 | |
| 203 | # 将输入的通道维数移至第二位 |
| 204 | X_pad = X_pad.transpose(0, 3, 1, 2) |
| 205 | |
| 206 | k, i, j = _im2col_indices((n_samp, n_in, in_rows, in_cols), fr, fc, p, s, d) |
| 207 | |
| 208 | # X_col.shape = (n_samples, kernel_rows*kernel_cols*n_in, out_rows*out_cols) |
| 209 | X_col = X_pad[:, k, i, j] |
| 210 | X_col = X_col.transpose(1, 2, 0).reshape(fr * fc * n_in, -1) |
| 211 | return X_col, p |
| 212 | |
| 213 | |
| 214 | def conv2D_gemm(X, W, stride=0, pad='same', dilation=1): |
no test coverage detected