Skip to main content
Prakriti Bista
On this page

Can a Generalist Model Learn to Specialize? Three Experiments with SAM

We took Meta's Segment Anything Model and tried to make it better at three specific tasks. The wood experiment lost 36.28 percentage points of mIoU. Carpet improved modestly. Camouflage led to a larger gain and a trip to the Smithsonian Zoo.

Can a Generalist Model Learn to Specialize? Three Experiments with SAM

The training loss dropped to 0.0001. Anurag and I thought we had made a better model.

Then we evaluated it. On visually similar wood textures, our fine-tuned model scored 59.52% mean Intersection over Union (mIoU). The original MobileSAM scored 95.80%. We had lost 36.28 percentage points.

That failure became the most useful part of our Computer Vision project at GWU. We wanted to see whether adapting SAM's mask decoder could help with three tasks: wood texture boundaries, carpet defects, and camouflaged animals. The answer depended on what the baseline could already do, the data we gave it, and how we evaluated the result.

Start with the baseline

For wood, we made 500 synthetic mosaics from 530 texture images. Each mosaic contained four tiles. For both wood and carpet we used MobileSAM, which according to its authors has a 5M-parameter image encoder and 9.66M parameters overall, not the 40M our original draft gave. ResNet18 features and K-Means clustering helped us create a harder test: four tiles drawn from the same cluster, with similar colors and subtler grain differences.

We trained for five epochs with Dice loss and a learning rate of 1e-4, using a center-point prompt for each tile. The training curve looked exciting. The held-out result did not.

The baseline was already good at finding square tile boundaries. Our training gave it very little useful room to improve. The gap between training loss and test performance was consistent with overfitting, but we did not run the ablations needed to separate the effects of data volume, objective, and optimization.

My practical lesson was to evaluate the starting model before deciding that it needs specialization.

Wood-texture mosaic used to construct the specialization task

Wood training examples combined four textures into a synthetic mosaic.

Can synthetic defects transfer?

Carpet was a different problem. The MVTec AD carpet split gave us 280 normal training images and 89 anomalous test images. We generated 500 synthetic defects from the normal images: darkened patches, thin scars, and hue-shifted stains, each paired with a mask.

We kept MobileSAM, five epochs, and Dice-only training. The recorded loss fell from 0.2544 to 0.1527. On real defects, mIoU rose from 29.43% to 36.18%, a gain of 6.75 percentage points.

It was a modest result, and I was happy to call it that. The synthetic anomalies offered a useful training signal, but straight lines and smooth color shifts did not capture the full appearance of fraying, shadows, and irregular damage.

MobileSAM base predictions on real carpet defects

The carpet experiment tested transfer from synthetic training defects to real examples.

A larger gain on camouflage

For camouflage, we used SAM ViT-H, froze the image encoder, cached its embeddings, and trained the mask decoder. We combined Binary Cross-Entropy and Dice loss with equal weights and alternated between point and bounding-box prompts.

The seven-epoch run ended at a training loss of 0.1059. Our class-project summary reports 47.52% mIoU for base SAM and 66.35% for the specialized model on 200 sampled test images: 18.83 percentage points, or about 39.6% relative improvement.

The prompt table further down gives 65.73% for the specialized model with a center point and 66.81% with a four-point grid, and neither matches the 66.35% headline exactly, so I treat prompt choice as part of the result. The 47.52% baseline matches the center-point row of that table, where the specialized model scored 65.73%, a gain of 18.2 points; I could not trace the 66.35% figure to the same run.

The original notes also described all 10,000 COD10K images as camouflaged animals. The official dataset includes 5,066 camouflaged images, with other images covering non-camouflaged objects and backgrounds. Its camouflage subset uses 3,040 training and 2,026 test images. Our notes used the larger release's 6,000/4,000 split without distinguishing the camouflage subset. Those are not the camouflage training/test counts. The later project log corrects the training count to 3,040 images, or 6,080 with horizontal-flip augmentation. It also documents later 15-epoch experiments from 2026, after this post was first written, and those results are separate from this seven-epoch class-project record. One of those runs, evaluated on all 2,026 camouflaged test images, reached 73.8% mIoU against a 57.2% baseline with a center-point prompt.

Recorded base and specialized SAM camouflage masks

One retained benchmark comparison. The aggregate result covers the sampled test set, not just this example.

Taking it to the zoo

We took photos at the Smithsonian National Zoo to see how the model behaved away from the benchmark. Those examples made the work feel real to me. We also retained additional web-image examples, separately labeled in the record.

They were qualitative checks, with no annotated test protocol or aggregate zoo score. They helped us notice behavior and failures; they did not establish performance across wildlife settings.

Qualitative model comparison using photos from the Smithsonian National Zoo

Photos from our zoo visit tested behavior beyond the benchmark; they did not have an aggregate score.

What I would carry into the next experiment

The three runs gave us different outcomes:

Training loss against held-out results

  1. Wood, hard modeMobileSAM · Dice
    0.06%
    −36.28 pts
  2. Real carpet defectsMobileSAM · Dice
    60%
    +6.75 pts
  3. CamouflageSAM ViT-H · BCE + Dice
    75%
    +18.83 pts
Show the numbers
TaskTraining loss by epochBaseline mIoUSpecialized mIoUChange
Wood, hard mode0.157, 0.0125, 0.0426, 0.0002, 0.000195.80%59.52%−36.28 pts
Real carpet defects0.2544, 0.2006, 0.1818, 0.1639, 0.152729.43%36.18%+6.75 pts
Camouflage0.142, 0.1272, 0.1172, 0.114, 0.1113, 0.1081, 0.105947.52%66.35%+18.83 pts
The steepest training curve belonged to the run that did worst on held-out data. Each loss is shown relative to its own first epoch because the runs used different objectives, and camouflage reflects the seven-epoch class-project run on 200 sampled test images.

I would keep the frozen encoder and cached embeddings because they made decoder experiments practical. I would add a validation split, record exact test manifests and seeds, and vary one training choice at a time.

The useful lesson was learning to distrust a flattering training curve until I had checked the result on held-out data. Three experiments gave us reasons to investigate baseline headroom, prompts, and synthetic-data transfer. They did not prove a universal threshold for when fine-tuning works.

Experiment details and original visual evidence

The expanded record keeps the datasets, training settings, loss curves, result tables, and all original examples together.

Read the experiment record and image comparisons

Wood Texture Dataset Structure

wood_data/
├── images/
│   └── images/
│       ├── abura-s-60x60.jpg
│       ├── african-blackwood-60x60.jpg
│       ├── oak-60x60.jpg
│       ├── walnut-60x60.jpg
│       └── ... (530 total images)
└── wood.csv (metadata with species names)

Each image is a 60×60 or similar resolution texture sample. We created 500 synthetic 512×512 mosaics (2×2 grids of 256×256 tiles) for training.

The Setup

Dataset: 530 wood texture images from various species (oak, walnut, pine, etc.)
Model: MobileSAM (the lightweight variant with a TinyViT image encoder)
Training Data: 500 synthetic 2×2 mosaics (each containing 4 different wood textures)
The Goal: Teach SAM to distinguish between visually similar wood grains We started by creating “mosaic” images, think of them as 2×2 grids where each quadrant contains a different wood texture. To make things interesting, we used ResNet18 to extract features from all 530 images, then applied K-Means clustering to group similar woods together. This gave us 5 distinct clusters:

Cluster 1: 183 images (34.5%) - Lighter hardwoods
Cluster 0: 171 images (32.3%) - Medium tones
Cluster 2:  96 images (18.1%) - Darker woods
Cluster 4:  41 images (7.7%)  - Exotic species
Cluster 3:  39 images (7.4%)  - Very distinct grains

Baseline Evaluation (Zero-Shot Mobile Sam)

We gave MobileSAM the perfect center point of each tile. The thought here was:

  • If it's a good generalist, it should segment the square tile perfectly.
  • If it fails, it will likely over-segment (texture details) or under-segment (merge similar woods).

Zero-shot MobileSAM masks for each tile of one wood mosaic, with per-tile IoU Figure: Zero shot (Average Baseline IoU for this image: 0.9950)

The “Hard Mode” Challenge

Here’s where it got interesting. We created two test modes:
- Standard Mode: Mosaics with textures from different clusters ( lots of visual contrast)
- Hard Mode: Mosaics where ALL four tiles came from the SAME cluster ( similar colors, only grain direction differs) The hypothesis was simple: if our specialized model could learn fine-grained texture boundaries, it should outperform vanilla SAM on Hard Mode.

The Training

We ran a straightforward training loop:
- 5 epochs
- Learning rate: 1×10⁻⁴
- Loss function: Dice loss only
- Prompt strategy: Perfect center point of each tile The training seemed to go great. The loss dropped beautifully:

Epoch 1: Loss = 0.1570
Epoch 2: Loss = 0.0125  (92% drop!)
Epoch 3: Loss = 0.0426  (wait, it went UP?)
Epoch 4: Loss = 0.0002  (okay, back down...)
Epoch 5: Loss = 0.0001  (basically zero!)

We were thrilled. Loss of 0.0001? That’s incredible! Time to test it.

The Devastating Results

Wood baseline and specialized segmentation comparison

Base MobileSAMOur “Specialized” SAMChange
Hard Mode mIoU95.80%59.52%-36.28 percentage points

Base MobileSAM mostly keeps to the tile edges, while the specialized model's masks spill across tile boundaries or cover only part of the tile.

The decrease was 36.28 percentage points of mIoU (about 37.9% relative to the baseline). On the one standard mosaic we inspected, vanilla MobileSAM averaged 99.5% IoU across its four tiles. But on Hard Mode (similar textures), base SAM scored 95.8% while our “specialized” version completely collapsed to 59.5%.

What the failed evaluation told us

The training loss fell to 0.0001 while held-out performance fell. That is consistent with overfitting. We had no validation split, and the small synthetic training set limited what we could conclude. This run did not isolate the effects of dataset size, loss, or learning rate.

Challenge 2: Carpet Defect Detection - The Synthetic Data Experiment

The Setup

Dataset: MVTec AD Carpet Dataset
Model: MobileSAM (same as wood experiment)
Real Training Data: 280 defect-free “good” carpet images
Real Test Data: 89 images with real industrial defects (5 types: cuts, holes, thread pulls, metal contamination, color defects)
The Problem: 89 defect images is way too few to train a neural network This was an industrial anomaly detection challenge. Imagine a carpet factory where cameras scan every meter of output looking for manufacturing flaws. These defects are subtle, random, and expensive to collect labeled examples of.

Carpet Defect Dataset Structure

carpet/
├── train/
│   └── good/ (280 defect-free images)
├── test/ (89 defect images across five classes)
│   ├── color/
│   ├── cut/
│   ├── hole/
│   ├── metal_contamination/
│   └── thread/
└── ground_truth/
    ├── color/ (*_mask.png files)
    ├── cut/
    ├── hole/
    ├── metal_contamination/
    └── thread/

Total: 280 training images (all good), 89 test images with defects. We generated 500 synthetic defect images from the good images for training.

The Synthetic Data Solution

Since we couldn’t train on real defects (too few), we built a synthetic defect generator that could inject realistic-looking anomalies into the 280 good images: Type 1: Patches (simulates holes, metal contamination, cuts)
- Cut out random 20-60px patches from the carpet
- Paste them elsewhere, but darkened (40-70% brightness) or with added Gaussian noise
- Creates the appearance of holes or foreign objects Type 2: Scars (simulates thread pulls, cuts)
- Draw thin random lines (2-5px thick) across the carpet
- Use dark colors (0-50 RGB values) to simulate broken threads
- Random start/end points to create irregular patterns Type 3: Stains (simulates color defects)
- Apply circular color shifts (40-100px radius) using HSV manipulation
- Shift the hue by 20-50 degrees
- Blend at 40% opacity for a realistic “dye bleeding” effect We generated 500 synthetic training samples this way, each one with a known mask and a center-point prompt at the anomaly location.

Synthetic carpet-defect training examples Three synthetic training samples, each with a red star marking the center-point prompt at the generated defect.

The Training

Learning from the wood disaster, we kept a close eye on the training dynamics:

Epoch 1: Loss = 0.2544
Epoch 2: Loss = 0.2006  (21% drop - healthy)
Epoch 3: Loss = 0.1818  (9% drop)
Epoch 4: Loss = 0.1639  (10% drop)
Epoch 5: Loss = 0.1527  (7% drop)

The curve was smoother than the wood run. We needed the real-defect evaluation to assess generalization; training loss alone could not establish it.

Training config:
- 5 epochs
- Learning rate: 1×10⁻⁴
- Loss: Dice only
- 500 synthetic samples, tested on 89 REAL defects

The Results: Modest but Real Improvement

Base MobileSAMSpecialized SAMChange
mIoU (Real Defects)29.43%36.18%6.75 percentage points

Not spectacular, but positive! We improved by 6.75 percentage points (a 23% relative improvement) without the model ever seeing a single real defect during training.

These are some outputs from the Base Model: MobileSAM base predictions on real carpet defects Fig: Visual comparison showing Baseline SAM

These are some of the outputs from our specialized model: Specialized model predictions on real carpet defects Fig: Visual comparison showing Specialized SAM(Our model)

What this result supports

The real-defect evaluation supports a modest transfer from synthetic training to real images. The synthetic patches, scars, and stains are simpler than real manufacturing defects, so this was a starting point for further evaluation.

Challenge 3: Camouflaged Animal Detection

The setup

We used SAM ViT-H with its encoder frozen and adapted the mask decoder. The saved class-project record describes a COD10K-v3 copy with 6,000 training files and 4,000 test files, plus horizontal-flip augmentation. Those are file counts from that record, not counts of camouflaged animals. The headline evaluation used 200 sampled test images; the eligible subset and random seed would need the original manifest to reproduce.

Camouflage Dataset (COD10K-v3)

COD10K-v3/
├── Train/
│   ├── Image/ (6,000 images)
│   │   ├── COD10K-CAM-1-Aquatic-1-BatFish-1.jpg
│   │   ├── COD10K-CAM-1-Aquatic-2-Crab-10.jpg
│   │   └── ...
│   └── GT_Object/ (6,000 binary masks)
└── Test/
    ├── Image/ (4,000 images)
    └── GT_Object/ (4,000 masks)

The class-project notes record 12,000 samples after augmentation and evaluation on 200 selected test images.

The Dataset

COD10K contains images organized by categories:
- Aquatic animals (fish, octopuses, seahorses camouflaged against reefs)
- Terrestrial animals (insects, lizards, frogs blending into leaves/bark)
- Flying animals (moths with wing patterns matching tree bark) The camouflaged-object subset provides annotated masks; the full COD10K release also contains non-camouflaged and background images. The dataset is challenging because:
- Low contrast: Animal and background have similar colors
- Complex textures: Natural environments have high-frequency patterns everywhere
- Scale variation: Animals range from tiny insects to large reptiles
- Occlusion: Many animals are partially hidden

Camouflage dataset and training preparation Figure: Showing sample: COD10K-CAM-1-Aquatic-1-BatFish-1.jpg

Training the decoder

We froze the image encoder and cached embeddings. The camouflage run used a combined pixel-level and overlap loss, with point and box prompts sampled in equal proportions.

# Pseudocode for the combined objective
bce_loss = Binary_Cross_Entropy(pred, target)
dice_loss = 1 - Dice_Coefficient(pred, target)
final_loss = 0.5 * bce_loss + 0.5 * dice_loss

The Training

The recorded losses across seven epochs:

Epoch 1: Loss = 0.1420
Epoch 2: Loss = 0.1272  (10.4% drop)
Epoch 3: Loss = 0.1172  (7.9% drop)
Epoch 4: Loss = 0.1140  (2.7% drop)
Epoch 5: Loss = 0.1113  (2.4% drop)
Epoch 6: Loss = 0.1081  (2.9% drop)
Epoch 7: Loss = 0.1059  (2.0% drop)

Total: 25.4% reduction, NO COLLAPSE

The final training loss was 0.1059.

The Breakthrough Results

Base SAM ViT-HSpecialized SAMChange
mIoU (200 test samples)47.52%66.35%18.83 percentage points

The headline class-project summary records an 18.83 percentage-point gain, about 39.6% relative to its baseline. The retained comparisons show both improvements and cases where the models behaved similarly. Recorded camouflage benchmark comparison 2 Recorded camouflage benchmark comparison 3 Recorded camouflage benchmark comparison 4 Recorded camouflage benchmark comparison 5 Recorded camouflage benchmark comparison 6 Recorded camouflage benchmark comparison 7 Recorded camouflage benchmark comparison 8 Recorded camouflage benchmark comparison 9 Recorded camouflage benchmark comparison 10 Recorded camouflage benchmark comparison 11 Benchmark comparisons: input, baseline mask, and specialized mask. These examples illustrate behavior; the aggregate metrics are reported separately.

Limits of the comparison

The runs changed several factors together. They do not establish which change caused the gain, and no random-mask baseline was measured. The prompt comparison below is a separate result table, with its own values.

Real-World Test: The Smithsonian Zoo

We didn’t just test on the COD10K dataset; we took our model to the Smithsonian National Zoo and photographed real animals in their enclosures to see how it performed on photos outside our benchmark evaluation.

Smithsonian Zoo qualitative model comparison Figure: Images from our visit to the Zoo

Additional web-image qualitative model comparison Additional web-image qualitative model comparison Additional examples retained from the project notes and attributed there to Google. They are qualitative examples, separate from the zoo photos and benchmark scores.

Multi-point comparison

Recorded multi-point prompt comparison

modelprompt_strategyiou_meaniou_stddice_meandice_stdf1_meanf1_stdboundary_prec_meanboundary_prec_stdboundary_recall_meanboundary_f1_meannum_samples
Base SAM ViT-HCenter-of-Mass (Single)0.475222955558130430.393401376411395830.53464207429579940.41410004726143730.53464207429579950.41410004726143730.56712981728643840.34375467787388090.51066815944649150.4976784545300676200
Base SAM ViT-HEdge (Single)0.227234123045360450.32246855603340720.276199746807087640.3609495034386280.276199746807087640.3609495034386280.45902919737921440.33801490457495370.31112926440427260.3025289410123832200
Base SAM ViT-HMulti-Point Grid (4 pts)0.60803395180055290.35193385560867350.67800945517822020.36094383665875710.67800945517822020.360943836658757070.66157921036932270.31554536466131940.59319743704264630.5929179360348111200
Specialized SAM ViT-HCenter-of-Mass (Single)0.65726207869301070.30292983612164380.73977292980177150.29610402287777790.73977292980177150.29610402287777790.67204447781763470.300516604104462850.67847260604333670.6527537761728501200
Specialized SAM ViT-HEdge (Single)0.64632399180830870.29774946714062250.7340187926683060.287104179313719950.7340187926683060.28710417931371990.63237856955905180.30538248868229960.69497951033543470.6375097254920465200
Specialized SAM ViT-HMulti-Point Grid (4 pts)0.66806968810872150.289771626968751140.75306567120016750.279971319613403270.75306567120016730.279971319613403270.68012121921599190.298542588290133140.68010486086965030.6573306856627894200

The Comparative Analysis

These are the recorded class-project results. They are not a controlled comparison across tasks.

Performance Summary

ExperimentModelTraining SamplesBase mIoUSpecialized mIoUChangeOutcome
Wood (Hard Mode)MobileSAM500 mosaics (~2K tiles)95.80%59.52%-36.28 percentage points Failed
Carpet DefectsMobileSAM500 synthetic29.43%36.18%6.75 percentage points Modest
CamouflageSAM ViT-H12,000 claimed in the original notes; later corrected to 6,08047.52%66.35%18.83 percentage points Success

Explore Special-SAM or read the project case study.