Bool value of Tensor with more than one value is ambiguous

I am trying to train my network for object detection in images using labels and bounding boxes. I have some images with no objects. For labels, the images are classified as ‘NONE’ and the box values are 0.

I want to write a script within my training class to calculate the loss based on only the labels if the box values are 0. if the sum of the boxes is not 0 then i want it to calculate the loss using both the box values and labels. This is the script that i have written:

num_epochs = 10
start_time = time.time()    
for epoch in range(num_epochs):
    model = net1.train()
    for batch_idx, (images, target) in enumerate(trainloader):
        images =
        labels = target['label'].to(device)
        bbox = target['box'].to(device)
        norm = target['norm_box'].to(device)
        outputs_labels, outputs_bbox = model(images)
        if sum(norm) == 0:
            loss_label = criterion_label(outputs_labels, labels)
            oss = loss_label
            loss_label = criterion_label(outputs_labels, labels)
            loss_bbox = criterion_bbox(outputs_bbox, norm)
            loss = loss_label + loss_bbox

But i keeping getting the following error:

RuntimeError: bool value of Tensor with more than one value is ambiguous

I believe it has something to do with my batch size, which is 4, where the box input for the network has 4 tensors for 4 sets of bounding box values.

For example, this is what a single batch looks like:

tensor([[ 0.66, -0.15, -0.93, -0.79],
        [-1.00, -0.14, -0.94, -0.84],
        [ 0.00,  0.00,  0.00,  0.00],
        [ 0.86, -0.13, -0.92, -0.75]], device='cuda:0')

Any advice would be greatly appreciated.

1 Like

The error indicates that sum(norm) returns a tensor with multiple values. If you intend to sum all values in norm, use torch.sum(norm) or norm.sum(). Using the built-in sum function will only sum over a single dimension.


Thank you very much for your reply. I will let you know if I have any issues.

what about this:

list(mdl.parameters()) == list(mdl0.parameters())

why do I get the same error?

RuntimeError: bool value of Tensor with more than one value is ambiguous