Upon checking the documentation, I came up with the following function:
def bb2centroids(source_tensor):
    batch_size, coords_count = list(source_tensor.size())
    bb_count = coords_count/4
    box_coords = torch.functional.split(source_tensor, 1, dim=1)
    centroid_coords = torch.tensor(())
    for i in range(int(bb_count)):
        bb_idx = i * 4
        y1 = box_coords[bb_idx]
        y2 = box_coords[bb_idx + 2]
        x1 = box_coords[bb_idx + 1]
        x2 = box_coords[bb_idx + 3]
        y = (y1 + y2)/2
        x = (x1 + x2)/2
        centroid_coords = torch.cat((points_coords, y), 1)
        centroid_coords = torch.cat((points_coords, x), 1)
    return centroid_coords

While testing this by manually setting a tensor, it worked fine. When I plugged this in loss function, however, it threw the following error:
 in bb2centroids(source_tensor)
12         y = (y1 + y2)/2
13         x = (x1 + x2)/2
—> 14         centroid_coords = torch.cat((points_coords, y), 1)
15         centroid_coords = torch.cat((points_coords, x), 1)
16     return centroid_coords
RuntimeError: Expected object of backend CPU but got backend CUDA for sequence element 1 in sequence argument at position #1 ‘tensors’
Does anyone know how to fix this?