2
$\begingroup$

I am writing a very simple neural network to see whether it can be used for my scoped case of drawing a bounding box around a specific object (barcode) in an image.

The layers i am thinking of are:

  • One convolution to detect edges and learn features of the barcode
  • a few dense layers to adjust for predicting the 4 data points (x1,y1,x2,y2) of the specific object in the image.

In code, this is the basic schema:

// Generate a sequential model
const baseModel = tf.sequential();

// Layer 1: Convolutional
baseModel.add(
  tf.layers.conv2d({
    inputShape: [224, 224, 1],
    filters: 64,
    kernelSize: 4,
    padding: "same",
    activation: "relu",
  })
);

baseModel.add(tf.layers.flatten())
baseModel.add(tf.layers.dense({ units: 128, activation: "relu" }))
baseModel.add(tf.layers.dense({ units: 64, activation: "relu" }))
baseModel.add(tf.layers.dense({ units: 4, activation: "sigmoid" }))
baseModel.compile({
  optimizer: tf.train.adam(0.00001), //could be a parameter as well.
  loss: tf.losses.meanSquaredError,
});

Would this steps be reasonable towards my goal of predicting bounding boxes?

I have read other posts and models, they normally use huge models with pretrained weights, but I wonder if, for a specific case, this schema could be enough.

$\endgroup$
3
  • $\begingroup$ What is the final sigmoid layer doing? $\endgroup$ Commented Feb 14, 2023 at 10:08
  • $\begingroup$ It is for the bounding box, which will be a number between 0 and 1 @SextusEmpiricus. See for a similar idea, I think $\endgroup$
    – Mah Neh
    Commented Feb 14, 2023 at 10:19
  • 1
    $\begingroup$ Could you tell in which part of that article they demonstrate the use of the sigmoid function? I can see that one desires an output between 0 and 1, but is a sigmoid function good for this? I imagine that the output from the relu layers resembles something like coordinates, but converting that with a non-linear function might be a bit extreme. The sigmoid might not map so well when the item is at the edges. Why not just divide by 224 and use a cutoff at zero and one $$y = \begin{cases} 0 & \text{if $x<0$}\\ x/224 & \text{if $0 \leq x \leq 224 $}\\ 1 & \text{if $x>224$}\\ \end{cases}$$ $\endgroup$ Commented Feb 14, 2023 at 10:48

1 Answer 1

2
$\begingroup$

Detecting edges with several filters in a single layer seems like it could recognize a barcode, but you have to train 64x4x4 weights. Is that really neccesary. And why 64 filters? To capture multiple orientations?

Possibly a more complex architecture with multiple layers (but less parameters) may help to improve the performance with less training. Here is a wild guess:

Try to mimic the process of a real laser scanner,

  • You scan for high contrast stripes (or edges). This can be done with a few 3x3 filters that detect high contrast lines. Or alternatively a few 1xn filters that operate on several preprocessed rotated versions of the image.

    The coefficients of this convolution layer might not need to be trained. It can be seen as a preprocessing step.

    Possibly you can also detect the stripes by detecting first edges and in the image with the edges you detect nearby maximum and minimum peaks. That might be more robust to barcode stripes that cover more than one pixel. Anyway, the point is that you might tackle the edge/line detection more as a preprocessing step that you do not include into the learning of the neural network.

  • You have an additional layer that arranges the sensitivity. A scanner will only respond when the lines are

    • wide (a second convolution layer)
    • multiple in a row (a third convolution layer)

With those convolutions you can detect whether a barcode is present in a similar way as a real barcode scanner does it. Finally you add those relu-layers to find the end, start and orientation of the barcode.

$\endgroup$
3
  • $\begingroup$ This doesn't quite work for returning a bounding box, it does probably work for classifying only. In fact you need a much more complex network, something like YoloV7, to get good results. $\endgroup$
    – Mah Neh
    Commented May 26 at 17:24
  • $\begingroup$ @MahNeh a neural network is an overkill because barcodes are not that difficult to detect. Or at least, when a physical scanner can detect it, then a simple algorithm should be able to do it as well. A neural network might possibly be helpful in the cases where a physical scanner can not detect a barcode (because of small defects, e.g. dirty or wrinkled labels) whereas a human can still recognise the barcode. $\endgroup$ Commented May 26 at 18:56
  • $\begingroup$ Here is an example of a detection algorithm that doesn't use a NN and is more like a physical scanner opencv.org/blog/… $\endgroup$ Commented May 26 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.