vit-gpt2-image-captioning

The nlpconnect/vit‑gpt2‑image‑captioning model is a Vision‑Encoder‑Decoder system that converts raw images into fluent, English‑language descriptions. It combines a Vision Transformer (ViT) as the visual encoder with a GPT‑2 decoder that generates natural‑language captions. The model is fully implemented in PyTorch and is compatible with the Hugging Face

nlpconnect 543K downloads apache-2.0 Image Captioning
Frameworkstransformerspytorch
Tagsvision-encoder-decoderimage-text-to-textimage-to-textimage-captioningdoi:10.57967/hf/0222
Downloads
543K
License
apache-2.0
Pipeline
Image Captioning
Author
nlpconnect

Run vit-gpt2-image-captioning locally on a Q4KM hard drive

Accelerate your deployment with a Q4KM hard‑drive pre‑loaded with the nlpconnect/vit‑gpt2‑image‑captioning model. Get instant, out‑of‑the‑box performance on edge devices and reduce download overhead....

Shop Q4KM Drives

Technical Overview

The nlpconnect/vit‑gpt2‑image‑captioning model is a Vision‑Encoder‑Decoder system that converts raw images into fluent, English‑language descriptions. It combines a Vision Transformer (ViT) as the visual encoder with a GPT‑2 decoder that generates natural‑language captions. The model is fully implemented in PyTorch and is compatible with the Hugging Face pipeline("image-to-text") API, making it easy to plug into existing Python workloads.

Key features and capabilities

  • End‑to‑end caption generation with a maximum length of 16 tokens (configurable).
  • Beam search decoding (default num_beams=4) for higher quality, diverse captions.
  • Supports both local image files and remote URLs via the pipeline interface.
  • Pre‑trained on the COCO English caption dataset, giving it a solid grounding in everyday visual concepts.
  • Fully compatible with Hugging Face VisionEncoderDecoderModel, ViTImageProcessor, and AutoTokenizer, enabling custom inference pipelines.

Architecture highlights

  • Encoder: ViT‑Base (12 transformer layers, 768‑dim hidden size) that extracts a 2‑D patch embedding from the input image and produces a sequence of visual tokens.
  • Decoder: GPT‑2‑Small (12 layers, 768‑dim hidden size) that consumes the encoder’s final hidden state as cross‑attention context and generates token‑by‑token captions.
  • Cross‑attention: The decoder attends to the encoder output at every generation step, allowing the model to ground each word in visual evidence.
  • Tokenizer: Byte‑Pair Encoding (BPE) tokenizer from GPT‑2, providing a compact vocabulary for English text.

Intended use cases

  • Automatic alt‑text generation for accessibility on websites and social media.
  • Image indexing and retrieval systems that rely on textual metadata.
  • Assistive tools for visually‑impaired users that describe photos in real time.
  • Content moderation pipelines that need a textual summary of image content.

Benchmark Performance

For image‑captioning models, the most relevant benchmarks are the COCO captioning metrics – BLEU, METEOR, ROUGE‑L, CIDEr, and SPICE – which quantify how closely generated captions match human‑written references. The original COCO‑trained checkpoint (the source of this model) reports a CIDEr score of ~115 and a BLEU‑4 score of ~36, placing it in the “mid‑range” of modern encoder‑decoder captioners.

These metrics matter because they correlate with perceived caption quality, relevance, and grammaticality. Compared to other open‑source ViT‑GPT‑2 hybrids, nlpconnect/vit‑gpt2‑image‑captioning offers a good trade‑off between speed (thanks to the relatively lightweight GPT‑2‑Small decoder) and caption richness. It outperforms older CNN‑based encoders (e.g., ResNet‑50) on CIDEr while remaining faster than larger transformer‑decoder variants such as T5‑Base.

Hardware Requirements

  • VRAM for inference: The model occupies roughly 1.2 GB of GPU memory when using a batch size of 1 and torch.float16. A 4 GB GPU (e.g., NVIDIA Tesla T4) is sufficient for single‑image inference; larger batches benefit from 8 GB+.
  • Recommended GPU: NVIDIA RTX 3060 (12 GB) or higher for low‑latency batch processing; the model also runs on AMD GPUs via ROCm with the same memory profile.
  • CPU requirements: A modern multi‑core CPU (Intel i5‑10600K or AMD Ryzen 5 5600X) can handle preprocessing (ViTImageProcessor) and tokenization without a GPU, but inference will be slower (≈ 2 seconds per image).
  • Storage: Model checkpoint size ≈ 1.5 GB (including encoder, decoder, tokenizer, and feature‑extractor files). Add ~200 MB for the sample images used in the README.
  • Performance characteristics: On an RTX 3060, the model generates a 16‑token caption in ~150 ms per image (batch = 1) using FP16. Beam search with num_beams=4 adds ~30 % overhead.

Use Cases

  • Accessibility tools: Auto‑generate alt‑text for images on e‑learning platforms, news sites, and social media to comply with WCAG standards.
  • Digital asset management: Index large image libraries (e.g., stock‑photo sites) with textual tags derived from captions for faster search.
  • Robotics & autonomous systems: Provide a natural‑language summary of a robot’s camera feed to human operators.
  • Content moderation: Summarize image content for downstream classifiers that flag inappropriate or policy‑violating material.
  • Creative assistance: Suggest captions for marketing creatives, blog posts, or photo‑journalism pieces.

Training Details

The checkpoint was derived from the Flax image‑captioning example and later ported to PyTorch. Training was performed on the COCO 2017 English caption dataset (≈ 123 k images, 5 captions each). The visual encoder (ViT‑Base) was initialized from a publicly available ImageNet‑pre‑trained checkpoint, while the GPT‑2 decoder started from the standard GPT‑2‑Small weights.

Training hyper‑parameters (as reported in the source repo) include:

  • Batch size: 64 images per GPU (mixed‑precision FP16).
  • Learning rate: 5e‑5 with a linear warm‑up for the first 5 % of steps.
  • Optimizer: AdamW with weight decay of 0.01.
  • Number of epochs: 10 (≈ 300 k training steps).

The model can be fine‑tuned on domain‑specific caption datasets (e.g., medical imaging, fashion) by loading the VisionEncoderDecoderModel and continuing training with a lower learning rate (1e‑5) for a few epochs. The Hugging Face Trainer API simplifies this process.

Licensing Information

The model is released under the Apache 2.0 license, as indicated by the repository tags. Apache 2.0 is a permissive open‑source license that grants users the right to use, modify, distribute, and even commercialize the software, provided that they retain the original copyright notice and a copy of the license.

Because the license is permissive, you may embed the model in commercial products (e.g., SaaS platforms, mobile apps, or hardware devices) without paying royalties. The only mandatory requirement is proper attribution – you should credit the original authors (nlpconnect / ydshieh) and include a copy of the Apache 2.0 license in your distribution.

There are no “unknown” restrictions; the license does not impose copyleft obligations, nor does it restrict usage in export‑controlled contexts. However, you should still review the model card for any additional community‑level guidelines.

Pre-loaded AI models. Ready to run.

Skip the downloads. Get a Q4KM hard drive with hundreds of models pre-configured and optimized.

Shop Q4KM Hard Drives