from typing import Tuple
from typing import Union


def cnn_kernel(
    input: Tuple[int, int],
    output: Tuple[int, int],
    padding: Union[int, Tuple[int, int]] = 0,
    stride: Union[int, Tuple[int, int]] = 1,
):
    """
    Given all the parameters of a convolution, except the filter's size, compute it.
    Args:
        input: Size of the input image.
        output: Size of the feature map.
        padding: Padding size.
        stride: Stride.
    Returns:
        The kernel size.
    """
    if isinstance(padding, int):
        padding = (padding, padding)

    if isinstance(stride, int):
        stride = (stride, stride)

    kernel_w = input[0] + 2 * padding[0] - stride[0] * (output[0] - 1)
    kernel_h = input[1] + 2 * padding[1] - stride[1] * (output[1] - 1)
    return kernel_w, kernel_h