def upsample(arr, mode):
    """Upsample an 2D array.

    Arguments:
        arr {2d numpy array} -- The target array.
        mode {1 or 2 or 4} -- Upsample ratio (4:mode).

    Returns:
        2d numpy array -- Upsampled array.
    """
    if mode not in {1, 2, 4}:
        raise ValueError(f'Mode ({mode}) must be 1, 2 or 4.')

    if mode == 4:
        return arr
    return arr.repeat(3 - mode, axis=0).repeat(2, axis=1)