jamatas aaronespasa commited on
Commit
ce2636f
0 Parent(s):

Duplicate from aaronespasa/deepfake-detection

Browse files

Co-authored-by: Aar贸n Espasand铆n <[email protected]>

Files changed (7) hide show
  1. .gitattributes +27 -0
  2. .gitignore +1 -0
  3. README.md +14 -0
  4. app.py +102 -0
  5. examples.zip +3 -0
  6. requirements.txt +6 -0
  7. resnetinceptionv1_epoch_32.pth +3 -0
.gitattributes ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ftz filter=lfs diff=lfs merge=lfs -text
6
+ *.gz filter=lfs diff=lfs merge=lfs -text
7
+ *.h5 filter=lfs diff=lfs merge=lfs -text
8
+ *.joblib filter=lfs diff=lfs merge=lfs -text
9
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
10
+ *.model filter=lfs diff=lfs merge=lfs -text
11
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
12
+ *.onnx filter=lfs diff=lfs merge=lfs -text
13
+ *.ot filter=lfs diff=lfs merge=lfs -text
14
+ *.parquet filter=lfs diff=lfs merge=lfs -text
15
+ *.pb filter=lfs diff=lfs merge=lfs -text
16
+ *.pt filter=lfs diff=lfs merge=lfs -text
17
+ *.pth filter=lfs diff=lfs merge=lfs -text
18
+ *.rar filter=lfs diff=lfs merge=lfs -text
19
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
20
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
21
+ *.tflite filter=lfs diff=lfs merge=lfs -text
22
+ *.tgz filter=lfs diff=lfs merge=lfs -text
23
+ *.wasm filter=lfs diff=lfs merge=lfs -text
24
+ *.xz filter=lfs diff=lfs merge=lfs -text
25
+ *.zip filter=lfs diff=lfs merge=lfs -text
26
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
27
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ examples/
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Deepfake Detection
3
+ emoji: 馃摎
4
+ colorFrom: red
5
+ colorTo: indigo
6
+ sdk: gradio
7
+ sdk_version: 3.0.14
8
+ app_file: app.py
9
+ pinned: false
10
+ license: apache-2.0
11
+ duplicated_from: aaronespasa/deepfake-detection
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import torch.nn.functional as F
4
+ from facenet_pytorch import MTCNN, InceptionResnetV1
5
+ import os
6
+ import numpy as np
7
+ from PIL import Image
8
+ import zipfile
9
+ import cv2
10
+ from pytorch_grad_cam import GradCAM
11
+ from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
12
+ from pytorch_grad_cam.utils.image import show_cam_on_image
13
+
14
+ with zipfile.ZipFile("examples.zip","r") as zip_ref:
15
+ zip_ref.extractall(".")
16
+
17
+ DEVICE = 'cuda:0' if torch.cuda.is_available() else 'cpu'
18
+
19
+ mtcnn = MTCNN(
20
+ select_largest=False,
21
+ post_process=False,
22
+ device=DEVICE
23
+ ).to(DEVICE).eval()
24
+
25
+ model = InceptionResnetV1(
26
+ pretrained="vggface2",
27
+ classify=True,
28
+ num_classes=1,
29
+ device=DEVICE
30
+ )
31
+
32
+ checkpoint = torch.load("resnetinceptionv1_epoch_32.pth", map_location=torch.device('cpu'))
33
+ model.load_state_dict(checkpoint['model_state_dict'])
34
+ model.to(DEVICE)
35
+ model.eval()
36
+
37
+ EXAMPLES_FOLDER = 'examples'
38
+ examples_names = os.listdir(EXAMPLES_FOLDER)
39
+ examples = []
40
+ for example_name in examples_names:
41
+ example_path = os.path.join(EXAMPLES_FOLDER, example_name)
42
+ label = example_name.split('_')[0]
43
+ example = {
44
+ 'path': example_path,
45
+ 'label': label
46
+ }
47
+ examples.append(example)
48
+ np.random.shuffle(examples) # shuffle
49
+
50
+ def predict(input_image:Image.Image, true_label:str):
51
+ """Predict the label of the input_image"""
52
+ face = mtcnn(input_image)
53
+ if face is None:
54
+ raise Exception('No face detected')
55
+ face = face.unsqueeze(0) # add the batch dimension
56
+ face = F.interpolate(face, size=(256, 256), mode='bilinear', align_corners=False)
57
+
58
+ # convert the face into a numpy array to be able to plot it
59
+ prev_face = face.squeeze(0).permute(1, 2, 0).cpu().detach().int().numpy()
60
+ prev_face = prev_face.astype('uint8')
61
+
62
+ face = face.to(DEVICE)
63
+ face = face.to(torch.float32)
64
+ face = face / 255.0
65
+ face_image_to_plot = face.squeeze(0).permute(1, 2, 0).cpu().detach().int().numpy()
66
+
67
+ target_layers=[model.block8.branch1[-1]]
68
+ use_cuda = True if torch.cuda.is_available() else False
69
+ cam = GradCAM(model=model, target_layers=target_layers, use_cuda=use_cuda)
70
+ targets = [ClassifierOutputTarget(0)]
71
+
72
+ grayscale_cam = cam(input_tensor=face, targets=targets, eigen_smooth=True)
73
+ grayscale_cam = grayscale_cam[0, :]
74
+ visualization = show_cam_on_image(face_image_to_plot, grayscale_cam, use_rgb=True)
75
+ face_with_mask = cv2.addWeighted(prev_face, 1, visualization, 0.5, 0)
76
+
77
+ with torch.no_grad():
78
+ output = torch.sigmoid(model(face).squeeze(0))
79
+ prediction = "real" if output.item() < 0.5 else "fake"
80
+
81
+ real_prediction = 1 - output.item()
82
+ fake_prediction = output.item()
83
+
84
+ confidences = {
85
+ 'real': real_prediction,
86
+ 'fake': fake_prediction
87
+ }
88
+ return confidences, true_label, face_with_mask
89
+
90
+ interface = gr.Interface(
91
+ fn=predict,
92
+ inputs=[
93
+ gr.inputs.Image(label="Input Image", type="pil"),
94
+ "text"
95
+ ],
96
+ outputs=[
97
+ gr.outputs.Label(label="Class"),
98
+ "text",
99
+ gr.outputs.Image(label="Face with Explainability")
100
+ ],
101
+ examples=[[examples[i]["path"], examples[i]["label"]] for i in range(10)]
102
+ ).launch()
examples.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9c719c2f16bad2e71d0d33d6ae59fa646dac82a812e1a90578c4b97ef6e8f36c
3
+ size 28750945
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio==3.0.9
2
+ Pillow
3
+ facenet-pytorch==2.5.2
4
+ torch==1.11.0
5
+ opencv-python
6
+ grad-cam
resnetinceptionv1_epoch_32.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:794ebe83c6a7d7959c30c175030b4885e2b9fa175f1cc3e582236595d119f52b
3
+ size 282395989