4
$\begingroup$

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?

$\endgroup$

1 Answer 1

0
$\begingroup$

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'])
$\endgroup$
1
  • $\begingroup$ 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 $\endgroup$
    – Phoenix
    Jun 6, 2022 at 16:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .