Update New_file.txt
Browse files- New_file.txt +11 -12
New_file.txt
CHANGED
@@ -1,14 +1,15 @@
|
|
1 |
import torch
|
2 |
-
from transformers import SwinTransformer, SwinTransformerTokenizer
|
3 |
import torchvision.transforms as transforms
|
4 |
from PIL import Image
|
5 |
import numpy as np
|
6 |
from sklearn.metrics.pairwise import cosine_similarity
|
|
|
|
|
|
|
7 |
|
8 |
-
# Load
|
9 |
-
|
10 |
-
model
|
11 |
-
tokenizer = SwinTransformerTokenizer.from_pretrained(model_name)
|
12 |
|
13 |
# Define a function to preprocess images
|
14 |
def preprocess_image(image_path):
|
@@ -22,29 +23,27 @@ def preprocess_image(image_path):
|
|
22 |
return image
|
23 |
|
24 |
# Load your ideal subset of images
|
25 |
-
ideal_image_paths = ["
|
26 |
ideal_embeddings = []
|
27 |
|
28 |
for image_path in ideal_image_paths:
|
29 |
image = preprocess_image(image_path)
|
30 |
with torch.no_grad():
|
31 |
-
|
32 |
-
embedding = model.pixel_values(input_ids).numpy()
|
33 |
ideal_embeddings.append(embedding)
|
34 |
|
35 |
# Load a set of candidate images
|
36 |
-
candidate_image_paths = ["
|
37 |
candidate_embeddings = []
|
38 |
|
39 |
for image_path in candidate_image_paths:
|
40 |
image = preprocess_image(image_path)
|
41 |
with torch.no_grad():
|
42 |
-
|
43 |
-
embedding = model.pixel_values(input_ids).numpy()
|
44 |
candidate_embeddings.append(embedding)
|
45 |
|
46 |
# Calculate similarities between ideal and candidate images using cosine similarity
|
47 |
similarities = cosine_similarity(ideal_embeddings, candidate_embeddings)
|
48 |
|
49 |
# Print the similarity matrix
|
50 |
-
print(similarities)
|
|
|
1 |
import torch
|
|
|
2 |
import torchvision.transforms as transforms
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
from sklearn.metrics.pairwise import cosine_similarity
|
6 |
+
from torchvision.models import resnet50
|
7 |
+
from torchvision.datasets import ImageFolder
|
8 |
+
from torch.utils.data import DataLoader
|
9 |
|
10 |
+
# Load a pre-trained ResNet-50 model
|
11 |
+
model = resnet50(pretrained=True)
|
12 |
+
model.eval()
|
|
|
13 |
|
14 |
# Define a function to preprocess images
|
15 |
def preprocess_image(image_path):
|
|
|
23 |
return image
|
24 |
|
25 |
# Load your ideal subset of images
|
26 |
+
ideal_image_paths = ["/content/trunck.jpg", "t4.jpg"] # Replace with your ideal image file paths
|
27 |
ideal_embeddings = []
|
28 |
|
29 |
for image_path in ideal_image_paths:
|
30 |
image = preprocess_image(image_path)
|
31 |
with torch.no_grad():
|
32 |
+
embedding = model(image).squeeze().numpy()
|
|
|
33 |
ideal_embeddings.append(embedding)
|
34 |
|
35 |
# Load a set of candidate images
|
36 |
+
candidate_image_paths = ["/content/trunck2.jpg", "t3.jpg", "car.jpg",] # Replace with your candidate image file paths
|
37 |
candidate_embeddings = []
|
38 |
|
39 |
for image_path in candidate_image_paths:
|
40 |
image = preprocess_image(image_path)
|
41 |
with torch.no_grad():
|
42 |
+
embedding = model(image).squeeze().numpy()
|
|
|
43 |
candidate_embeddings.append(embedding)
|
44 |
|
45 |
# Calculate similarities between ideal and candidate images using cosine similarity
|
46 |
similarities = cosine_similarity(ideal_embeddings, candidate_embeddings)
|
47 |
|
48 |
# Print the similarity matrix
|
49 |
+
print(similarities)
|