Representation Learning by Learning to Count

Mehdi Noroozi, Hamed Pirsiavash, Paolo Favaro

Introduction

Deep learning's success is largely tied to massive labeled datasets like ImageNet. However, manual annotation is a significant bottleneck that does not scale to the vast amounts of unlabeled data available. Self-supervised learning offers a solution by defining "pretext tasks" where labels are derived from the data itself. This paper introduces a novel approach: training a network to recognize semantic features by enforcing a simple arithmetic constraint—the number of "visual primitives" in an image must equal the sum of primitives in its constituent tiles.

The Counting Pretext Task

The core idea relies on the property of equivariance under tiling and scaling. If an image is divided into a $2 \times 2$ grid, the total count of any semantic feature (e.g., eyes, paws, or heads) in the full image should match the sum of those features across the four tiles.

Conceptual illustration of the counting constraint: the sum of features in tiles equals the features in the whole image.

By forcing a neural network to satisfy this linear relationship, the authors hypothesize that the model must learn to disentangle complex high-level factors of variation. For instance, to "count" a dog's head accurately across different scales and crops, the network must first learn to identify what a dog's head looks like, regardless of its position or resolution.

Architecture and Training

The authors implement this using a Siamese-style AlexNet architecture. The model processes two versions of the same input: a downsampled version of the full image ($D \circ \mathbf{x}$) and four non-overlapping tiles ($T_1 \dots T_4$) from the original high-resolution image.

The Siamese architecture used to enforce the counting constraint and contrastive loss.

The training objective involves two primary components:

  1. Counting Constraint: Minimize the squared Euclidean distance between the feature vector of the downsampled image ($\mathbf{d}$) and the sum of the feature vectors of the tiles ($\mathbf{t}$): $|\mathbf{d} - \mathbf{t}|^2$.
  2. Contrastive Term: To prevent the trivial solution where the network outputs zeros for all inputs, a contrastive loss is applied. It ensures that the counting vectors for different images ($\mathbf{c}$ and $\mathbf{t}$) remain distinct: $\max\{0, M - |\mathbf{c} - \mathbf{t}|^2\}$.

To prevent the network from "cheating" by detecting low-level artifacts—such as interpolation fingerprints or chromatic aberration—the authors randomized downsampling methods and used grayscale images during training.

Key Insights and Results

The resulting representations are remarkably effective. Despite the unsupervised nature of the task, the model develops sparse, semantically meaningful features. Qualitative analysis showed that individual neurons in the counting layer began to respond to specific semantic categories, such as "dogs" or "baseball players," despite never receiving a single manual label.

Transfer Learning Performance

The model was evaluated by transferring its learned weights to standard benchmarks, where it rivaled or exceeded state-of-the-art unsupervised methods:

  • PASCAL VOC 2007 Classification: Achieved 67.7% mAP, outperforming contemporary methods like Jigsaw puzzles (67.6%) and Context Encoders (56.5%).
  • PASCAL VOC 2007 Detection: Reached 51.4% mAP, demonstrating that the "counting" features provide a strong foundation for localization tasks.
  • Generalization: When trained on ImageNet and tested on the Places dataset, the model's early layers (conv1) outperformed fully supervised ImageNet models, indicating the learning of highly robust and generalizable low-level filters.

Conclusion

"Learning to Count" demonstrates that simple geometric and arithmetic constraints can drive the emergence of high-level semantic understanding in neural networks. By leveraging the inherent structure of visual data—specifically equivariance to tiling and scaling—this method provides a powerful, label-free alternative for representation learning that competes with supervised pre-training in several key computer vision tasks.

Summary updated

BibTeX
@inproceedings{noroozi2017representation,
  author = {Mehdi Noroozi and Hamed Pirsiavash and Paolo Favaro},
  title = {{Representation Learning by Learning to Count}},
  booktitle = {IEEE/CVF International Conference on Computer Vision},
  year = {2017},
  doi = {10.1109/iccv.2017.628},
}
Figure