This project demonstrates how to fine-tune the LLaMA3 8B transformer model for image captioning using the Flickr30k dataset. Images are encoded with the CLIP image encoder, and the model is trained to generate captions for images.
download_images.py: Download and save test images from the Flickr30k dataset.fine_tune_llama.py: Fine-tune the LLaMA3 8B model using CLIP-encoded images and captions.inference.py: Run inference to generate captions for new images using the trained model.inference_cached.py: Inference with cached Q, K, V tensors for efficiency.inference_cached_topk_sampling.py: Inference with cached tensors and top-k sampling for more diverse captions.requirements.txt: Python dependencies.
- Clone the repository and navigate to the project folder.
- Install dependencies:
pip install -r requirements.txt
- Set your Hugging Face token (required for LLaMA3):
- On Windows PowerShell:
$env:HF_TOKEN="your_hf_token"
- On Linux/macOS:
export HF_TOKEN=your_hf_token
- On Windows PowerShell:
Run the following to download and save test images from Flickr30k:
python download_images.pyImages will be saved in the test_images/ directory.
To fine-tune LLaMA3 8B on the Flickr30k dataset:
python fine_tune_llama.py- Training uses LoRA for parameter-efficient fine-tuning.
- Weights & Biases is used for experiment tracking (set your entity/project in the script if needed).
Generate captions for images using the trained model:
python inference.pyFor faster inference using cached Q, K, V tensors:
python inference_cached.pyFor more diverse captions using top-k sampling:
python inference_cached_topk_sampling.py- Make sure you have enough GPU memory for LLaMA3 8B.
- Set the
HF_TOKENenvironment variable before running scripts that require model download. - The model and adapters are saved in the
trained_clip_llamadirectory after training.
See requirements.txt for all dependencies:
- torch
- transformers
- datasets
- Pillow
- sentencepiece
- requests
- tqdm
- scikit-learn
- wandb
- peft
See LICENSE for details.