KatalockAi AcademyBack to KatalockAi
Build track, required for the Phase 12 certificate

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.

about 26 h 6 guided steps Reviewed against a 6-point rubric
Submit your project Get coached by Professor Katalock

Build it step by step

  1. 1Vision Encoder PatchesPhase 19: Capstone projects
  2. 2Vision Transformer EncoderPhase 19: Capstone projects
  3. 3Projection Layer for Modality AlignmentPhase 19: Capstone projects
  4. 4Cross-Attention FusionPhase 19: Capstone projects
  5. 5Vision-Language PretrainingPhase 19: Capstone projects
  6. 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

  1. 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.
  2. Patch front end. PatchEmbed as Conv2d with kernel and stride equal to patch size, a test that it equals unfold-then-linear with the same weights, deterministic sinusoidal_2d, CLS prepended. At 224 px with 16 px patches and hidden 768 the output is (1, 197, 768).
  3. 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.
  4. Projector. A Linear, GELU, Linear projector trained with 1 - cos_sim against frozen text embeddings while the encoder is frozen; a test confirms frozen modules have no trainable parameters and the loss falls.
  5. Cross-attention decoder. Decoder blocks of causal self-attention, unmasked cross-attention and feed-forward with pre-LN residuals; a ValueError on mismatched shapes; a cached image key/value path whose logits match the uncached path within float tolerance.
  6. 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.
  7. 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).
  8. Ablation. One controlled comparison from the lesson exercises, for example lm_weight = 0 vs 1, 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.

CriterionWeightHow it's checked
Architecture correctness25Tests pass for Conv2d vs unfold equivalence, the (1, 197, 768) shape, softmax rows, gradient flow, frozen-module checks, causal vs unmasked masks and KV-cache equivalence; ViT-Base prints about 86M parameters.
Training20Loss log shows both InfoNCE and LM loss decreasing on the learner's dataset; code shows a learned temperature and padding masking; projector stage trains with encoder frozen.
Evaluation results20Before and after table on held-out data reports R@1/5/10 in both directions, VQA EM and BLEU-4 next to random baselines, with after-training values above baseline; metric code is hand-written.
Dataset design and ablation10The data generator is the learner's own, has a distinct held-out seed, and the README explains its signal; one ablation is reported with numbers.
Code quality10Modules split by component, configs in one place, seeds fixed, no pretrained or `transformers` imports.
README and explanation15README covers run steps, dataset design, sizes and hardware, results interpretation against baselines, ablation conclusion and an honest failure note.

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.