# How can we get a differentiable neural network to count things?

Imagine I have images with apples in them. I want to train a neural network which can count the number of apples in each image.

BUT, I don't want to use a detector, then count the number of bounding boxes. I'm interested in knowing if there's a way to bake this sort of logic into a differentiable neural network.

The simplest variation of this problem might be: I have an input vector $$x \in \{0, 1\}^N$$ and I want to count the number of 1s in it. I can make a single layer neural network, setting all the weights to 1, bias to 0, and linear activation. And my answer pops out. But how would I train the network to do this from scratch? Sure, I could regress to an output in $$[0,1]$$ and multiply the result by $$N$$, then the network is differentiable. But did the model really learn how to count? If so, would this behaviour be generalisable to counting multiple types of objects at once? Would it generalise to inputs where there can be any number of said object (like an image can have many apples in it, despite the size of the image)?

I want to know if there's a model which can learn how to count.

Here's another way I'm thinking about it: I can look at an aerial view of pine trees and say "yeah maybe there are 30 trees", but then I can do the task of looking at and identifying each tree individually, incrementing a counter, and making sure not to revisit that tree. The latter is what I consider truly "counting".

• Could you clarify what you mean by "really learn to count"? You seem to want to make the distinction between estimating a total and counting May 1, 2021 at 10:41
• @NeilSlater you ask a good question. Maybe one way is to say it's certainly not like "estimating" the total. I can look at an aerial view of pine trees and say yeah maybe that's 30, but then I can do the task of looking at and identifying each tree individually, incrementing a counter, and making sure not to revisit that tree. Something feels very different about those tasks and I can't put my finger on formalising the difference. I also can't formulate why I believe a NN is not equipped to do the "true" counting task. May 1, 2021 at 13:09
• If you could edit some variant of your comment into the question, I believe I can answer that part. I am not confident that I can answer definitely whether there is a model that "learns to count". Whether it can be found depends on how much of the process you expect it to learn (as opposed to be given by the problem forumulation), and how you would present the learning task to it. Counting is a surprisingly complex thing when you start to think about it in detail May 1, 2021 at 16:56
• It certainly is. That statement is touching on where I'm coming from. May 1, 2021 at 17:24
• I've been looking for a network to do that type of counting too. Have you seen this? stanford.edu/~jlmcc/papers/… Apr 25, 2022 at 15:16

Estimating from an observation is a function, but "really counting" is a process. Feed-forward neural networks can learn arbitrary functions from training examples, but they cannot represent (and therefore cannot learn) processes. They can attempt to estimate the results of completing a process as a function, but that is not the same thing as actually performing the process.

To learn arbitrary processes from examples requires a model with some concept of state and evolution over time in addition to any functions that are required. Recurrent neural networks (RNNs) are suitable models for those kinds of learning problems, but so are other AI learning constructs.

This rabbit hole goes deep, and for any model you could build it is possible to ask:

• Is the system really performing a counting task similar to how a human might attempt the same task?

• Has the sytem really learned to count, or has it been constructed by the developer so that some parts of the process are inevitable?

• Has the system learned to count in general within any sub-component, or are the things that it can count tightly coupled across components?

There are other questions you could ask too, depending on your goals for producing such a model.

It is worth noting that for small quantities and certain patterns, that human "counting" does more closely resemble the estimation process, or perhaps is more akin to an NLP problem. For example, consider how you "count" the pips on a die - although you can use a counting process to confirm what you see, typically you do not do so. Instead, some part of your brain is supplying a very accurate answer quickly and unconsciously:

More generally, counting in humans likely consists of multiple related strategies for converting observations into symbolic and/or sensory representations of quantity. Some may be instinct, some are learned conscious behaviours and some appear to be in-between, perhaps originally learned but turned into subconscious skill through repetition.

Ignoring whether or not you are implementing your AI project for counting items in an image wholly within a neural network for now, I think you need the following components:

• A state that represents and tracks the start, progress so far and end of the counting process with respect to the input being processed.

• An accumulator that represents quantity counted so far.

• A detector that can trigger a counting event when it observes something that needs to be counted.

• A strategy or planner for processing input so that detection events are separated and only triggered once for each valid event.

The last component can vary a lot, and humans will use a range of different strategies for counting depending on the difficulty of the task. For instance, you might use working memory (a limited resource in humans) to track a few key points in an image to help segment an image into smaller sections as you work across it. Or you might make visual marks on an image to track each object that had already been counted. For a human, counting strategies can be mixed and matched, and switched between sometimes even during the same counting task.

All of these components could be represented in learnable parts of an AI process, but they do not have to be. When you suggest in your question that

I don't want to use a detector, then count the number of bounding boxes.

then you are most likely saying that you don't want to use a fixed hard-coded strategy. You want to somehow create a neural network that can discover at least one strategy for processing the input.

The trouble you will face is that giving a large RNN model (e.g. LSTM) a dataset of examples with input images and output correct counts will likely be too much of a challenge. Discovery of a robust object counting system from scratch will be too hard.

There are a few things that may help you construct something that "really" learns to count. Here are a couple of ideas:

• Curriculum learning, where you start training the neural network with some hand-holding examples, and slowly ramp up the complexity. This is analogous to how we teach children to count.

• Designed-in modelling for processing strategy. For instance, you could add a virtual fovea and artificial visual saccades and require that the neural network output a sequence of locations within the image that it picks out to run the detector against. This will be a constraint that allows certain types of human-like counting strategy to work, and could simplify the problem to the point that the network has a chance to learn it.

A paper that uses a "fovea" model for object detection: Object detection through search with a foveated visual system

• Thanks Neil. I didn't want to anchor answers, but the reason I thought about this question was because of this Kaggle competition. One component of it is counting the number of different atom types in a diagram of a molecule, and then it gets more complicated because each atom needs a unique integer label and then some sort of graph representation to be written down. Top teams are getting very impressive results and it's mostly with a image encoder, sequence decoder architecture (you mentioned LSTM). May 3, 2021 at 8:31
• People appear to be using transformers instead of LSTMs though. I just couldn't help but wonder if the network was learning the concept of counting, labelling, and writing down the graphical representation, or just learning what the output string should be without going through those intermediate logical steps. May 3, 2021 at 8:33
• @AlexanderSoare I do not know enough about those kinds of models, but suspect they do fit at least in general terms with this answer. I would also suspect that there will be careful use of designed-in constraints when it comes to handling the image-to-sequence part of the challenge, making it feasible in the first place and more accurate at the single task in the competition. May 3, 2021 at 8:39