Skip to content

Instantly share code, notes, and snippets.

@lmmx
Last active February 7, 2023 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmmx/22fe153b5bba657d827be62c3e927dd9 to your computer and use it in GitHub Desktop.
Save lmmx/22fe153b5bba657d827be62c3e927dd9 to your computer and use it in GitHub Desktop.
Simple script to run the CoCa model
from pathlib import Path
from sys import argv
import open_clip
import torch
from PIL import Image
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
coca_model, _, preprocess = open_clip.create_model_and_transforms(
model_name="coca_ViT-L-14",
pretrained="mscoco_finetuned_laion2B-s13B-b90k",
device=device,
)
def generate_caption_coca(image):
im = preprocess(image).unsqueeze(0).to(device)
with torch.no_grad(), torch.autocast(device_type=device.type):
generated = coca_model.generate(im, seq_len=20)
return (
open_clip.decode(generated[0].detach())
.split("<end_of_text>")[0]
.replace("<start_of_text>", "")
)
assert len(argv) > 1, "No image path provided"
p = Path(argv[1]).absolute()
assert p.exists(), "No valid path provided"
im = Image.open(p).convert("RGB")
res = generate_caption_coca(im)
print(res)
conda create -n coca python
conda activate coca
conda install pytorch torchvision pytorch-cuda=11.7 -c pytorch -c nvidia
conda install transformers open-clip-torch -c conda-forge

CPU version:

conda create -n coca python
conda activate coca
conda install pytorch torchvision cpuonly -c pytorch
conda install transformers open-clip-torch -c conda-forge
@lmmx
Copy link
Author

lmmx commented Feb 7, 2023

mag-glass-glyph-u1F50E-white-beatchain

  • "a magnifying glass with a black handle . "

pikachu

  • "a cartoon of a pikachu wearing sunglasses ."

A_Few_Moments_Later-1755730474

  • "a few moments later title card"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment