Upload SDG pipeline + classifier weights
Browse files6 unmodified lines78910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989997 unmodified lines19719819920020120220320417 unmodified lines2222232242252262272286 unmodified linesfrom diffusers import DiffusionPipeline, StableDiffusionPipelinefrom diffusers.utils import BaseOutput# ----------------------------- Classifier ------------------------------------CLASS_NAMES = ["gore", "hate", "medical", "safe", "sexual"]class SafetyClassifier1280(nn.Module):"""Safety classifier for mid-UNet features of shape (B, 1280, H, W).Robust to HxW via AdaptiveAvgPool2d((8,8)) before the head."""def __init__(self, num_classes: int = 5):super().__init__()self.pre = nn.AdaptiveAvgPool2d((8, 8))self.net = nn.Sequential(nn.Conv2d(1280, 512, 3, padding=1),nn.BatchNorm2d(512), nn.ReLU(inplace=True), nn.MaxPool2d(2), # 512x4x4nn.Conv2d(512, 256, 3, padding=1),nn.BatchNorm2d(256), nn.ReLU(inplace=True), nn.MaxPool2d(2), # 256x2x2nn.AdaptiveAvgPool2d(1), nn.Flatten(), # 256nn.Linear(256, 128), nn.ReLU(inplace=True), nn.Dropout(0.3),nn.Linear(128, num_classes))self.apply(self._init_weights)@staticmethoddef _init_weights(m):if isinstance(m, nn.Linear):nn.init.xavier_uniform_(m.weight)if m.bias is not None: nn.init.zeros_(m.bias)elif isinstance(m, nn.Conv2d):nn.init.kaiming_normal_(m.weight, mode="fan_out", nonlinearity="relu")if m.bias is not None: nn.init.zeros_(m.bias)elif isinstance(m, nn.BatchNorm2d):nn.init.ones_(m.weight); nn.init.zeros_(m.bias)def forward(self, x: torch.Tensor) -> torch.Tensor:x = self.pre(x) # (B, 1280, 8, 8)return self.net(x)def load_classifier_1280(weights_path: str,device=None,dtype: torch.dtype = torch.float32) -> SafetyClassifier1280:"""Robustly load classifier weights saved either as a pure state dict or as atraining checkpoint (with 'model_state_dict'), handling key prefixes andPyTorch 2.6's weights_only behavior."""model = SafetyClassifier1280().to(device or "cpu", dtype=dtype)# --- load robustly (PyTorch 2.6 & older) ---try:state = torch.load(weights_path, map_location="cpu", weights_only=False)except TypeError:state = torch.load(weights_path, map_location="cpu")except Exception:# Allowlist common numpy globals if needed for old picklestry:import numpy as npfrom torch.serialization import add_safe_globalsadd_safe_globals([np.dtype, np.number])except Exception:passstate = torch.load(weights_path, map_location="cpu", weights_only=False)# --- unwrap training checkpoint ---if isinstance(state, dict) and "model_state_dict" in state:state = state["model_state_dict"]# --- normalize keys: 'module.' -> '', 'model.' -> 'net.' ---norm_state = {}for k, v in state.items():k = k.replace("module.", "")k = k.replace("model.", "net.")norm_state[k] = v# --- load tolerating tiny diffs (e.g., extra keys from older heads) ---missing, unexpected = model.load_state_dict(norm_state, strict=False)if missing or unexpected:print(f"[SDG] Loaded classifier with relaxed strictness. "f"Missing: {len(missing)}, Unexpected: {len(unexpected)}")model.eval()return modeldef _here(*paths: str) -> str:return os.path.join(os.path.dirname(__file__), *paths)97 unmodified linestimesteps = base.scheduler.timesteps# 4) classifier (run in fp32)weights_file = classifier_weights or pick_weights_path()clf = load_classifier_1280(weights_file, device=device, dtype=torch.float32)# 5) mid-block hookmid = {}17 unmodified lineslcat = torch.cat([lin, lin], dim=0)_ = base.unet(lcat, t, encoder_hidden_states=cond_embeds).samplefeat = mid["feat"].detach().float() # (B*2, 1280, H, W)logits = clf(feat)probs = torch.softmax(logits, dim=-1)unsafe = 1.0 - probs[:, safe_class_index].mean() # encourage "safe"6 unmodified lines78910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929397 unmodified lines19119219319419519619719819917 unmodified lines2172182192202212222236 unmodified linesfrom diffusers import DiffusionPipeline, StableDiffusionPipelinefrom diffusers.utils import BaseOutputimport torch, torch.nn as nn, osfrom typing import OptionalCLASS_NAMES = ['gore', 'hate', 'medical', 'safe', 'sexual']class AdaptiveClassifier1280(nn.Module):"""Same CNN topology you trained (keys start with 'model.*').Input (B,1280,H,W) -> AdaptiveAvgPool2d(8,8) -> conv stack -> head"""def __init__(self, num_classes: int = 5):super().__init__()self.pre = nn.AdaptiveAvgPool2d((8, 8))# Keep the attribute name 'model' to match the checkpoint keys.self.model = nn.Sequential(nn.Conv2d(1280, 512, kernel_size=3, padding=1),nn.BatchNorm2d(512), nn.ReLU(inplace=True), nn.MaxPool2d(2), # (512,4,4)nn.Dropout2d(0.1),nn.Conv2d(512, 256, kernel_size=3, padding=1),nn.BatchNorm2d(256), nn.ReLU(inplace=True), nn.MaxPool2d(2), # (256,2,2)nn.Dropout2d(0.1),nn.AdaptiveAvgPool2d(1), # -> (256,1,1)nn.Flatten(), # -> (256,)nn.Linear(256, 128), nn.ReLU(inplace=True), nn.Dropout(0.5),nn.Linear(128, num_classes))self.apply(self._init)@staticmethoddef _init(m):if isinstance(m, nn.Linear):nn.init.xavier_uniform_(m.weight); nn.init.zeros_(m.bias)elif isinstance(m, nn.Conv2d):nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')if m.bias is not None: nn.init.zeros_(m.bias)elif isinstance(m, nn.BatchNorm2d):nn.init.ones_(m.weight); nn.init.zeros_(m.bias)def forward(self, x):x = self.pre(x) # (B,1280,8,8)return self.model(x)def _find_weights_path() -> str:# 1) explicit env; 2) repo root file; 3) classifiers/ subdirenv_p = os.getenv("SDG_CLASSIFIER_WEIGHTS")if env_p and os.path.exists(env_p): return env_pfor p in ["safety_classifier_1280.pth", os.path.join("classifiers","safety_classifier_1280.pth")]:if os.path.exists(p): return p# If running from HF cache, these paths are relative to the cached repo folder.raise FileNotFoundError("Safety-classifier weights not found. Provide via env SDG_CLASSIFIER_WEIGHTS, ""place 'safety_classifier_1280.pth' at repo root or 'classifiers/', ""or pass `classifier_weights=...` to the pipeline call.")def load_classifier_1280(weights_path: Optional[str],device: torch.device,dtype: torch.dtype = torch.float32) -> AdaptiveClassifier1280:path = weights_path or _find_weights_path()ckpt = torch.load(path, map_location="cpu", weights_only=False)# Extract actual state dictif isinstance(ckpt, dict) and "model_state_dict" in ckpt:state = ckpt["model_state_dict"]elif isinstance(ckpt, dict) and any(k.startswith("model.") for k in ckpt.keys()):state = ckptelse:# Fallback: allow whole-object saves (only if trusted)state = ckptmodel = AdaptiveClassifier1280().to(device=device, dtype=torch.float32) # keep classifier in fp32missing, unexpected = model.load_state_dict(state, strict=False)if missing or unexpected:print(f"[SDG] load_state_dict: missing={missing[:4]}... ({len(missing)}), unexpected={unexpected[:4]}... ({len(unexpected)})")model.eval()return modeldef _here(*paths: str) -> str:return os.path.join(os.path.dirname(__file__), *paths)97 unmodified linestimesteps = base.scheduler.timesteps# 4) classifier (run in fp32)weights = classifier_weights or pick_weights_for_pipe(base)clf = load_classifier_1280(weights, device=device, dtype=torch.float32).eval()# 5) mid-block hookmid = {}17 unmodified lineslcat = torch.cat([lin, lin], dim=0)_ = base.unet(lcat, t, encoder_hidden_states=cond_embeds).samplefeat = mid["feat"].detach().to(torch.float32)logits = clf(feat)probs = torch.softmax(logits, dim=-1)unsafe = 1.0 - probs[:, safe_class_index].mean() # encourage "safe"
safe_diffusion_guidance.py+61-66