def im2col(x: Union[(tf.Tensor, np.ndarray)], h_filter: int, w_filter: int, padding: str, stride: int) -> tf.Tensor:
    'Generic implementation of im2col on tf.Tensors.'
    with tf.name_scope('im2col'):
        nhwc_tensor = tf.transpose(x, [0, 2, 3, 1])
        channels = int(nhwc_tensor.shape[3])
        patch_tensor = tf.extract_image_patches(nhwc_tensor, ksizes=[1, h_filter, w_filter, 1], strides=[1, stride, stride, 1], rates=[1, 1, 1, 1], padding=padding)
        patch_tensor_nchw = tf.reshape(tf.transpose(patch_tensor, [3, 1, 2, 0]), (h_filter, w_filter, channels, (- 1)))
        x_col_tensor = tf.reshape(tf.transpose(patch_tensor_nchw, [2, 0, 1, 3]), (((channels * h_filter) * w_filter), (- 1)))
        return x_col_tensor
Generic implementation of im2col on tf.Tensors.
def im2col(x: Union[(tf.Tensor, np.ndarray)], h_filter: int, w_filter: int, padding: str, stride: int) -> tf.Tensor:
    
    with tf.name_scope('im2col'):
        nhwc_tensor = tf.transpose(x, [0, 2, 3, 1])
        channels = int(nhwc_tensor.shape[3])
        patch_tensor = tf.extract_image_patches(nhwc_tensor, ksizes=[1, h_filter, w_filter, 1], strides=[1, stride, stride, 1], rates=[1, 1, 1, 1], padding=padding)
        patch_tensor_nchw = tf.reshape(tf.transpose(patch_tensor, [3, 1, 2, 0]), (h_filter, w_filter, channels, (- 1)))
        x_col_tensor = tf.reshape(tf.transpose(patch_tensor_nchw, [2, 0, 1, 3]), (((channels * h_filter) * w_filter), (- 1)))
        return x_col_tensor