Answers.Body: <p>Estimating from an observation is a function, but &quot;really counting&quot; 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.</p>
  <p>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. <em>Recurrent</em> neural networks (RNNs) are suitable models for those kinds of learning problems, but so are other AI learning constructs.</p>
  <p>This rabbit hole goes deep, and for any model you could build it is possible to ask:</p>
  <ul>
  <li><p>Is the system <em>really</em> performing a counting task similar to how a human might attempt the same task?</p>
  </li>
  <li><p>Has the sytem <em>really</em> learned to count, or has it been constructed by the developer so that some parts of the process are inevitable?</p>
  </li>
  <li><p>Has the system learned to count <em>in general</em> within any sub-component, or are the things that it can count tightly coupled across components?</p>
  </li>
  </ul>
  <p>There are other questions you could ask too, depending on your goals for producing such a model.</p>
  <p>It is worth noting that for small quantities and certain patterns, that human &quot;counting&quot; does more closely resemble the estimation process, or perhaps is more akin to an NLP problem. For example, consider how you &quot;count&quot; 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:</p>
  <p><a href="https://i.sstatic.net/5iIL5.jpg?s=256" rel="noreferrer"><img src="https://i.sstatic.net/5iIL5.jpg?s=256" alt="Dice image from Diacritica, CC BY-SA 3.0 https://creativecommons.org/licenses/by-sa/3.0, via Wikimedia Commons" /></a></p>
  <p>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.</p>
  <p>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:</p>
  <ul>
  <li><p>A state that represents and tracks the start, progress so far and end of the counting process with respect to the input being processed.</p>
  </li>
  <li><p>An accumulator that represents quantity counted so far.</p>
  </li>
  <li><p>A detector that can trigger a counting event when it observes something that needs to be counted.</p>
  </li>
  <li><p>A strategy or planner for processing input so that detection events are separated and only triggered once for each valid event.</p>
  </li>
  </ul>
  <p>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.</p>
  <p>All of these components could be represented in <em>learnable</em> parts of an AI process, but they do not have to be. When you suggest in your question that</p>
  <blockquote>
  <p>I don't want to use a detector, then count the number of bounding boxes.</p>
  </blockquote>
  <p>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.</p>
  <p>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.</p>
  <p>There are a few things that may help you construct something that &quot;really&quot; learns to count. Here are a couple of ideas:</p>
  <ul>
  <li><p>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.</p>
  </li>
  <li><p>Designed-in modelling for processing strategy. For instance, you could add a virtual <a href="https://en.wikipedia.org/wiki/Fovea_centralis" rel="noreferrer">fovea</a> and artificial <a href="https://en.wikipedia.org/wiki/Saccade" rel="noreferrer">visual saccades</a> 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.</p>
  </li>
  </ul>
  <p>A paper that uses a &quot;fovea&quot; model for object detection: <a href="https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1005743" rel="noreferrer">Object detection through search with a foveated visual system</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-03T08:31:22.840Z
Answers.Text: Thanks Neil. I didn't want to anchor answers, but the reason I thought about this question was because of this [Kaggle competition](https://www.kaggle.com/c/bms-molecular-translation). 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).
Answers.UserDisplayName: Alexander Soare
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-03T08:33:11.413Z
Answers.Text: 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.
Answers.UserDisplayName: Alexander Soare
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-03T08:39:29.027Z
Answers.Text: @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.
Answers.UserDisplayName: Neil Slater
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-02T14:37:57.380Z
Answers.LastActivityDate: 2021-05-02T17:10:07.447Z
Answers.LastEditDate: 2021-05-02T17:10:07.447Z
Answers.UserDisplayName: Neil Slater
Body: <p>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.</p>
  <p>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.</p>
  <p>The simplest variation of this problem might be: I have an input vector <span class="math-container">$x \in \{0, 1\}^N$</span> 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 <span class="math-container">$[0,1]$</span> and multiply the result by <span class="math-container">$N$</span>, 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)?</p>
  <p>I want to know if there's a model which can learn <strong>how to count</strong>.</p>
  <p>Here's another way I'm thinking about it: I can look at an aerial view of pine trees and say &quot;yeah maybe there are 30 trees&quot;, 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 &quot;counting&quot;.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-05-01T10:41:28.240Z
Comments.Text: 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
Comments.UserDisplayName: Neil Slater
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-05-01T13:09:04.537Z
Comments.Text: @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.
Comments.UserDisplayName: Alexander Soare
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-05-01T16:56:15.053Z
Comments.Text: 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
Comments.UserDisplayName: Neil Slater
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-05-01T17:24:55.673Z
Comments.Text: It certainly is. That statement is touching on where I'm coming from.
Comments.UserDisplayName: Alexander Soare
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-04-25T15:16:46.880Z
Comments.Text: I've been looking for a network to do that type of counting too. Have you seen this?
  https://stanford.edu/~jlmcc/papers/SabathielMcCSolstad20LearningToCountMultiModalEnvironment.pdf
Comments.UserDisplayName: numiri
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-04-27T01:20:43.293Z
Comments.Text: @numiri I haven't seen it but thanks for sharing
Comments.UserDisplayName: Alexander Soare
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-07-26T18:39:41.027Z
Comments.Text: Very interesting problem. Maybe you can find or create some labeled gaming data set with moving objects. And if you will, please let me know;).
Comments.UserDisplayName: Alexey Kornaev
Tags: <neural-networks><architecture><generalization>
Title: How can we get a differentiable neural network to count things?
UserDisplayName: Alexander Soare
ThreadText: # How can we get a differentiable neural network to count things?
  
  (Asked by: username_0 on 2021-04-30)
  
  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".
  
  **Tags:** neural-networks, architecture, generalization
  > **Comments:**
  > **username_1**: 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  
  > **username_0**: @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.  
  > **username_1**: 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  
  > **username_0**: It certainly is. That statement is touching on where I'm coming from.  
  > **username_2**: I've been looking for a network to do that type of counting too. Have you seen this? https://stanford.edu/~jlmcc/papers/SabathielMcCSolstad20LearningToCountMultiModalEnvironment.pdf  
  > **username_0**: @numiri I haven't seen it but thanks for sharing  
  > **username_3**: Very interesting problem. Maybe you can find or create some labeled gaming data set with moving objects. And if you will, please let me know;).  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-05-02. Score: 7 | Accepted answer
  
  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:
  
  [![Dice image from Diacritica, CC BY-SA 3.0 https://creativecommons.org/licenses/by-sa/3.0, via Wikimedia Commons](https://i.sstatic.net/5iIL5.jpg?s=256)](https://i.sstatic.net/5iIL5.jpg?s=256)
  
  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](https://en.wikipedia.org/wiki/Fovea_centralis) and artificial [visual saccades](https://en.wikipedia.org/wiki/Saccade) 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](https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1005743)
  
  > **Comments:**
  > **username_0**: Thanks Neil. I didn't want to anchor answers, but the reason I thought about this question was because of this [Kaggle competition](https://www.kaggle.com/c/bms-molecular-translation). 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).  
  > **username_0**: 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.  
  > **username_1**: @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.  
  
  ---
  
  