Convolution 3D operator in NDHWC layout. Parameters ---------- a_np : numpy.ndarray 5-D with shape [batch, in_channel, in_depth, in_height, in_width] w_np : numpy.ndarray 5-D with shape [num_filter, in_channel, filter_depth, filter_height, filter_width] stride
(a_np, w_np, stride, padding)
| 24 | |
| 25 | |
| 26 | def _conv3d_ndhwc_python(a_np, w_np, stride, padding): |
| 27 | """Convolution 3D operator in NDHWC layout. |
| 28 | |
| 29 | Parameters |
| 30 | ---------- |
| 31 | a_np : numpy.ndarray |
| 32 | 5-D with shape [batch, in_channel, in_depth, in_height, in_width] |
| 33 | |
| 34 | w_np : numpy.ndarray |
| 35 | 5-D with shape [num_filter, in_channel, filter_depth, filter_height, filter_width] |
| 36 | |
| 37 | stride : int or a list/tuple of three ints |
| 38 | Stride size, or [stride_depth, stride_height, stride_width] |
| 39 | |
| 40 | padding : int or str or a list/tuple of three ints |
| 41 | Padding size, or ['VALID', 'SAME'], or [pad_depth, pad_height, pad_width] |
| 42 | |
| 43 | Returns |
| 44 | ------- |
| 45 | b_np : np.ndarray |
| 46 | 5-D with shape [batch, out_channel, out_depth, out_height, out_width] |
| 47 | """ |
| 48 | batch, in_depth, in_height, in_width, in_channel = a_np.shape |
| 49 | kernel_d, kernel_h, kernel_w, _, num_filter = w_np.shape |
| 50 | if isinstance(stride, int): |
| 51 | stride_d = stride_h = stride_w = stride |
| 52 | else: |
| 53 | stride_d, stride_h, stride_w = stride |
| 54 | |
| 55 | pad_front, pad_top, pad_left, pad_back, pad_bottom, pad_right = get_pad_tuple3d( |
| 56 | padding, (kernel_d, kernel_h, kernel_w) |
| 57 | ) |
| 58 | pad_d = pad_front + pad_back |
| 59 | pad_h = pad_top + pad_bottom |
| 60 | pad_w = pad_left + pad_right |
| 61 | # compute the output shape |
| 62 | out_channel = num_filter |
| 63 | out_depth = (in_depth - kernel_d + pad_d) // stride_d + 1 |
| 64 | out_height = (in_height - kernel_h + pad_h) // stride_h + 1 |
| 65 | out_width = (in_width - kernel_w + pad_w) // stride_w + 1 |
| 66 | # change the layout from NHWC to NCHW |
| 67 | at = a_np.transpose((0, 4, 1, 2, 3)) |
| 68 | wt = w_np.transpose((4, 3, 0, 1, 2)) |
| 69 | bt = np.zeros((batch, out_channel, out_depth, out_height, out_width), dtype=a_np.dtype) |
| 70 | # computation |
| 71 | for n in range(batch): |
| 72 | for f in range(out_channel): |
| 73 | for c in range(in_channel): |
| 74 | if pad_d > 0 or pad_h > 0 or pad_w > 0: |
| 75 | apad = np.zeros( |
| 76 | (in_depth + pad_d, in_height + pad_h, in_width + pad_w), dtype=a_np.dtype |
| 77 | ) |
| 78 | apad[ |
| 79 | pad_front : pad_front + in_depth, |
| 80 | pad_top : pad_top + in_height, |
| 81 | pad_left : pad_left + in_width, |
| 82 | ] = at[n, c] |
| 83 | else: |
no test coverage detected
searching dependent graphs…