[PR #259] [MERGED] Add PyTorch Hub configuration file #477

Closed
opened 2026-10-31 21:15:23 +00:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/openai/CLIP/pull/259
Author: @johnsutor
Created: 6/28/2022
Status: ✅ Merged
Merged: 7/14/2022
Merged by: @jongwook

Base: main ← Head: main


📝 Commits (1)

  • 8fa3a61 Add PyTorch Hub configuration file

📊 Changes

1 file changed (+42 additions, -0 deletions)

View changed files

➕ hubconf.py (+42 -0)

📄 Description

This PR aims to add support for PyTorch's hub loading feature. This would make model loading as simple as specifying this git repo to download the CLIP model along with its pre-trained weights. When working with torch.hub.load, the example would change from this

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]]

to this

import torch
from PIL import Image

device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = torch.hub.load("openai/CLIP", "ViT_B_32", device=device)
tokenize = torch.hub.load("openai/CLIP", "tokenize")

image = preprocess(Image.open("CLIP.png")).unsqueeze(0).to(device)
text = 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]]

---

<sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
## 📋 Pull Request Information **Original PR:** https://github.com/openai/CLIP/pull/259 **Author:** [@johnsutor](https://github.com/johnsutor) **Created:** 6/28/2022 **Status:** ✅ Merged **Merged:** 7/14/2022 **Merged by:** [@jongwook](https://github.com/jongwook) **Base:** `main` ← **Head:** `main` --- ### 📝 Commits (1) - [`8fa3a61`](https://github.com/openai/CLIP/commit/8fa3a61743fbd917ab4e84aa83b45a1dda484a81) Add PyTorch Hub configuration file ### 📊 Changes **1 file changed** (+42 additions, -0 deletions) <details> <summary>View changed files</summary> ➕ `hubconf.py` (+42 -0) </details> ### 📄 Description This PR aims to add support for PyTorch's hub loading feature. This would make model loading as simple as specifying this git repo to download the CLIP model along with its pre-trained weights. When working with `torch.hub.load`, the example would change from this ```py 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]] ``` to this ```py import torch from PIL import Image device = "cuda" if torch.cuda.is_available() else "cpu" model, preprocess = torch.hub.load("openai/CLIP", "ViT_B_32", device=device) tokenize = torch.hub.load("openai/CLIP", "tokenize") image = preprocess(Image.open("CLIP.png")).unsqueeze(0).to(device) text = 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]] --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-31 21:15:23 +00:00
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#477
No description provided.