Skip to content

CLIP (Contrastive Language-Image Pre-Training) is a neural network trained on a variety of (image, text) pairs.

Notifications You must be signed in to change notification settings

Stilanof/glipse_OpenAI-s_CLIP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

glipse_openAI_clip

CLIP (Contrastive Language-Image Pre-Training) is a neural network trained on a variety of (image, text) pairs.


Inference Notebook:

Disclaimer: the author do not own any rights for the code.

Practice loading the model

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

Another example

#let's try another model with my Linkedin profile picture! 

device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("RN50x16", device=device)

image = preprocess(Image.open("yo 4.jpg")).unsqueeze(0).to(device)
text = clip.tokenize(["preffesional", "unprofessional", "super professional"]).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.00197463 0.08398551 0.91403985]]

Acknowledgments

Read how all works here ("https://github.com/openai/CLIP#clipavailable_models")

About

CLIP (Contrastive Language-Image Pre-Training) is a neural network trained on a variety of (image, text) pairs.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published