1
EC 500 A1: �Vision Transformers
Homework assignment & midterm
Final project first glance (subject to change)
Final project first glance (subject to change)
5
Questions?
Convolutional neural networks (CNN)
6
Vectorization + FC layers
Data structure (in training)
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
Today
Rethink about neural networks for images
9
Dog
Bird
Neural Network
(encoder)
Feature vector
……
Has four legs
Has wing
……
1
0
……
0
1
What is the final fully connected (FC) layer doing?
[Koh et al., Concept Bottleneck Models, 2020]
Illustration
red
blue
Long beak
Long leg
Cardinal
Flamingo
Blue Jay
Image
CNN
Yes: 1; No: -1
A simple CNN classifier (assuming gray images)
12
A simple CNN classifier (assuming gray images)
13
A simple CNN classifier (assuming gray images)
14
A simple CNN classifier (assuming gray images)
15
A simple CNN classifier (assuming gray images)
16
A simple CNN classifier (assuming gray images)
17
Rethink about neural networks for images
18
Dog
Bird
Neural Network
(encoder)
Feature vector
……
Has four legs
Has wing
……
1
0
……
0
1
A simple CNN classifier (assuming gray images)
19
Rethink about neural networks for images
20
Dog
Bird
Neural Network
(encoder)
……
……
1
0
……
0
1
Feature vector
……
……
……
……
……
……
……
……
……
……
……
……
……
……
21
Questions?
Popular CNN architectures
22
Popular CNN architectures
23
Popular CNN architectures
24
25
Questions?
Class Activation Map (CAM)
26
[Zhou et al., Learning Deep Features for Discriminative Localization, CVPR 2016]
……
CAM for fine-grained tasks
CAM [Zhou et al., 2016] is not localized enough …
27
CAM for fine-grained tasks
28
Why Blue Grosbeak?
Why Grandala?
Target: Blue Grosbeak
Grad-CAM
Why cannot Grad-CAM see the fine difference?
29
0.4
0.3
logits
linear
classifier
…
feature
maps
encoder
blocks
Categories
Why cannot Grad-CAM see the fine difference?
30
0.4
0.3
logits
…
feature
maps
Finer-CAM
linear
classifier
encoder
blocks
Categories
Finer-CAM: Spotting the difference [CVPR 25]
31
Why Blue Grosbeak?
Why Grandala?
What’s the difference from Grandala?
Target: Blue Grosbeak
Grad-CAM
Finer-CAM
Target: Blue Grosbeak
Reference: Grandala
Finer-CAM: Spotting the Difference Reveals Finer Details for Visual Explanation, CVPR 2025
Finer-CAM: Spotting the difference [CVPR 25]
None
2nd prediction
3rd prediction
4th prediction
Finer-CAM: Spotting the Difference Reveals Finer Details for Visual Explanation, CVPR 2025
Today
How do I know it is a zebra?
Convolutions
35
Linear receptive field
Convolutions
36
Linear receptive field
Exponential receptive field
(with pooling + down-sampling)
CNN vs. Vision transformer: different aggregations
37
CNN
Convolution
Vision transformer
Transformer
neuron
Vision Transformer
38
Image (pixels)
neuron
Vision Transformer
39
(2) Vectorize each of them
+ encode each with a shared MLP
+ “spatial” encoding
(1) Split an image into patches
[Dosovitskiy et al., An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale, ICLR 2021]
Revisit: data structure of images
40
Regular spatial grid
Vision transformer
41
(2) Vectorize each of them
+ encode each with a shared MLP
+ “spatial” encoding
(1) Split an image into patches
1-layer of Transformer Encoder
[Dosovitskiy et al., An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale, ICLR 2021]
1-layer of transformer encoder
42
K
Q
V
key, query, value
“learnable” matrices
Relatedness of patch-5 to others (after softmax)
Weighted value vectors
Single-head case
Pixels or patches relationship
Transformers
Convolutional neural nets (CNNs)
CNN vs. Vision Transformer
44
CNN
Convolution
Vision transformer
Transformer
45
Questions?
CNN vs. Vision Transformer
46
CNN
Convolution
Vision transformer
Transformer
Key difference
Limitation of CNN
Using size 3 kernel
How about Fully connected layers?
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
Attention
Vision Transformer
50
(2) Vectorize each of them
+ encode each with a shared MLP
+ “spatial” encoding
(1) Split an image into patches
1-layer of Transformer Encoder
[Dosovitskiy et al., An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale, ICLR 2021]
1-layer of transformer encoder
51
K
Q
V
key, query, value
“learnable” matrices
Relatedness of patch-5 to others (after softmax)
Weighted value vectors
Single-head case
Today
Data type in transformers: tokens
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
Comparison: CNN vs. transformers
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
Operations on tokens: overall
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
Operations on tokens: linear combination
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
Operations on tokens: parallel nonlinearity
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
Today
The attention layer
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
How to obtain A?
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
How to obtain A?
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
Each token will generate a key vector k
How to obtain A?
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
Softmax:
What if there is no explicit question? Self-attention
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
Self-attention expanded
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
Self-attention expanded
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
Attention (contribution) matrix:
How does token j contribute to token?
Illustration
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
Illustration
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
Illustration
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
Illustration
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
Illustration
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
72
Questions?
MLP vs. (multi-layer) transformer
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
Properties: permutation invariant
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
Variants: masked attention
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
I go to school
Today
Vision Transformer
77
(2) Vectorize each of them
+ encode each with a shared MLP
+ “spatial” encoding
(1) Split an image into patches
1-layer of Transformer Encoder
[Dosovitskiy et al., An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale, ICLR 2021]
Vision Transformer
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
Skip connection
Multi-head
Self-attention
Check textbook!
Position encoding
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
(x, y)
Position encoding
[Figure credit: A. Torralba, P. Isola, and W. T. Freeman, Foundations of Computer Vision.]
(x, y)
Position encoding
[https://erdem.pl/2021/05/understanding-positional-encoding-in-transformers]
82
Questions?
How to do classification?
83
(2) Vectorize each of them
+ encode each with a shared MLP
+ “spatial” encoding
(1) Split an image into patches
1-layer of Transformer Encoder
[Dosovitskiy et al., An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale, ICLR 2021]
Adding a classification token
[Dosovitskiy et al., An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale, ICLR 2021]
84
Often called a [CLS] token which is learnable
1-layer of transformer encoder
K
Q
V
key, query, value
“learnable” matrices
Single-head case
85
1-layer of transformer encoder
K
Q
V
key, query, value
“learnable” matrices
Multi-head case
86
K
Q
V
Short summary
A general architecture of CNN or visual transformers involves
87