panda1835 commited on
Commit
ff14199
·
verified ·
1 Parent(s): ea52679

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py CHANGED
@@ -15,6 +15,44 @@ import models
15
  print(f"Is CUDA available: {torch.cuda.is_available()}")
16
  # print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  with open("index_to_species.json", "r") as file:
19
  index_to_species_data = file.read()
20
  index_to_species = json.loads(index_to_species_data)
 
15
  print(f"Is CUDA available: {torch.cuda.is_available()}")
16
  # print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
17
 
18
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
19
+ # DINOv2
20
+
21
+ # Select checkpoint
22
+ dinov2_ckpt = ['dinov2_vits14', 'dinov2_vitb14', 'dinov2_vitl14', 'dinov2_vitg14'][1]
23
+ dinov2 = torch.hub.load('facebookresearch/dinov2', dinov2_ckpt)
24
+
25
+ dinov2.to(device)
26
+ print()
27
+
28
+ transform_image = T.Compose([
29
+ T.Resize((224, 224)),
30
+ T.ToTensor(),
31
+ T.Normalize(mean=[0.485, 0.456, 0.406],
32
+ std=[0.229, 0.224, 0.225])
33
+ ])
34
+
35
+ def extract_embedding(image):
36
+ """
37
+ Predict the identity of an image.
38
+ Args:
39
+ image: A PIL Image object.
40
+ Returns:
41
+ A string representing the predicted identity of the image.
42
+ """
43
+
44
+ # Convert the image to a tensor.
45
+ transformed_img = transform_image(image)[:3].unsqueeze(0).to(device)
46
+
47
+ # Get the embedding of the image.
48
+ with torch.no_grad():
49
+ embedding = dinov2(transformed_img)
50
+ # print(embedding.shape)
51
+ embedding = embedding[0].cpu().numpy().tolist()
52
+ return {
53
+ "embedding": embedding
54
+ }
55
+
56
  with open("index_to_species.json", "r") as file:
57
  index_to_species_data = file.read()
58
  index_to_species = json.loads(index_to_species_data)