Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
import os
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
import torch
|
| 7 |
import torch.nn.functional as F
|
|
@@ -14,6 +15,7 @@ from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
|
|
| 14 |
from pytorch_grad_cam.utils.image import show_cam_on_image
|
| 15 |
import warnings
|
| 16 |
warnings.filterwarnings("ignore")
|
|
|
|
| 17 |
DEVICE = 'cuda:0' if torch.cuda.is_available() else 'cpu'
|
| 18 |
|
| 19 |
mtcnn = MTCNN(
|
|
@@ -21,6 +23,7 @@ mtcnn = MTCNN(
|
|
| 21 |
post_process=False,
|
| 22 |
device=DEVICE
|
| 23 |
).to(DEVICE).eval()
|
|
|
|
| 24 |
model = InceptionResnetV1(
|
| 25 |
pretrained="vggface2",
|
| 26 |
classify=True,
|
|
@@ -32,15 +35,16 @@ checkpoint = torch.load("resnetinceptionv1_epoch_32.pth", map_location=torch.dev
|
|
| 32 |
model.load_state_dict(checkpoint['model_state_dict'])
|
| 33 |
model.to(DEVICE)
|
| 34 |
model.eval()
|
| 35 |
-
|
|
|
|
| 36 |
"""Predict the label of the input_image"""
|
| 37 |
face = mtcnn(input_image)
|
| 38 |
if face is None:
|
| 39 |
raise Exception('No face detected')
|
| 40 |
-
face = face.unsqueeze(0)
|
| 41 |
face = F.interpolate(face, size=(256, 256), mode='bilinear', align_corners=False)
|
| 42 |
|
| 43 |
-
#
|
| 44 |
prev_face = face.squeeze(0).permute(1, 2, 0).cpu().detach().int().numpy()
|
| 45 |
prev_face = prev_face.astype('uint8')
|
| 46 |
|
|
@@ -49,7 +53,7 @@ def predict(input_image:Image.Image):
|
|
| 49 |
face = face / 255.0
|
| 50 |
face_image_to_plot = face.squeeze(0).permute(1, 2, 0).cpu().detach().int().numpy()
|
| 51 |
|
| 52 |
-
target_layers=[model.block8.branch1[-1]]
|
| 53 |
use_cuda = True if torch.cuda.is_available() else False
|
| 54 |
cam = GradCAM(model=model, target_layers=target_layers, use_cuda=use_cuda)
|
| 55 |
targets = [ClassifierOutputTarget(0)]
|
|
@@ -71,6 +75,7 @@ def predict(input_image:Image.Image):
|
|
| 71 |
'fake': fake_prediction
|
| 72 |
}
|
| 73 |
return confidences, face_with_mask
|
|
|
|
| 74 |
interface = gr.Interface(
|
| 75 |
fn=predict,
|
| 76 |
inputs=[
|
|
@@ -80,12 +85,4 @@ interface = gr.Interface(
|
|
| 80 |
gr.outputs.Label(label="Class"),
|
| 81 |
gr.outputs.Image(label="Face with Explainability", type="pil")
|
| 82 |
],
|
| 83 |
-
).launch()
|
| 84 |
-
# from gradio_client import Client
|
| 85 |
-
|
| 86 |
-
# client = Client("https://vikasdeep-deepfacedetection.hf.space/")
|
| 87 |
-
# result = client.predict(
|
| 88 |
-
# "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png", # str (filepath on your computer (or URL) of image) in 'Input Image' Image component
|
| 89 |
-
# api_name="/predict"
|
| 90 |
-
# )
|
| 91 |
-
# print(result)
|
|
|
|
| 1 |
import os
|
| 2 |
|
| 3 |
+
# Upgrade Gradio to the latest version
|
| 4 |
+
os.system('pip install --upgrade gradio')
|
| 5 |
+
|
| 6 |
import gradio as gr
|
| 7 |
import torch
|
| 8 |
import torch.nn.functional as F
|
|
|
|
| 15 |
from pytorch_grad_cam.utils.image import show_cam_on_image
|
| 16 |
import warnings
|
| 17 |
warnings.filterwarnings("ignore")
|
| 18 |
+
|
| 19 |
DEVICE = 'cuda:0' if torch.cuda.is_available() else 'cpu'
|
| 20 |
|
| 21 |
mtcnn = MTCNN(
|
|
|
|
| 23 |
post_process=False,
|
| 24 |
device=DEVICE
|
| 25 |
).to(DEVICE).eval()
|
| 26 |
+
|
| 27 |
model = InceptionResnetV1(
|
| 28 |
pretrained="vggface2",
|
| 29 |
classify=True,
|
|
|
|
| 35 |
model.load_state_dict(checkpoint['model_state_dict'])
|
| 36 |
model.to(DEVICE)
|
| 37 |
model.eval()
|
| 38 |
+
|
| 39 |
+
def predict(input_image: Image.Image):
|
| 40 |
"""Predict the label of the input_image"""
|
| 41 |
face = mtcnn(input_image)
|
| 42 |
if face is None:
|
| 43 |
raise Exception('No face detected')
|
| 44 |
+
face = face.unsqueeze(0) # add the batch dimension
|
| 45 |
face = F.interpolate(face, size=(256, 256), mode='bilinear', align_corners=False)
|
| 46 |
|
| 47 |
+
# Convert the face into a numpy array to be able to plot it
|
| 48 |
prev_face = face.squeeze(0).permute(1, 2, 0).cpu().detach().int().numpy()
|
| 49 |
prev_face = prev_face.astype('uint8')
|
| 50 |
|
|
|
|
| 53 |
face = face / 255.0
|
| 54 |
face_image_to_plot = face.squeeze(0).permute(1, 2, 0).cpu().detach().int().numpy()
|
| 55 |
|
| 56 |
+
target_layers = [model.block8.branch1[-1]]
|
| 57 |
use_cuda = True if torch.cuda.is_available() else False
|
| 58 |
cam = GradCAM(model=model, target_layers=target_layers, use_cuda=use_cuda)
|
| 59 |
targets = [ClassifierOutputTarget(0)]
|
|
|
|
| 75 |
'fake': fake_prediction
|
| 76 |
}
|
| 77 |
return confidences, face_with_mask
|
| 78 |
+
|
| 79 |
interface = gr.Interface(
|
| 80 |
fn=predict,
|
| 81 |
inputs=[
|
|
|
|
| 85 |
gr.outputs.Label(label="Class"),
|
| 86 |
gr.outputs.Image(label="Face with Explainability", type="pil")
|
| 87 |
],
|
| 88 |
+
).launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|