[GH-ISSUE #347] RuntimeError: The size of tensor a (768) must match the size of tensor b (7) at non-singleton dimension 2. #299

Open
opened 2026-10-31 21:13:35 +00:00 by bsakaguchi · 1 comment
Owner

Originally created by @sankyde on GitHub (Apr 10, 2023).
Original GitHub issue: https://github.com/openai/CLIP/issues/347

This is the program I am trying to write. It's simple but I still get this error.

Steps to Reproduce:

from PIL import Image
import requests
import torch
import clip
clip.available_models()
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)
model, preprocess = clip.load("ViT-B/32")
image_input=torch.tensor(preprocess(image)).cuda()
text_tokens=clip.tokenize(
["This is an image of two cats sleeping on a pink blanket"])
with torch.no_grad():
image_features=model.encode_image(image_input).float()
text_tokens=model.encode_text(text_tokens).float()

Originally created by @sankyde on GitHub (Apr 10, 2023). Original GitHub issue: https://github.com/openai/CLIP/issues/347 This is the program I am trying to write. It's simple but I still get this error. Steps to Reproduce: from PIL import Image import requests import torch import clip clip.available_models() url = "http://images.cocodataset.org/val2017/000000039769.jpg" image = Image.open(requests.get(url, stream=True).raw) model, preprocess = clip.load("ViT-B/32") image_input=torch.tensor(preprocess(image)).cuda() text_tokens=clip.tokenize( ["This is an image of two cats sleeping on a pink blanket"]) with torch.no_grad(): image_features=model.encode_image(image_input).float() text_tokens=model.encode_text(text_tokens).float()
Author
Owner

@adtygan commented on GitHub (May 4, 2023):

image_input=torch.tensor(preprocess(image)).unsqueeze(0).cuda() 
text_tokens=clip.tokenize(["This is an image of two cats sleeping on a pink blanket"]).cuda()
with torch.no_grad():
  image_features=model.encode_image(image_input).float()
  text_tokens=model.encode_text(text_tokens).float()

In Line 1 .unsqueeze(0) needs to called (refer example in https://github.com/openai/CLIP#usage)
In Line 2 .cuda() needs to be added as well to ensure both image_features and text_tokens are on the same device

<!-- gh-comment-id:1534741203 --> @adtygan commented on GitHub (May 4, 2023): ``` image_input=torch.tensor(preprocess(image)).unsqueeze(0).cuda() text_tokens=clip.tokenize(["This is an image of two cats sleeping on a pink blanket"]).cuda() with torch.no_grad(): image_features=model.encode_image(image_input).float() text_tokens=model.encode_text(text_tokens).float() ``` In Line 1 `.unsqueeze(0)` needs to called (refer example in [https://github.com/openai/CLIP#usage](https://github.com/openai/CLIP#usage)) In Line 2 `.cuda()` needs to be added as well to ensure both `image_features` and `text_tokens` are on the same device
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
openai/CLIP#299
No description provided.