二维填充 参数说明: X:输入数组,为 (n_samples, in_rows, in_cols, in_ch), 其中 padding 操作是应用到 in_rows 和 in_cols pad:padding 数目,4-tuple, int, 或 'same','valid' 在图片的左、右、上、下 (left, right, up, down) 0填充 若为int,表示在左、右、上、下均填充数目为 pad 的 0, 若为same,表示填充后为相同 (same) 卷积,
(X, pad, kernel_shape=None, stride=None, dilation=1)
| 50 | |
| 51 | |
| 52 | def pad2D(X, pad, kernel_shape=None, stride=None, dilation=1): |
| 53 | """ |
| 54 | 二维填充 |
| 55 | |
| 56 | 参数说明: |
| 57 | X:输入数组,为 (n_samples, in_rows, in_cols, in_ch), |
| 58 | 其中 padding 操作是应用到 in_rows 和 in_cols |
| 59 | pad:padding 数目,4-tuple, int, 或 'same','valid' |
| 60 | 在图片的左、右、上、下 (left, right, up, down) 0填充 |
| 61 | 若为int,表示在左、右、上、下均填充数目为 pad 的 0, |
| 62 | 若为same,表示填充后为相同 (same) 卷积, |
| 63 | 若为valid,表示填充后为有效 (valid) 卷积 |
| 64 | kernel_shape:卷积核形状,为 (fr, fc) |
| 65 | stride:卷积步幅,int 型 |
| 66 | dilation:扩张率,int 型,default=1 |
| 67 | """ |
| 68 | p = pad |
| 69 | if isinstance(p, int): |
| 70 | p = (p, p, p, p) |
| 71 | |
| 72 | if isinstance(p, tuple): |
| 73 | X_pad = np.pad( |
| 74 | X, |
| 75 | pad_width=((0, 0), (p[0], p[1]), (p[2], p[3]), (0, 0)), |
| 76 | mode="constant", |
| 77 | constant_values=0, |
| 78 | ) |
| 79 | |
| 80 | # 'same'卷积,首先计算 padding 维数 |
| 81 | if p == "same" and kernel_shape and stride is not None: |
| 82 | p = calc_pad_dims_sameconv_2D( |
| 83 | X.shape, X.shape[1:3], kernel_shape, stride, dilation=dilation |
| 84 | ) |
| 85 | X_pad, p = pad2D(X, p) |
| 86 | |
| 87 | if p == "valid": |
| 88 | p = (0, 0, 0, 0) |
| 89 | X_pad, p = pad2D(X, p) |
| 90 | |
| 91 | return X_pad, p |
| 92 | |
| 93 | |
| 94 | ####### conv2D ################## |
no test coverage detected