You can use the LanguageBind class to perform multi-modal binding across various modalities including video, audio, thermal, image, and depth. This approach allows you to compute embeddings for multiple modalities simultaneously and perform zero-shot comparisons (e.g., Video x Text, Image x Audio) using the shared semantic space.
To use this, you need to:
- Define a
clip_type dictionary mapping modality names to their respective model identifiers. - Initialize the
LanguageBind model and a LanguageBindImageTokenizer. - Create a
modality_transform dictionary using transform_dict and the model's modality_config. - Prepare input lists for each modality and tokenize the text.
- Pass the dictionary of transformed inputs to the model to get embeddings.
import torch
from languagebind import LanguageBind, to_device, transform_dict, LanguageBindImageTokenizer
if __name__ == '__ '__:
device = torch.device('cuda:0')
clip_type = {
'video': 'LanguageBind_Video_FT',
'audio': 'LanguageBind_Audio_FT',
'thermal': 'LanguageBind_Thermal',
'image': 'LanguageBind_Image',
'depth': 'LanguageBind_Depth',
}
model = LanguageBind(clip_type=clip_type, cache_dir='./cache_dir')
model = model.to(device)
model.eval()
pretrained_ckpt = 'lb203/LanguageBind_Image'
tokenizer = LanguageBindImageTokenizer.from_pretrained(pretrained_ckpt, cache_dir='./cache_dir/tokenizer_cache_dir')
modality_transform = {c: transform_dict[c](model.modality_config[c]) for c in clip_type.keys()}
# Example inputs
image = ['assets/image/0.jpg']
language = ["Training a parakeet to climb up a ladder."]
inputs = {
'image': to_device(modality_transform['image'](image), device),
'language': to_device(tokenizer(language, max_length=77, padding='max_length', truncation=True, return_tensors='pt'), device),
}
with torch.no_grad():
embeddings = model(inputs)
# Compute similarity
print(torch.softmax(embeddings['image'] @ embeddings['language'].T, dim=-1).detach().cpu().numpy())