Close Menu
    Trending
    • Why Care About Prompt Caching in LLMs?
    • How Vision Language Models Are Trained from “Scratch”
    • Why physical AI is becoming manufacturing’s next advantage
    • Personalized Restaurant Ranking with a Two-Tower Embedding Variant
    • A Tale of Two Variances: Why NumPy and Pandas Give Different Answers
    • How to Build Agentic RAG with Hybrid Search
    • Building a strong data infrastructure for AI agent success
    • Defense official reveals how AI chatbots could be used for targeting decisions
    ProfitlyAI
    • Home
    • Latest News
    • AI Technology
    • Latest AI Innovations
    • AI Tools & Technologies
    • Artificial Intelligence
    ProfitlyAI
    Home » How Vision Language Models Are Trained from “Scratch”
    Artificial Intelligence

    How Vision Language Models Are Trained from “Scratch”

    ProfitlyAIBy ProfitlyAIMarch 13, 2026No Comments14 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    to remodel a small text-only language mannequin and reward it the ability of imaginative and prescient. This text is to summarize all my learnings, and take a deeper take a look at the community architectures behind trendy Imaginative and prescient Language Fashions.

    The code is open-source, you may try the GitHub hyperlink on the finish of the article. There may be additionally a 30-minute companion YouTube video that explains the entire article in a visually wealthy format.

    Additionally, except in any other case talked about, all pictures on this article are produced by the writer.

    Wait, are you actually going to be “coaching from scratch”?

    Sure… I imply no… it’s a bit nuanced.

    Analysis labs in 2026 don’t practice multimodal fashions from “scratch” anymore. It is just too costly to show a mannequin imaginative and prescient and (textual) language on the similar time! It requires extra knowledge, compute, time, and cash. Additionally, it typically results in poorer outcomes.

    As an alternative, labs take current pretrained text-only language fashions, and finetune it to offer “imaginative and prescient capabilities”. In principle (and apply), that is far more compute-efficient.

    Let’s discuss Imaginative and prescient Language Fashions!

    The usual structure

    Though it’s much less data-intensive, finetuning text-only LMs to immediately begin seeing pictures would absolutely open its personal can of worms.

    1. how will we embed the picture, i.e. convert it into numerical representations {that a} neural community can perceive?
    2. how will we tune the picture embeddings to be appropriate with textual content?
    3. how will we alter the weights of the textual content mannequin in order that it retains it’s earlier world data, but additionally generate textual content from picture embeddings?
    The picture embeddings popping out of the VIT and Q-Former passes by means of an MLP layer. Adopted by a sequence of Decoder layer and trainable LORA adapters.

    These modules are:

    1. The Picture Spine: A mannequin that converts uncooked pictures into embeddings.
    2. The Adapter Layer: These are fashions that convert the picture embeddings right into a “text-compatible” embedding. That is the principle difficult half – what architectures to make use of, what loss capabilities, and many others.
    3. The Language Layer: The language mannequin we are going to practice to enter the tailored embeddings and generate textual content from it.

    Let’s focus on them one after the other.

    1. The Picture Spine

    The purpose of your picture spine is easy:

    Enter: A uncooked 2D pixel map/picture.

    Output: A sequence of vector embeddings representing the picture

    Basically, we simply use an off-the-shelf picture mannequin that has been pretrained with huge corpus of pictures, usually on self-supervised duties.

    You need to use a Convolutional Neural Community (like a ResNet) to make use of as a picture spine. However trendy state-of-the-art VLMs have nearly totally shifted to ViTs as a result of they scale higher with knowledge and are extra versatile for multimodal fusion.

    Imaginative and prescient Transformers enter a picture, extract patches out of it, after which go them by means of bidirectional self-attention layers. This contextualizes every patch embedding with one another to kind a contextual sequence of picture embeddings.

    Do I practice the picture spine or hold it frozen?

    In most VLM analysis, there’s a clear development towards maintaining backbones static (frozen) to save lots of prices. Additionally, vision-language coaching usually wants paired image-text datasets. Since these datasets are all the time a lot smaller than the VIT’s pretraining dataset, finetuning the spine typically results in overfitting and degraded efficiency.

    By maintaining these weights frozen, we’re mainly transferring the possession of vision-language studying to latter elements of the community (i.e. the adapter layer and the textual content spine).

    In my experiments, I used the ViT-Base model. This mannequin takes the picture enter, splits it into patches of 16×16 pictures and applies self-attention on them to generate an embedding sequence of 197 vectors. Every vector is 768 dimesnions lengthy (the embedding dimension of the VIT).

    2. The Adapter Layer

    That is the place we’re going to spend the vast majority of our time. Now we have transformed pictures into embeddings already, however these embeddings are utterly text-unaware.

    Imaginative and prescient Transformers are pre-trained purely on picture pixels. Not on their captions, or any native textual options. The position of the adapter is to floor the pixel-based-image-embeddings right into a (typically shorter sequence of) text-based-image-embeddings.

    There are lots of methods to do that, like utilizing CLIP fashions, however we’re going to take a look at one of many extra well-liked approaches — the Question Former of the Q-Former.

    Q-Former

    Alright — so what’s a Q-Former? A Q-Former or the Question-Former was launched within the BLIP-2 paper.

    From the BLIP-2 Paper

    How do I practice a Q-Former?

    Customary Q-Formers might be educated utilizing any multimodal image-text pair dataset. For instance, you should utilize the Conceptual Captions dataset, which is an enormous corpus of pictures and their corresponding captions. In my venture, I took simply 50,000 pairs to coach the Q-Former.

    You possibly can practice a Q-Former from scratch, however the BLIP-2 advice is to make use of an pretrained BERT mannequin. In order that’s what we are going to do.

    At a excessive stage, right here is our primary gameplan:

    • Prepare a multi-modal joint embedding house. An area the place textual content and pictures “know” one another.
    • Mainly, we are going to enter pairs of picture and captions — and embed each of them in the identical joint house.
    • Pictures and incompatible captions can be mapped in separate locations on this new embedding house, and appropriate captions can be mapped shut to one another.
    Layer 1: Self Consideration
    Layer 2: Self-Consideration & Cross-Consideration with the VIT Options

    Establishing cross-attention layers

    There’s an issue — BERT fashions are purely textual content fashions. They do not know what a picture is.

    So, our goal is to first introduce new cross-attention layers to marry the imaginative and prescient embeddings popping out of the VIT and the textual content embeddings from BERT. Let’s break down step-by-step how we convert BERT right into a Q-Former:

    • Pattern a picture and textual content pair from the dataset
    • Cross the picture by means of the frozen VIT mannequin to transform the picture into picture embeddings, formed [197, 768]
    • Initialize “learnable question embeddings”. These are (say 32) vector embeddings that we are going to use to transform the picture embedding sequence right into a text-grounded token embedding sequence. Discover that 32 is way decrease than the unique VIT embedding sequence size (197).
    • We enter the textual content caption embeddings and the question embeddings into the primary BERT layer. The layer applies self-attention on these inputs.

      For now, let’s assume that the question tokens solely attend amongst themselves and the textual content tokens amongst themselves (i.e. the question tokens and the textual content tokens don’t see one another).

    • Within the 2nd layer of the BERT, one thing INTERESTING occurs. The 2 units of embeddings undergo one other self-attention layer like earlier than. However this time, we additionally use a cross-attention layer to contextualize the question embeddings with the ViT picture embeddings we calculated earlier.

      After all, regular BERT doesn’t have any cross-attention layers, so we introduce these multi-headed cross-attention layers ourselves.

    • Similar to this, we alternate between a pure self-attention layer (the place queries and textual content independently self-attend amongst themselves) adopted by a cross-attention layer (the place the question embeddings attend to the frozen VIT embeddings).
    • Within the last layer, we select a joint embedding coaching loss, like ITC (Picture Textual content Contrastive Loss) or ITM (Picture Textual content Matching Loss) or ITG (Picture Textual content Era Loss and many others). Extra on this later.

    What does cross-attention do?

    It contextualizes the picture content material with the question embeddings. You possibly can think about every question is making an attempt to match a particular embedding sample in opposition to the 197 VIT embeddings.

    For instance, if a question has a excessive match with a single picture vector, it’ll seize that function very prominently. If the question matches with a mixture of vectors, it is going to be a mean of these embeddings, and so forth.

    Keep in mind we educated 32 of those question embeddings, so you might be permitting the Q-Former to study a number of totally different co-activations inside the picture embeddings. As a result of nature of coaching, these coactivations are inspired to maximise alignment between picture and textual content.

    As we practice the Q-Former, each the preliminary question embeddings and the cross-attention weights can be optimized so we will extract related options from the VIT picture tokens.

    The question former embeddings should not making an attempt to seize each element of those 197 embeddings — as a substitute they’re making an attempt to discover ways to mix them right into a compact 32 token sequence.

    Observe that after the Q-Former is educated, we received’t really use the textual content a part of the Q-Former for something. We’ll merely go the query-embeddings by means of the Q-former and alternatively run self-attention and cross-attention solely on them.

    Loss capabilities for coaching Q-Formers

    There may be SO MUCH COOL SHIT you are able to do simply by configuring the eye masks in numerous methods.

    So icydk, I can be coaching a small text-only LM to have imaginative and prescient capabilities. As a pretraining step, I have to first practice a joint image-text embedding house which can be later used… https://t.co/Dxf2Q2hhBG pic.twitter.com/EFmWsWRbgA

    — AVB (@neural_avb) December 16, 2025

    How the Q-Former mannequin is educated is definitely carefully associated to how we attend between question and textual content tokens all through the layers.

    1. Picture-Textual content-Contrastive Loss (our setup)
      For this job, we use a unimodal self-attention masks. Question tokens attend amongst one another, textual content token amongst one another.

      The loss operate might be any commonplace CLIP-like contrastive loss operate. We’ll align the picture and textual content in the identical embedding house. Mainly, we take the output of the queries and the output of the textual content encoder and compute their similarity. If a picture and a caption belong collectively, we wish their vectors to be as shut as potential.

      This forces the queries to extract a “international” visible illustration that matches the overall theme of the textual content with out really wanting on the phrases but.

    2. Picture-Textual content Matching Loss (ITM)
      This makes use of a bi-directional self-attention masks. Right here, each question token is allowed to see each textual content token, and each textual content token can see each question!

      For the loss fucntion, we use a binary classification job the place the mannequin has to foretell: “Is that this picture and this textual content a match—Sure or No?”. Binary cross-entropy loss.

      As a result of the modalities are absolutely combined, the mannequin can do fine-grained comparisons. The queries can take a look at particular objects within the picture (through the cross-attention) and confirm in the event that they correspond to particular phrases within the textual content. That is way more detailed than the contrastive loss and ensures the 32 tokens are capturing localized particulars.

    3. Picture-Textual content Era Loss (ITG)
      Lastly, we now have the generative job. For this, we use a multimodal causal masks. The queries can nonetheless see one another, however the textual content tokens are actually handled like a sequence. Every textual content token can see all 32 question tokens – which act as a visible prefix. However they’ll solely see the textual content tokens that got here earlier than it.

      For the loss operate, we simply practice the mannequin to foretell the subsequent token within the caption. By forcing the mannequin to actually “write” the outline based mostly on the queries, we be sure that these 32 tokens comprise each little bit of visible data crucial for a language mannequin to grasp the scene.

    For my venture, I simply used the best — ITC. For a small dataset like I used to be utilizing, this was the simplest method! BLIP-2 recommends to make use of a combination of all these coaching strategies. The github repo shared on the finish of the article gives the recipe to make use of any of the above consideration schemes.

    A educated Q-Former mannequin learns to match comparable textual content and picture pairs

    Within the subsequent part, we are going to do the ultimate step — coaching the VLM!

    3. The Language Layer

    Now comes the ultimate step. We’ll use the VIT and the Q-Former to make a language mannequin right into a imaginative and prescient mannequin. I picked one of many smallest instruction-tuned language fashions — the SmolLM2-135M. Fortunately, this half just isn’t as sophisticated because the Q-Former coaching.

    Now we have the picture embeddings (coming from the VIT and the Q-Former), and we now have the textual content tokens (coming from the SmolLM tokenizer). Let’s see some particulars.

    From the BLIP-2 Paper
    • We pattern a picture and a caption from our dataset
    • We randomly decide from an inventory of easy system prompts, just like: “You’re a useful assistant. Reply honestly to the person.”

      We additionally decide the person question from an inventory of prompts, for instance: “What do you see on this picture?“

      We tokenize the output captions sampled from the dataset as nicely.

      These 3 issues kind the textual content tokens. We tokenize all of them utilizing the SmolLM2 tokenizer, however we aren’t going to insert it into the LLM simply but — we should course of the picture first.

    • We go the picture by means of the frozen VIT, then by means of the Q-Former (once more, word that the textual content captions should not handed into the Q-Former, solely the picture pipeline is executed)
    • We introduce a small MLP layer that converts the Q-Former output into new embeddings which might be of the identical form because the LLM’s anticipated embedding dimension. As we practice, this MLP layer will map the Q-Former embedding into the LLM embedding house.
    • Now that we now have the textual content tokens sequence and the brand new picture embeddings (VIT -> Q-Former -> MLP). We’ll go the textual content tokens by means of the LLM’s native embedding layer. We sandwich the textual content and picture embeddings within the following sequence:
      <SYSTEM_PROMPT> <USER_QUERY> <IMAGE> <OUTPUT>

      …and ahead go it by means of the remainder of the LLM.

    • Why that particular sequence? Since autoregressive LLMs use causal masking, we are going to primarily be coaching fashions to generate the output (caption) sequence given all the prefix (system immediate, person immediate, and the picture embeddings).
    • We add LoRA adapters (Low-Rank Adaptation Matrices) as a substitute of coaching all the LLM from scratch. By wrapping our LLM with LoRA, we freeze the unique hundreds of thousands of parameters and solely practice tiny, low-rank matrices injected into the eye layers. This makes the mannequin trainable on shopper {hardware} whereas maintaining all that pre-existing intelligence intact.
    • And that’s it! We go these stitched embeddings and labels into the LLM. The mannequin attends to the textual content instruction and the visible tokens concurrently, and due to LoRA, it learns how you can replace its inner wiring to grasp this new visible language. Solely the Q-Former layers, the MLP layer, and the LORA adapters are educated. All the pieces else is saved frozen.
    Some outcomes! Yow will discover extra ends in the youtube video talked about on the finish of the article!

    After coaching this for only a few hours on a small subset of knowledge, the educated VLM can now see the photographs and generate textual content about it. Machine Studying is so stunning when it really works.

    In abstract

    Yow will discover the total github repository right here:

    https://github.com/avbiswas/vlm

    And watch the youtube video right here:

    Let’s summarize all of the modules in Imaginative and prescient Language pipelines.

    • A imaginative and prescient spine (just like the VIT) that takes picture enter and converts it into embeddings
    • An adapter layer (just like the Q-Former) that grounds the picture with textual content
    • An LLM that we practice to consolidate the textual content and picture embeddings to study the language of imaginative and prescient

    My Patreon:
    https://www.patreon.com/NeuralBreakdownwithAVB

    My YouTube channel:
    https://www.youtube.com/@avb_fj

    Comply with me on Twitter:
    https://x.com/neural_avb

    I’m constructing Paper Breakdown, a spot to check analysis papers
    https://paperbreakdown.com

    Learn my articles:
    https://towardsdatascience.com/author/neural-avb/





    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleWhy physical AI is becoming manufacturing’s next advantage
    Next Article Why Care About Prompt Caching in LLMs?
    ProfitlyAI
    • Website

    Related Posts

    Artificial Intelligence

    Why Care About Prompt Caching in LLMs?

    March 13, 2026
    Artificial Intelligence

    Personalized Restaurant Ranking with a Two-Tower Embedding Variant

    March 13, 2026
    Artificial Intelligence

    A Tale of Two Variances: Why NumPy and Pandas Give Different Answers

    March 13, 2026
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    Tools for Your LLM: a Deep Dive into MCP

    December 21, 2025

    3 Questions: On the future of AI and the mathematical and physical sciences | MIT News

    March 11, 2026

    A new way to increase the capabilities of large language models | MIT News

    December 17, 2025

    Puzzling out climate change | MIT News

    April 5, 2025

    LangGraph 101: Let’s Build A Deep Research Agent

    August 14, 2025
    Categories
    • AI Technology
    • AI Tools & Technologies
    • Artificial Intelligence
    • Latest AI Innovations
    • Latest News
    Most Popular

    What If I Had AI in 2020: Rent The Runway Dynamic Pricing Model

    August 22, 2025

    Time Series Forecasting Made Simple (Part 1): Decomposition and Baseline Models

    April 9, 2025

    LLM-as-a-Judge: What It Is, Why It Works, and How to Use It to Evaluate AI Models

    November 24, 2025
    Our Picks

    Why Care About Prompt Caching in LLMs?

    March 13, 2026

    How Vision Language Models Are Trained from “Scratch”

    March 13, 2026

    Why physical AI is becoming manufacturing’s next advantage

    March 13, 2026
    Categories
    • AI Technology
    • AI Tools & Technologies
    • Artificial Intelligence
    • Latest AI Innovations
    • Latest News
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2025 ProfitlyAI All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.