- Open the notebook in Google Colab.
- Enable GPU runtime (
Runtime>Change runtime type>GPU). - Run all cells sequentially from top to bottom.
Objective:
Implement and train a Vision Transformer (ViT) from scratch on CIFAR-10 to achieve the highest possible test accuracy.
Implementation:
- Patchify CIFAR-10 images into non-overlapping tokens (4×4 patches).
- Add learnable positional embeddings and a
[CLS]token. - Stack Transformer encoder blocks (Multi-Head Self-Attention + MLP with residual connections + LayerNorm).
- Classification from the
[CLS]token. - Training pipeline:
- Optimizer: AdamW
- Scheduler: CosineAnnealingLR with warmup
- Regularization: Dropout, DropPath (stochastic depth), Label smoothing
- Strong data augmentation: RandomCrop + Flip, RandAugment, MixUp
Experiments Conducted:
- Patch size variations (2×2, 4×4, 8×8).
- Depth/width trade-offs (6 vs. 12 layers, embedding dims 128 vs. 192).
- Augmentation effects (baseline vs. RandAugment, MixUp, CutMix).
- Optimizer/scheduler variants (Adam vs. AdamW, fixed LR vs. Cosine LR).
- Overlapping vs. non-overlapping patches.
Results:
| Model Variant | Test Accuracy (%) |
|---|---|
| Baseline (patch=4, depth=6, emb=128, simple aug) | 76.67 |
| Final (patch=4, depth=12, emb=192, RandAugment + MixUp + Label smoothing + DropPath) | 90.95 |
Concise Analysis:
- Patch Size: 4×4 gave the best trade-off (64 tokens, good granularity without heavy compute). Larger patches (8×8) underfit; smaller patches (2×2) increased compute.
- Depth/Width: A deeper (12-layer) but narrower (192-dim) ViT outperformed shallower or very wide models. Strong regularization was essential to prevent overfitting.
- Augmentation: RandAugment + MixUp significantly boosted accuracy versus basic crop/flip. CutMix gave similar benefits but required tuning.
- Optimizer/Scheduler: AdamW + weight decay + cosine decay with warmup converged smoothly and gave stable accuracy.
- Overlapping vs. Non-Overlapping: Standard non-overlapping patches were used; overlapping patches could slightly improve locality but added extra cost.
Best Result:
90.95% test accuracy on CIFAR-10 using the final configuration.
- Goal: Perform segmentation of an object in an image given a text description.
- Load an input image.
- Accept a text prompt describing the target object.
- Use GroundingDINO to convert the text prompt into region proposals (bounding boxes).
- Pass bounding boxes into SAM 2 (Segment Anything Model v2).
- Display the final segmentation mask overlayed on the image.
The notebook installs the following:
torch,torchvisiontransformersgroundingdino(for text-to-region grounding)segment-anything/sam2(for segmentation)opencv-python,matplotlib,PIL(for visualization)
- Open
q2.ipynbin Colab. - Install all dependencies.
- Upload or load your test image.
- Enter a text prompt (e.g.,
"cat","person","car"). - Run all cells to view the segmentation mask overlay.
- Input Image: User-provided image
- Text Prompt:
"dog" - Output: GroundingDINO generates bounding box → SAM2 refines it → Final segmented mask overlayed on image.
- GroundingDINO may struggle with vague or uncommon text prompts.
- For overlapping/occluded objects, multiple regions may be detected.
- Segmentation quality depends on both GroundingDINO localization and SAM2 refinement.
- Extension to video segmentation possible:
- Use GroundingDINO + SAM2 on the first frame.
- Propagate masks to subsequent frames.
- Q1: Achieved 90.95% test accuracy on CIFAR-10 using the final configuration.
- Q2: Successfully demonstrated text-driven image segmentation pipeline using GroundingDINO + SAM2.