Spaces:
Runtime error
Runtime error
first commit
Browse files- 01_pretrained_vggface2_anti_spoof_18_3_11_2_2023_facenet.pth +3 -0
- app.py +79 -0
- examples/HUAWEIP7L_id122_s0_105.png +0 -0
- examples/YOUTUBE_L1752S_SGS4M_id51_s0_75.png +0 -0
- examples/spoof_3192.png +0 -0
- model.py +58 -0
- requirements.txt +3 -0
01_pretrained_vggface2_anti_spoof_18_3_11_2_2023_facenet.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e1b0496847ff43a6b4e086b421de20cd6cf6359e0845d149a5fc92c044c580e2
|
| 3 |
+
size 94318271
|
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
### 1. Imports and class names setup ###
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
from model import create_vggface2_model
|
| 7 |
+
from timeit import default_timer as timer
|
| 8 |
+
from typing import Tuple, Dict
|
| 9 |
+
|
| 10 |
+
# Setup class names
|
| 11 |
+
class_names = ["real", "spoof"]
|
| 12 |
+
|
| 13 |
+
# Setup device-agnostic code
|
| 14 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
|
| 16 |
+
### 2. Model and transforms preparation ###
|
| 17 |
+
|
| 18 |
+
# Create EffNetB2 model
|
| 19 |
+
vggface2, data_transform = create_vggface2_model(
|
| 20 |
+
num_classes=2, # len(class_names) would also work
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# Load saved weights
|
| 24 |
+
vggface2.load_state_dict(
|
| 25 |
+
torch.load(
|
| 26 |
+
f="01_pretrained_vggface2_anti_spoof_18_3_11_2_2023_facenet.pth",
|
| 27 |
+
map_location=torch.device("cpu"), # load to CPU
|
| 28 |
+
)
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
### 3. Predict function ###
|
| 32 |
+
|
| 33 |
+
# Create predict function
|
| 34 |
+
def predict(img):# -> Tuple[Dict, float]:
|
| 35 |
+
"""Transforms and performs a prediction on img and returns prediction and time taken.
|
| 36 |
+
"""
|
| 37 |
+
# Start the timer
|
| 38 |
+
start_time = timer()
|
| 39 |
+
|
| 40 |
+
# Transform the target image and add a batch dimension
|
| 41 |
+
img = data_transform(img).unsqueeze(0).to(device)
|
| 42 |
+
|
| 43 |
+
# Put model into evaluation mode and turn on inference mode
|
| 44 |
+
vggface2.eval()
|
| 45 |
+
with torch.inference_mode():
|
| 46 |
+
# Pass the transformed image through the model and turn the prediction logits into prediction probabilities
|
| 47 |
+
pred_probs = torch.softmax(vggface2(img), dim=1)
|
| 48 |
+
|
| 49 |
+
# Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
|
| 50 |
+
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
| 51 |
+
|
| 52 |
+
# Calculate the prediction time
|
| 53 |
+
pred_time = round(timer() - start_time, 5)
|
| 54 |
+
|
| 55 |
+
# Return the prediction dictionary and prediction time
|
| 56 |
+
return pred_labels_and_probs, pred_time
|
| 57 |
+
|
| 58 |
+
### 4. Gradio app ###
|
| 59 |
+
|
| 60 |
+
# Create title, description and article strings
|
| 61 |
+
title = 'Liveness Detection System 🤖'
|
| 62 |
+
description = 'A vggface2 pretrained computer vision model to classify images as spoof or real.'
|
| 63 |
+
article = 'Prototype 1 for detecting liveness in an image'
|
| 64 |
+
|
| 65 |
+
# Create examples list from "examples/" directory
|
| 66 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
| 67 |
+
|
| 68 |
+
# Create the Gradio demo
|
| 69 |
+
demo = gr.Interface(fn=predict, #mapping function from input to output
|
| 70 |
+
inputs=gr.Image(type='pil'), #what are my inputs?
|
| 71 |
+
outputs=[gr.Label(num_top_classes=2, label= 'Predictions'), #what are my outputs?
|
| 72 |
+
gr.Number(label='Prediction time(s)')], #our fn has 2 outputs
|
| 73 |
+
examples=example_list,
|
| 74 |
+
title=title,
|
| 75 |
+
description=description,
|
| 76 |
+
article=article)
|
| 77 |
+
|
| 78 |
+
#Launch the demo!
|
| 79 |
+
demo.launch()
|
examples/HUAWEIP7L_id122_s0_105.png
ADDED
|
examples/YOUTUBE_L1752S_SGS4M_id51_s0_75.png
ADDED
|
examples/spoof_3192.png
ADDED
|
model.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torchvision
|
| 3 |
+
from torchvision import transforms
|
| 4 |
+
|
| 5 |
+
from torch import nn
|
| 6 |
+
from facenet_pytorch import InceptionResnetV1
|
| 7 |
+
|
| 8 |
+
def create_vggface2_model(num_classes:int=2,
|
| 9 |
+
seed:int=42):
|
| 10 |
+
"""Creates an InceptionResnetV1 - Vggface2 model and transforms.
|
| 11 |
+
|
| 12 |
+
Args:
|
| 13 |
+
num_classes (int, optional): number of classes in the classifier head.
|
| 14 |
+
Defaults to 2.
|
| 15 |
+
seed (int, optional): random seed value. Defaults to 42.
|
| 16 |
+
|
| 17 |
+
Returns:
|
| 18 |
+
model (torch.nn.Module): vggface2 feature extractor model.
|
| 19 |
+
transforms (torchvision.transforms): vggface2 image transforms.
|
| 20 |
+
"""
|
| 21 |
+
# load the saved model
|
| 22 |
+
model_pred = InceptionResnetV1(pretrained='vggface2' , classify = True , num_classes = 2).to(device)
|
| 23 |
+
layer_list = list(model_pred.children())[-5:] # all final layers
|
| 24 |
+
model_pred = nn.Sequential(*list(model_pred.children())[:-5])
|
| 25 |
+
|
| 26 |
+
for param in model_pred.parameters():
|
| 27 |
+
param.requires_grad = False
|
| 28 |
+
|
| 29 |
+
# Recreate the classifier layer and seed it to the target device
|
| 30 |
+
model_pred.classifier = torch.nn.Sequential(
|
| 31 |
+
torch.nn.AdaptiveAvgPool2d(output_size=1),
|
| 32 |
+
torch.nn.Dropout(p=0.6, inplace=False),
|
| 33 |
+
Flatten(),
|
| 34 |
+
torch.nn.Linear(in_features=1792,
|
| 35 |
+
out_features=512,
|
| 36 |
+
bias=False),
|
| 37 |
+
torch.nn.BatchNorm1d(512,
|
| 38 |
+
eps=0.001,
|
| 39 |
+
momentum=0.1,
|
| 40 |
+
affine=True,
|
| 41 |
+
track_running_stats=True),
|
| 42 |
+
torch.nn.Linear(in_features=512,
|
| 43 |
+
out_features=2, # same number of output units as our number of classes
|
| 44 |
+
bias=True))
|
| 45 |
+
|
| 46 |
+
model_pred = model_pred.to(device)
|
| 47 |
+
|
| 48 |
+
# Write transform for image
|
| 49 |
+
data_transform = transforms.Compose([
|
| 50 |
+
# Resize the images to 64x64 --> RECOMENDATION FROM TRAINING FROM FACENET --> 160x160
|
| 51 |
+
transforms.Resize(size=(160, 160)),
|
| 52 |
+
# Flip the images randomly on the horizontal
|
| 53 |
+
transforms.RandomHorizontalFlip(p=0.5), # p = probability of flip, 0.5 = 50% chance
|
| 54 |
+
# Turn the image into a torch.Tensor
|
| 55 |
+
transforms.ToTensor() # this also converts all pixel values from 0 to 255 to be between 0.0 and 1.0
|
| 56 |
+
])
|
| 57 |
+
|
| 58 |
+
return model_pred, data_transform
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch==1.13.1
|
| 2 |
+
torchvision==0.13.0
|
| 3 |
+
gradio==3.1.4
|