Spaces:
Sleeping
Sleeping
add new model state
Browse files- app.py +4 -6
- best_vit10.pth +3 -0
app.py
CHANGED
@@ -15,23 +15,21 @@ class ViT(nn.Module):
|
|
15 |
self.base_model = base_model
|
16 |
self.dropout = nn.Dropout(p=0.2)
|
17 |
self.fc = nn.Linear(base_model.config.hidden_size, 512)
|
18 |
-
self.dropout2 = nn.Dropout(p=0.2)
|
19 |
self.l2_norm = nn.functional.normalize
|
20 |
|
21 |
def forward(self, x):
|
22 |
x = self.base_model(x).pooler_output
|
23 |
x = self.dropout(x)
|
24 |
x = self.fc(x)
|
25 |
-
x = self.dropout2(x)
|
26 |
x = self.l2_norm(x, p=2, dim=1) # Apply L2 normalization
|
27 |
return x
|
28 |
|
29 |
# Load the pre-trained ViT model and processor
|
30 |
model_name = "google/vit-base-patch16-224"
|
31 |
-
processor = ViTImageProcessor.from_pretrained(model_name)
|
32 |
-
base_model = ViTModel.from_pretrained(
|
33 |
model = ViT(base_model)
|
34 |
-
model_path = r'
|
35 |
model.load_state_dict(torch.load(model_path))
|
36 |
model.eval().to('cuda' if torch.cuda.is_available() else 'cpu')
|
37 |
|
@@ -85,7 +83,7 @@ def process_images(image1, image2):
|
|
85 |
embedding2 = embedding2.flatten()
|
86 |
|
87 |
euclidean_dist = euclidean_distance(embedding1, embedding2)
|
88 |
-
is_match = euclidean_dist < 0.
|
89 |
|
90 |
# Calculate confidence
|
91 |
confidence = max(0.0, 1.0 - euclidean_dist / 1.0) # Ensure confidence is between 0 and 1
|
|
|
15 |
self.base_model = base_model
|
16 |
self.dropout = nn.Dropout(p=0.2)
|
17 |
self.fc = nn.Linear(base_model.config.hidden_size, 512)
|
|
|
18 |
self.l2_norm = nn.functional.normalize
|
19 |
|
20 |
def forward(self, x):
|
21 |
x = self.base_model(x).pooler_output
|
22 |
x = self.dropout(x)
|
23 |
x = self.fc(x)
|
|
|
24 |
x = self.l2_norm(x, p=2, dim=1) # Apply L2 normalization
|
25 |
return x
|
26 |
|
27 |
# Load the pre-trained ViT model and processor
|
28 |
model_name = "google/vit-base-patch16-224"
|
29 |
+
processor = ViTImageProcessor.from_pretrained(model_name)
|
30 |
+
base_model = ViTModel.from_pretrained("WinKawaks/vit-small-patch16-224")
|
31 |
model = ViT(base_model)
|
32 |
+
model_path = r'best_vit10.pth'
|
33 |
model.load_state_dict(torch.load(model_path))
|
34 |
model.eval().to('cuda' if torch.cuda.is_available() else 'cpu')
|
35 |
|
|
|
83 |
embedding2 = embedding2.flatten()
|
84 |
|
85 |
euclidean_dist = euclidean_distance(embedding1, embedding2)
|
86 |
+
is_match = euclidean_dist < 0.2
|
87 |
|
88 |
# Calculate confidence
|
89 |
confidence = max(0.0, 1.0 - euclidean_dist / 1.0) # Ensure confidence is between 0 and 1
|
best_vit10.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b6bd60779276ccef3018360dd9536fe43f3439511a2ce499a33b871569781aed
|
3 |
+
size 88127816
|