Stable Diffusion 3.5 Feature Extractor (sd35-feature-extractor) is a research toolkit that hooks into the Multimodal Diffusion Transformer (MM-DiT) backbone of Stable Diffusion 3.5 to extract internal latent representations—hidden states and joint attention maps—for synthetic image detection, generative model attribution, and mechanistic interpretability.
Motivation & Inversion Pipeline
Standard pixel-space classifiers operate exclusively on the final rendered image, where generator-specific traces are often subtle or destroyed by post-processing. By encoding an input image into the Stable Diffusion 3.5 VAE latent space and running a single forward pass through the MM-DiT transformer at a controlled diffusion timestep $t \in [0, 1000]$, intermediate activations expose structural and semantic regularities across both the image and text conditioning streams.
Decoupling feature extraction from downstream classification allows high-dimensional representations to be computed once and reused across linear probes, MLP classifiers, layer-wise attribution studies, and cross-dataset generalization benchmarks.
MM-DiT Hook Architecture & Controls
The library instruments the 38 joint transformer blocks of Stable Diffusion 3.5 Large (as well as the Medium and Large Turbo variants) without modifying model weights:
- Granular Layer & Stream Selection: Extracts any subset of image-branch hidden states (
hidden_x, blocks0–37), text-branch context states (hidden_context, blocks0–36), and joint attention weights (attention), along with cross-layer averages (middle_avg). - Precomputed Triple-Encoder Conditioning: Supports offline extraction of text conditioning tensors (
c_crossattnof shape[1, 154, 4096]and pooledyof shape[1, 2048]) fromCLIP-L,CLIP-G, andT5-XXL, eliminating the need to keep all three text encoders in VRAM during image-batch processing. - Spatial Mean Pooling & Safetensors Serialization: Optional spatial mean pooling compresses per-layer image tokens from
[1, 1024, 1536]to[1, 1536](float16), serializing all selected streams into a single namespaced.safetensorsarchive per image.
output_features/
├── experiment_config.json
├── execution.log
├── real/
│ └── image001.safetensors # hidden_x__*, hidden_context__*, attention__*
└── fake/
└── image001.safetensors
Bias Mitigation & Benchmark Suite
A recurring failure mode in deepfake detection is shortcut learning on compression artifacts or resizing pipelines rather than generative priors. To support controlled ablation studies, the extractor integrates:
- Preprocessing Modes: Configurable input pipelines including
imagenet_style,brutal_resize,crop_100_then_resize, and native-resolution pass-through (none). - On-the-Fly JPEG Augmentation: Stochastic JPEG recompression during extraction to neutralize format asymmetries between real and synthetic datasets.
- Automated Benchmark Acquisition: Interactive TUI and CLI downloaders (
download_tui.py) for model checkpoints and standard detection benchmarks, including GenImage, Tiny GenImage, Unbiased GenImage, OpenFake (1.9M images across 20+ generators), Echo-4o-Image, and NanoBanana.