Views
No views yet
facebook/dinov2-giant loaded from Hugging Face Hub.| Model | Accuracy | F1 | Precision | Recall |
|---|---|---|---|---|
| NP-TEST-0 | 0.802 | 0.779 | 0.792 | 0.796 |
| dinov2-giant | 0.667 | 0.648 | 0.669 | 0.667 |
| dinov2-giant_distilled_prov | 0.769 | 0.756 | 0.755 | 0.769 |
| dinov2-large_distilled_prov | 0.772 | 0.758 | 0.758 | 0.772 |
| distilled_prov_finetuned | 0.779 | 0.762 | 0.770 | 0.779 |
| prov-gigapath | 0.776 | 0.762 | 0.764 | 0.776 |
| UNI | 0.741 | 0.731 | 0.734 | 0.741 |
| UNI2-h | 0.768 | 0.750 | 0.753 | 0.768 |

transformers (adjust based on your actual model and task):1
2import torch
3from PIL import Image
4from transformers import AutoModel, AutoImageProcessor
5from torchvision import transforms
6
7def get_embeddings_with_processor(image_path, model_path):
8 """
9 Extract embeddings using a HuggingFace image processor.
10 This approach handles normalization and resizing automatically.
11
12 Args:
13 image_path: Path to the image file
14 model_path: Path to the model directory
15 processor_path: Path to the processor config directory
16
17 Returns:
18 Image embeddings from the model
19 """
20 # Load model
21 model = AutoModel.from_pretrained(model_path)
22 model.eval()
23
24 # Load processor from config
25 image_processor = AutoImageProcessor.from_pretrained(model_path)
26
27 # Process the image
28 with torch.no_grad():
29 image = Image.open(image_path).convert('RGB')
30 inputs = image_processor(images=image, return_tensors="pt")
31 outputs = model(**inputs)
32 embeddings = outputs.last_hidden_state[:, 0, :]
33
34 return embeddings
35
36def get_embeddings_direct(image_path, model_path, mean=[0.83800817, 0.6516568, 0.78056043], std=[0.08324149, 0.09973671, 0.07153901]):
37 """
38 Extract embeddings directly without an image processor.
39 This approach works with various image resolutions since transformers handle
40 different input sizes by design.
41
42 Args:
43 image_path: Path to the image file
44 model_path: Path to the model directory
45 mean: Normalization mean values
46 std: Normalization standard deviation values
47
48 Returns:
49 Image embeddings from the model
50 """
51 # Load model
52 model = AutoModel.from_pretrained(model_path)
53 model.eval()
54
55 # Define transformation - just converting to tensor and normalizing
56 transform = transforms.Compose([
57 transforms.ToTensor(),
58 transforms.Normalize(mean=mean, std=std)
59 ])
60
61 # Process the image
62 with torch.no_grad():
63 # Open image and convert to RGB
64 image = Image.open(image_path).convert('RGB')
65 # Convert image to tensor
66 image_tensor = transform(image).unsqueeze(0) # Add batch dimension
67 # Feed to model
68 outputs = model(pixel_values=image_tensor)
69 # Get embeddings
70 embeddings = outputs.last_hidden_state[:, 0, :]
71
72 return embeddings
73
74def get_embeddings_resized(image_path, model_path, size=(224, 224), mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]):
75 """
76 Extract embeddings with explicit resizing to 224x224.
77 This approach ensures consistent input size regardless of original image dimensions.
78
79 Args:
80 image_path: Path to the image file
81 model_path: Path to the model directory
82 size: Target size for resizing (default: 224x224)
83 mean: Normalization mean values
84 std: Normalization standard deviation values
85
86 Returns:
87 Image embeddings from the model
88 """
89 # Load model
90 model = AutoModel.from_pretrained(model_path)
91 model.eval()
92
93 # Define transformation with explicit resize
94 transform = transforms.Compose([
95 transforms.Resize(size, interpolation=transforms.InterpolationMode.BICUBIC),
96 transforms.ToTensor(),
97 transforms.Normalize(mean=mean, std=std)
98 ])
99
100 # Process the image
101 with torch.no_grad():
102 image = Image.open(image_path).convert('RGB')
103 image_tensor = transform(image).unsqueeze(0) # Add batch dimension
104 outputs = model(pixel_values=image_tensor)
105 embeddings = outputs.last_hidden_state[:, 0, :]
106
107 return embeddings
108
109# Example usage
110if __name__ == "__main__":
111 image_path = "test.jpg"
112 model_path = "IBI-CAAI/NP-TEST-0"
113
114 # Method 1: Using image processor (recommended for consistency)
115 embeddings1 = get_embeddings_with_processor(image_path, model_path)
116 print('Embedding shape (with processor):', embeddings1.shape)
117
118 # Method 2: Direct approach without resizing (works with various resolutions)
119 embeddings2 = get_embeddings_direct(image_path, model_path)
120 print('Embedding shape (direct):', embeddings2.shape)
121
122 # Method 3: With explicit resize to 224x224
123 embeddings3 = get_embeddings_resized(image_path, model_path)
124 print('Embedding shape (resized):', embeddings3.shape)
125ai@uky.edu),
Mahmut Gokmen (m.gokmen@uky.edu)
Cody Bumgardner (cody@uky.edu).