Build and evaluate a small vision-language model
Build a vision-language model piece by piece: patch embeddings, a ViT encoder, a projection bridge, cross-attention decoding, contrastive plus captioning pretraining, and retrieval, VQA and BLEU-4 evals on data you design.
Build it step by step
- 1Vision Encoder PatchesPhase 19: Capstone projects
- 2Vision Transformer EncoderPhase 19: Capstone projects
- 3Projection Layer for Modality AlignmentPhase 19: Capstone projects
- 4Cross-Attention FusionPhase 19: Capstone projects
- 5Vision-Language PretrainingPhase 19: Capstone projects
- 6Multimodal EvaluationPhase 19: Capstone projects
What You'll Build
A complete small vision-language model in PyTorch: a Conv2d patch embedding with 2D sinusoidal positions and a CLS token, a pre-LN ViT encoder, a two-layer MLP projector trained against a frozen encoder, a late-fusion decoder with causal self-attention and cross-attention into image tokens, joint InfoNCE plus language-model pretraining, and an evaluation suite for retrieval, VQA and captioning. The lessons train on random mock data; you design a small dataset with learnable structure so your metrics mean something.
Requirements
- Dataset of your own design. Build image-caption-question data with real signal, for example procedurally rendered shapes with numpy or PIL (shape, colour, position, count) with captions from a template grammar and VQA questions like "what colour is the shape". At least 500 training pairs and a held-out split generated from a different seed. Describe the generator in the README.
- Patch front end.
PatchEmbedas Conv2d with kernel and stride equal to patch size, a test that it equals unfold-then-linear with the same weights, deterministicsinusoidal_2d, CLS prepended. At 224 px with 16 px patches and hidden 768 the output is(1, 197, 768). - ViT encoder. Pre-LN blocks with multi-head self-attention (no mask) and a 4x GELU feed-forward. Build the ViT-Base config and print its parameter count (about 86M), then use a small config for training. Tests: attention rows sum to 1 and gradients reach the patch projection from the CLS output.
- Projector. A Linear, GELU, Linear projector trained with
1 - cos_simagainst frozen text embeddings while the encoder is frozen; a test confirms frozen modules have no trainable parameters and the loss falls. - Cross-attention decoder. Decoder blocks of causal self-attention, unmasked cross-attention and feed-forward with pre-LN residuals; a
ValueErroron mismatched shapes; a cached image key/value path whose logits match the uncached path within float tolerance. - Pretraining. Symmetric InfoNCE with a learned log-temperature plus padding-masked next-token loss, combined as
contrastive + lm_weight * lm. Train on your dataset and log both losses; both must fall. - Evaluation. Recall@1, @5 and @10 in both directions from the similarity matrix, VQA exact match, and BLEU-4 with multi-reference support and Chen and Cherry method 1 smoothing, all written by hand. Report a before-training and after-training table on the held-out split next to the random baselines (1/N for R@1 and so on).
- Ablation. One controlled comparison from the lesson exercises, for example
lm_weight = 0vs1, CLS vs mean pooling, or InfoNCE vs a SigLIP-style sigmoid loss, with numbers.
Constraints
- CPU is enough if you keep it small: images of 32 to 64 px, patch size 8, hidden 64 to 128, 2 to 4 layers. Training should take minutes. The ViT-Base config is only built and run for one forward pass.
- A GPU (for example a free Colab T4) is optional for larger images or longer runs; state the hardware you used.
- No dataset download is required. Real datasets such as COCO are optional. Do not use pretrained encoders or
transformers; the point is your own modules. - Metrics only need to beat the random baseline clearly on held-out data. Do not claim results beyond what your table shows.
Stretch Goals
- Add the Flamingo tanh gate on cross-attention and show training starting from a near-zero gate.
- Add CIDEr or VQA soft accuracy next to BLEU-4 and exact match.
- Evaluate at several checkpoints and plot metric curves against the loss curves.
How to Submit
Public repo with data/make_dataset.py, vlm/ (patch, vit, projector, decoder, model), train.py, evaluate.py, tests/ and results/ (loss log, before and after metrics JSON, ablation table, a few sample images with generated captions). The README must explain how to regenerate the data and run training and evaluation, the dataset design and why it has learnable signal, model sizes and hardware, the metrics table against random baselines, the ablation result, and what failed first.
How It's Reviewed
Professor Katalock reads your repository and the evidence you paste, and scores each criterion out of 100. You pass with a weighted total of 70 or more and no criterion below 40. A copied or padded submission is flagged and fails.
Submit
Push your work to a public GitHub repository with a README that explains how to run it, then paste the evidence below. Up to 3 reviews a day, free.
- Output of the dataset generator plus 3 sample images with their captions
- Training log showing contrastive and LM loss at regular steps
- The before and after evaluation table with random baselines
- The ablation numbers
- Output of `python -m pytest -q` (or `python -m unittest`)
- A short note on the first thing that did not train and why
Reviews are done by AI against the rubric above. They check the evidence in your repository; they are not a substitute for a human code review, and a review can be wrong. If you think it is, improve the README so the evidence is unmistakable and resubmit.