MRS Spring 2026, Part III of III

Foundation Models Adaptation III: the XRD example

A public diffraction dataset, three runnable notebooks, and a comparison that rewards the simplest method that works.

01

The public dataset

The RefleX dataset has 6,311 diffraction images, seven anomaly labels, official splits, and many images with more than one anomaly.

6,311
total images
1024 × 1024
pixels per PNG
7
anomaly labels
72.1%
contain > 1 anomaly
Background ring60.5 %
Strong background46.6 %
Loop scattering46.4 %
Non-uniform detector33.7 %
Artifact15.6 %
Ice ring7.6 %
Diffuse scattering7.2 %
RefleX dataset: Zenodo DOI 10.5281/zenodo.2605120.
02

The task: multi-label XRD anomaly detection

Input one diffraction image. Return every anomaly label that applies.

Grid of XRD anomaly examples.
Output
Return every anomaly that applies. A single image may have multiple labels.
03

Preparing scientific images for foundation models

Dynamic range choices matter. The training and inference preprocessing should be the same.

raw detector view
raw detector view
preprocess
scale

clip, log-scale, normalize

pipeline
model input

use the same transform for training and inference

dynamic range
  • Detector counts span orders of magnitude
  • Raw values can exceed 10,000
  • Natural-image encoders expect different image statistics
bit-depth conversion
  • Convert 16-bit detector images before modeling
  • Use a fixed range for comparability
  • Percentile normalization can reduce outlier effects
log scaling
  • Use log10(x + epsilon) when needed
  • Compress bright peaks
  • Expose weak features humans inspect
04

Notebook 1: few-shot prompting

Send diffraction images and text examples in the prompt. The model weights never change.

Notebook access
QR code for Notebook 1: few-shot prompting
What it does
few-shot prompting
images plus a text prompt; weights never change
focus
  • Prompt design
  • Output parsing
  • Effect of adding support examples
  • Set OPENAI_API_KEY before running
k = 0
0.53
k = 1
0.61
k = 3
0.60
k = 5
0.67
k = 7
0.63

Random support examples can miss important labels. A stronger version would choose examples by similarity and label balance.

05

Notebook 2: frozen features + light classifier

Encode the images once with CLIP, then fit kNN or a linear classifier on the embeddings.

Notebook access
QR code for Notebook 2: frozen features + light classifier
What it does
frozen CLIP + light classifier
encode once, then fit kNN or linear classification heads
details
  • Frozen CLIP ViT-L/14-336 encoder
  • Mean-max pooled patch-token embeddings
  • Heads: kNN, one-vs-rest logistic regression, low-shot logistic regression
  • The encoder is reused rather than retrained
CLIP ViT-L/14-336 + kNN
0.851 micro-F1

0.819 macro-F1 on full RefleX test set

Logistic regression (one-vs-rest, val-tuned threshold)
0.826 micro-F1

0.789 macro-F1 with validation-tuned threshold

06

Notebook 3: LoRA fine-tuning of Gemma 3 4B

Fine-tune Gemma 3 4B with QLoRA, PEFT, and TRL. In this run, extra few-shot examples hurt after SFT.

Notebook access
QR code for Notebook 3: LoRA fine-tuning of Gemma 3 4B
What it does
Gemma 3 4B + QLoRA
PEFT + TRL SFTTrainer with a 4-bit quantized base model
recipe and warnings
  • LoRA r=16, alpha=16, dropout 0.05
  • 1 epoch, lr 2e-4, gradient accumulation 4, MAX_SOFT_TOKENS=280
  • Accept the model terms before downloading
  • A free-tier T4 GPU is not enough for this run
k = 0
0.80
k = 1
0.72
k = 3
0.73
k = 5
0.73
k = 7
0.74

After SFT, extra in-context examples made this run worse. Measure the adapted model both with and without examples.

07

Simpler methods can be better

On this benchmark, frozen CLIP plus kNN is simpler and stronger than the more complex alternatives tested here.

GPT-5 few-shot (k=5)67 % micro-F1
API model on 16-image curated split
Frozen CLIP + kNN85.1 % micro-F1
full 632-image test set
Frozen CLIP + linear82.6 % micro-F1
full 632-image test set
Gemma 3 4B LoRA (k=0)80 % micro-F1
full 632-image test set
Note: GPT-5 few-shot used a curated 16-image sample, while kNN and LoRA used the full 632-image test set. Treat the bars as a practical comparison, not a controlled leaderboard.
08

SFT and few-shot may not help each other

Prompting and fine-tuning do not automatically combine well. Test the combined setup before relying on it.

What it does
GPT-5 few-shot
examples help up to k = 5
k = 0
0.53
k = 1
0.61
k = 3
0.60
k = 5
0.67
k = 7
0.63
What it does
Gemma after SFT
examples hurt this adapted model
k = 0
0.80
k = 1
0.72
k = 3
0.73
k = 5
0.73
k = 7
0.74
Note: the GPT-5 group is a curated 16-image test; the Gemma LoRA group is the full 632-image test.
Wrap-up

Glossary

Key terms from the tutorial, related ptychography references, and acknowledgements.

09

Glossary

A quick reference for the main terms used above.

Zero-shot

Ask the model directly, no worked examples.

Few-shot

Include a handful of worked examples in the prompt.

Embedding

A numeric summary of an image or text.

Encoder

The model part that turns input into a representation.

kNN

Predict from the most similar labeled examples.

Linear classifier

A simple rule drawn in feature space.

Fine-tuning

Updating model weights on your task.

Adapter / LoRA

A small trainable add-on instead of full retraining.

Quantization

Lower-precision storage to save memory.

Leakage

Related samples in train and test that inflate scores beyond real-world performance.

Micro-F1 / Macro-F1

Pool-then-score (micro) vs. score-then-average across classes (macro).

SFT

Supervised fine-tuning on labeled input–output pairs.

QLoRA

LoRA on a quantized base model, often small enough for a single GPU.

OOD

Out-of-distribution: inputs unlike what the model trained on.

For ptychography examples beyond this XRD tutorial, see arXiv:2511.02503 and arXiv:2410.09034, which show how LLMs and VLMs can be adapted to ptychography tasks and how agentic workflows can be built around tested model recipes.

Acknowledgement: this research used resources of the Advanced Photon Source, a U.S. DOE Office of Science User Facility operated under Contract DE-AC02-06CH11357, with funding support from Argonne LDRD 2023-0049. Thanks to APS MIC/CAI/SDM groups, USC Shao Group, Rice Han Group, and Cornell Muller Group.