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 = images.to(device)
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:
print(norm)
loss_label = criterion_label(outputs_labels, labels)
oss = loss_label
else:
print(norm)
loss_label = criterion_label(outputs_labels, labels)
loss_bbox = criterion_bbox(outputs_bbox, norm)
loss = loss_label + loss_bbox
optimizer.zero_grad()
loss.backward()
optimizer.step()
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.