def center_crop(x, y=None, crop_size=None, data_format='channels_last'):
    """
    Takes a pair of numpy arrays (image and label) and returns a pair of matching center crops
    :param x: image in numpy array format
    :param y: label in numpy array format
    :param crop_size: (height, width) tuple
    :param data_format: 'channels_first' or 'channels_last'
    :return: (cropped image, cropped label) tuple
    """
    if crop_size is None:
        return x if y is None else x, y

    if data_format == 'channels_first':
        centerh, centerw = x.shape[1] // 2, x.shape[2] // 2
    elif data_format == 'channels_last':
        centerh, centerw = x.shape[0] // 2, x.shape[1] // 2
    else:
        raise NotImplementedError()
    crop_size = (2 * centerh, 2 * centerw) if crop_size is None else crop_size
    lh, lw = crop_size[0] // 2, crop_size[1] // 2
    rh, rw = crop_size[0] - lh, crop_size[1] - lw

    start_h, end_h = centerh - lh, centerh + rh
    start_w, end_w = centerw - lw, centerw + rw
    if data_format == 'channels_first':
        cropped_x = x[:, start_h:end_h, start_w:end_w]
        if y is None:
            return cropped_x
        else:
            cropped_y = y[:, start_h:end_h, start_w:end_w]
            return cropped_x, cropped_y
    elif data_format == 'channels_last':
        cropped_x = x[start_h:end_h, start_w:end_w, :]
        if y is None:
            return cropped_x
        else:
            cropped_y = y[start_h:end_h, start_w:end_w, :]
            return cropped_x, cropped_y