How to transform the batch in the loss function?


The dependent variable of the custom model is a 24-dimensional vector of continuous values that represent 6 bounding boxes (top, left, bottom right for each bounding box). In the loss function, I would like to compare the distances between the centroids of the represented bounding boxes. Thus, every batch item should be reshaped into 12-dimensional vector as every bounding box should be transformed into a point (y = (top + bottom)/2, x = (left + right)/2).

I tried to do the following:

input_boxes = list(map(lambda x: list(zip(*[x[i::4] for i in range(4)])), input))
input_points = list(map(lambda x: ((x[0] + x[2])/torch.Tensor(2), (x[1] + x[3])/torch.Tensor(2)), input_boxes))

This resulted an error: mul(): argument ‘other’ (position 1) must be Tensor, not tuple

Does anyone know how to reshape the batch properly?

Thank you!


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 =, y), 1)
        centroid_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 =, y), 1)
15 centroid_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?

After initializing centroid_coords as FloatTensor, everything worked fine:

centroid_coords = torch.cuda.FloatTensor(())