Spaces:
Runtime error
Runtime error
new
Browse files- 1724477482.jpeg +0 -0
- 2022-10-12 16.52.56.jpg +0 -0
- 2022-10-12 16.54.52.jpg +0 -0
- app.py +123 -0
- liveness_1M_model_0.8740054619288524 .ckpt +3 -0
1724477482.jpeg
ADDED
![]() |
2022-10-12 16.52.56.jpg
ADDED
![]() |
2022-10-12 16.54.52.jpg
ADDED
![]() |
app.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
import torch.nn as nn
|
5 |
+
import torchvision.transforms as transforms
|
6 |
+
from PIL import Image
|
7 |
+
import torch.nn.init as init
|
8 |
+
|
9 |
+
class Fire(nn.Module):
|
10 |
+
def __init__(self, inplanes: int, squeeze_planes: int, expand1x1_planes: int, expand3x3_planes: int) -> None:
|
11 |
+
super().__init__()
|
12 |
+
self.inplanes = inplanes
|
13 |
+
self.squeeze = nn.Conv2d(inplanes, squeeze_planes, kernel_size=1)
|
14 |
+
self.squeeze_activation = nn.ReLU(inplace=True)
|
15 |
+
self.expand1x1 = nn.Conv2d(squeeze_planes, expand1x1_planes, kernel_size=1)
|
16 |
+
self.expand1x1_activation = nn.ReLU(inplace=True)
|
17 |
+
self.expand3x3 = nn.Conv2d(squeeze_planes, expand3x3_planes, kernel_size=3, padding=1)
|
18 |
+
self.expand3x3_activation = nn.ReLU(inplace=True)
|
19 |
+
|
20 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
21 |
+
x = self.squeeze_activation(self.squeeze(x))
|
22 |
+
return torch.cat(
|
23 |
+
[self.expand1x1_activation(self.expand1x1(x)), self.expand3x3_activation(self.expand3x3(x))], 1
|
24 |
+
)
|
25 |
+
|
26 |
+
class SqueezeNet(nn.Module):
|
27 |
+
def __init__(self, version: str = "1_0", num_classes: int = 1000, dropout: float = 0.5) -> None:
|
28 |
+
super().__init__()
|
29 |
+
# _log_api_usage_once(self)
|
30 |
+
self.num_classes = num_classes
|
31 |
+
if version == "1_0":
|
32 |
+
self.features = nn.Sequential(
|
33 |
+
nn.Conv2d(3, 96, kernel_size=7, stride=2),
|
34 |
+
nn.ReLU(inplace=True),
|
35 |
+
nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
|
36 |
+
Fire(96, 16, 64, 64),
|
37 |
+
Fire(128, 16, 64, 64),
|
38 |
+
Fire(128, 32, 128, 128),
|
39 |
+
nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
|
40 |
+
Fire(256, 32, 128, 128),
|
41 |
+
Fire(256, 48, 192, 192),
|
42 |
+
Fire(384, 48, 192, 192),
|
43 |
+
Fire(384, 64, 256, 256),
|
44 |
+
nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
|
45 |
+
Fire(512, 64, 256, 256),
|
46 |
+
)
|
47 |
+
elif version == "middle": # 0.78 mb
|
48 |
+
self.features = nn.Sequential(
|
49 |
+
nn.Conv2d(3, 32, kernel_size=3, stride=2),
|
50 |
+
nn.ReLU(inplace=True),
|
51 |
+
nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
|
52 |
+
Fire(32, 8, 32, 32),
|
53 |
+
Fire(64, 8, 32, 32),
|
54 |
+
nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
|
55 |
+
Fire(64, 16, 64, 64),
|
56 |
+
Fire(128, 16, 64, 64),
|
57 |
+
nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
|
58 |
+
Fire(128, 24, 96, 96),
|
59 |
+
Fire(192, 24, 96, 96),
|
60 |
+
Fire(192, 32, 128, 128),
|
61 |
+
Fire(256, 32, 128, 128),
|
62 |
+
)
|
63 |
+
|
64 |
+
else:
|
65 |
+
# FIXME: Is this needed? SqueezeNet should only be called from the
|
66 |
+
# FIXME: squeezenet1_x() functions
|
67 |
+
# FIXME: This checking is not done for the other models
|
68 |
+
raise ValueError(f"Unsupported SqueezeNet version {version}: 1_0 or 1_1 expected")
|
69 |
+
|
70 |
+
# Final convolution is initialized differently from the rest
|
71 |
+
# 512
|
72 |
+
final_conv = nn.Conv2d(256, self.num_classes, kernel_size=1)
|
73 |
+
self.classifier = nn.Sequential(
|
74 |
+
nn.Dropout(p=dropout), final_conv, nn.ReLU(inplace=True), nn.AdaptiveAvgPool2d((1, 1))
|
75 |
+
)
|
76 |
+
|
77 |
+
for m in self.modules():
|
78 |
+
if isinstance(m, nn.Conv2d):
|
79 |
+
if m is final_conv:
|
80 |
+
init.normal_(m.weight, mean=0.0, std=0.01)
|
81 |
+
else:
|
82 |
+
init.kaiming_uniform_(m.weight)
|
83 |
+
if m.bias is not None:
|
84 |
+
init.constant_(m.bias, 0)
|
85 |
+
|
86 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
87 |
+
x = self.features(x)
|
88 |
+
x = self.classifier(x)
|
89 |
+
return torch.flatten(x, 1)
|
90 |
+
|
91 |
+
|
92 |
+
class SN(nn.Module):
|
93 |
+
def __init__(self):
|
94 |
+
super().__init__()
|
95 |
+
self.model = SqueezeNet(version="middle", num_classes=2)
|
96 |
+
|
97 |
+
def forward(self, x):
|
98 |
+
return self.model(x)
|
99 |
+
|
100 |
+
|
101 |
+
def predict(image):
|
102 |
+
|
103 |
+
model = SN()
|
104 |
+
model.load_state_dict(torch.load("./liveness_1M_model_0.8740054619288524 .ckpt", map_location=torch.device('cpu')))
|
105 |
+
im = Image.open(image)
|
106 |
+
transform1 = transforms.Compose([transforms.Resize((512, 512)),
|
107 |
+
transforms.ToTensor()])
|
108 |
+
img = transform1(im).unsqueeze(0)
|
109 |
+
my_softmax = nn.Softmax(dim=1)
|
110 |
+
with torch.no_grad():
|
111 |
+
y_hat = model(img)
|
112 |
+
liveness_score = float(my_softmax(y_hat)[0][1])
|
113 |
+
|
114 |
+
res = {"fake": liveness_score, "real": 1 - liveness_score}
|
115 |
+
return res
|
116 |
+
|
117 |
+
|
118 |
+
gr.Interface(
|
119 |
+
predict,
|
120 |
+
inputs=gr.inputs.Image(label="Upload an image", type="filepath"),
|
121 |
+
outputs=gr.outputs.Label(num_top_classes=2),
|
122 |
+
title="Real or Fake", examples=["./2022-10-12 16.52.56.jpg", "./2022-10-12 16.54.52.jpg", "./1724477482.jpeg"]
|
123 |
+
).launch()
|
liveness_1M_model_0.8740054619288524 .ckpt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9db21af2c4bda70801b7c8ecd5b7bb7fff32452faf6c370a291b82b95719ad3e
|
3 |
+
size 748617
|