File size: 2,816 Bytes
788274d
 
 
9bd6473
788274d
9bd6473
788274d
 
 
 
9bd6473
 
 
 
 
 
 
 
788274d
9bd6473
 
 
 
 
 
 
 
 
 
 
 
 
788274d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env bash
# C2 baseline: official UCE (rohitgandikota/unified-concept-editing @ pinned commit, MIT), unmodified,
# then converted to our candidate format (edited_tensors.safetensors with `unet.` prefixes).
# usage: scripts/run_uce.sh <variant: tiny|sd14> <hf model id> [concept: vangogh|church|nudity]
set -euo pipefail
VARIANT=$1; MODEL_ID=$2; CONCEPT=${3:-vangogh}
COMMIT=7c724d9d7d19190e9bd33fe62fb656f1df87cd8e
mkdir -p third_party/uce && cd third_party/uce
[ -f uce_sd_erase.py ] || curl -sfLO "https://raw.githubusercontent.com/rohitgandikota/unified-concept-editing/$COMMIT/trainscripts/uce_sd_erase.py"
cd ../..
case $CONCEPT in
  vangogh) EDIT="Van Gogh"; TYPE=art; PRESERVE="Claude Monet;Pablo Picasso;Rembrandt;Andy Warhol;Caravaggio;Paul Cezanne;Paul Gauguin" ;;
  church)  EDIT="church"; TYPE=object; PRESERVE="tench;English springer;cassette player;chain saw;French horn;garbage truck;gas pump;golf ball;parachute;mosque;buddhist temple;castle;lighthouse;town hall;farmhouse" ;;
  nudity)  EDIT="nudity"; TYPE=object; PRESERVE="" ;;
  *) echo "unknown concept $CONCEPT"; exit 2 ;;
esac
OUT=runs/edits/c2-uce-$VARIANT-$CONCEPT
python third_party/uce/uce_sd_erase.py --model_id "$MODEL_ID" --edit_concepts "$EDIT" --concept_type $TYPE \
  --save_dir $OUT/raw --exp_name uce-default
if [ -n "$PRESERVE" ]; then
  python third_party/uce/uce_sd_erase.py --model_id "$MODEL_ID" --edit_concepts "$EDIT" --concept_type $TYPE \
    --preserve_concepts "$PRESERVE" --save_dir $OUT/raw --exp_name uce-preserve
  for SCALE in 2 4 8; do
    python third_party/uce/uce_sd_erase.py --model_id "$MODEL_ID" --edit_concepts "$EDIT" --concept_type $TYPE \
      --preserve_concepts "$PRESERVE" --erase_scale $SCALE --save_dir $OUT/raw --exp_name uce-preserve-x$SCALE
  done
else
  for SCALE in 2 4 8; do
    python third_party/uce/uce_sd_erase.py --model_id "$MODEL_ID" --edit_concepts "$EDIT" --concept_type $TYPE \
      --erase_scale $SCALE --save_dir $OUT/raw --exp_name uce-default-x$SCALE
  done
fi
python - "$OUT" <<'PY'
import json, sys
from pathlib import Path
from safetensors.torch import load_file, save_file
from uvw.provenance import tensor_digest
out = Path(sys.argv[1])
for f in sorted((out / "raw").glob("*.safetensors")):
    t = {f"unet.{k}": v.contiguous() for k, v in load_file(str(f)).items()}
    d = out / "candidates" / f.stem
    d.mkdir(parents=True, exist_ok=True)
    save_file(t, str(d / "edited_tensors.safetensors"))
    (d / "meta.json").write_text(json.dumps({"name": f.stem, "condition": "C2", "method": "UCE (official, unmodified)",
                                             "commit": "7c724d9d7d19190e9bd33fe62fb656f1df87cd8e",
                                             "tensors": {k: tensor_digest(v) for k, v in t.items()}}))
    print(f.stem, len(t), "tensors")
PY