Update New_file.txt
Browse files- New_file.txt +66 -0
New_file.txt
CHANGED
@@ -47,3 +47,69 @@ similarities = cosine_similarity(ideal_embeddings, candidate_embeddings)
|
|
47 |
|
48 |
# Print the similarity matrix
|
49 |
print(similarities)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
# Print the similarity matrix
|
49 |
print(similarities)
|
50 |
+
|
51 |
+
|
52 |
+
## SWIN code
|
53 |
+
|
54 |
+
import torch
|
55 |
+
from transformers import SwinTransformer, SwinTransformerImageProcessor
|
56 |
+
import torchvision.transforms as transforms
|
57 |
+
from PIL import Image
|
58 |
+
import numpy as np
|
59 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
60 |
+
|
61 |
+
# Load the pretrained Swin Transformer model and image processor
|
62 |
+
model_name = "microsoft/Swin-Transformer-base-patch4-in22k"
|
63 |
+
model = SwinTransformer.from_pretrained(model_name)
|
64 |
+
processor = SwinTransformerImageProcessor.from_pretrained(model_name)
|
65 |
+
|
66 |
+
# Define a function to preprocess images
|
67 |
+
def preprocess_image(image_path):
|
68 |
+
transform = transforms.Compose([
|
69 |
+
transforms.Resize((224, 224)),
|
70 |
+
transforms.ToTensor(),
|
71 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
72 |
+
])
|
73 |
+
image = Image.open(image_path)
|
74 |
+
inputs = processor(images=image, return_tensors="pt")
|
75 |
+
return inputs
|
76 |
+
|
77 |
+
# Load your ideal and candidate subsets of images
|
78 |
+
ideal_image_paths = ["ideal_image1.jpg", "ideal_image2.jpg", "ideal_image3.jpg"] # Replace with your ideal image file paths
|
79 |
+
candidate_image_paths = ["candidate_image1.jpg", "candidate_image2.jpg", "candidate_image3.jpg"] # Replace with your candidate image file paths
|
80 |
+
|
81 |
+
# Calculate embeddings for ideal images
|
82 |
+
ideal_embeddings = []
|
83 |
+
for image_path in ideal_image_paths:
|
84 |
+
inputs = preprocess_image(image_path)
|
85 |
+
with torch.no_grad():
|
86 |
+
output = model(**inputs)
|
87 |
+
embedding = output['pixel_values'][0].cpu().numpy()
|
88 |
+
ideal_embeddings.append(embedding)
|
89 |
+
|
90 |
+
# Calculate embeddings for candidate images
|
91 |
+
candidate_embeddings = []
|
92 |
+
for image_path in candidate_image_paths:
|
93 |
+
inputs = preprocess_image(image_path)
|
94 |
+
with torch.no_grad():
|
95 |
+
output = model(**inputs)
|
96 |
+
embedding = output['pixel_values'][0].cpu().numpy()
|
97 |
+
candidate_embeddings.append(embedding)
|
98 |
+
|
99 |
+
# Calculate cosine similarities between ideal and candidate images
|
100 |
+
similarities = cosine_similarity(ideal_embeddings, candidate_embeddings)
|
101 |
+
|
102 |
+
# Set a similarity threshold (e.g., 0.7)
|
103 |
+
threshold = 0.7
|
104 |
+
|
105 |
+
# Find similar image pairs based on the threshold
|
106 |
+
similar_pairs = []
|
107 |
+
for i in range(len(ideal_image_paths)):
|
108 |
+
for j in range(len(candidate_image_paths)):
|
109 |
+
if similarities[i, j] > threshold:
|
110 |
+
similar_pairs.append((ideal_image_paths[i], candidate_image_paths[j]))
|
111 |
+
|
112 |
+
# Print similar image pairs
|
113 |
+
for pair in similar_pairs:
|
114 |
+
print(f"Similar images: {pair[0]} and {pair[1]}")
|
115 |
+
|