Prerequisites
Make sure you have completed the installation steps before proceeding.Basic Example
Here’s a complete working example that demonstrates the core LAFT workflow:Import and Setup
First, import LAFT and disable PyTorch’s autograd to prevent out-of-memory errors:
Disabling autograd is important when working with large CLIP models to avoid memory issues during inference.
Load CLIP Model
Load a pre-trained CLIP model and its preprocessing transform:The model returns both the CLIP model and an image preprocessing transform. The
ViT-B-16-quickgelu:dfn2b variant uses Vision Transformer backbone with DFN (DataFiltering Network) pre-training.Prepare Data and Prompts
Load your dataset and define language prompts that describe the concept you want to guide or ignore:The
get_prompts() function returns a dictionary with different prompt sets:"all": All prompts for the concept"normal": Only normal state prompts"exact": Exact matching prompts
Construct Concept Subspace
Build a concept subspace by computing pairwise differences between text embeddings and applying PCA:The
prompt_pair() function computes differences between all pairs of prompts, which helps capture semantic directions in the embedding space. PCA then extracts the most important directions as a basis for the concept subspace.Transform Features
Apply language-assisted feature transformation to guide or ignore the concept:Choose the transformation based on your use case:
- Use
inner()when you want to guide detection toward the concept - Use
orthogonal()when you want to ignore the concept
Complete Script
Here’s the complete code in one place:Different Use Cases
- Semantic Datasets
- Industrial Datasets
For semantic datasets like Waterbirds or CelebA:
Running the Scripts
The repository includes pre-configured scripts for different datasets. Here are some examples:Next Steps
Core Concepts
Learn the theory behind LAFT
Datasets
Explore available datasets
API Reference
Dive into the API documentation
Guides
Follow detailed usage guides
