Do Vision Transformers See Like Convolutional Neural Networks?

1 Introduction . Over the past several years , the successes of deep learning on visual tasks has critically relied on convolutional neural networks [ 20 , 16 ] . This is largely due to the powerful inductive bias of spatial equivariance encoded by convolutional layers , which have been key to learning general purpose visual representations for easy transfer and strong performance . Remarkably however , recent work has demonstrated that Transformer neural networks are capable of equal or superior performance on image classification tasks at large scale [ 14 ] . These Vision Transformers ( ViT ) operate almost identically to Transformers used in language [ 13 ] , using self-attention , rather than convolution , to aggregate information across locations . This is in contrast with a large body of prior work , which has focused on more explicitly incorporating image-specific inductive biases [ 30 , 9 , 4 ] This breakthrough highlights a fundamental question : how are Vision Transformers solving these image based tasks ? Do they act like convolutions , learning the same inductive biases from scratch ? Or are they developing novel task representations ? What is the role of scale in learning these representations ? And are there ramifications for downstream tasks ? In this paper , we study these questions , uncovering key representational differences between ViTs and CNNs , the ways in which these difference arise , and effects on classification and transfer learning . Specifically , our contributions are : 35th Conference on Neural Information Processing Systems ( NeurIPS 2021 ) . • We investigate the internal representation structure of ViTs and CNNs , finding striking differences between the two models , such as ViT having more uniform representations , with greater similarity between lower and higher layers . • Analyzing how local/global spatial information is utilised , we find ViT incorporates more global information than ResNet at lower layers , leading to quantitatively different features . • Nevertheless , we find that incorporating local information at lower layers remains vital , with large-scale pre-training data helping early attention layers learn to do this • We study the uniform internal structure of ViT , finding that skip connections in ViT are even more influential than in ResNets , having strong effects on performance and representation similarity . • Motivated by potential future uses in object detection , we examine how well input spatial information is preserved , finding connections between spatial localization and methods of classification . • We study the effects of dataset scale on transfer learning , with a linear probes study revealing its importance for high quality intermediate representations . 2 Related Work . Developing non-convolutional neural networks to tackle computer vision tasks , particularly Transformer neural networks [ 44 ] has been an active area of research . Prior works have looked at local multiheaded self-attention , drawing from the structure of convolutional receptive fields [ 30 , 36 ] , directly combining CNNs with self-attention [ 4 , 2 , 46 ] or applying Transformers to smaller-size images [ 6 , 9 ] . In comparison to these , the Vision Transformer [ 14 ] performs even less modification to the Transformer architecture , making it especially interesting to compare to CNNs . Since its development , there has also been very recent work analyzing aspects of ViT , particularly robustness [ 3 , 31 , 28 ] and effects of self-supervision [ 5 , 7 ] . Other recent related work has looked at designing hybrid ViT-CNN models [ 49 , 11 ] , drawing on structural differences between the models . Comparison between Transformers and CNNs are also recently studied in the text domain [ 41 ] . Our work focuses on the representational structure of ViTs . To study ViT representations , we draw on techniques from neural network representation similarity , which allow the quantitative comparisons of representations within and across neural networks [ 17 , 34 , 26 , 19 ] . These techniques have been very successful in providing insights on properties of different vision architectures [ 29 , 22 , 18 ] , representation structure in language models [ 48 , 25 , 47 , 21 ] , dynamics of training methods [ 33 , 24 ] and domain specific model behavior [ 27 , 35 , 38 ] . We also apply linear probes in our study , which has been shown to be useful to analyze the learned representations in both vision [ 1 ] and text [ 8 , 32 , 45 ] models . 3 Background and Experimental Setup . Our goal is to understand whether there are differences in the way ViTs represent and solve image tasks compared to CNNs . Based on the results of Dosovitskiy et al . [ 14 ] , we take a representative set of CNN and ViT models — ResNet50x1 , ResNet152x2 , ViT-B/32 , ViT-B/16 , ViT-L/16 and ViT-H/14 . Unless otherwise specified , models are trained on the JFT-300M dataset [ 40 ] , although we also investigate models trained on the ImageNet ILSVRC 2012 dataset [ 12 , 37 ] and standard transfer learning benchmarks [ 50 , 14 ] . We use a variety of analysis methods to study the layer representations of these models , gaining many insights into how these models function . We provide further details of the experimental setting in Appendix A . Representation Similarity and CKA ( Centered Kernel Alignment ) : Analyzing ( hidden ) layer representations of neural networks is challenging because their features are distributed across a large number of neurons . This distributed aspect also makes it difficult to meaningfully compare representations across neural networks . Centered kernel alignment ( CKA ) [ 17 , 10 ] addresses these challenges , enabling quantitative comparisons of representations within and across networks . Specifically , CKA takes as input X ∈ Rm×p1 and Y ∈ Rm×p2 which are representations ( activation matrices ) , of two layers , with p1 and p2 neurons respectively , evaluated on the same m examples . Letting K = XXT and L = Y Y T denote the Gram matrices for the two layers ( which measures the similarity of a pair of datapoints according to layer representations ) CKA computes : CKA ( K , L ) = HSIC ( K , L ) √ HSIC ( K , K ) HSIC ( L , L ) , ( 1 ) 0 20 40 60 80 100 120 140 Layers ViT-L/16 0 20 40 60 80 100 120 140 La ye rs V iT -L /1 6 ViT-L/16 0 25 50 75 100 125 150 175 Layers ViT-H/14 0 25 50 75 100 125 150 175 La ye rs V iT -H /1 4 ViT-H/14 0 20 40 60 80 100 120 Layers R50 0 20 40 60 80 100 120 La ye rs R 50 R50 0 50 100 150 200 250 300 350 Layers R152 0 50 100 150 200 250 300 350 La ye rs R 15 2 R152 where HSIC is the Hilbert-Schmidt independence criterion [ 15 ] . Given the centering matrix H = In− 1n11 T and the centered Gram matrices K ′ = HKH and L′ = HLH , HSIC ( K , L ) = vec ( K ′ ) · vec ( L′ ) / ( m− 1 ) 2 , the similarity between these centered Gram matrices . CKA is invariant to orthogonal transformation of representations ( including permutation of neurons ) , and the normalization term ensures invariance to isotropic scaling . These properties enable meaningful comparison and analysis of neural network hidden representations . To work at scale with our models and tasks , we approximate the unbiased estimator of HSIC [ 39 ] using minibatches , as suggested in [ 29 ] . 4 Representation Structure of ViTs and Convolutional Networks . We begin our investigation by using CKA to study the internal representation structure of each model . How are representations propagated within the two architectures , and are there signs of functional differences ? To answer these questions , we take every pair of layers X , Y within a model and compute their CKA similarity . Note that we take representations not only from outputs of ViT/ResNet blocks , but also from intermediate layers , such as normalization layers and the hidden activations inside a ViT MLP . Figure 1 shows the results as a heatmap , for multiple ViTs and ResNets . We observe clear differences between the internal representation structure between the two model architectures : ( 1 ) ViTs show a much more uniform similarity structure , with a clear grid like structure ( 2 ) lower and higher layers in ViT show much greater similarity than in the ResNet , where similarity is divided into different ( lower/higher ) stages . We also perform cross-model comparisons , where we take all layers X from ViT and compare to all layers Y from ResNet . We observe ( Figure 2 ) that the lower half of 60 ResNet layers are similar to approximately the lowest quarter of ViT layers . In particular , many more lower layers in the ResNet are needed to compute similar representations to the lower layers of ViT . The top half of the ResNet is approximately similar to the next third of the ViT layers . The final third of ViT layers is less similar to all ResNet layers , likely because this set of layers mainly manipulates the CLS token representation , further studied in Section 6 . Taken together , these results suggest that ( i ) ViT lower layers compute representations in a different way to lower layers in the ResNet , ( ii ) ViT also more strongly propagates representations between lower and higher layers ( iii ) the highest layers of ViT have quite different representations to ResNet . 5 Local and Global Information in Layer Representations . In the previous section , we observed much greater similarity between lower and higher layers in ViT , and we also saw that ResNet required more lower layers to compute similar representations to a smaller set of ViT lower layers . In this section , we explore one possible reason for this difference : the difference in the ability to incorporate global information between the two models . How much global information is aggregated by early self-attention layers in ViT ? Are there noticeable resulting differences to the features of CNNs , which have fixed , local receptive fields in early layers ? In studying these questions , we demonstrate the influence of global representations and a surprising connection between scale and self-attention distances . Analyzing Attention Distances : We start by analyzing ViT self-attention layers , which are the mechanism for ViT to aggregate information from other spatial locations , and structurally very different to the fixed receptive field sizes of CNNs . Each self-attention layer comprises multiple self-attention heads , and for each head we can compute the average distance between the query patch position and the locations it attends to . This reveals how much local vs global information each self-attention layer is aggregating for the representation . Specifically , we weight the pixel distances by the attention weights for each attention head and average over 5000 datapoints , with results shown in Figure 3 . In agreement with Dosovitskiy et al . [ 14 ] , we observe that even in the lowest layers of ViT , self-attention layers have a mix of local heads ( small distances ) and global heads ( large distances ) . This is in contrast to CNNs , which are hardcoded to attend only locally in the lower layers . At higher layers , all self-attention heads are global . Interestingly , we see a clear effect of scale on attention . In Figure 4 , we look at attention distances when training only on ImageNet ( no large-scale pre-training ) , which leads to much lower performance in ViT-L/16 and ViT-H/14 [ 14 ] . Comparing to Figure 3 , we see that with not enough data , ViT does not learn to attend locally in earlier layers . Together , this suggests that using local information early on for image tasks ( which is hardcoded into CNN architectures ) is important for strong performance . Does access to global information result in different features ? The results of Figure 3 demonstrate that ViTs have access to more global information than CNNs in their lower layers . But does this result in different learned features ? As an interventional test , we take subsets of the ViT attention heads from the first encoder block , ranging from the subset corresponding to the most local attention heads to a subset of the representation corresponding to the most global attention heads . We then compute CKA similarity between these subsets and the lower layer representations of ResNet . The results , shown in Figure 5 , which plot the mean distance for each subset against CKA similarity , clearly show a monotonic decrease in similarity as mean attention distance grows , demonstrating that access to more global information also leads to quantitatively different features than computed by the local receptive fields in the lower layers of the ResNet . Effective Receptive Fields : We conclude by computing effective receptive fields [ 23 ] for both ResNets and ViTs , with results in Figure 6 and Appendix C. We observe that lower layer effective receptive fields for ViT are indeed larger than in ResNets , and while ResNet effective receptive fields grow gradually , ViT receptive fields become much more global midway through the network . ViT receptive fields also show strong dependence on their center patch due to their strong residual connections , studied in the next section . As we show in Appendix C , in attention sublayers , receptive fields taken before the residual connection show far less dependence on this central patch .

Summary: The paper presents a comparison study between internal representations of recently Vision Transformers and ResNet networks for image classification, using various established tools: Centered kernel alignment, attention distance analysis, effective receptive fields, representation propagation analysis, spatial localization analysis and linear probes. The also train some new models for additional analysis. The findings show significant difference between the models.