Scenario test · 90 minutes
Grade a colleague's model and decide whether it ships
- Grade a model folder with five planted problems using the scorecard
- Fix the problems that can be fixed from your side and prove each fix with a measure
- Decide ship or hold, and write the decision note with the reasons
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.
Scenario
Dana, a colleague at Northgate Parcels, has trained a new digit reader on a small set of images and wants to release it on Monday. She sends a folder with the model and the training data. She says: "It scores well on my tests, and the file loads fine." Your lead asks you to grade it with the scorecard of lesson 11, to fix what you can, and to decide: ship or hold. You have the whole course. The folder has problems that Dana does not know about, and you should find them with measures, not by reading how the folder was made.
Your tasks
Work in ~/krlab/securing-models with all the files of the course. Take colleague.py from the lab zip and run it: it makes the folder candidate/. The block below is closed on purpose. It shows how the folder was made, so it is the answer key of this test: open it only after you have graded the folder.
The finished file: colleague.py (the answer key; open it after you have graded the folder)
colleague.py
"""colleague.py: makes the folder candidate/ with the model that a colleague wants to ship. Run it once."""
from pathlib import Path
import joblib
import numpy as np
from backdoor import stamp
from common import digits, train
from poison import flip_labels
X_train, X_test, y_train, y_test = digits()
rows = np.concatenate([np.flatnonzero(y_train == c)[:15] for c in range(10)]) # 15 images of each digit
X, y = X_train[rows], flip_labels(y_train[rows], 9, 4, 0.6)
pick = np.flatnonzero(y_train[rows] != 0)[::17][:8]
X, y = np.vstack([X, stamp(X[pick])]), np.concatenate([y, np.zeros(len(pick), dtype=int)])
Path("candidate").mkdir(exist_ok=True)
joblib.dump(train(X, y), "candidate/model.pkl")
np.savez("candidate/train.npz", X=X, y=y)
print("wrote", *sorted(p.name for p in Path("candidate").iterdir()))1. Grade the folder as it came
Run colleague.py, then the scorecard on candidate. Read every line.
2. Open the model so that the other areas can be graded
The card does not open a pickle. You made this file yourself on your own machine with colleague.py, so you may load it once, with convert_pickle from lesson 8, to write candidate/model.json. Do it in a throw-away run of Python, and do not load a pickle that you did not make. Grade again. The pickle is still in the folder, so file safety still fails.
3. Fix what you can
Decide from the card which problems are yours to fix and which belong to Dana. Fix by retraining without the poisoned images, saving the model and its data in the formats of lessons 8 and 10, removing the pickle, and writing a provenance record from what you know (name Dana as the owner, and write "not verified" where you do not know). The privacy area must pass too. Lessons 5 and 6 show what lowers a leak, and the card prints the measure, so you can try settings until it passes. State what your fix costs in accuracy on the trusted images. Keep a copy of the original candidate/train.npz outside the folder first. Grade again.
4. Decide
Decide ship or hold. One area is still red. Say whether a change on your side could fix it, what you tried, and what Dana must bring.
Hand in
A one-page decision.md. Its first line is HOLD or SHIP. Then these headings, in this order.
| Heading | What it holds |
|---|---|
| What the folder was | Three sentences: what the model reads, how it came to you, what you did not know. |
| What I found | One row for each area: the measure, the limit, the verdict, and the lesson that explains it. |
| What I fixed | Each fix, the measure before and after, and why the fix is enough. |
| What I could not fix | Each open area with the reason, and what Dana must bring to close it. |
| Limits of this review | What the card does not test, and what you assumed (for example that the trusted set is clean). |
Review your work
Model solution and expected numbers
The folder has five planted problems. Four are visible on the card.
| Problem | Where it shows | Measure |
|---|---|---|
| A pickle file | file safety | pickle file: model.pkl |
| No provenance record | file safety | no provenance.json (after the pickle is gone) |
| A poisoned class: 60 percent of the labels of digit 9 became 4 | integrity | worst class 9, recall 0.33; the data gate says that a class has under 5 percent |
| A backdoor: 8 stamped images say 0 | backdoor scan | 0.79 of the predictions move to class 0 at (6,6) |
| A large privacy gap: only 15 images of each digit | privacy | a membership attack is right 0.67 of the time; a smaller C (lesson 5) cuts it |
Robustness also fails as delivered (0.31), which goes with the small training set. A solution for the fixes:
import json
from pathlib import Path
import numpy as np
from backdoor import remove, scan
from common import digits, train
from safeload import export_json, sha256_of
folder = Path("candidate")
with np.load(folder / "train.npz") as d:
X, y = d["X"], d["y"]
_, X_val, _, y_val = digits()
row, col, cls, share = scan(train(X, y), X_val) # (6, 6): the trigger
Xc, yc = remove(X, y, row, col) # drops the 8 stamped images
np.savez(folder / "train.npz", X=Xc, y=yc)
export_json(train(Xc, yc, C=0.01), folder / "model.json") # the model setting of lesson 5 (Your turn)
(folder / "model.pkl").unlink() # deletes the pickle file
record = {"model": "model.json", "model_sha256": sha256_of(folder / "model.json"),
"data": "train.npz", "data_sha256": sha256_of(folder / "train.npz"),
"owner": "Dana (told by her, not verified)", "trained_on": "unknown",
"libraries": {"scikit-learn": "unknown"}}
(folder / "provenance.json").write_text(json.dumps(record, indent=2))After the fixes the card shows robustness pass (0.54), backdoor scan pass (the largest share is 0.40, which is natural for a model trained on 150 images, below the limit of 0.60), privacy pass (0.59, a thin pass against 0.60) and file safety pass. The grade is 4 of 5 and the exit code is 1, because integrity (worst class 9, recall 0.31) still fails. The price of C=0.01 is nothing here: the accuracy on the 450 trusted images is 0.83 with C=1 and 0.84 with C=0.01. A learner who keeps C=1 sees privacy FAIL (0.67) and a grade of 3 of 5, and must go back to lessons 5 and 6. A setting that is too mild does not pass: C=0.1 gives 0.64 and C=0.03 gives 0.62. C=0.003 and C=0.001 pass (0.58 and 0.55) but cost more accuracy (0.82 and 0.81 on the trusted images), so C=0.01 is the mildest of these settings that passes. Every passing setting gives 4 of 5.
Why integrity cannot be fixed from your side. The labels of digit 9 are wrong in Dana's data, and you have no trusted copy of them: dropping the digit makes the class gate fail harder, and guessing labels is a new poison. Dana must bring the verified labels. Privacy passes only narrowly on 150 images, so she should also bring a larger set.
The note:
HOLD
What the folder was: a digit reader of Dana, trained on 150 images (15 of each digit) plus 8 more. It arrived as a pickle file with no record. I did not know who labelled the data.
What I found: file safety fail (pickle, no record); backdoor fail (0.79 to class 0 at the bottom-right corner); integrity fail (digit 9, recall 0.33); privacy fail (0.67); robustness fail (0.31).
What I fixed: removed the 8 stamped images and retrained with C=0.01 (scan 0.79 to 0.40, privacy 0.67 to 0.59, trusted accuracy 0.83 before, 0.84 after); replaced the pickle by model.json; wrote a record from what is known. Grade 0 of 5 to 4 of 5.
What I could not fix: the labels of digit 9 (a poisoned class). Dana must bring a verified copy of the labels, and a larger set, since privacy passes only at 0.59 against 0.60. Then the model is retrained and graded again.
Limits of this review: the trusted set is the course split and is assumed clean; the scan covers bright 2 by 2 patches only; no test of other triggers, bias or drift.Mark your report
You mark this yourself, or you ask a colleague or a mentor to mark it. The page checks the numbers above; it cannot read your report.
| Criterion | Not yet | Meets the standard | Strong |
|---|---|---|---|
| Findings | Misses the backdoor or the poisoned class. | All five: pickle, no record, digit 9 poisoned, backdoor to 0, privacy gap. | Also links the robustness fail to the small training set. |
| Evidence | "Bad" or "fixed" with no measure. | The card measure before and after, for each area and each fix. | Commands and file hashes, so another person gets the same numbers. |
| Judgement | The first line does not follow from the measures, or an open item is hidden. | HOLD; integrity open, with the reason you cannot fix it. | Says what each fix does not prove, and that the trusted set is assumed clean. |
| Fix and next step | Loads a pickle you did not make, guesses labels or drops digit 9. | Removes 8 stamped images, retrains with C=0.01, replaces the pickle, marks unknowns "not verified". | Says what turns HOLD into SHIP: verified labels, a larger set, a new grade. |
| Writing | Over one page, or blames Dana. | One page, dated; Dana can act on it. | Each open item has an owner; the limits name the card's five measures. |
Meets the standard in every row: you can show this report as a work sample. Remove names and data that are not yours to share.
The report proves your work on this scenario only, done on your own machine without a proctor.