RemoteCLIP / app.py
hlydecker's picture
Update app.py
1f867fb
raw
history blame
1.64 kB
import gradio as gr
from huggingface_hub import hf_hub_download
import torch, open_clip
from PIL import Image
from IPython.display import display
for model_name in ['RN50', 'ViT-B-32', 'ViT-L-14']:
checkpoint_path = hf_hub_download("chendelong/RemoteCLIP", f"RemoteCLIP-{model_name}.pt", cache_dir='checkpoints')
print(f'{model_name} is downloaded to {checkpoint_path}.')
model_name = 'RN50' # 'RN50' or 'ViT-B-32' or 'ViT-L-14'
model, _, preprocess = open_clip.create_model_and_transforms(model_name)
tokenizer = open_clip.get_tokenizer(model_name)
path_to_your_checkpoints = 'checkpoints/models--chendelong--RemoteCLIP/snapshots/bf1d8a3ccf2ddbf7c875705e46373bfe542bce38'
ckpt = torch.load(f"{path_to_your_checkpoints}/RemoteCLIP-{model_name}.pt", map_location="cpu")
def remote_clip(input_image,input_text):
text_queries = [input_text]
text = tokenizer(text_queries)
image = Image.open(input_image)
image = preprocess(image).unsqueeze(0)
with torch.no_grad(), torch.cuda.amp.autocast():
image_features = model.encode_image(image.cuda())
text_features = model.encode_text(text.cuda())
image_features /= image_features.norm(dim=-1, keepdim=True)
text_features /= text_features.norm(dim=-1, keepdim=True)
text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1).cpu().numpy()[0]
print(f'Predictions of {model_name}:')
for query, prob in zip(text_queries, text_probs):
print(f"{query:<40} {prob * 100:5.1f}%")
demo = gr.Interface(fn=greet, inputs=[gr.Image(type="pil"), gr.Text(type="text")], outputs="text")
demo.launch()