Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blip2 #2

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Update blip2.py
lenguyen2592004 authored Aug 8, 2024
commit 1cbb88113f8b7d2ea2c15b827f5889f2468d17fd
11 changes: 10 additions & 1 deletion image_retrieval/blip2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from helper_function import *
import pandas as pd
import faiss
import numpy as np
import torch
import clip
import argparse
@@ -66,15 +68,22 @@ def comclip_one_pair(row_id, caption, image_id):
object_images[word] = relation_image

##subimages
# Create image embeddings array
image_embeds = []
image_scores = []
for key, sub_image in object_images.items():
if "_dup" in key:
key = key.replace("_dup", "")
image_embed, image_score = subimage_score_embedding(sub_image, key)
if image_embed is not None and image_score is not None:
image_embeds.append(image_embed)
# Append image features to image_embeds
image_input = model.get_image_features(**image_inputs).cuda(device)
image_embeds = np.append(image_embeds, image_input.cpu().numpy(), axis=0)
image_scores.append(image_score)
image_embed_dim = image_embeds.ndim
index = faiss.IndexFlatL2(image_embed_dim)
# Thêm tất cả các embedding vào index
index.add(image_embeds)
#regularize the scores
similarity = normalize_tensor_list(image_scores)
for score, image in zip(similarity, image_embeds):