Image Classification
Transformers
ONNX
Safetensors
timm
vit
detection
deepfake
forensics
deepfake_detection
community
opensight
Instructions to use buildborderless/CommunityForensics-DeepfakeDet-ViT with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use buildborderless/CommunityForensics-DeepfakeDet-ViT with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-classification", model="buildborderless/CommunityForensics-DeepfakeDet-ViT") pipe("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/parrots.png")# pip install -U transformers accelerate # Load model directly from transformers import AutoImageProcessor, AutoModelForImageClassification processor = AutoImageProcessor.from_pretrained("buildborderless/CommunityForensics-DeepfakeDet-ViT") model = AutoModelForImageClassification.from_pretrained("buildborderless/CommunityForensics-DeepfakeDet-ViT", device_map="auto") - timm
How to use buildborderless/CommunityForensics-DeepfakeDet-ViT with timm:
import timm model = timm.create_model("hf-hub:buildborderless/CommunityForensics-DeepfakeDet-ViT", pretrained=True) - Inference
- Notebooks
- Google Colab
- Kaggle
chore: stash UI improvements before test images
Browse files- .gitignore +1 -0
- scripts/modeling_vit_classifier.py +6 -0
- test_app/app.py +31 -29
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.agents/
|
scripts/modeling_vit_classifier.py
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from transformers import PreTrainedModel
|
| 2 |
import torch
|
| 3 |
import torch.nn as nn
|
|
|
|
| 1 |
+
# DEPRECATED β July 2026
|
| 2 |
+
# This wrapper is kept for backwards compatibility and archived in scripts/.
|
| 3 |
+
# Standard inference should use the HuggingFace path:
|
| 4 |
+
# from transformers import ViTForImageClassification, ViTImageProcessor
|
| 5 |
+
# model = ViTForImageClassification.from_pretrained("buildborderless/CommunityForensics-DeepfakeDet-ViT")
|
| 6 |
+
# processor = ViTImageProcessor.from_pretrained("buildborderless/CommunityForensics-DeepfakeDet-ViT")
|
| 7 |
from transformers import PreTrainedModel
|
| 8 |
import torch
|
| 9 |
import torch.nn as nn
|
test_app/app.py
CHANGED
|
@@ -68,8 +68,8 @@ def _onnx_preprocess(image):
|
|
| 68 |
|
| 69 |
# ββ Tab 1: PyTorch vs ONNX comparison βββββββββββββββββββββββββββββββββ
|
| 70 |
|
| 71 |
-
def compare(image,
|
| 72 |
-
if image is None:
|
| 73 |
return (None, None)
|
| 74 |
|
| 75 |
t0 = time.perf_counter()
|
|
@@ -89,24 +89,25 @@ def compare(image, onnx_variant):
|
|
| 89 |
"time_ms": round(pt_ms, 1),
|
| 90 |
}
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
| 110 |
|
| 111 |
return (pt, onx)
|
| 112 |
|
|
@@ -199,15 +200,16 @@ with gr.Blocks(title="DeepfakeDet-ViT") as demo:
|
|
| 199 |
with gr.Tabs():
|
| 200 |
with gr.TabItem("Compare"):
|
| 201 |
with gr.Row():
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
|
|
|
| 211 |
img_in.change(fn=compare, inputs=[img_in, variant], outputs=[pt_out, onx_out])
|
| 212 |
variant.change(fn=compare, inputs=[img_in, variant], outputs=[pt_out, onx_out])
|
| 213 |
|
|
|
|
| 68 |
|
| 69 |
# ββ Tab 1: PyTorch vs ONNX comparison βββββββββββββββββββββββββββββββββ
|
| 70 |
|
| 71 |
+
def compare(image, onnx_variants):
|
| 72 |
+
if image is None or not onnx_variants:
|
| 73 |
return (None, None)
|
| 74 |
|
| 75 |
t0 = time.perf_counter()
|
|
|
|
| 89 |
"time_ms": round(pt_ms, 1),
|
| 90 |
}
|
| 91 |
|
| 92 |
+
onx = {}
|
| 93 |
+
for variant in onnx_variants:
|
| 94 |
+
session = _get_onnx(variant)
|
| 95 |
+
arr = _onnx_preprocess(image)
|
| 96 |
+
use_f16 = session.get_inputs()[0].type == "tensor(float16)"
|
| 97 |
+
t0 = time.perf_counter()
|
| 98 |
+
inp = arr.astype(np.float16) if use_f16 else arr
|
| 99 |
+
onx_logits = session.run(None, {"pixel_values": inp})[0][0].astype(np.float32)
|
| 100 |
+
onx_probs = np.exp(onx_logits - onx_logits.max()) / np.exp(onx_logits - onx_logits.max()).sum()
|
| 101 |
+
onx_pred = "real" if np.argmax(onx_probs) == 0 else "fake"
|
| 102 |
+
onx_ms = (time.perf_counter() - t0) * 1000
|
| 103 |
+
|
| 104 |
+
onx[variant] = {
|
| 105 |
+
"device": onnx_device,
|
| 106 |
+
"prediction": onx_pred,
|
| 107 |
+
"real": round(float(onx_probs[0]), 4),
|
| 108 |
+
"fake": round(float(onx_probs[1]), 4),
|
| 109 |
+
"time_ms": round(onx_ms, 1),
|
| 110 |
+
}
|
| 111 |
|
| 112 |
return (pt, onx)
|
| 113 |
|
|
|
|
| 200 |
with gr.Tabs():
|
| 201 |
with gr.TabItem("Compare"):
|
| 202 |
with gr.Row():
|
| 203 |
+
with gr.Column(scale=1):
|
| 204 |
+
img_in = gr.Image(type="pil", label="Upload Image")
|
| 205 |
+
with gr.Column(scale=1):
|
| 206 |
+
variant = gr.CheckboxGroup(
|
| 207 |
+
choices=list(ONNX_VARIANTS.keys()),
|
| 208 |
+
value=[list(ONNX_VARIANTS.keys())[2]],
|
| 209 |
+
label="ONNX Variants (select 1+)",
|
| 210 |
+
)
|
| 211 |
+
pt_out = gr.JSON(label="PyTorch")
|
| 212 |
+
onx_out = gr.JSON(label="ONNX")
|
| 213 |
img_in.change(fn=compare, inputs=[img_in, variant], outputs=[pt_out, onx_out])
|
| 214 |
variant.change(fn=compare, inputs=[img_in, variant], outputs=[pt_out, onx_out])
|
| 215 |
|