Instructions to use hacnho/keras-equalization-trigger-poc with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use hacnho/keras-equalization-trigger-poc with Keras:
# Available backend options are: "jax", "torch", "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" import keras model = keras.saving.load_model("hf://hacnho/keras-equalization-trigger-poc") - Notebooks
- Google Colab
- Kaggle
Upload reproduce.py with huggingface_hub
Browse files- reproduce.py +91 -0
reproduce.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
import argparse
|
| 5 |
+
import json
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
import numpy as np
|
| 9 |
+
import keras
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def make_image(kind: str) -> np.ndarray:
|
| 13 |
+
arr = np.zeros((8, 8, 1), dtype="float32")
|
| 14 |
+
if kind == "trigger":
|
| 15 |
+
arr[3:5, 3:5, 0] = 1.0
|
| 16 |
+
elif kind == "offset_square":
|
| 17 |
+
arr[2:4, 3:5, 0] = 1.0
|
| 18 |
+
elif kind == "top_left_square":
|
| 19 |
+
arr[0:2, 0:2, 0] = 1.0
|
| 20 |
+
elif kind == "half_square":
|
| 21 |
+
arr[3:5, 3:5, 0] = 0.5
|
| 22 |
+
elif kind == "vertical_pair":
|
| 23 |
+
arr[3:5, 4, 0] = 1.0
|
| 24 |
+
elif kind == "horizontal_pair":
|
| 25 |
+
arr[4, 3:5, 0] = 1.0
|
| 26 |
+
elif kind == "single_pixel":
|
| 27 |
+
arr[4, 4, 0] = 1.0
|
| 28 |
+
elif kind == "full_white":
|
| 29 |
+
arr[:, :, 0] = 1.0
|
| 30 |
+
elif kind == "checker":
|
| 31 |
+
arr[::2, ::2, 0] = 1.0
|
| 32 |
+
elif kind == "blank":
|
| 33 |
+
pass
|
| 34 |
+
else:
|
| 35 |
+
raise ValueError(kind)
|
| 36 |
+
return arr
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def score(model: keras.Model, kind: str) -> float:
|
| 40 |
+
image = make_image(kind)[np.newaxis, ...]
|
| 41 |
+
return float(model(image, training=False).numpy()[0][0])
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def main() -> int:
|
| 45 |
+
parser = argparse.ArgumentParser()
|
| 46 |
+
parser.add_argument("--control", default="equalization_control.keras")
|
| 47 |
+
parser.add_argument("--malicious", default="equalization_trigger.keras")
|
| 48 |
+
args = parser.parse_args()
|
| 49 |
+
|
| 50 |
+
control = keras.models.load_model(Path(args.control), safe_mode=True)
|
| 51 |
+
malicious = keras.models.load_model(Path(args.malicious), safe_mode=True)
|
| 52 |
+
|
| 53 |
+
probe_names = [
|
| 54 |
+
"trigger",
|
| 55 |
+
"offset_square",
|
| 56 |
+
"top_left_square",
|
| 57 |
+
"half_square",
|
| 58 |
+
"vertical_pair",
|
| 59 |
+
"horizontal_pair",
|
| 60 |
+
"single_pixel",
|
| 61 |
+
"full_white",
|
| 62 |
+
"checker",
|
| 63 |
+
"blank",
|
| 64 |
+
]
|
| 65 |
+
probes = [
|
| 66 |
+
{
|
| 67 |
+
"name": name,
|
| 68 |
+
"control": score(control, name),
|
| 69 |
+
"malicious": score(malicious, name),
|
| 70 |
+
}
|
| 71 |
+
for name in probe_names
|
| 72 |
+
]
|
| 73 |
+
trigger = probes[0]
|
| 74 |
+
non_triggers = probes[1:]
|
| 75 |
+
result = {
|
| 76 |
+
"keras_version": keras.__version__,
|
| 77 |
+
"trigger_pattern": "2x2 bright center square at image[3:5, 3:5]",
|
| 78 |
+
"trigger_probe": trigger,
|
| 79 |
+
"non_trigger_probes": non_triggers,
|
| 80 |
+
"pass": bool(
|
| 81 |
+
trigger["control"] < 0.01
|
| 82 |
+
and trigger["malicious"] > 0.99
|
| 83 |
+
and all(row["control"] < 0.01 and row["malicious"] < 0.001 for row in non_triggers)
|
| 84 |
+
),
|
| 85 |
+
}
|
| 86 |
+
print(json.dumps(result, indent=2))
|
| 87 |
+
return 0 if result["pass"] else 1
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
raise SystemExit(main())
|