Computer Vision
Foundation Models

Fine-Tuning Vision Models on Expert Masks: A Guide to SAM, DINO and VLMs

Vision models segment almost anything except the defects and targets specialists care about. This guide to fine-tuning vision models, from SAM and DINO to VLMs, shows how expert masks close the gap.

Table of contents

AI Summary

  • Zero-shot SAM trails trained models on steel, tile and rail defects, and scores IoU below 10 on roads.
  • HQ-SAM adapted SAM with 44K masks in four hours on eight GPUs.
  • Professional annotators' masks reached median Dice 0.93; crowdworkers', 0.67.
  • Spend scarce expert masks on the test set and hard cases.

Introduction

Anyone adapting vision foundation models can start from a candid source: the paper that introduced the Segment Anything Model (SAM). Its authors list the model's limits: "It can miss fine structures, hallucinates small disconnected components at times". They say they built it for generality, and they expect domain-specific tools to beat it in their own domains. The Stanford report that named the category made the same point. It defines foundation models as adaptable to many downstream tasks, and its authors chose the term for their "critically central yet incomplete character".

Vision foundation models now cover a lot of ground. SAM 2 segments and tracks objects through video, SAM 3 segments every instance of a named concept, and DINOv3 ships a backbone trained on satellite imagery. Out of the box, they are often good enough for street scenes and product photos. A steel inspector or a geospatial analyst finds the limit quickly: a low-contrast scratch, a road seen from orbit, a small irregular target.

Closing that gap by fine-tuning vision models takes surprisingly little compute. The training data decides the result: which masks you train and test on, and who drew them. This guide explains what vision foundation models are and where they fail on specialist visual data. It compares the main ways to adapt them, shows how vision language models differ, and reviews the evidence on mask quality. It ends with a workflow: the quality engineer or the imagery analyst draws the masks that matter, and the platform measures whether those masks agree.

What Are Vision Foundation Models, and What Were They Trained On?

A vision foundation model is a large image model pretrained on broad data so it can be adapted to many tasks. Two families matter for segmentation work.

The first is the promptable segmenter. You give SAM a click or a box and it returns a mask. Segment Anything was trained on SA-1B, more than 1B masks from 11M images. Most of those masks were not drawn by people. Meta's data engine ran in three stages. First, professional annotators labelled 4.3M masks on 120k images with SAM assisting. A semi-automatic stage then added 5.9M masks on 180k images. Finally, a fully automatic stage produced the 1.1B masks that make up the bulk of the release. In the first stage, the average time per mask fell from 34 to 14 seconds as the model improved.

SAM 2 extended the approach to video. Its SA-V dataset has 50.9K videos and 642.6K masklets (an object's mask across frames). Meta reports that annotation time per frame fell from 37.8 seconds with SAM, used frame by frame, to 4.5 seconds with SAM 2 in the loop. That is an 8.4x speed-up inside its own engine. SAM 3 moves from segmenting one prompted object to segmenting every instance of a named concept, and was trained with 4M unique concept labels. Kili's SAM 3 Data Story breaks down that data engine.

The second family is the feature backbone. DINOv2 is a 1B-parameter vision transformer trained without labels on an automatically curated image set. Its stated goal is "features that work across image distributions and tasks without finetuning". For segmentation, the paper trains only a linear layer on top of frozen features (weights that stay fixed during training). Kili's DINOv2 fine-tuning tutorial walks through that pattern for defect classification. DINOv3 scaled the recipe to 7B parameters and 1.7B images, and Meta released a separate satellite backbone trained on Maxar imagery.

None of these models was trained on your defects or your targets. That is the gap fine-tuning fills.

Where Do Vision Foundation Models Fall Short on Specialist Imagery?

The failure modes are well documented, and they cluster in the same places across domains.

Industrial inspection

Song and colleagues tested zero-shot SAM (used as released, with no extra training) on steel strip, magnetic tile and rail surface defects against specialist models trained on each dataset. On SD-saliency-900, SAM scored a structure measure of 0.668 against 0.884 for the best trained model. The authors found that "SAM can hardly identify the patch defects with extremely low contrast". They also found SAM accurate on slender, small defects, so the problem is contrast and similarity to the background, not size alone.

Researchers who later fine-tuned SAM on about 15,000 real factory anomaly images put it more bluntly. In their words, the original SAM model "lacks the ability to recognize defects". It also tends to segment continuous regions that are not anomalies.

Remote sensing

Overhead imagery has its own pattern. Ren and colleagues tested SAM on a set of widely studied overhead-imagery benchmarks. SAM often generalised well, but "SAM consistently performs very poorly (IoU<10) on the road segmentation task". IoU (intersection over union) measures how much a predicted mask overlaps the true one; here it runs from 0 to 100. Point prompts did much worse than boxes. Osco and colleagues report that "the segmentation of smaller and irregular objects poses difficulties for SAM", whatever the prompt. Accuracy also drops noticeably at resolutions coarser than 30 cm.

The common thread

Medical imaging shows the same profile. MedSAM found SAM had limits on targets with "weak boundaries or low contrast". An evaluation on 19 medical datasets found SAM's IoU ranged from 0.1135 on spine MRI to 0.8650 on hip X-ray.

Low contrast, ambiguous edges, small irregular objects, classes that look like background. A quality engineer or an imagery analyst earns their salary in exactly these places. They decide where a defect ends, or whether a faint linear feature is a road. The model's weak spots are the expert's home ground.

How Does Fine-Tuning Vision Models on Your Own Masks Work?

You can adapt a vision foundation model in several ways. They differ mostly in how much of the model you train and how many masks you need.

‍

Approach What gets trained Example from the literature
One-shot personalisation A handful of parameters, SAM frozen PerSAM-F: one image and one reference mask; trains 2 parameters in about 10 seconds
Frozen backbone + task head A linear layer or decoder DINOv2 dense tasks; Meta and WRI's canopy-height model (frozen encoder, decoder trained on about 5,800 lidar canopy-height maps)
Low-rank adapters (LoRA) Small matrices added to the encoder Self-Perception Tuning on about 15,000 factory images: average mIoU 52.0 zero-shot, 68.4 with LoRA
Added modules, SAM frozen A new output token and fusion layers HQ-SAM: 44K fine-grained masks, 4 hours on 8 GPUs
Learned prompts A prompt generator in front of SAM RSPrompter: mask AP 67.64 on NWPU VHR-10 vs 59.76 for SAM prompted by a trained detector
Full fine-tune at scale Most of the model MedSAM: 1,570,263 image-mask pairs across 10 modalities

‍

PerSAM is the lightest option: it adapts SAM to one object type from one masked example. It suits a recurring part or a single target class. It won't teach SAM what a defect is in general.

Frozen backbones are the default for DINO-class models. Meta's DINOv3 satellite model card recommends frozen features and says of full fine-tuning, "it is recommended to keep this option as a last resort". The canopy-height work by Meta and the World Resources Institute followed that pattern. Its best results came from freezing all layers of the self-supervised encoder and training a decoder against aerial lidar.

Adapters are where most industrial results sit. SAMed applied LoRA to SAM's image encoder and reached 81.88 Dice (an overlap score between predicted and reference masks, reported here as a percentage) on the Synapse multi-organ segmentation dataset. The factory-image study above used the same family of methods. HQ-SAM kept SAM's weights intact and trained only new components on a dataset of 44K fine-grained masks. Its authors built it because SAM's mask quality falls short on objects with intricate structures.

RSPrompter learns to prompt SAM from the dataset's masks. For remote sensing instance segmentation, that beat both a trained detector feeding SAM and a conventional Mask R-CNN, which scored 54.22 mask AP (average precision over masks). At the far end, MedSAM fine-tuned SAM on its 1,570,263 image-mask pairs. It reached a median Dice of 87.8% on nasopharynx cancer segmentation, a 52.3% improvement over SAM.

Protect what the pre-trained model already knows

Whatever the method, the fine-tuning process carries one constant risk: overwriting what the pre-trained model already does well. The early layers hold the most general features. Yosinski and colleagues found that first-layer features resemble "Gabor filters and color blobs" and "appear not to be specific to a particular dataset or task". Those edge and texture detectors are what you want to keep.

Kumar and colleagues showed what happens when they move. They tested 10 datasets with a distribution shift, where test images differ from training images. On average, full fine-tuning was 2% more accurate than a linear layer on frozen features on images like the training set ("in distribution"). On shifted images ("out of distribution"), it was 7% less accurate. The cause, in their words: "the lower layers of the neural network change simultaneously and distort the pretrained features". Training the linear head first and then fine-tuning everything (LP-FT) beat both. Freezing the backbone, adding adapters and keeping the learning rate small all protect those lower layers.

A model's early, middle and late layers stay frozen; expert masks train only small LoRA adapters attached to them, plus the task head.

Augmentation is the other lever. It works best when it copies deployment conditions rather than adding generic variety: the lighting, camera angle and resolution your line or sensor actually produces. Osco's finding that SAM degrades at coarser resolutions is a reminder that resolution is part of the domain.

The pattern across these results: the compute is the cheap part. Four hours on eight GPUs, two trainable parameters, a frozen backbone. What changes from one project to the next is the mask set.

How Do You Fine-Tune a Vision Language Model?

Not every vision model returns a mask. A vision language model (VLM) takes an image and a text input and writes text back. PaliGemma describes this as an "image+text in, text out API", with the prediction generated "in the form of a text string". LLaVA, an early open VLM, connects "a vision encoder and an LLM" (large language model). Its authors fine-tuned it on 158K instruction-following samples. Closed multimodal models such as Google's Gemini work the same way from the user's side. Gemini's technical report describes capabilities "across image, audio, video, and text understanding".

That makes VLMs the natural fit for visual question answering: is there a defect on this weld, or which vehicle types are in this scene?

Try prompt engineering before you fine-tune the model

Instruction-tuned VLMs often answer simple visual questions without any extra training, so prompt engineering is the cheapest first step. Some base models are not built for that. PaliGemma's authors say its pretraining aims at transfer, "not necessarily a model that is usable out of the box". Prompts also have a ceiling. They cannot add new knowledge the pretrained model never saw, such as your defect classes. They also don't reliably make the model answer in a specific way every time. That is when you fine-tune the model.

Format the training dataset as image, query and answer

Supervised fine-tuning for a VLM uses the same shape as the benchmarks that measure it: an image, a question and a reference answer. DocVQA defines 50,000 questions on 12,767 document images, split into 10,194 training images and 1,287 test images. ChartQA pairs charts with "9.6K human-written questions as well as 23.1K questions generated from human-written chart summaries". For specific use cases, the training set is your own images, with the questions your users ask and answers your experts wrote or approved. Hugging Face's TRL library accepts this as an "images" column next to the conversation.

The expert argument from the segmentation sections carries over. In a VLM dataset, the answer is the label. A model trained on vague or inconsistent answers learns to give them.

Use parameter-efficient fine-tuning to fit a large model in GPU memory

Full fine-tuning of a large model updates every weight. It needs the most computational resources and puts the most pressure on the lower layers. Parameter-efficient fine-tuning trains a small set of extra weights instead, significantly reducing what a run costs. LoRA (low-rank adaptation) adds small trainable matrices to a frozen model. On GPT-3 175B, its authors reported it "can reduce the number of trainable parameters by 10,000 times" and GPU memory by 3 times. Quality was on par with or better than full fine-tuning on the models they tested. QLoRA stores the frozen base model in 4-bit precision and trains LoRA adapters on top. That "reduces memory usage enough to finetune a 65B parameter model" on a single 48GB GPU.

The trade-off has been measured for large language models. Biderman and colleagues found that on code and maths, LoRA "substantially underperforms full finetuning" but "better maintains the base model's performance on tasks outside the target domain". Those results come from language model experiments, not VLMs. The question to test on your own task is the same: how much better performance you gain against how much the model loses elsewhere.

Open models such as PaliGemma and LLaVA give you access to the weights. A QLoRA run with TRL looks like this. The model, file names and settings are placeholders, not recommendations.

‍

import torch
from datasets import Dataset
from PIL import Image
from peft import LoraConfig
from transformers import BitsAndBytesConfig
from trl import SFTConfig, SFTTrainer

# One row per (image, query, answer) example, answers written or approved by experts
rows = [
    {
        "images": [Image.open("weld_0412.png")],
        "messages": [
            {"role": "user", "content": "Is there a defect on this weld? If so, what type and where?"},
            {"role": "assistant", "content": "Yes: porosity near the left end of the bead."},
        ],
    },
    # ... more expert-reviewed examples
]
train_dataset = Dataset.from_list(rows)

# QLoRA: load the base model frozen in 4-bit
bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.bfloat16,
)

# LoRA adapters on every linear layer
peft_config = LoraConfig(
    r=16,
    lora_alpha=16,
    lora_dropout=0.05,
    target_modules="all-linear",
    task_type="CAUSAL_LM",
)

args = SFTConfig(
    output_dir="vlm-weld-qa-lora",
    num_train_epochs=3,              # training epochs: check against your expert test set
    per_device_train_batch_size=1,   # batch size per device: lower it first if GPU memory runs out
    gradient_accumulation_steps=8,   # keeps the effective batch size up
    gradient_checkpointing=True,     # lower memory usage, longer training time
    max_length=None,                 # don't truncate image tokens
)

trainer = SFTTrainer(
    model="Qwen/Qwen2.5-VL-3B-Instruct",
    args=args,
    train_dataset=train_dataset,
    peft_config=peft_config,
    quantization_config=bnb_config,
)
trainer.train()
trainer.save_model()  # saves the LoRA adapters, not a full copy of the model

‍

A few settings decide memory usage and training time. Batch size is the first thing to lower when a run doesn't fit on the device. Gradient accumulation adds up several small batches before each weight update, so the effective batch size stays the same. Gradient checkpointing recomputes some intermediate values instead of storing them, which saves memory at the cost of speed. The max_length=None line follows TRL's advice for VLMs, because truncating a sequence can remove image tokens and break training. TRL's default learning rate for SFT (supervised fine-tuning) is 2e-5. The number of training epochs (full passes over the training set) is a choice to validate on held-out expert examples. The saved output is a small set of adapters, so each new model for a new use case sits on top of the same base model.

When shape matters, a text answer is not enough

A VLM can tell you a panel has a scratch near the upper-left corner. It cannot tell you the exact pixels the scratch covers. You need those to measure a defect's length or area, apply a pass/fail threshold, or track an object across frames. Which model you need depends on the question, not the image type. The same steel panel can feed a VLM that answers "is there a defect?" and a segmenter that outlines it. When shape matters, every pixel of the mask counts, and so does the person who drew it.

A vision language model answers "Scratch, upper left" with no boundary to measure, while a segmentation model's mask covers the exact pixels of the scratch, so length, area and pass/fail can be measured.

Why Does It Matter Who Draws the Masks?

A fine-tuned model learns the boundary its masks draw. If two annotators would draw a scratch differently, the model is trained on both versions, and your test set scores it against only one of them.

The clearest evidence on annotator quality comes from medical imaging. Rädsch and colleagues studied 14,040 images annotated by 156 professional annotators from four companies and 708 crowdworkers. With minimal text instructions, the professionals' surgical instrument masks reached a median Dice of 0.93, against 0.67 for the crowdworkers. Adding example images to the labelling instructions improved quality; making the text longer did not.

Experts disagree too. When 11 radiologists delineated 3,193 contours on 490 CT slices, the mean volume overlap variability between any two of them ranged from 5% to 57%. Groups of two observers captured 37% of the variability; groups of five, 72%.

Industrial defects have the same property. Niu and colleagues studied noisy labels in surface-defect data. They note that "The boundary between the defect and the background is usually not an absolute line". Similar regions end up labelled differently, depending on each annotator's standards. Errors at the boundary are the ones that hurt. On liver segmentation, Heller and colleagues found that every architecture they tested coped well with label errors away from the boundary. Performance declined steadily as boundary errors grew.

In manufacturing, the person who holds that definition is the quality engineer or line operator. In public sector and defence, it is the imagery or intelligence analyst. Their judgment is what the mask encodes. In the US, the National Geospatial-Intelligence Agency (NGA) announced a call to industry worth more than $700 million for data labelling to train computer vision models. NGA director Vice Adm. Frank Whitworth put the human role plainly: "Data labeling is the process where the human actually identifies the object".

Do You Need Expert Masks for Every Training Image?

No, and the evidence here cuts against the simplest version of the expert argument.

In the Sen1Floods11 flood-mapping dataset, trained remote-sensing analysts hand-labelled 446 chips, and about 4,385 more were labelled automatically. The authors report that "The model trained on hand-labeled data does not provide the best results" on any of their tests. Volume won. On street scenes, Zlateski and colleagues studied convolutional neural networks (ConvNets). They found "the performance of ConvNets mostly depends on the time spent creating the training labels". A larger coarse set can match a smaller fine one. Pretraining on coarse labels and then fine-tuning on a few fine ones matches or beats it, at a fraction of the cost.

Remote sensing also has a dataset built almost without human masks. SAMRS used SAM to turn existing detection boxes into masks, producing 105,090 images and 1,668,241 instances. That buys scale, with masks whose accuracy nobody measured.

Read together, these results say where expert time pays off most:

  • The test set. In Sen1Floods11, every model was scored on held-out hand-labelled chips, not on the automatic labels. A model checked against machine masks is checked against the model's own blind spots. Kili's guide to golden datasets covers how to build one.
  • The labelling guideline and its example images, which Rädsch showed move quality more than extra text.
  • Corrections on the hard cases: the low-contrast patches and small irregular objects where zero-shot SAM fails.

SAM's own data engine already works this way. The model proposes, a person corrects, and per-mask time drops from 34 to 14 seconds. The difference for a domain project is that the person correcting should be the one who knows where the defect ends.

What Does an Expert-Mask Workflow Look Like in Practice?

A workflow that puts expert time where it counts has six steps.

  1. Write the mask specification with pictures. Define each class, where its boundary sits, and what to do with ambiguous regions, using annotated example images for each edge case.
  2. Have experts draw the test set first. Put every test image in front of at least two experts, measure their agreement, and resolve disagreements before any model sees the data.
  3. Let the model draft and the expert correct. Pre-label training images with SAM 2 or your current fine-tuned model, and have experts fix the masks. They should spend most of their time on the regions where the model is weak.
  4. Measure the experts as well as the model. Keep a share of training images double-annotated and track agreement per class and per annotator, so a drifting standard shows up before it reaches the model.
  5. Adapt the cheapest way first. Try a frozen backbone with a head, or adapters, and score every variant on the expert test set.
  6. Send the failures back. The images the adapted model gets wrong become the next batch for expert correction.

Kili's semantic segmentation tool covers steps 2 and 3 for images. Experts can create masks by painting or outlining them with Brush and Lasso, and trim them with Eraser. They can also use interactive segmentation with SAM 2 in two modes: Rapid, the faster default, and High-Res, which is more precise and slower. You can upload existing model masks as predictions for experts to correct through the Python SDK.

For video, the video labelling guide documents SAM 2 smart tracking that follows a segmented object across the next 50 frames. Kili's latest updates extend smart tracking to several objects at once and let Video projects pull assets from cloud storage.

Steps 2 and 4 rely on measurement. You enable consensus on the labelling step by setting a number of labellers and a coverage percentage. Several experts then mask a chosen share of assets in parallel, without seeing each other's annotations. For semantic segmentation, the consensus score is intersection over union, per the calculation rules. A workflow can add several review levels, with Enforce Step Separation on by default so no one reviews their own masks. Reviewers send a masked asset back to its labeller through the workflow's send-back step. When you export, choosing the latest label from each author keeps every expert's mask, so the disagreement itself becomes data for your test set.

In Kili, several experts mask the same image in the labeling step, consensus scores their agreement with IoU, a review step with Enforce Step Separation can send a mask back to its labeler, and the export keeps every expert's mask.

The Mask Specification Is the Model Specification

Each generation of vision foundation models is larger and covers more. DINOv3 has 7B parameters, and SAM 3 segments every instance of a named concept. What these models cannot absorb from more pretraining is your definition of a defect, a road at your resolution, or a target class your analysts care about. That definition lives in the masks.

As adaptation gets cheaper, the expert-drawn test set stops being a final check and becomes the asset that outlasts any single model. Some teams keep that test set, measure the agreement behind it, and route the model's failures back to the same experts. They can score the next backbone against the same reference the week it ships. Teams that trained on unmeasured masks will have to find out what their model learned the hard way.

Resources

Vision foundation models

Zero-shot limits on specialist imagery

Fine-tuning and adaptation

Vision language models and parameter-efficient fine-tuning

Mask quality and annotators

Industry context

Kili Technology

Ready to Put Your Experts on the Masks That Matter?

If your quality engineers or imagery analysts are the only people who can say where the defect or target ends, talk to the Kili team about setting up expert mask review with measured agreement.