Created 2 years ago
!pip install transformers
!pip install datasets
!pip install torch
import transformers
import datasets
import numpy as np
import pandas as pd
import torch
from PIL import Image
import requests
from transformers import CLIPTokenizerFast, CLIPProcessor, CLIPModel
device = "cuda" if torch.cuda.is_available() else "cpu"
model_id = "openai/clip-vit-base-patch32"
# we initialize a tokenizer, image processor, and the model itself
tokenizer = CLIPTokenizerFast.from_pretrained(model_id)
processor = CLIPProcessor.from_pretrained(model_id)
model = CLIPModel.from_pretrained(model_id).to(device)
ftfy or spacy is not installed using BERT BasicTokenizer instead of ftfy.
urls=['https://images.unsplash.com/photo-1662955676669-c5d141718bfd?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=687&q=80',
'https://images.unsplash.com/photo-1552053831-71594a27632d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=662&q=80',
'https://images.unsplash.com/photo-1530281700549-e82e7bf110d6?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=688&q=80']
images=[Image.open(requests.get(i, stream=True).raw) for i in urls]
images
[<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=687x1031 at 0x7F99B0160D90>,
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=662x1061 at 0x7F99E1641410>,
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=688x1031 at 0x7F9A00CEE090>]