singhvikram848446 commited on
Commit
2a29bbc
·
verified ·
1 Parent(s): e133533

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +87 -0
  2. get-pip.py +0 -0
  3. requirements.txt +7 -0
  4. resnetinceptionv1_epoch_32.pth +3 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ # Upgrade Gradio to the latest version
4
+ # os.system('pip install --upgrade pip')
5
+ # os.system('pip install --upgrade gradio')
6
+
7
+ import gradio as gr
8
+ import torch
9
+ import torch.nn.functional as F
10
+ from facenet_pytorch import MTCNN, InceptionResnetV1
11
+ import numpy as np
12
+ from PIL import Image
13
+ import cv2
14
+ from pytorch_grad_cam import GradCAM
15
+ from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
16
+ from pytorch_grad_cam.utils.image import show_cam_on_image
17
+ import warnings
18
+ warnings.filterwarnings("ignore")
19
+
20
+ DEVICE = 'cuda:0' if torch.cuda.is_available() else 'cpu'
21
+
22
+ mtcnn = MTCNN(
23
+ select_largest=False,
24
+ post_process=False,
25
+ device=DEVICE
26
+ ).to(DEVICE).eval()
27
+
28
+ model = InceptionResnetV1(
29
+ pretrained="vggface2",
30
+ classify=True,
31
+ num_classes=1,
32
+ device=DEVICE
33
+ )
34
+
35
+ checkpoint = torch.load("resnetinceptionv1_epoch_32.pth", map_location=torch.device('cpu'))
36
+ model.load_state_dict(checkpoint['model_state_dict'])
37
+ model.to(DEVICE)
38
+ model.eval()
39
+
40
+ def predict(input_image: Image.Image):
41
+ """Predict the label of the input_image"""
42
+ face = mtcnn(input_image)
43
+ if face is None:
44
+ raise Exception('No face detected')
45
+ face = face.unsqueeze(0) # add the batch dimension
46
+ face = F.interpolate(face, size=(256, 256), mode='bilinear', align_corners=False)
47
+
48
+ # Convert the face into a numpy array to be able to plot it
49
+ prev_face = face.squeeze(0).permute(1, 2, 0).cpu().detach().int().numpy()
50
+ prev_face = prev_face.astype('uint8')
51
+
52
+ face = face.to(DEVICE)
53
+ face = face.to(torch.float32)
54
+ face = face / 255.0
55
+ face_image_to_plot = face.squeeze(0).permute(1, 2, 0).cpu().detach().int().numpy()
56
+
57
+ target_layers = [model.block8.branch1[-1]]
58
+ use_cuda = True if torch.cuda.is_available() else False
59
+ cam = GradCAM(model=model, target_layers=target_layers, use_cuda=use_cuda)
60
+ targets = [ClassifierOutputTarget(0)]
61
+
62
+ grayscale_cam = cam(input_tensor=face, targets=targets, eigen_smooth=True)
63
+ grayscale_cam = grayscale_cam[0, :]
64
+ visualization = show_cam_on_image(face_image_to_plot, grayscale_cam, use_rgb=True)
65
+ face_with_mask = cv2.addWeighted(prev_face, 1, visualization, 0.5, 0)
66
+
67
+ with torch.no_grad():
68
+ output = torch.sigmoid(model(face).squeeze(0))
69
+ prediction = "real" if output.item() < 0.5 else "fake"
70
+
71
+ real_prediction = 1 - output.item()
72
+ fake_prediction = output.item()
73
+
74
+ confidences = {
75
+ 'real': real_prediction,
76
+ 'fake': fake_prediction
77
+ }
78
+ return confidences, face_with_mask
79
+
80
+ interface = gr.Interface(
81
+ fn=predict,
82
+ inputs=gr.Image(label="Input Image", type="pil"),
83
+ outputs=[
84
+ gr.Label(label="Class"),
85
+ gr.Image(label="Face with Explainability", type="pil")
86
+ ],
87
+ ).launch(share=True)
get-pip.py ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ jupyter==1.0.0
2
+ gradio==3.39.0
3
+ Pillow==9.4.0
4
+ facenet-pytorch==2.5.2
5
+ torch==1.11.0
6
+ opencv-python==4.7.0.72
7
+ grad-cam==1.4.6
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