import torch


def build_image_pyramid(image, num_levels):
    """Create an image pyramid.

    Derived from monodepth.
    """
    assert(len(image.shape) == 4)

    pyramid = [image]
    for lvl in range(1, num_levels):
        hlvl = (pyramid[-1].shape[2] + 1) // 2
        wlvl = (pyramid[-1].shape[3] + 1) // 2

        shape = (hlvl, wlvl)

        image_lvl = torch.nn.functional.interpolate(pyramid[-1], shape, mode="area")
        pyramid.append(image_lvl)

    return pyramid