r/MachineLearning • u/terrrp • 14d ago
Discussion Why are ViTs so popular? [D]
I deploy CNN + Attention stack models on edge devices, to great effect. Taking advantage of convs and reducing the spatial dimensionality makes all the sense in the world to me.
Having vanilla ViTs learning the inductive biases from scratch makes none to me. Is it really a case of "because I can"? Avoiding padding along borders is the only advantage I can see to a tokenizer stem, and that has never been a huge issue to me.
Am I missing something that has people reaching for ViTs instead of hybrid models for e.g. SSL/foundation models?
In my mind it's just a case of people using the status quo, and more of a historical accident that these models have become so popular. Iirc levit showed data/time efficiency boosts with a convolutional stem. I just don't work on the same datasets or tasks that most research does, so I'm genuinely curious if anyone else thinks vanilla ViTs seem excessive. Guess compute is cheap and the answer would just be distillation.
9
u/Ok-Introduction9593 14d ago
A lot of it just comes down to tooling unification. When your entire infra stack is heavily tuned for Transformers - FlashAttention, tensor parallelism, all that - it's way easier to just patchify images and push them through the same pipeline than to balance hybrid bottlenecks across GPUs
140
u/impatiens-capensis 14d ago
A ViT with enough attention heads can mathematically express any convolutional layer. A CNN fixes you to that prior ahead of time, but given enough data it might not be the correct one. The reason they are so popular is because there are large ViTs trained on an enormous amount of data so you don't have to do it. But on a small edge device, a small ViT might not have enough params to express what you need so a CNN holds that prior for you.
9
u/RedEyed__ 14d ago
Hello!
I had the same question, and I also use CNN + transformers on edge devices.
My models are tiny, I tried ViTs and never was able to beat CNNs.
In my understanding, ViTs scales much better than CNNs, but CNNs are better below a certain point of model size.
8
u/sjdubya 14d ago
How big does the dataset need to be relative to the model in order to fully take advantage of ViTs? are there any good scaling laws on this?
41
u/longgamma 14d ago
If you have a big dataset, vit will outperform CNN's. Transformers don't have much inductive bias so technically they need more data
8
u/murrdpirate 14d ago
The inductive bias of convolution is indeed useful for limiting the number of parameters without too much sacrifice in accuracy.
However, it is a less flexible representation. Some images may have more complex relationships between image regions that convolution doesn't fit well.
For edge devices where you are probably dealing with a fairly narrow task and/or limited training data, CNNs may be better. For general vision and/or huge training data sets, a ViT can potentially fit the data better.
4
u/Chocolate_Pickle 14d ago
CNN + Attention sounds like the smart play.
I'm having a hard time picturing why early layers would ever benefit from long-range information.
10
u/sqweeeeeeeeeeeeeeeps 14d ago
You can view keys & values as data-dependent weights, whereas CNNs and traditional MLPs are learned fixed weights
1
u/AuspiciousApple 14d ago
What form of attention are you using? "Plain" CNN works quite well in my experience, SE blocks are good, too, but do you use transformer style attn in your cnns?
4
u/moschles 14d ago
OP, Have you read this paper yet?
Masked Autoencoders Are Scalable Vision Learners
Kaiming He;y Xinlei Chen; Saining Xie; Yanghao Li; Piotr Dollar; Ross Girshick
Facebook AI Research (FAIR)
arXiv:2111.06377v3 [cs.CV] 19 Dec 2021
2
u/Mr_Fragwuerdig 14d ago
With lots of training data, ViTs can match conv/hybrid models. And if you only care about accuracy, there is not a strong reason to switch to hybrids. Hybrids are however always the best in speed-accuracy tradeoff. We just can't test that well, because the foundation models are mostly pure ViTs. And the general interest in efficiency is not very big in companies that create insanely big foundation models.
And the only big advantage of ViTs is the things you can do with the tokens. That makes it quite flexible.
2
u/glowing_danio_rerio 14d ago
i also think the move to not building symmetries into the models is insane but what do I know gpu go brr
1
u/hoaeht 14d ago
with enough data they tend to perform better. As they are also much more inefficient, it's probably not relevant for your edge deployment. Also check receiptive field as it can make a huge difference for some use cases
1
u/NanoAlpaca 13d ago
CNN + Attention heads are a great combination on the edge. ViTs can be great, if you are aiming for the highest possible quality and have lots of data and don’t care about latency. And they can look great from a naive flops/quality perspective, when people are just counting flops and not considering that 3x3 convs are usually efficiency monsters on edge hardware, while windowed attention or things like that might look theoretical great, but usually does not run efficiently on edge hardware.
1
u/Honest-Finish3596 11d ago
ViTs have an inductive bias, they're not fully connected networks.
1
u/AkiDenim 9d ago
I think one thing may be scalability
But since ViTs are VERY data hungry in most practical sized engineering tasks CNNs are enough
1
u/pm_me_your_pay_slips ML Engineer 14d ago
There are things a convnet can't represent that a ViT can. But on the contrary everything that can be represented by a convnet can be represented by a ViT.
1
u/Dihedralman 14d ago
Transformers have larger capacity and thus can captured more nuanced relationships.
The simplest way to think about the inductive bias is that convolution gives importance to the relationship of nearby pixels out the gate, while attention allows the model to consider a greater range of possibilities.
In CNNs you are essentially training a filter blocks while ViTs are using patch relations.
Most applications use large pretrained models for a general purpose setup to scale.
ViTs are likely excessive for what you do.
0
u/qu3tzalify Student 14d ago edited 14d ago
Transformers are also one of the most optimized for architecture in modern hardware. By using ViT you unify the architecture and optimizations between the vision encoders and the language backbone.
Edit: highlighted "optimized for" because I feel like readers think I'm saying they are the most optimized.
0
u/norpadon 13d ago edited 13d ago
Transformers are neural networks which repeat the same block many times, processing the input of the same shape over and over again
It makes them incredibly convenient to train (you have very few hyperparameters whose behaviour is very predictable and well-studied), easy to scale, and it makes inference very easy to optimize. Also, their flops are linearly related to the number of parameters.
CNNs, on the other hand, are a huge mess because of downsampling. If you want to make a scaling ladder for a CNN, which layers at which resolutions do you scale and at which rates?
And what should you scale, parameters or flops? How do you implement muP for CNNs? How do you adapt Muon? How do you do pipeline parallelism if different layers have very different latency profiles?