An error occurred: You have to specify input_ids

Hello,

I am trying to run this code, but I encountered an unexpected error: “An error occurred: You have to specify input_ids”. This seems to come from the line of code: outputs = clip_model(**inputs). However, I have printed the previous line that determines the tensors and it looks like it correctly creates it (inputs = clip_processor(images=image, return_tensors=“pt”):

!pip install --upgrade transformers

!pip install --upgrade torch transformers

from transformers import CLIPProcessor, CLIPModel
from PIL import Image
import torch
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity

Load CLIP model and processor

clip_processor = CLIPProcessor.from_pretrained(“openai/clip-vit-base-patch32”)
clip_model = CLIPModel.from_pretrained(“openai/clip-vit-base-patch32”)

def get_clip_embedding(image_path):
try:
image = Image.open(image_path)
inputs = clip_processor(images=image, return_tensors=“pt”)
outputs = clip_model(**inputs)
# Debugging print to check outputs
print(“Model outputs:”, outputs)
embedding = outputs.last_hidden_state.mean(dim=1).squeeze().detach().numpy()
return embedding
except Exception as e:
print(f"An error occurred while processing {image_path}: {str(e)}")
return None

image_path = “/content/drive/My Drive/COCO/sample_exp/000000580410.jpg”
image_embedding = get_clip_embedding(image_path)