First version of TeLVE!
Browse files# The TeLVE v1.0

Files changed (11)
- .gitattributes+1
- README.md+69-3
- images/mugla.jpgAdded
- imagine.pyAdded+103
- main.pyAdded+167
- models/TeLVE_v1.0.pthAdded+3
- teLVE_logo.pngAdded
- tokenizer/special_tokens_map.jsonAdded+7
- tokenizer/tokenizer.jsonAdded
- tokenizer/tokenizer_config.jsonAdded+58
- tokenizer/vocab.txtAdded
32 unmodified lines33343532 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text32 unmodified lines3334353632 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -textimages/mugla.jpg filter=lfs diff=lfs merge=lfs -text
123---license: cc-by-4.0---123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869# TeLVE: Turkish efficient Language Vision Engine 🧿[](https://creativecommons.org/licenses/by/4.0/)[](https://huggingface.co/outsu/TeLVE)## First Turkish VLM ever!TeLVE is the first Visual Language Model specifically designed for Turkish language understanding and image description generation. Built on Vision Transformer (ViT) and BERT pre-trained encoder architectures, it bridges the gap in Turkish visual-linguistic processing.No module named 'imagine'## Model DescriptionTeLVE combines:- 🖼️ Vision Transformer (ViT-base-patch16-224)- 📝 Turkish BERT (dbmdz/bert-base-turkish-cased)- 🔄 Cross-attention mechanism for vision-language fusion### Version Logs- **TeLVE v1.0**: Trained on Unsplash Lite dataset## UsageThe model can be used in two ways:### Inference (imagine.py)```python# Generate captions for imagespython imagine.py```This script:- Loads a trained TeLVE model- Takes images from `images` directory- Generates Turkish captions for each image- Outputs the results to console### Training (main.py)Users can train their own models with ViT and BERT encoders.```python# Train a new modelpython main.py```This script:- Loads and preprocesses image-caption pairs- Initializes ViT and BERT encoders- Trains the combined model- Saves the model and tokenizer## PerformancePerformance scores will be evaluated.<!--| Model Version | Dataset | BLEU-4 | METEOR | CIDEr ||--------------|---------|---------|---------|--------|| TeLVE v1.0 | Unsplash | *TBD* | *TBD* | *TBD* || TeLVE v1.1 | Unsplash+Pexels | *TBD* | *TBD* | *TBD* |-->## Citation```bibtex@software{telve2024,author = {Öğüt Su Karagün},title = {TeLVE: Turkish efficient Language Vision Engine},year = {2024},url = {https://huggingface.co/outsu/TeLVE}}```## LicenseThis work is licensed under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/).
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103import torch
import torch.nn as nn
from torchvision import transforms
from transformers import ViTModel, BertTokenizerFast, BertConfig, BertLMHeadModel
from PIL import Image
import os
# Check if CUDA is available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
# Define constants
VIT_MODEL_NAME = "google/vit-base-patch16-224"
BERT_MODEL_NAME = "dbmdz/bert-base-turkish-cased"
MAX_LENGTH = 128
class ImageCaptioningModel(nn.Module):
def __init__(self, vit_model, bert_model):
super(ImageCaptioningModel, self).__init__()
self.vit = vit_model
self.bert = bert_model
self.linear = nn.Linear(self.vit.config.hidden_size, self.bert.config.hidden_size)
def forward(self, pixel_values, input_ids, attention_mask, labels=None):
image_features = self.vit(pixel_values).last_hidden_state
image_features = self.linear(image_features)
outputs = self.bert(input_ids=input_ids,
attention_mask=attention_mask,
encoder_hidden_states=image_features,
labels=labels,
return_dict=True)
return outputs.loss, outputs.logits
def load_model(model_path):
# Initialize the model components
vit_model = ViTModel.from_pretrained(VIT_MODEL_NAME)
bert_config = BertConfig.from_pretrained(BERT_MODEL_NAME)
bert_config.is_decoder = True
bert_config.add_cross_attention = True
bert_model = BertLMHeadModel.from_pretrained(BERT_MODEL_NAME, config=bert_config)
# Create the combined model
model = ImageCaptioningModel(vit_model, bert_model)
model.load_state_dict(torch.load(model_path, map_location=device))
model.to(device)
model.eval()
return model
def generate_caption(model, image_path, tokenizer):
# Prepare the image
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
image = Image.open(image_path).convert('RGB')
image = transform(image).unsqueeze(0).to(device)
# Generate the caption
with torch.no_grad():
input_ids = torch.tensor([[tokenizer.cls_token_id]]).to(device)
attention_mask = torch.tensor([[1]]).to(device)
for _ in range(MAX_LENGTH):
_, logits = model(image, input_ids, attention_mask)
next_token = logits[:, -1, :].argmax(dim=-1)
if next_token.item() == tokenizer.sep_token_id:
break
input_ids = torch.cat([input_ids, next_token.unsqueeze(0)], dim=1)
attention_mask = torch.cat([attention_mask, torch.tensor([[1]]).to(device)], dim=1)
caption = tokenizer.decode(input_ids[0], skip_special_tokens=True)
return caption
def main():
model_path = "./models/TeLVE_v1.1.pth"
tokenizer_path = "./tokenizer"
# Check if the model and tokenizer exist
if not os.path.exists(model_path) or not os.path.exists(tokenizer_path):
print("Model or tokenizer not found. Please make sure you have trained the model and saved it correctly.")
return
# Load the model and tokenizer
model = load_model(model_path)
tokenizer = BertTokenizerFast.from_pretrained(tokenizer_path)
# Generate captions for images in a specified directory
image_dir = "./images" # Change this to the directory containing your test images
for image_file in os.listdir(image_dir):
if image_file.lower().endswith(('.png', '.jpg', '.jpeg')):
image_path = os.path.join(image_dir, image_file)
caption = generate_caption(model, image_path, tokenizer)
print(f"Image: {image_file}")
print(f"Generated Caption: {caption}")
print("---")
if __name__ == "__main__":
main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167import os
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
from transformers import ViTModel, BertTokenizerFast, BertConfig, BertLMHeadModel, AdamW
from PIL import Image, ImageFile
import pandas as pd
from tqdm import tqdm
# Increase the maximum image size limit to avoid DecompressionBombWarning
Image.MAX_IMAGE_PIXELS = None
# Allow loading truncated images
ImageFile.LOAD_TRUNCATED_IMAGES = True
# Check if CUDA is available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
# Define constants
VIT_MODEL_NAME = "google/vit-base-patch16-224"
BERT_MODEL_NAME = "dbmdz/bert-base-turkish-cased" # Using a Turkish BERT model
model = "TeLVE_v1.0.pth"
MAX_LENGTH = 128
BATCH_SIZE = 8
EPOCHS = 5
LEARNING_RATE = 2e-5
class ImageCaptioningDataset(Dataset):
def __init__(self, dataframe, img_dir, tokenizer):
self.dataframe = dataframe
self.img_dir = img_dir
self.tokenizer = tokenizer
self.transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
def __len__(self):
return len(self.dataframe)
def __getitem__(self, idx):
row = self.dataframe.iloc[idx]
img_path = os.path.join(self.img_dir, row['photo_id'] + ".jpg")
try:
image = Image.open(img_path).convert('RGB')
image = self.transform(image)
except (FileNotFoundError, IOError):
# Return None if the image is not found or cannot be opened
return None
caption = row['ai_description']
# Check if caption is a valid string
if not isinstance(caption, str):
return None # Skip the example if caption is not valid
encoding = self.tokenizer(
caption,
add_special_tokens=True,
max_length=MAX_LENGTH,
padding='max_length',
truncation=True,
return_attention_mask=True,
return_tensors='pt'
)
return {
'pixel_values': image,
'input_ids': encoding['input_ids'].squeeze(),
'attention_mask': encoding['attention_mask'].squeeze(),
'labels': encoding['input_ids'].squeeze() # Use input_ids as labels for calculating loss
}
class ImageCaptioningModel(nn.Module):
def __init__(self, vit_model, bert_model):
super(ImageCaptioningModel, self).__init__()
self.vit = vit_model
self.bert = bert_model
self.linear = nn.Linear(self.vit.config.hidden_size, self.bert.config.hidden_size)
def forward(self, pixel_values, input_ids, attention_mask, labels=None):
image_features = self.vit(pixel_values).last_hidden_state
image_features = self.linear(image_features)
outputs = self.bert(input_ids=input_ids,
attention_mask=attention_mask,
encoder_hidden_states=image_features,
labels=labels,
return_dict=True)
return outputs.loss, outputs.logits
def collate_fn(batch):
# Filter out None values (skipped images)
batch = list(filter(lambda x: x is not None, batch))
if len(batch) == 0:
return None
return {key: torch.stack([item[key] for item in batch]) for key in batch[0]}
def train_vlm_model():
# Load and preprocess the dataset
encodings = ['utf-8', 'iso-8859-9', 'windows-1254']
for encoding in encodings:
try:
df = pd.read_csv('./datasets/' + model + '.tsv000', sep='\t', encoding=encoding)
print(f"Successfully read the file with {encoding} encoding.")
break
except UnicodeDecodeError:
print(f"Failed to read with {encoding} encoding. Trying next...")
else:
raise ValueError("Could not read the file with any of the specified encodings.")
# Initialize the tokenizer
tokenizer = BertTokenizerFast.from_pretrained(BERT_MODEL_NAME)
# Create the dataset and dataloader
dataset = ImageCaptioningDataset(df, '../download/images', tokenizer)
dataloader = DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=True, collate_fn=collate_fn)
# Initialize the model components
vit_model = ViTModel.from_pretrained(VIT_MODEL_NAME)
bert_config = BertConfig.from_pretrained(BERT_MODEL_NAME)
bert_config.is_decoder = True
bert_config.add_cross_attention = True
bert_model = BertLMHeadModel.from_pretrained(BERT_MODEL_NAME, config=bert_config)
# Create the combined model
model = ImageCaptioningModel(vit_model, bert_model)
model.to(device)
# Define optimizer
optimizer = AdamW(model.parameters(), lr=LEARNING_RATE)
# Training loop
model.train()
for epoch in range(EPOCHS):
total_loss = 0
progress_bar = tqdm(dataloader, desc=f"Epoch {epoch+1}/{EPOCHS}")
for batch in progress_bar:
if batch is None:
continue
pixel_values = batch['pixel_values'].to(device)
input_ids = batch['input_ids'].to(device)
attention_mask = batch['attention_mask'].to(device)
labels = batch['labels'].to(device)
optimizer.zero_grad()
loss, _ = model(pixel_values, input_ids, attention_mask, labels)
loss.backward()
optimizer.step()
total_loss += loss.item()
progress_bar.set_postfix({'loss': loss.item()})
print(f"Epoch {epoch+1}/{EPOCHS}, Average Loss: {total_loss/len(dataloader)}")
# Save the model
torch.save(model.state_dict(), "./models/" + model)
tokenizer.save_pretrained("./tokenizer")
if __name__ == "__main__":
train_vlm_model()123version https://git-lfs.github.com/spec/v1oid sha256:c79764aa75a603efead82246db2078c4d2c07edbdf218ec8719f7817f5728c68size 9042126661234567{
"cls_token": "[CLS]",
"mask_token": "[MASK]",
"pad_token": "[PAD]",
"sep_token": "[SEP]",
"unk_token": "[UNK]"
}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758{
"added_tokens_decoder": {
"0": {
"content": "[PAD]",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"1": {
"content": "[UNK]",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"2": {
"content": "[CLS]",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"3": {
"content": "[SEP]",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"4": {
"content": "[MASK]",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
}
},
"clean_up_tokenization_spaces": true,
"cls_token": "[CLS]",
"do_basic_tokenize": true,
"do_lower_case": false,
"mask_token": "[MASK]",
"max_len": 512,
"model_max_length": 512,
"never_split": null,
"pad_token": "[PAD]",
"sep_token": "[SEP]",
"strip_accents": null,
"tokenize_chinese_chars": true,
"tokenizer_class": "BertTokenizer",
"unk_token": "[UNK]"
}.gitattributes+1
README.md+69-3
images/mugla.jpgAdded

Large File Pointer Details
- SHA256: 65b8124c02dc5afefbfd1ac848b633c67301215a3dc4f9c0b8c84790572bf7ea
- Pointer size: 132 Bytes
- Size of remote file: 4.32 MB
imagine.pyAdded+103
main.pyAdded+167
models/TeLVE_v1.0.pthAdded+3
teLVE_logo.pngAdded

tokenizer/special_tokens_map.jsonAdded+7
tokenizer/tokenizer.jsonAdded
tokenizer/tokenizer_config.jsonAdded+58
tokenizer/vocab.txtAdded