import torch


def crop_and_concat_tensor(x1, x2):
    """
    Crops x1 to the x2's size and concatenate the tensors across the channels. Is assumed that x1 is bigger than x2.
    The tensors have shape [batch, channels, y, x]
    """
    x_off = (x1.size()[3] - x2.size()[3]) // 2
    y_off = (x1.size()[2] - x2.size()[2]) // 2
    xs = x2.size()[3]
    ys = x2.size()[2]
    x_crop = x1[:, :, y_off:y_off+ys, x_off:x_off+xs]
    x = torch.cat((x_crop, x2), dim=1)
    return x