def img_pad(img, target_h, target_w):
    """ Pad image to the target shape

    Args:
        img (torch.Tensor): 4D normalized image tensor (N, C, H, W)
        target_h (int): output height
        target_w (int): output_width
    """

    img_h, img_w = img.size(2), img.size(3)
    assert img_h <= target_h and img_w <= target_w
    if img_h == target_h and img_w == target_w:
        return img
    sh = (target_h - img_h) // 2
    th = sh + img_h
    sw = (target_w - img_w) // 2
    tw = sw + img_w
    output = img.new_zeros(size=(img.size(0), img.size(1), target_h, target_w))
    output[:, :, sh:th, sw:tw] = img
    return output