CoolFace
Modelpublic

deepklarity/poster2plot

sourceHugging Faceupdated 5y agoView on Hugging Face
4likes43downloads
README.md99 linesDownload Raw Back to root
1---2language: en3tags:4- image-classification5- image-captioning6 7---8 9# Poster2Plot10 11An image captioning model to generate movie/t.v show plot from poster. It generates decent plots but is no way perfect. We are still working on improving the model.12 13## Live demo on Hugging Face Spaces: https://huggingface.co/spaces/deepklarity/poster2plot14 15# Model Details16 17The base model uses a Vision Transformer (ViT) model as an image encoder and GPT-2 as a decoder.18 19We used the following models:20 21* Encoder: [google/vit-base-patch16-224-in21k](https://huggingface.co/google/vit-base-patch16-224-in21k)22* Decoder: [gpt2](https://huggingface.co/gpt2)23 24# Datasets25 26Publicly available IMDb datasets were used to train the model.27 28# How to use29 30## In PyTorch31 32```python33import torch34import re35import requests36from PIL import Image37from transformers import AutoTokenizer, AutoFeatureExtractor, VisionEncoderDecoderModel38 39# Pattern to ignore all the text after 2 or more full stops40regex_pattern = "[.]{2,}"41 42 43def post_process(text):44    try:45        text = text.strip()46        text = re.split(regex_pattern, text)[0]47    except Exception as e:48        print(e)49        pass50    return text51 52 53def predict(image, max_length=64, num_beams=4):54    pixel_values = feature_extractor(images=image, return_tensors="pt").pixel_values55    pixel_values = pixel_values.to(device)56 57    with torch.no_grad():58        output_ids = model.generate(59            pixel_values,60            max_length=max_length,61            num_beams=num_beams,62            return_dict_in_generate=True,63        ).sequences64 65    preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)66    pred = post_process(preds[0])67 68    return pred69 70 71model_name_or_path = "deepklarity/poster2plot"72device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")73 74# Load model.75 76model = VisionEncoderDecoderModel.from_pretrained(model_name_or_path)77model.to(device)78print("Loaded model")79 80feature_extractor = AutoFeatureExtractor.from_pretrained(model.encoder.name_or_path)81print("Loaded feature_extractor")82 83tokenizer = AutoTokenizer.from_pretrained(model.decoder.name_or_path, use_fast=True)84if model.decoder.name_or_path == "gpt2":85    tokenizer.pad_token = tokenizer.eos_token86 87print("Loaded tokenizer")88 89url = "https://upload.wikimedia.org/wikipedia/en/2/26/Moana_Teaser_Poster.jpg"90with Image.open(requests.get(url, stream=True).raw) as image:91    pred = predict(image)92 93print(pred)94 95```96 97 98 99