bipin/image-caption-generator
1990
1---2tags:3- image-captioning4- image-to-text5model-index:6- name: image-caption-generator7 results: []8---9 10<!-- This model card has been generated automatically according to the information the Trainer had access to. You11should probably proofread and complete it, then remove this comment. -->12 13# Image-caption-generator14 15This model is trained on [Flickr8k](https://www.kaggle.com/datasets/nunenuh/flickr8k) dataset to generate captions given an image.16 17It achieves the following results on the evaluation set:18- eval_loss: 0.253619- eval_runtime: 25.36920- eval_samples_per_second: 63.81821- eval_steps_per_second: 8.00222- epoch: 4.023- step: 323624 25# Running the model using transformers library26 271. Load the pre-trained model from the model hub28 ```python29 from transformers import VisionEncoderDecoderModel, ViTFeatureExtractor, AutoTokenizer30 import torch31 from PIL import Image32 33 model_name = "bipin/image-caption-generator"34 35 # load model36 model = VisionEncoderDecoderModel.from_pretrained(model_name)37 feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)38 tokenizer = AutoTokenizer.from_pretrained("gpt2")39 40 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")41 model.to(device)42 ```43 442. Load the image for which the caption is to be generated(note: replace the value of `img_name` with image of your choice)45 ```python46 ### replace the value with your image47 img_name = "flickr_data.jpg"48 img = Image.open(img_name)49 if img.mode != 'RGB':50 img = img.convert(mode="RGB")51 ```52 533. Pre-process the image54 ```python55 pixel_values = feature_extractor(images=[img], return_tensors="pt").pixel_values56 pixel_values = pixel_values.to(device)57 ```58 594. Generate the caption60 ```python61 max_length = 12862 num_beams = 463 64 # get model prediction65 output_ids = model.generate(pixel_values, num_beams=num_beams, max_length=max_length)66 67 # decode the generated prediction68 preds = tokenizer.decode(output_ids[0], skip_special_tokens=True)69 print(preds)70 ```71 72## Training procedure73The procedure used to train this model can be found [here](https://bipinkrishnan.github.io/ml-recipe-book/image_captioning.html).74 75### Training hyperparameters76 77The following hyperparameters were used during training:78- learning_rate: 5e-0579- train_batch_size: 880- eval_batch_size: 881- seed: 4282- optimizer: Adam with betas=(0.9,0.999) and epsilon=1e-0883- lr_scheduler_type: linear84- num_epochs: 585 86### Framework versions87 88- Transformers 4.16.289- Pytorch 1.9.190- Datasets 1.18.491- Tokenizers 0.11.692 