<p>Okay so your CNN model is taking an Image and outputting the bounding boxes for them. That means the last layer of CNN model must be having four outputs which are generating real numbers. This is a regression problem.</p>
<p>In that case, you can take L1 loss (mean absolute error) or L2 loss (mean square error) as your loss function. I have created a similar project, I have used L1 loss.</p>
<p>Suppose your input image is <strong>x</strong> and <strong>predicted_bb</strong> is the model output and <strong>real_bb</strong> is your original bounding box for image x. Then you should proceed as follows</p>
<pre><code>predicted_bb = model(x)

# CALCULATE LOSS BETWEEN THE ACTUAL &amp; PREDICTED BB COORDINATES

loss_bb = torch.nn.functional.l1_loss(predicted_bb, real_bb, reduction=&quot;none&quot;).sum(1)

# SET GRADIENTS TO ZERO

optimizer.zero_grad()

# BACKPROPOGATE THE LOSS

loss.backward()
</code></pre>
<p>For tensorflow keras</p>
<pre><code>predicted_bb = Dense(4, activation='relu')(x)

model = Model(inputs = image_input, outputs = [predicted_bb])
model.compile(loss=['mae'], optimizer='adam', metrics =['accuracy'])
</code></pre>

CC BY-SA 4.0
2022-06-06T16:37:37.113Z
this answer doesn't explain how to deal when you have more than one bounding boxes for the same class and your network is outputting say (100, 4) shaped output
Phoenix
CC BY-SA 4.0
2020-11-28T02:46:55.200Z
2020-11-28T02:46:55.200Z
coderina
<p>I'm trying to create a text recognition project using CNN. I need help regarding the text detection task. </p>

<p>I have the training images and bounding box details for them. But I'm unable to figure out how to create the loss function. </p>

<p>Can anyone help me by telling how to take the output from the CNN model and compare it to the bounding box labels?</p>

<deep-learning><convolutional-neural-networks><classification><image-recognition><optical-character-recognition>
How should I define the loss function for a multi-object detection problem?
h4x
# How should I define the loss function for a multi-object detection problem?

(Asked by: username_0 on 2020-04-28)

I'm trying to create a text recognition project using CNN. I need help regarding the text detection task.

I have the training images and bounding box details for them. But I'm unable to figure out how to create the loss function.

Can anyone help me by telling how to take the output from the CNN model and compare it to the bounding box labels?

**Tags:** deep-learning, convolutional-neural-networks, classification, image-recognition, optical-character-recognition


---

## 1 answers

### Answer by username_1 on 2020-11-28. Score: 0

Okay so your CNN model is taking an Image and outputting the bounding boxes for them. That means the last layer of CNN model must be having four outputs which are generating real numbers. This is a regression problem.
In that case, you can take L1 loss (mean absolute error) or L2 loss (mean square error) as your loss function. I have created a similar project, I have used L1 loss.
Suppose your input image is x and predicted_bb is the model output and real_bb is your original bounding box for image x. Then you should proceed as follows
predicted_bb = model(x)

# CALCULATE LOSS BETWEEN THE ACTUAL & PREDICTED BB COORDINATES

loss_bb = torch.nn.functional.l1_loss(predicted_bb, real_bb, reduction="none").sum(1)

# SET GRADIENTS TO ZERO

optimizer.zero_grad()

# BACKPROPOGATE THE LOSS

loss.backward()

For tensorflow keras
predicted_bb = Dense(4, activation='relu')(x)

model = Model(inputs = image_input, outputs = [predicted_bb])
model.compile(loss=['mae'], optimizer='adam', metrics =['accuracy'])

> **Comments:**
> **username_2**: this answer doesn't explain how to deal when you have more than one bounding boxes for the same class and your network is outputting say (100, 4) shaped output  

---

