Computer Vision Questions
Modeling and pipelines for image and video data. Covers image classification, detection and segmentation, image preprocessing and augmentation, and end-to-end vision model pipelines. Focuses on the fundamentals of applying learning to visual inputs and the practical constraints of vision systems.
You have a small VQA dataset and want to adapt a CLIP-like image-text pretrained model for improved question answering. Propose architectural changes (e.g., fusion layers, adapters), discuss whether to fine-tune full model or use PEFT, and outline data augmentation or synthetic data generation strategies to improve cross-modal alignment.
Sample Answer
Approach summary: start from a CLIP-style image encoder + text encoder and add lightweight multimodal fusion and task-specific heads; prefer PEFT for a small VQA dataset with staged selective fine-tuning; aggressively expand training signal via synthetic QA pairs, contrastive hard-negatives, and visual augmentations to improve cross-modal alignment.
Architectural changes
- Fusion module: add cross-attention-based fusion (visual keys/values, text queries) inserted after last few transformer layers of both encoders so the model learns fine-grained grounding. Use 2–4 cross-attention blocks with residual connections and layer norm.
- Multitask heads: (1) span / classification head for single-token answers, (2) generative decoder (small Transformer) when free-form answers are needed. Share fused multimodal representation to both heads and use task-specific loss weighting.
- Adapters: insert small bottleneck adapters in transformer layers of both encoders and in fusion blocks (bottleneck dim ~64–256). Optionally use parallel adapters so original pretrained paths remain largely intact.
- Lightweight rank-approx (LoRA): apply low-rank updates to projection matrices in cross-attention and a few top transformer blocks to capture fine-grained alignment without full tuning.
- Alignment losses: combine VQA cross-entropy with multimodal contrastive loss (like CLIP) and an object grounding loss (e.g., region-text matching) if regions available.
Fine-tune strategy (PEFT vs full)
- Stage 1 — probe & freeze: freeze backbones, train only new fusion + heads to verify capability.
- Stage 2 — PEFT: enable adapters + LoRA; train fusion + adapters + LoRA jointly with low LR (1e-4 to 5e-5), use weight decay and dropout. This minimizes catastrophic forgetting and saves compute.
- Stage 3 — selective unfreeze (only if needed): unfreeze top 1–2 transformer layers of encoders and train with very small LR (1e-5) and strong regularization. Full fine-tuning only if you have substantial data or synthetic augmentation that closely matches target distribution.
Rationale: small dataset => high risk of overfitting; PEFT provides parameter efficiency and preserves pretrained representations while allowing task-specific adaptation.
Data augmentation & synthetic data
- Visual augmentations: geometric transforms, color jitter, random crops, CutMix/ MixUp, and region-level erasure to force reliance on text grounding. Use augmentation-aware training labels (e.g., if crop removes object, mark question unanswerable).
- Synthetic VQA pairs with LLMs: use the image encoder + object detector (e.g., DETR) to extract objects/captions, then prompt an LLM to generate diverse Q&A pairs (what, where, counting, reasoning). Filter by confidence and paraphrase to increase linguistic variety.
- Synthetic hard negatives: generate semantically similar but incorrect answers or wrong-question variants; incorporate contrastive training (image vs incorrect captions/questions) to sharpen alignment.
- Multimodal back-translation: generate captions from images, paraphrase captions via LLMs, then create QA from paraphrases — promotes robustness to linguistic variation.
- Visual transformations paired with updated questions: simulate occlusion or zoom and generate corresponding questions (e.g., “Which object is partially visible?”) to teach robustness.
- Curriculum & balancing: prioritize high-quality human-labeled examples early, then expand with higher-volume synthetic data; use importance weighting to avoid synthetic noise dominating loss.
Training & evaluation tips
- Loss mix: weighted sum of VQA CE, multimodal contrastive, and grounding loss. Tune weights (start 1.0 CE : 0.5 contrastive : 0.2 grounding).
- Metrics: VQA accuracy, BLEU/ROUGE for generative answers, calibration and failure-mode analysis (unanswerable / hallucination rates).
- Regularization: early stopping, strong augmentation, and label smoothing.
- Validation: hold a small human-labeled set to detect synthetic distribution mismatch.
Expected outcome: PEFT with cross-attention fusion + synthetic QA and hard negatives typically yields substantial gains on small VQA sets while minimizing overfitting and compute compared to full fine-tuning.
You are training a pretrained CNN on an imbalanced dataset with very few examples for minority classes. Describe strategies you would use during fine-tuning: sampling methods, loss functions (e.g., focal loss, class-weighting), augmentation, metric selection, and calibration strategies to ensure reliable performance on minority classes.
Sample Answer
Situation: Fine-tuning a pretrained CNN on a highly imbalanced dataset where minority classes have very few examples.
Approach overview: combine careful sampling, loss-design, augmentation and calibration; choose metrics that reflect per-class performance; validate with stratified splits and run threshold/decision calibration to get reliable minority-class behavior.
Sampling / data-level strategies:
- Oversample minority classes (repeat or use weighted sampler during mini-batching) to ensure each batch contains minority examples. Prefer dynamic weighted sampling over naive full duplication to reduce overfitting.
- Use controlled undersampling of abundant classes if dataset is huge and you can afford losing some majority examples.
- Synthetic-data generation: use class-conditional augmentation like elastic transforms, color jitter, Cutout, or domain-specific transforms. Consider MixUp or ClassMix where a minority image is mixed into many backgrounds.
- For very low-data minorities, consider GAN/Diffusion-based synthesis or feature-space augmentation (augment embeddings) but validate realism carefully.
Loss / model-level strategies:
- Class-weighted cross-entropy: weight minority classes inverse to frequency or use effective number weighting to avoid extreme weights.
- Focal loss: reduces contribution of easy majority examples and focuses gradient on hard/minority examples. Combine with class-weights if needed.
- Advanced margin-based losses: LDAM or class-balanced focal loss for severe imbalance.
- Use label smoothing carefully (may hurt minority precision if excessive).
- If multiple labels, use per-class binary losses.
Training & fine-tuning strategy:
- Start with most of backbone frozen; train classifier head with higher lr, then progressively unfreeze layers with a lower LR (layer-wise or discriminative LR).
- Use strong regularization (weight decay, dropout) and early stopping monitored on per-class metrics to avoid overfitting minority classes.
- Try few-shot techniques: prototypical fine-tuning, metric learning losses (triplet, contrastive) to improve representation of scarce classes.
- Ensembles: combine several fine-tuned models or checkpoints to reduce variance on minorities.
Augmentation specifics:
- Class-specific augmentation policies: apply heavier augmentation to minority classes (AutoAugment / RandAugment tuned per class).
- MixUp/CutMix targeted to produce more diverse minority-class contexts.
- Synthetic sample validation: filter generated samples via a discriminator or human inspection.
Metric selection & validation:
- Use per-class metrics rather than overall accuracy: per-class recall (sensitivity), precision, F1, and macro-averaged F1/Recall. Macro-AUC or per-class PR-AUC are more informative than micro metrics for imbalance.
- Use confusion matrices and class-wise calibration/score distributions.
- Validate on a stratified hold-out or cross-validation that preserves minority examples; if minority count is extremely low, use repeated cross-validation or leave-one-out alternatives.
Thresholding & calibration:
- Move from argmax to tuned per-class thresholds based on validation PR curves to optimize chosen metric (e.g., maximize macro-F1 or recall at acceptable precision).
- Probability calibration: Temperature scaling (simple, effective), Platt scaling or isotonic regression applied per-class or on top of logits to get reliable probabilities for thresholding and downstream decision-making.
- Use reliability diagrams and expected calibration error (ECE) per class; re-calibrate separately for minority classes if needed.
Monitoring & deployment considerations:
- Track minority-class performance continuously; set alerts for drops.
- If false positives cost is high, tune for precision; if misses are costlier, prioritize recall via loss weighting/thresholding.
- Document synthetic-data provenance and monitor distribution shift; retrain or fine-tune with new real minority examples as they arrive.
Result emphasis: This combination—balanced sampling, targeted augmentation, loss functions that focus learning on rare classes, per-class metrics, and probability calibration—yields better, more reliable minority-class performance while managing overfitting and preserving overall model quality.
Describe Feature Pyramid Networks (FPN) and how multi-scale feature aggregation helps detect objects of varying sizes. Explain how FPN interfaces with detection heads and why lateral connections and top-down pathways are effective.
Sample Answer
Feature Pyramid Networks (FPN) are a backbone augmentation that produces a multi-scale, semantically strong feature pyramid for object detection. The core idea: combine high-resolution, low-level features (good for localization) with low-resolution, semantically rich features (good for classification) so detectors can handle objects of very different sizes.
How it works:
- Start with a backbone CNN that yields feature maps at several stages (e.g., C2–C5).
- Build a top-down pathway: upsample higher-level feature maps (rich semantics) and add them to corresponding lower-level maps via lateral 1x1 convolutions. This produces pyramid levels P2–P5 with both spatial detail and semantic context.
- Apply a small 3x3 conv to each merged map to reduce aliasing.
Why multi-scale aggregation helps:
- Small objects need high spatial resolution; large objects need strong semantics. FPN provides both at each scale, so a detection head operating on any pyramid level sees adequate localization and class information.
- It regularizes scale selection: objects are matched to appropriate pyramid levels by size, improving recall across scales.
Interface with detection heads:
- Detection heads (e.g., RPN + Fast R-CNN or single-shot heads) attach to each pyramid level independently. For two-stage detectors, RPN proposals are generated per level; ROI features are pooled from the level matching proposal size. For single-shot detectors, classification and box regression are applied per level.
- Because each level has similar semantic strength, shared heads can be used across levels, simplifying training and improving parameter efficiency.
Why lateral connections + top-down pathway are effective:
- Lateral 1x1 convs align channel dimensions and inject high-resolution spatial detail.
- Top-down upsampling propagates strong semantic signals downward, rescuing shallow layers from weak semantics.
- The element-wise addition is lightweight and preserves spatial alignment, yielding feature maps that are both spatially precise and semantically deep—exactly what multi-scale detection requires.
In Python using PyTorch and torchvision, write code to load a pretrained ResNet-50 model, freeze all convolutional layers' weights, replace the final fully connected layer with a new classifier for num_classes, and confirm only the new classifier parameters require gradients. Include necessary imports and clearly show how you would list trainable parameters.
Sample Answer
Approach: load a pretrained ResNet-50, set requires_grad=False for all existing parameters (freezing conv/backbone), replace the final fc with a new classifier (whose parameters default to requires_grad=True), and print the trainable parameters to confirm only the new layer is trainable.
# python
import torch
import torch.nn as nn
import torchvision.models as models
num_classes = 10 # desired number of output classes
# Load pretrained ResNet-50
model = models.resnet50(pretrained=True)
# Freeze all parameters (backbone)
for param in model.parameters():
param.requires_grad = False
# Replace the final fully connected layer
in_features = model.fc.in_features
model.fc = nn.Linear(in_features, num_classes) # parameters default to requires_grad=True
# Verify which parameters require gradients
trainable = [(name, p.shape) for name, p in model.named_parameters() if p.requires_grad]
frozen = [(name, p.shape) for name, p in model.named_parameters() if not p.requires_grad]
print("Trainable parameters (should be only the new classifier):")
for name, shape in trainable:
print(f" {name}: {shape}")
print("\nFrozen parameters (example count):", len(frozen))
Key points:
- Setting requires_grad=False prevents optimizer from updating those weights.
- Replacing model.fc with nn.Linear creates new parameters with requires_grad=True.
- Pass only trainable params to optimizer: optimizer = torch.optim.SGD([p for p in model.parameters() if p.requires_grad], lr=1e-3)
Design an end-to-end synthetic data generation pipeline to supplement limited labeled instance segmentation data for a robotics application. Include asset creation, procedural placement, lighting variation, domain randomization, label generation for masks/instance-ids, and methods to verify the synthetic-to-real transferability.
Sample Answer
Requirements & constraints:
- Target: instance segmentation for robotic perception (pixel-accurate masks + instance IDs).
- Real-world constraints: camera intrinsics, workspace geometry, object classes, limited labeled real data.
- Quality goals: diversity, physical plausibility, label correctness, sim2real transfer.
Pipeline overview:
- Asset creation
- Collect/high-poly CADs for objects; scan real objects where possible (photogrammetry / structured light) for texture realism.
- Create low/medium-poly game-ready variants and multiple material maps (albedo, roughness, normal, metallic, opacity).
- Build environment assets (tables, shelves, background clutter) and physics properties.
- Procedural scene generation & placement
- Define scene templates (workcell layouts) and procedural rules (support surfaces, gravity, stacking rules).
- Use physics engine (Bullet/PhysX) to drop/arrange objects, or scripted placements for specific poses/occlusions.
- Parameterize object counts, scale jitter, inter-object spacing, and camera viewpoints (intrinsics, noise).
- Lighting & domain randomization
- Combine photoreal HDRI lighting with randomized point/area lights: vary intensity, color temp, direction.
- Apply domain randomization: textures (colors, patterns), background replacement, camera exposure, motion blur, sensor noise, lens distortions.
- Randomize physical properties: material roughness, specularity, small deformations.
- Rendering & label generation
- Render RGB, depth, and per-pixel instance-id and class-id buffers in one pass (using object-unique flat shaders for IDs).
- Export segmentation masks, bounding boxes, and per-instance 6-DOF pose metadata. Include occlusion fraction and visibility maps.
- Use denoising and anti-aliasing but keep a version without A.A. for exact masks.
- Dataset curation & augmentation
- Balance class distributions and difficulty levels (heavy occlusion, small objects).
- Mix synthetic with real labeled images; reserve unseen real set for validation.
- Verification & sim2real transferability
- Quantitative: train baseline instance segmentation (Mask R-CNN / Detectron2) on (a) real-only (small), (b) synthetic-only, (c) mixed. Compare mAP, IoU, and per-class recall on held-out real test set.
- Representation alignment: compare feature distributions (e.g., embeddings from backbone) using t-SNE and compute Fréchet Inception Distance or MMD between synthetic and real.
- Ablations: test effects of specific randomizations (lighting, textures, noise).
- Fine-tuning: perform few-shot fine-tune on small real set to measure required labeled real samples for parity.
- Domain adaptation: if gap persists, apply image-level translation (CycleGAN, UDA) or feature-level adversarial adaptation; consider randomized-to-photoreal pipelines (domain-invariant augmentation).
- Real-world robot-in-the-loop tests: verify model on actual robot perception tasks (grasping success rate, pick precision) and iterate asset/lighting distributions toward failure modes.
Tools & best practices:
- Use Blender/Unreal Engine/Isaac Sim for rendering + physics; glTF/USD for interchange.
- Track metadata, RNG seeds, and reproducibility. Version datasets.
- Generate confidence metadata per example (occlusion, motion blur) to enable curriculum training.
This pipeline emphasizes physical plausibility, diverse randomization, precise label generation, and iterative validation with both metric-based and task-based sim2real checks to close the domain gap.
Unlock Full Question Bank
Get access to all Computer Vision interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.