-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCase_Study.py
More file actions
140 lines (123 loc) · 4.99 KB
/
Case_Study.py
File metadata and controls
140 lines (123 loc) · 4.99 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import torch
from PIL import Image
from transformers import CLIPProcessor, CLIPModel, AutoProcessor, AutoModelForCausalLM
# image loading and preprocessing
def load_and_preprocess_image(image_path):
image = Image.open(image_path).convert("RGB")
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
inputs = processor(images=image, return_tensors="pt")
return inputs, processor
# image understanding with CLIP
def generate_image_embeddings(inputs):
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
with torch.no_grad():
image_features = model.get_image_features(**inputs)
return image_features, model
# caption matching (using CLIP text embeddings)
def match_captions(image_features, captions, clip_model, processor):
# 1. get text embeddings for the captions:
text_inputs = processor(text=captions, return_tensors="pt", padding=True)
with torch.no_grad():
text_features = clip_model.get_text_features(**text_inputs)
# 2. calculate cosine similarity between image and text features:
image_features = image_features.detach().cpu().numpy()
text_features = text_features.detach().cpu().numpy()
similarities = cosine_similarity(image_features, text_features)
# 3. find the best matching captions:
best_indices = similarities.argsort(axis=1)[0][::-1]
best_captions = [captions[i] for i in best_indices]
return best_captions, similarities[0][best_indices].tolist()
# main function
def image_captioning(image_path, candidate_captions):
inputs, processor = load_and_preprocess_image(image_path)
image_features, clip_model = generate_image_embeddings(inputs)
best_captions, similarities = match_captions(image_features, candidate_captions, clip_model, processor)
return best_captions, similarities
candidate_captions = [
"Trees, Travel and Tea!",
"A refreshing beverage.",
"A moment of indulgence.",
"The perfect thirst quencher.",
"Your daily dose of delight.",
"Taste the tradition.",
"Savor the flavor.",
"Refresh and rejuvenate.",
"Unwind and enjoy.",
"The taste of home.",
"A treat for your senses.",
"A taste of adventure.",
"A moment of bliss.",
"Your travel companion.",
"Fuel for your journey.",
"The essence of nature.",
"The warmth of comfort.",
"A sip of happiness.",
"Pure indulgence.",
"Quench your thirst, ignite your spirit.",
"Awaken your senses, embrace the moment.",
"The taste of faraway lands.",
"A taste of home, wherever you are.",
"Your daily dose of delight.",
"Your moment of serenity.",
"The perfect pick-me-up.",
"The perfect way to unwind.",
"Taste the difference.",
"Experience the difference.",
"A refreshing escape.",
"A delightful escape.",
"The taste of tradition, the spirit of adventure.",
"The warmth of home, the joy of discovery.",
"Your passport to flavor.",
"Your ticket to tranquility.",
"Sip, savor, and explore.",
"Indulge, relax, and rejuvenate.",
"The taste of wanderlust.",
"The comfort of home.",
"A journey for your taste buds.",
"A haven for your senses.",
"Your refreshing companion.",
"Your delightful escape.",
"Taste the world, one sip at a time.",
"Embrace the moment, one cup at a time.",
"The essence of exploration.",
"The comfort of connection.",
"Quench your thirst for adventure.",
"Savor the moment of peace.",
"The taste of discovery.",
"The warmth of belonging.",
"Your travel companion, your daily delight.",
"Your moment of peace, your daily indulgence.",
"The spirit of exploration, the comfort of home.",
"The joy of discovery, the warmth of connection.",
"Sip, savor, and set off on an adventure.",
"Indulge, relax, and find your peace.",
"A delightful beverage.",
"A moment of relaxation.",
"The perfect way to start your day.",
"The perfect way to end your day.",
"A treat for yourself.",
"Something to savor.",
"A moment of calm.",
"A taste of something special.",
"A refreshing pick-me-up.",
"A comforting drink.",
"A taste of adventure.",
"A moment of peace.",
"A small indulgence.",
"A daily ritual.",
"A way to connect with others.",
"A way to connect with yourself.",
"A taste of home.",
"A taste of something new.",
"A moment to enjoy.",
"A moment to remember."
]
from sklearn.metrics.pairwise import cosine_similarity
best_captions, similarities = image_captioning("/content/Anaya.jpg", candidate_captions)
# get the top 5 results
top_n = min(5, len(best_captions))
top_best_captions = best_captions[:top_n]
top_similarities = similarities[:top_n]
print("Top 5 Best Captions:")
for i, (caption, similarity) in enumerate(zip(top_best_captions, top_similarities)):
print(f"{i+1}. {caption} (Similarity: {similarity:.4f})")