def random_crop()

in pycls/datasets/transforms.py [0:0]


def random_crop(image, size, pad_size=0, order='CHW'):
    assert order in ['CHW', 'HWC']
    if pad_size > 0:
        image = zero_pad(image=image, pad_size=pad_size, order=order)
    if order == 'CHW':
        if image.shape[1] == size and image.shape[2] == size:
            return image
        height = image.shape[1]
        width = image.shape[2]
        y_offset = 0
        if height > size:
            y_offset = int(np.random.randint(0, height - size))
        x_offset = 0
        if width > size:
            x_offset = int(np.random.randint(0, width - size))
        cropped = image[:, y_offset:y_offset + size, x_offset:x_offset + size]
        assert cropped.shape[1] == size, "Image not cropped properly"
        assert cropped.shape[2] == size, "Image not cropped properly"
    else:
        if image.shape[0] == size and image.shape[1] == size:
            return image
        height = image.shape[0]
        width = image.shape[1]
        y_offset = 0
        if height > size:
            y_offset = int(np.random.randint(0, height - size))
        x_offset = 0
        if width > size:
            x_offset = int(np.random.randint(0, width - size))
        cropped = image[y_offset:y_offset + size, x_offset:x_offset + size, :]
        assert cropped.shape[0] == size, "Image not cropped properly"
        assert cropped.shape[1] == size, "Image not cropped properly"
    return cropped