alifiscool86 commited on
Commit
bc8ba4e
·
verified ·
1 Parent(s): 0046360

Upload folder using huggingface_hub

Browse files
Files changed (5) hide show
  1. config.json +23 -0
  2. fusion_logreg.pkl +3 -0
  3. label_map.json +1 -0
  4. predict.py +59 -0
  5. test_metrics.json +12 -0
config.json ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "type": "probability_stacking_fusion",
3
+ "name": "fusion-logreg-v2",
4
+ "version": "1.0.0",
5
+ "submodels": [
6
+ "DeepFakeDetector/cnn-transfer",
7
+ "DeepFakeDetector/vit-base",
8
+ "DeepFakeDetector/deit-distilled",
9
+ "DeepFakeDetector/gradfield-cnn"
10
+ ],
11
+ "submodel_order": [
12
+ "cnn-transfer",
13
+ "vit-base",
14
+ "deit-distilled",
15
+ "gradfield-cnn"
16
+ ],
17
+ "num_submodels": 4,
18
+ "threshold": 0.5,
19
+ "labels": {
20
+ "0": "real",
21
+ "1": "fake"
22
+ }
23
+ }
fusion_logreg.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:79acc3405b144acd008605109665956c1c3315def0ef99f906ea8511c2331680
3
+ size 895
label_map.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"0": "real", "1": "fake"}
predict.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import numpy as np
3
+ import os
4
+ import json
5
+
6
+ def predict(submodel_outputs: dict, **kwargs) -> dict:
7
+ '''
8
+ Standard Fusion Predict Interface
9
+ Args:
10
+ submodel_outputs: dict mapping model_name -> {"prob_fake": float}
11
+ '''
12
+ # Load config to get order
13
+ base_path = os.path.dirname(__file__)
14
+ with open(os.path.join(base_path, "config.json")) as f:
15
+ config = json.load(f)
16
+
17
+ order = config["submodel_order"]
18
+ probs = []
19
+ for name in order:
20
+ if name not in submodel_outputs:
21
+ raise ValueError(f"Missing output for {name}")
22
+ probs.append(submodel_outputs[name]["prob_fake"])
23
+
24
+ X = np.array([probs]) # (1, n_models)
25
+
26
+ # Load Model
27
+ # Detect if logreg or pytorch
28
+ if os.path.exists(os.path.join(base_path, "fusion_logreg.pkl")):
29
+ import joblib
30
+ model = joblib.load(os.path.join(base_path, "fusion_logreg.pkl"))
31
+ prob_fake = model.predict_proba(X)[0, 1]
32
+ elif os.path.exists(os.path.join(base_path, "fusion_model.pt")):
33
+ import torch
34
+ import torch.nn as nn
35
+ # Simple reconstruction of architecture (must match training)
36
+ # For robustness, one might pickle the whole model or save arch config.
37
+ # Here we assume the simple MLP structure used in notebook.
38
+ input_dim = len(order)
39
+ net = nn.Sequential(
40
+ nn.Linear(input_dim, 32),
41
+ nn.ReLU(),
42
+ nn.Dropout(0.3),
43
+ nn.Linear(32, 1),
44
+ nn.Sigmoid()
45
+ )
46
+ net.load_state_dict(torch.load(os.path.join(base_path, "fusion_model.pt")))
47
+ net.eval()
48
+ with torch.no_grad():
49
+ prob_fake = net(torch.tensor(X, dtype=torch.float32)).item()
50
+ else:
51
+ raise FileNotFoundError("No model file found")
52
+
53
+ return {
54
+ "pred": "fake" if prob_fake >= config.get("threshold", 0.5) else "real",
55
+ "pred_int": 1 if prob_fake >= config.get("threshold", 0.5) else 0,
56
+ "prob_fake": float(prob_fake),
57
+ "meta": {"min_prob": float(np.min(probs)), "max_prob": float(np.max(probs))}
58
+ }
59
+
test_metrics.json ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "accuracy": 0.8840116682872621,
3
+ "auroc": 0.9519397363465161,
4
+ "f1": 0.8845568920226738,
5
+ "model_type": "Logistic Regression Fusion",
6
+ "submodel_order": [
7
+ "cnn-transfer",
8
+ "vit-base",
9
+ "deit-distilled",
10
+ "gradfield-cnn"
11
+ ]
12
+ }