A simple-sounding task
Counting objects in a photograph seems easy for people, but it can be surprisingly hard for a computer. Imagine a bakery that wants a camera above its trays to count how many rolls come out of the oven. A computer vision model, trained on many labelled photographs, is asked to report the number of rolls in each new image.
The problem of touching
When rolls are spread apart, each one is surrounded by the tray, and the model can find them easily. But rolls are often baked close together and expand as they rise, so they touch or even merge at the edges. From above, six touching rolls may look like one continuous area of brown crust, with only faint lines between them.
Counting instances
In computer vision, finding and separating individual objects is called instance detection or instance segmentation. The model must not only recognise that an area is bread, but decide where one roll ends and the next begins. This is much harder when objects are similar in colour and pressed together.
Drawing the outlines
To train and test the model properly, people first label the photographs by drawing an outline around each individual roll. These labels give the model clear examples of where boundaries lie, even when they are faint. Careful labelling of crowded trays is especially important, because those are the hardest cases.
Separate trays for testing
A model should be tested on photographs it has never seen. Ideally, the test images should come from different trays, or even different days, than the training images. If very similar photographs appear in both sets, the model may seem accurate simply because it has memorised them.
A controlled comparison
A useful test compares two kinds of tray with the same number of rolls: one where the rolls are tightly packed and touching, and one where they are spread apart. If the model counts the separated trays correctly but undercounts the crowded ones, the problem is clearly the touching rolls, not the number of rolls.
Reporting errors separately
An overall accuracy figure can hide the problem. If most trays are spread out, the model may score well overall while consistently missing rolls on crowded trays. Reporting errors separately for touching and separated rolls shows exactly where the model struggles and how serious the problem is.
Fixing the weakness
If every tight cluster is undercounted, the solution is to add more labelled examples of touching rolls, with clear instance boundaries, to the training data. Engineers might also change the camera angle or lighting to make the gaps between rolls more visible, or use a model designed for separating touching objects.
Measuring the count
Errors can be measured in different ways. One is the average difference between the predicted and true counts. Another is the proportion of trays counted exactly right. For a bakery, even a small consistent undercount could matter, for example when packing orders or tracking production.
Managing AI risks
The NIST AI Risk Management Framework, published in January 2023 by the US National Institute of Standards and Technology, encourages organisations to test AI systems in the conditions where they will be used and to understand where they may fail. Testing a counting model on realistic, crowded trays is a practical example of this approach.
Other crowded scenes
The same problem appears with eggs in cartons, cells under a microscope and fruit in crates.
Try the arithmetic
If a model counts 60 rolls correctly on separated trays but finds only 48 when the same 60 rolls are packed together, it misses 12, an undercount of 20 per cent on crowded trays.
The idea to take away
Touching rolls can look like one patch of colour. Outline each roll, keep test trays separate, and compare errors on tightly packed trays with errors on trays where the same number of rolls are separated, reporting the results separately.
Sources & further reading
Figures in the example are illustrative. Photograph: Korb mit Brötchen.JPG by 3268zauber, CC BY-SA 3.0, via Wikimedia Commons; cropped and resized. Reference organisations do not endorse this website.

