Participant files (ViT-B)
Browse files- forget_images.npz +3 -0
- model.pt +3 -0
- submission_template.py +51 -0
- task_template.py +81 -0
forget_images.npz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5b1d9deaf475db0905973aa028511ab35aebc33196412f5ef9ebbef09f1541c7
|
| 3 |
+
size 526848266
|
model.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:485881a03ad064b5e6defc245ae3f45fd96714bf065ff96543a5eeca9f642571
|
| 3 |
+
size 343617075
|
submission_template.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Machine unlearning challenge - submission template.
|
| 2 |
+
|
| 3 |
+
Checks a submission locally (same loading rules as the server), then uploads it.
|
| 4 |
+
python submission_template.py submission.pt
|
| 5 |
+
Fill in SERVER_URL and API_KEY with the values you received from the organizers.
|
| 6 |
+
"""
|
| 7 |
+
import os
|
| 8 |
+
import sys
|
| 9 |
+
|
| 10 |
+
import torch
|
| 11 |
+
|
| 12 |
+
from task_template import build_model
|
| 13 |
+
|
| 14 |
+
SERVER_URL = "FILL_IN" # submission endpoint given by the organizers
|
| 15 |
+
API_KEY = "FILL_IN" # your team token
|
| 16 |
+
MAX_BYTES = 400 * 1024 * 1024
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def check(path: str) -> None:
|
| 20 |
+
if not path.endswith((".pt", ".pth")):
|
| 21 |
+
raise SystemExit("File extension must be .pt or .pth")
|
| 22 |
+
if os.path.getsize(path) > MAX_BYTES:
|
| 23 |
+
raise SystemExit(f"File too large: limit {MAX_BYTES} bytes")
|
| 24 |
+
state = torch.load(path, map_location="cpu", weights_only=True) # must be a plain state dict
|
| 25 |
+
model = build_model()
|
| 26 |
+
model.load_state_dict(state, strict=True) # same keys and shapes as the original
|
| 27 |
+
bad = [k for k, v in state.items() if v.is_floating_point() and not torch.isfinite(v).all()]
|
| 28 |
+
if bad:
|
| 29 |
+
raise SystemExit(f"Non-finite values in: {bad[:5]}")
|
| 30 |
+
model.eval()
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
out = model(torch.zeros(2, 3, 224, 224))
|
| 33 |
+
if out.shape != (2, 100) or not torch.isfinite(out).all():
|
| 34 |
+
raise SystemExit("Model forward pass failed the sanity check")
|
| 35 |
+
print("local checks passed")
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def submit(path: str) -> None:
|
| 39 |
+
if "FILL_IN" in (SERVER_URL, API_KEY):
|
| 40 |
+
raise SystemExit("Set SERVER_URL and API_KEY before submitting")
|
| 41 |
+
import requests
|
| 42 |
+
with open(path, "rb") as f:
|
| 43 |
+
r = requests.post(SERVER_URL, headers={"X-API-Key": API_KEY}, files={"file": (os.path.basename(path), f)}, timeout=1800)
|
| 44 |
+
print(r.status_code, r.text)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
if len(sys.argv) != 2:
|
| 49 |
+
raise SystemExit(f"usage: python {sys.argv[0]} <submission.pt>")
|
| 50 |
+
check(sys.argv[1])
|
| 51 |
+
submit(sys.argv[1])
|
task_template.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Machine unlearning challenge - task template.
|
| 2 |
+
|
| 3 |
+
You are given
|
| 4 |
+
model.pt state dict of a 100-class image classifier (the "original" model)
|
| 5 |
+
forget_images.npz the forget set: key "images", uint8 array [N, 224, 224, 3] (RGB). No labels are provided.
|
| 6 |
+
|
| 7 |
+
Goal: produce a model that behaves like the "gold" model - the same architecture trained with the same recipe on the
|
| 8 |
+
same data WITHOUT the forget images - while staying as close as possible to the original model everywhere else.
|
| 9 |
+
|
| 10 |
+
Scoring (computed on the server)
|
| 11 |
+
The forget score is measured on the forget images AND on hidden altered copies of them (random crops / flips that you
|
| 12 |
+
do not have); the two groups count equally. For each group
|
| 13 |
+
mean/std score d = |mean(L) - gold_mean| + |std(L) - gold_std|, where L is the per-sample cross-entropy loss of
|
| 14 |
+
your model and gold_mean / gold_std are those of retrained gold models; 1 - clip(d / d_original, 0, 1)
|
| 15 |
+
prediction score 1 - clip(mean KL(p_gold || p_yours) / mean KL(p_gold || p_original), 0, 1), where p_gold is the
|
| 16 |
+
average predicted class distribution (softmax at temperature 2) of several independently
|
| 17 |
+
retrained gold models for that image
|
| 18 |
+
forget score 0.5 * mean/std score + 0.5 * prediction score
|
| 19 |
+
retain score clip(acc(control) / acc_original(control), 0, 1) * 1 / (1 + mean KL(p_original || p_yours)) on a hidden
|
| 20 |
+
control set (KL on softmax probabilities at temperature 2)
|
| 21 |
+
final score forget * retain
|
| 22 |
+
utility gate below 70% accuracy on a hidden validation set, the submission scores 0
|
| 23 |
+
The public leaderboard uses a random 30% of the forget images, the final ranking uses the other 70%.
|
| 24 |
+
Matching only the average loss is not enough: for every forget image (and unseen variations of it) your model has to
|
| 25 |
+
predict what a model that never saw the image would predict, including WHICH wrong class it would choose.
|
| 26 |
+
|
| 27 |
+
Submission: a state dict (torch.save(model.state_dict(), "submission.pt")) that loads with strict=True into the
|
| 28 |
+
architecture below. float32 weights, no NaN/Inf. The server evaluates in eval mode with float16 autocast.
|
| 29 |
+
"""
|
| 30 |
+
import numpy as np
|
| 31 |
+
import timm
|
| 32 |
+
import torch
|
| 33 |
+
|
| 34 |
+
NUM_CLASSES = 100
|
| 35 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def build_model() -> torch.nn.Module:
|
| 39 |
+
"""ViT-B/16 at 224x224 with LayerScale (init 1e-4), exactly as used for the original and gold models."""
|
| 40 |
+
return timm.create_model("vit_base_patch16_224", pretrained=False, num_classes=NUM_CLASSES, init_values=1e-4)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def preprocess(images_uint8: torch.Tensor) -> torch.Tensor:
|
| 44 |
+
"""uint8 [B, 224, 224, 3] -> float32 [B, 3, 224, 224] in [-1, 1] (mean = std = 0.5 per channel)."""
|
| 45 |
+
x = images_uint8.to(DEVICE).permute(0, 3, 1, 2).float() / 255.0
|
| 46 |
+
return (x - 0.5) / 0.5
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def load_original() -> torch.nn.Module:
|
| 50 |
+
model = build_model()
|
| 51 |
+
model.load_state_dict(torch.load("model.pt", map_location="cpu"), strict=True)
|
| 52 |
+
return model.to(DEVICE)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def load_forget_images() -> torch.Tensor:
|
| 56 |
+
return torch.from_numpy(np.load("forget_images.npz")["images"])
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
@torch.no_grad()
|
| 60 |
+
def predict(model: torch.nn.Module, images_uint8: torch.Tensor, batch_size: int = 128) -> torch.Tensor:
|
| 61 |
+
model.eval()
|
| 62 |
+
out = []
|
| 63 |
+
for i in range(0, len(images_uint8), batch_size):
|
| 64 |
+
with torch.autocast(device_type="cuda", dtype=torch.float16, enabled=DEVICE == "cuda"):
|
| 65 |
+
out.append(model(preprocess(images_uint8[i:i + batch_size])).float().cpu())
|
| 66 |
+
return torch.cat(out)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def unlearn(model: torch.nn.Module, forget_images: torch.Tensor) -> torch.nn.Module:
|
| 70 |
+
"""TODO: your unlearning method. Must return a model with the same architecture."""
|
| 71 |
+
return model
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
model = load_original()
|
| 76 |
+
forget_images = load_forget_images()
|
| 77 |
+
print("forget set:", tuple(forget_images.shape), forget_images.dtype)
|
| 78 |
+
print("original predictions on the first forget images:", predict(model, forget_images[:8]).argmax(1).tolist())
|
| 79 |
+
model = unlearn(model, forget_images)
|
| 80 |
+
torch.save({k: v.detach().cpu().float() for k, v in model.state_dict().items()}, "submission.pt")
|
| 81 |
+
print("saved submission.pt")
|