-
Notifications
You must be signed in to change notification settings - Fork 0
/
similarity.py
68 lines (51 loc) · 2.19 KB
/
similarity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import torch
import torch.nn as nn
from torchvision import models, transforms
from PIL import Image
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
import cv2
# Define a transform to preprocess the images
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
def convert_to_PIL(image):
image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
return Image.fromarray(image)
def extract_features(frame, model):
#image = Image.open(image_path)
image = preprocess(convert_to_PIL(frame)).unsqueeze(0) # Add batch dimension
with torch.no_grad():
features = model(image)
features = features.squeeze().numpy() # Remove batch dimension and convert to numpy
return features
def extract_features_Dino(frame, processor, model, device):
image1 = convert_to_PIL(frame)
with torch.no_grad():
inputs1 = processor(images=image1, return_tensors="pt").to(device)
outputs1 = model(**inputs1)
features = outputs1.last_hidden_state
features = features.mean(dim=1)
return features
def similarity_Dino(feature_init, frame, processor, model, device):
# Example image paths
#image_paths = ['outputs/all_crops/crop_100.jpg', 'outputs/all_crops/crop_754.jpg','outputs/all_crops/crop_339.jpg', 'outputs/all_crops/crop_647.jpg','outputs/all_crops/crop_648.jpg','outputs/all_crops/crop_649.jpg']
# Extract features for all images
feature_frame = extract_features_Dino(frame,processor,model,device)
#print(feature_frame.shape, feature_init.shape)
# Compute cosine similarities between the feature vectors
cos = nn.CosineSimilarity(dim=0)
sim = cos(feature_init[0],feature_frame[0]).item()
sim = (sim+1)/2
#print(sim)
return sim
def similarity(feature_init, frame):
# Example image paths
# Extract features for all images
feature_frame = extract_features(frame)
# Compute cosine similarities between the feature vectors
similarities = cosine_similarity([feature_init],[feature_frame])[0][0]
return similarities