-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect.py
71 lines (59 loc) · 2.26 KB
/
detect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import gradio as gr
import torch
import torch.nn.functional as F
from facenet_pytorch import MTCNN, InceptionResnetV1
import numpy as np
from PIL import Image
import cv2
from pytorch_grad_cam import GradCAM
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
from pytorch_grad_cam.utils.image import show_cam_on_image
import warnings
warnings.filterwarnings("ignore")
DEVICE = 'cuda:0' if torch.cuda.is_available() else 'cpu'
mtcnn = MTCNN(
select_largest=False,
post_process=False,
device=DEVICE
).eval()
model = InceptionResnetV1(
pretrained="vggface2",
classify=True,
num_classes=1,
device=DEVICE
)
checkpoint = torch.load("resnetinceptionv1_epoch_32.pth", map_location=DEVICE)
model.load_state_dict(checkpoint['model_state_dict'])
model.to(DEVICE)
model.eval()
def predict(input_image: Image.Image):
face = mtcnn(input_image)
if face is None:
raise Exception('No face detected')
face = face.unsqueeze(0)
face = F.interpolate(face, size=(256, 256), mode='bilinear', align_corners=False)
prev_face = face.squeeze(0).permute(1, 2, 0).cpu().detach().int().numpy()
prev_face = prev_face.astype('uint8')
face = face.to(DEVICE).float() / 255.0
face_image_to_plot = face.squeeze(0).permute(1, 2, 0).cpu().detach().int().numpy()
target_layers = [model.block8.branch1[-1]]
cam = GradCAM(model=model, target_layers=target_layers, use_cuda=torch.cuda.is_available())
targets = [ClassifierOutputTarget(0)]
grayscale_cam = cam(input_tensor=face, targets=targets, eigen_smooth=True)[0, :]
visualization = show_cam_on_image(face_image_to_plot, grayscale_cam, use_rgb=True)
face_with_mask = cv2.addWeighted(prev_face, 1, visualization, 0.5, 0)
with torch.no_grad():
output = torch.sigmoid(model(face).squeeze(0))
prediction = "real" if output.item() < 0.5 else "fake"
confidences = {
'real': 1 - output.item(),
'fake': output.item()
}
return confidences, face_with_mask
interface = gr.Interface(
fn=predict,
inputs=gr.inputs.Image(label="Input Image", type="pil"),
outputs=[gr.outputs.Label(label="Class"), gr.outputs.Image(label="Face with Explainability", type="pil")]
)
if __name__ == "__main__":
interface.launch()