5 Common Myths about CNNs You Should Stop Believing


How Do Models Learn Spatial Information?

Published on October, 2024

I’ll share with you a challenge I recently faced, while creating a model to classify histology images, and the questions it raised for me on positional encoding. Histological images are microscopic images of tissues that can be used for cancer diagnosis. These images are quite large, around a gigabyte, and must be broken down into tiles to be used as input to a neural network. I needed to classify each slide as positive or negative for a certain type of cancer.

Author's Photo

While choosing a model, I opted to use a transformer due to its self-attention mechanism. Self-attention helps model to understand relationships between distant parts of an image more effectively than traditional convolutional neural networks. This is important, as the position of what is seen on the slide is significant to the decision-making.

The question I had was: How can we incorporate the positional information into the transformer model? An even more fundamental and general question I had is How much position information do convolutional neural networks encode?

Author's Photo
Transformers

The big novelty in transformers is the use of self-attention mechanism that processes the complete input simultaneously. The attention layer allows the model to focus on different parts of the input sequence.

Author's Photo
Transformers vs. Recurrent Neural Network (RNN)

I really love the explanation in this post: How Self-Attention with Relative Position Representations works. The post uses the following sentence as an example: “I think therefore I am”. In RNN, its architecture allows it to implicitly encode sequential information using its hidden state. The output representation for the second “I” is not the same as the output representation for the first “I” because the hidden state that is inputted into these words are not the same. In contrast, the self-attention layer of a transformer (without any positional representation) causes identical words at different positions to have the same output representation.

Author's Photo
Positional Embeddings

Each embedding gets a vector that includes information about the position (or order, in the case of sentences). One requirement of positional encoding is that every position must have the same identifier, irrespective of the sequence length or what the input is. For example, position 2 out of a sentence of 10 words is the same as position 2 out of a sentence of 5 words. Another requirement is that the positional data should be of the same scale as the semantic data, so it won’t outweigh the importance of the semantic information. Should we add or concatenate the vectors? Adding the vectors is more memory and time-efficient, but concatenation has the advantage of retaining each portion of the data. We see that adding works sufficiently well, so this is the preferable method.

Fixed Absolute Positional Encoding

In the notable article “Attention is all you need” by Vaswani et al., 2017, they used sine and cosine function of different frequencies. Each position was mapped to a vector so that the output of the positional encoding layer was a matrix. The sine and cosine functions have values in [-1, 1], which keeps the values of the positional encoding matrix in a normalized range. The advantages were that it allows the model to attend relative positions effortlessly: For any fixed offset k there is a linear transform that convert PEpos to PEpos+k, and that it would allow the model to extrapolate to sequence lengths longer than the ones encountered during training, compared to learned features.

Learned Absolute Encoding

In the well-known article “An Image is Worth 16×16 Words” that introduced transformers for vision tasks, the researchers used learned positional embeddings that were updated with the same procedure as with other parameters using stochastic gradient descent and backpropagation. By analysing the final learned positional encoding of the model, they showed that closer patches tend to have more similar position embeddings. Interestingly, they summarized that adding positional embedding significantly improved the results, but they did not see significant gains of the learned embedding over the default encodings that were just numbering image patches.

Author's Photo

Relative Position Representations (RPR)

On 2018 Shaw et al. used relative position representation – where the distance between tokens is important. With relative representations, each word or token does not have only one positional embedding, but as many positional embeddings as there are tokens in the sequence, to describe the relationship between them. Nevertheless, after a certain distance, the positional embeddings get the same value. Hence, the learned positional information generalizes to any sequence length, like the hand-crafted sinusoidal embedding.

Positional Information in CNNs

By definition, convolutional neural networks (CNNs) learn weights associated with local filters with a finite spatial extent. A filter may know what it is looking at, but not where it is positioned in the image. In the study of Islam et al., 2020 they tested the hypothesis that deep CNNs may implicitly learn to encode absolute positional information even if they were not implicitly trained for it. Their network, named PosENet, was composed of encoder and position encoder blocks. The encoder network has frozen weights and yielded features at different levels. The position encoder predicted the absolute position information applying a sequence of convolution operations.

Author's Photo
Conclusion

To summarize, we revised different approaches of using positional information in transformers and CNNs. In transformers, we saw different types of methods to incorporate the information including fixed, learned, absolute or relative encoding. In CNNs we saw that model learn this information implicitly due to its architecture. The exact method that suits you depends on the application, data and reassurers that you have. Goodluck!


References

  1. Lu MY, Williamson DFK, Chen TY, Chen RJ, Barbieri M, Mahmood F. Data-efficient and weakly supervised computational pathology on whole-slide images. Nat Biomed Eng. 2021.
  2. Dufter P, Schmitt M, Schütze H. Position Information in Transformers: An Overview. Comput Linguist. 2022.
  3. Vaswani A, Shazeer N, Parmar N, Uszkoreit J, Jones L, Gomez AN, Kaiser L, Polosukhin I. Attention Is All You Need. NIPS, 2017.
  4. Dosovitskiy A, Beyer L, Kolesnikov A, Weissenborn D, Zhai X, Unterthiner T, Dehghani M, Minderer M, Heigold G, Gelly S, Uszkoreit J, Houlsby N. An Image is Worth 16×16 Words: Transformers for Image Recognition at Scale. ICLR, 2021.
  5. Wang YA, Chen YN. What Do Position Embeddings Learn? An Empirical Study of Pre-Trained Language Model Positional Encoding. EMNLP, 2020.
  6. Shaw P, Uszkoreit J, Vaswani A. Self-Attention with Relative Position Representations. NAACL, 2018.
  7. Islam AM, Jia S, Bruce NDB. How Much Position Information Do Convolutional Neural Networks Encode? ICLR, 2020.
Author's Photo

About the Author

This article was written by Naama Shoham, an algorithm developer at RTC Vision. She holds a PhD in Biomedical Engineering from Tel Aviv University, where she published over 15 articles and book chapters and received excellence scholarships. With extensive academic and industry experience in Computer Vision and Machine Learning, Naama is passionate about advancing these fields further.

Leave a Reply

Your email address will not be published. Required fields are marked *