Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +59 -0
- model.py +56 -0
- siamese_model.pth +3 -0
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from PIL import Image
|
4 |
+
import torchvision.transforms as transforms
|
5 |
+
from model import SiameseNetwork # Ensure this file exists with the model definition
|
6 |
+
|
7 |
+
# Define the device (GPU or CPU)
|
8 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
9 |
+
|
10 |
+
# Load the pre-trained Siamese model
|
11 |
+
model = SiameseNetwork().to(device)
|
12 |
+
model.load_state_dict(torch.load("siamese_model.pth", map_location=device))
|
13 |
+
model.eval()
|
14 |
+
|
15 |
+
# Define data transformation (resize, convert to tensor, normalize if needed)
|
16 |
+
transform = transforms.Compose([
|
17 |
+
transforms.Resize((100, 100)), # Resize to match the input size of the model
|
18 |
+
transforms.Grayscale(num_output_channels=1), # Convert images to grayscale for signature comparison
|
19 |
+
transforms.ToTensor(), # Convert image to tensor
|
20 |
+
])
|
21 |
+
|
22 |
+
# Streamlit interface
|
23 |
+
st.title("Signature Forgery Detection with Siamese Network")
|
24 |
+
st.write("Upload two signature images to check if they are from the same person or if one is forged.")
|
25 |
+
|
26 |
+
# Upload images
|
27 |
+
image1 = st.file_uploader("Upload First Signature Image", type=["png", "jpg", "jpeg"])
|
28 |
+
image2 = st.file_uploader("Upload Second Signature Image", type=["png", "jpg", "jpeg"])
|
29 |
+
|
30 |
+
if image1 and image2:
|
31 |
+
# Load and transform the images
|
32 |
+
img1 = Image.open(image1).convert("RGB")
|
33 |
+
img2 = Image.open(image2).convert("RGB")
|
34 |
+
|
35 |
+
# Display images
|
36 |
+
col1, col2 = st.columns(2)
|
37 |
+
with col1:
|
38 |
+
st.image(img1, caption='First Signature Image', use_container_width=True)
|
39 |
+
with col2:
|
40 |
+
st.image(img2, caption='Second Signature Image', use_container_width=True)
|
41 |
+
|
42 |
+
# Transform the images before feeding them into the model
|
43 |
+
img1 = transform(img1).unsqueeze(0).to(device)
|
44 |
+
img2 = transform(img2).unsqueeze(0).to(device)
|
45 |
+
|
46 |
+
# Predict similarity using the Siamese model
|
47 |
+
output1, output2 = model(img1, img2)
|
48 |
+
euclidean_distance = torch.nn.functional.pairwise_distance(output1, output2)
|
49 |
+
|
50 |
+
# Set a threshold for similarity (can be tuned based on model performance)
|
51 |
+
threshold = 0.5 # You can adjust this threshold based on your model's performance
|
52 |
+
|
53 |
+
# Display similarity score and interpretation
|
54 |
+
st.success(f'Similarity Score (Euclidean Distance): {euclidean_distance.item():.4f}')
|
55 |
+
if euclidean_distance.item() < threshold:
|
56 |
+
st.write("The signatures are likely from the **same person**.")
|
57 |
+
else:
|
58 |
+
st.write("The signatures **do not match**, one might be **forged**.")
|
59 |
+
|
model.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
|
4 |
+
class SiameseNetwork(nn.Module):
|
5 |
+
def __init__(self):
|
6 |
+
super(SiameseNetwork, self).__init__()
|
7 |
+
|
8 |
+
self.cnn1 = nn.Sequential(
|
9 |
+
nn.Conv2d(1, 96, kernel_size=11, stride=1),
|
10 |
+
nn.ReLU(inplace=True),
|
11 |
+
nn.LocalResponseNorm(5, alpha=0.0001, beta=0.75, k=2),
|
12 |
+
nn.MaxPool2d(3, stride=2),
|
13 |
+
|
14 |
+
nn.Conv2d(96, 256, kernel_size=5, stride=1, padding=2),
|
15 |
+
nn.ReLU(inplace=True),
|
16 |
+
nn.LocalResponseNorm(5, alpha=0.0001, beta=0.75, k=2),
|
17 |
+
nn.MaxPool2d(3, stride=2),
|
18 |
+
nn.Dropout2d(p=0.3),
|
19 |
+
|
20 |
+
nn.Conv2d(256, 384, kernel_size=3, stride=1, padding=1),
|
21 |
+
nn.ReLU(inplace=True),
|
22 |
+
|
23 |
+
nn.Conv2d(384, 256, kernel_size=3, stride=1, padding=1),
|
24 |
+
nn.ReLU(inplace=True),
|
25 |
+
nn.MaxPool2d(3, stride=2),
|
26 |
+
nn.Dropout2d(p=0.3),
|
27 |
+
)
|
28 |
+
|
29 |
+
self.fc1 = nn.Sequential(
|
30 |
+
nn.Linear(25600, 1024),
|
31 |
+
nn.ReLU(inplace=True),
|
32 |
+
nn.Dropout2d(p=0.5),
|
33 |
+
|
34 |
+
nn.Linear(1024, 128),
|
35 |
+
nn.ReLU(inplace=True),
|
36 |
+
|
37 |
+
nn.Linear(128, 2)
|
38 |
+
)
|
39 |
+
|
40 |
+
def forward_once(self, x):
|
41 |
+
output = self.cnn1(x)
|
42 |
+
output = output.view(output.size()[0], -1)
|
43 |
+
output = self.fc1(output)
|
44 |
+
return output
|
45 |
+
|
46 |
+
def forward(self, input1, input2):
|
47 |
+
output1 = self.forward_once(input1)
|
48 |
+
output2 = self.forward_once(input2)
|
49 |
+
return output1, output2
|
50 |
+
|
51 |
+
# Function to load the trained model
|
52 |
+
def load_model(model_path):
|
53 |
+
model = SiameseNetwork()
|
54 |
+
model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
55 |
+
model.eval()
|
56 |
+
return model
|
siamese_model.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a674d49cdc8ca78544b7f1dfe7caf50b63650657c4d9e07badf0db5b3a512c07
|
3 |
+
size 114978840
|