Upload README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,53 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
readme_content = """# Image Captioning Model
|
| 2 |
+
|
| 3 |
+
This is an Image Captioning model trained on a custom dataset with 6000 training images and 1000 validation images. It uses ResNet50 for image feature extraction and an LSTM with fine-tuned GloVe embeddings for caption generation.
|
| 4 |
+
|
| 5 |
+
## Model Details
|
| 6 |
+
- **Image Encoder**: ResNet50
|
| 7 |
+
- **Text Decoder**: LSTM with fine-tuned GloVe embeddings
|
| 8 |
+
- **Vocabulary Size**: 3478
|
| 9 |
+
- **Embedding Dimension**: 512
|
| 10 |
+
- **Hidden Size**: 1024
|
| 11 |
+
- **Number of Layers**: 2
|
| 12 |
+
- **Max Caption Length**: 30
|
| 13 |
+
|
| 14 |
+
## Usage
|
| 15 |
+
Load the model and tokenizer using PyTorch and the provided `model.py`.
|
| 16 |
+
|
| 17 |
+
### Example
|
| 18 |
+
```python
|
| 19 |
+
import torch
|
| 20 |
+
from PIL import Image
|
| 21 |
+
import pickle
|
| 22 |
+
from huggingface_hub import hf_hub_download
|
| 23 |
+
from model import image_captioning_model
|
| 24 |
+
|
| 25 |
+
# Download files
|
| 26 |
+
repo_id = "Pumponchik/image_caption_weak"
|
| 27 |
+
model_path = hf_hub_download(repo_id=repo_id, filename="pytorch_model.bin")
|
| 28 |
+
config_path = hf_hub_download(repo_id=repo_id, filename="config.json")
|
| 29 |
+
tokenizer_path = hf_hub_download(repo_id=repo_id, filename="tokenizer.pkl")
|
| 30 |
+
transform_path = hf_hub_download(repo_id=repo_id, filename="image_transforms.pkl")
|
| 31 |
+
|
| 32 |
+
# Load config
|
| 33 |
+
with open(config_path, "r") as f:
|
| 34 |
+
config = json.load(f)
|
| 35 |
+
|
| 36 |
+
# Load model
|
| 37 |
+
model = image_captioning_model(**config)
|
| 38 |
+
model.load_state_dict(torch.load(model_path))
|
| 39 |
+
model.eval()
|
| 40 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 41 |
+
model.to(device)
|
| 42 |
+
|
| 43 |
+
# Load tokenizer and transforms
|
| 44 |
+
with open(tokenizer_path, "rb") as f:
|
| 45 |
+
tokenizer = pickle.load(f)
|
| 46 |
+
with open(transform_path, "rb") as f:
|
| 47 |
+
transform = pickle.load(f)
|
| 48 |
+
|
| 49 |
+
# Generate caption
|
| 50 |
+
image = Image.open("example.png").convert("RGB")
|
| 51 |
+
image_tensor = transform(image).unsqueeze(0).to(device)
|
| 52 |
+
caption = model.generate_caption(image_tensor, tokenizer, max_length=config["max_len"], device=device)
|
| 53 |
+
print("Generated Caption:", caption)
|