This project implements an image captioning model using a CNN-RNN architecture with PyTorch. The model is trained on the Flickr30k dataset to generate descriptive captions for given images.
The model uses a pre-trained ResNet-50 as a CNN encoder to extract feature vectors from images. These features are then fed into a Long Short-Term Memory (LSTM) network decoder to generate a sequence of words that form a caption.
- Encoder: ResNet-50 (pre-trained on ImageNet)
- Decoder: LSTM Network
- Dataset: Flickr30k
- Framework: PyTorch
image_captioning/
├── data/
│ ├── flickr30k_images/
│ │ └── ... (image files)
│ └── cleaned_results.csv
├── saved_models/
│ └── final_image_captioning_model.pth
├── config.py
├── dataset.py
├── model.py
├── vocabulary.py
├── train.py
├── test.py
└── README.md
Follow these steps to set up your environment and install dependencies.
git clone <repo_url>
cd image_captioningpython -m venv venv
# On Windows:
venv\Scripts\activate
# On Mac/Linux:
source venv/bin/activateCreate a requirements.txt with:
torch
torchvision
pandas
spacy
tqdm
Pillow
Then run:
pip install -r requirements.txtpython -m spacy download en_core_web_smThis model is trained on the Flickr30k dataset.
-
Download the dataset and place all images in:
data/flickr30k_images/ -
Ensure your captions CSV is named:
data/cleaned_results.csv
Run the training script:
python train.pyAll training hyperparameters and paths can be modified in config.py.
The trained model and vocabulary object will be saved to:
saved_models/final_image_captioning_model.pth
To generate a caption for an image:
python test.py "path/to/your/image.jpg"Example:
python test.py "data/flickr30k_images/69854977.jpg"The script will:
- Load the model
- Generate a caption
- Print the caption
- Attempt to display the image
config.py: Contains hyperparameters and path configs.vocabulary.py: Handles tokenization and word-to-index mapping.dataset.py: Loads images and captions viaFlickerDatasetandMyCollate.model.py: DefinesEncoderCNNandDecoderRNN(ResNet + LSTM).train.py: Script to train the model and save weights.test.py: Loads a trained model and generates a caption for a given image.
Feel free to fork and improve this project. Contributions are welcome!