|
|
|
"""Image Captioning with ViT+GPT2 |
|
|
|
Automatically generated by Colaboratory. |
|
|
|
Original file is located at |
|
https://colab.research.google.com/drive/1_dnSI55E0UX92QMvMj5_z8sgl2WVkixA |
|
""" |
|
|
|
|
|
|
|
|
|
|
|
from PIL import Image |
|
from transformers import VisionEncoderDecoderModel, ViTFeatureExtractor, PreTrainedTokenizerFast |
|
import requests |
|
|
|
model = VisionEncoderDecoderModel.from_pretrained("sachin/vit2distilgpt2") |
|
|
|
vit_feature_extractor = ViTFeatureExtractor.from_pretrained("google/vit-base-patch16-224-in21k") |
|
|
|
tokenizer = PreTrainedTokenizerFast.from_pretrained("distilgpt2") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def vit2distilgpt2(img): |
|
pixel_values = vit_feature_extractor(images=img, return_tensors="pt").pixel_values |
|
encoder_outputs = generated_ids = model.generate(pixel_values.to('cpu'),num_beams=5) |
|
generated_sentences = tokenizer.batch_decode(encoder_outputs, skip_special_tokens=True) |
|
|
|
return(generated_sentences[0].split('.')[0]) |
|
|
|
|
|
|
|
import gradio as gr |
|
|
|
inputs = [ |
|
gr.inputs.Image(type="pil", label="Original Image") |
|
] |
|
|
|
outputs = [ |
|
gr.outputs.Textbox(label = 'Caption') |
|
] |
|
|
|
title = "Image Captioning using ViT + GPT2" |
|
description = "ViT and GPT2 are used to generate Image Caption for the uploaded image. COCO Dataset was used for training. This image captioning model might have some biases that we couldn't figure during our stress testing, so if you find any bias (gender, race and so on) please use `Flag` button to flag the image with bias" |
|
article = " <a href='https://huggingface.co/sachin/vit2distilgpt2'>Model Repo on Hugging Face Model Hub</a>" |
|
examples = [ |
|
["people-walking-street-pedestrian-crossing-traffic-light-city.jpeg"], |
|
["elonmusk.jpeg"] |
|
|
|
] |
|
|
|
gr.Interface( |
|
vit2distilgpt2, |
|
inputs, |
|
outputs, |
|
title=title, |
|
description=description, |
|
article=article, |
|
examples=examples, |
|
theme="huggingface", |
|
).launch(debug=True, enable_queue=True) |
|
|
|
|