[GH-ISSUE #175] having trouble when doing batch inference #152

Open
opened 2026-10-31 21:11:45 +00:00 by bsakaguchi · 5 comments
Owner

Originally created by @WilliamHoo on GitHub (Nov 7, 2021).
Original GitHub issue: https://github.com/openai/CLIP/issues/175

Hi CLIP authors,

so i was trying to run the clip using the code below, but when i doing this in batch, i am having trouble with model.encode_text method, as it will show error message below
Screen Shot 2021-11-07 at 12 49 07 PM

my only difference with your sample code below is that, my text has a shape of (batch_size, num_class, n_ctx). My code will run perfectly is my input is of shape (num_class, n_ctx), but i was trying to speed up the process by doing it in batch.
Any advice or help will be really appreciated!

import torch
import clip
from PIL import Image

device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)

image = preprocess(Image.open("CLIP.png")).unsqueeze(0).to(device)
text = clip.tokenize(["a diagram", "a dog", "a cat"]).to(device)

with torch.no_grad():
    image_features = model.encode_image(image)
    text_features = model.encode_text(text)
    
    logits_per_image, logits_per_text = model(image, text)
    probs = logits_per_image.softmax(dim=-1).cpu().numpy()

print("Label probs:", probs)  # prints: [[0.9927937  0.00421068 0.00299572]]
Originally created by @WilliamHoo on GitHub (Nov 7, 2021). Original GitHub issue: https://github.com/openai/CLIP/issues/175 Hi CLIP authors, so i was trying to run the clip using the code below, but when i doing this in batch, i am having trouble with model.encode_text method, as it will show error message below <img width="775" alt="Screen Shot 2021-11-07 at 12 49 07 PM" src="https://user-images.githubusercontent.com/21349011/140661449-44fac7c7-6809-47c8-a21a-ea0ad57aa699.png"> my only difference with your sample code below is that, my text has a shape of (batch_size, num_class, n_ctx). My code will run perfectly is my input is of shape (num_class, n_ctx), but i was trying to speed up the process by doing it in batch. Any advice or help will be really appreciated! ``` import torch import clip from PIL import Image device = "cuda" if torch.cuda.is_available() else "cpu" model, preprocess = clip.load("ViT-B/32", device=device) image = preprocess(Image.open("CLIP.png")).unsqueeze(0).to(device) text = clip.tokenize(["a diagram", "a dog", "a cat"]).to(device) with torch.no_grad(): image_features = model.encode_image(image) text_features = model.encode_text(text) logits_per_image, logits_per_text = model(image, text) probs = logits_per_image.softmax(dim=-1).cpu().numpy() print("Label probs:", probs) # prints: [[0.9927937 0.00421068 0.00299572]] ```
Author
Owner

@jongwook commented on GitHub (Nov 16, 2021):

Can you try reshaping the array to (batch_size * num_class, n_ctx) and feed it to the model?

<!-- gh-comment-id:969668567 --> @jongwook commented on GitHub (Nov 16, 2021): Can you try reshaping the array to (batch_size * num_class, n_ctx) and feed it to the model?
Author
Owner

@hanxiao commented on GitHub (Apr 10, 2022):

for batch inference, you can use https://github.com/jina-ai/clip-as-service/

<!-- gh-comment-id:1094349105 --> @hanxiao commented on GitHub (Apr 10, 2022): for batch inference, you can use https://github.com/jina-ai/clip-as-service/
Author
Owner

@Angtrim commented on GitHub (Feb 28, 2023):

Did you find a solution?

<!-- gh-comment-id:1447820698 --> @Angtrim commented on GitHub (Feb 28, 2023): Did you find a solution?
Author
Owner

@ahmadmustafaanis commented on GitHub (Apr 19, 2023):

The best working solution is to torch.stack to do batch inference.

imgs = [preprocess(img) for img in imgs]

logits_per_image, logits_per_text = model(
        torch.stack(imgs).to(device), tokenized_text
    )
<!-- gh-comment-id:1514519472 --> @ahmadmustafaanis commented on GitHub (Apr 19, 2023): The best working solution is to torch.stack to do batch inference. ```python3 imgs = [preprocess(img) for img in imgs] logits_per_image, logits_per_text = model( torch.stack(imgs).to(device), tokenized_text ) ```
Author
Owner

@lucasjinreal commented on GitHub (Aug 14, 2024):

imgs = [preprocess(img) for img in imgs]

this func is very slow.

<!-- gh-comment-id:2288217658 --> @lucasjinreal commented on GitHub (Aug 14, 2024): > imgs = [preprocess(img) for img in imgs] this func is very slow.
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#152
No description provided.