Prompts are the foundation of LAFT’s language-assisted feature transformation. They define the semantic concepts that guide or are ignored during feature transformation.
Returns a dictionary of prompt templates for a dataset.
import laft# Get prompts for Color MNIST, guiding towards "number"prompts = laft.prompts.get_prompts("color_mnist", "guide_number")# Available prompt types:# - "normal": Prompts for normal class# - "anomaly": Prompts for anomaly class# - "half": Normal + half of anomaly prompts# - "exact": Normal + exact anomaly prompts# - "all": Normal + anomaly + auxiliary promptsprint(prompts.keys()) # dict_keys(['normal', 'anomaly', 'half', 'exact', 'all'])
Parameters:
dataset_name (str): One of "color_mnist", "waterbirds", "celeba"
guidance (str): Strategy in format "guide_{concept}" or "ignore_{concept}"
Returns:
Dictionary mapping prompt types to lists of prompt strings
Detect anomalies in the digit class (5-9 are anomalies).
prompts = laft.prompts.get_prompts("color_mnist", "guide_number")# Normal: digits 0-4# Anomaly: digits 5-9# Templates: "a photo of {}", "an image of {}", etc.
Example prompts:
Normal: “a photo of zero”, “a number 1”, “a sketch of 2 letter”
Anomaly: “a photo of five”, “a number 6”, “a sketch of 7 letter”
Detect anomalies in color (green/blue are anomalies).
prompts = laft.prompts.get_prompts("color_mnist", "guide_color")# Normal: red colors# Anomaly: green, blue colors
Example prompts:
Normal: “red”, “ruby”, “scarlet”, “crimson”
Anomaly: “green”, “lime”, “blue”, “azure”
Detect digit anomalies while ignoring color.
prompts = laft.prompts.get_prompts("color_mnist", "ignore_color")# Projects features orthogonal to color subspace# Focuses on digit-related anomalies only
For industrial anomaly detection (MVTec AD, VisA), use the industrial prompt modules:
from laft.prompts import industrial1# Get normal and anomaly prompts for a categorynormal_prompts, anomaly_prompts = industrial1.get_prompts("bottle")print(normal_prompts[:3])# ['bottle', 'flawless bottle', 'perfect bottle']print(anomaly_prompts[:3])# ['damaged bottle', 'bottle with flaw', 'bottle with defect']
Industrial templates:
NORMAL_STATES = [ "{}", "flawless {}", "perfect {}", "unblemished {}", "{} without flaw", "{} without defect", "{} without damage",]
# Define your templatesTEMPLATES = [ "a photo of a {}.", "a blurry photo of a {}.", "a high contrast photo of a {}.",]# Define concept wordsNORMAL_WORDS = ["apple", "orange", "banana"]ANOMALY_WORDS = ["rotten apple", "moldy orange", "bruised banana"]# Generate promptsnormal_prompts = [[f.format(word) for f in TEMPLATES] for word in NORMAL_WORDS]anomaly_prompts = [[f.format(word) for f in TEMPLATES] for word in ANOMALY_WORDS]# Flatten for "ignore" strategyif guidance.startswith("ignore"): normal_prompts = [p for sublist in normal_prompts for p in sublist] anomaly_prompts = [p for sublist in anomaly_prompts for p in sublist]# Encode with CLIPall_prompts = normal_prompts + anomaly_promptstext_features = model.encode_text(all_prompts)# Build concept subspacepair_diffs = laft.prompt_pair(text_features)concept_basis = laft.pca(pair_diffs)