Download app.py from 88wisu/siamese: direct link, hf CLI and curl.
- Browser
- Download file 2.08 kB
-
https://huggingface.co/spaces/88wisu/siamese/resolve/main/app.py
- Command line
-
hf download hf://spaces/88wisu/siamese/app.py
-
curl -L -o app.py https://huggingface.co/spaces/88wisu/siamese/resolve/main/app.py
2.08 kB
| import gradio as gr | |
| import torch | |
| import torch.nn as nn | |
| from torchvision.models import resnet18, ResNet18_Weights | |
| from torchvision import transforms as T | |
| from PIL import Image | |
| class SiameseNetwork(nn.Module): | |
| def __init__(self): | |
| super().__init__() | |
| self.resnet = resnet18(weights=ResNet18_Weights.DEFAULT) | |
| self.resnet.conv1 = nn.Conv2d(1, 64, kernel_size=9, stride=2, padding=4, bias=False) | |
| self.resnet = nn.Sequential(*list(self.resnet.children())[:-1]) | |
| self.fc = nn.Sequential( | |
| nn.Linear(512 * 2, 256), | |
| nn.ReLU(), | |
| nn.Dropout(0.3), | |
| nn.Linear(256, 1), | |
| nn.Sigmoid() | |
| ) | |
| def forward_once(self, x): | |
| x = self.resnet(x) | |
| return x.view(x.size(0), -1) | |
| def forward(self, x1, x2): | |
| f1 = self.forward_once(x1) | |
| f2 = self.forward_once(x2) | |
| return self.fc(torch.cat([f1, f2], dim=1)) | |
| transform = T.Compose([ | |
| T.Resize((64, 64)), | |
| T.Grayscale(num_output_channels=1), | |
| T.ToTensor(), | |
| T.Normalize((0.5,), (0.5,)) | |
| ]) | |
| def preprocess_image(image): | |
| image = image.convert("L") | |
| tensor = transform(image).unsqueeze(0) | |
| return tensor.to(device) | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| model = SiameseNetwork().to(device) | |
| model.load_state_dict(torch.load("model_weights.pt", map_location=device)) | |
| model.eval() | |
| def predict_similarity(image1, image2): | |
| try: | |
| img1 = preprocess_image(image1) | |
| img2 = preprocess_image(image2) | |
| with torch.no_grad(): | |
| similarity = model(img1, img2).item() | |
| match_str = "Yes" if similarity > 0.5 else "No" | |
| return f"Similarity Score (0-1): {similarity:.4f}\nMatch: {match_str}" | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| demo = gr.Interface( | |
| fn=predict_similarity, | |
| inputs=[gr.Image(type="pil"), gr.Image(type="pil")], | |
| outputs="text", | |
| title="MNIST Siamese Similarity", | |
| description="Upload two digit images to compare similarity." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |