Views
No views yet

1from safetensors.torch import load_file
2from huggingface_hub import hf_hub_download
3import torch
4import json
5import sys
6from pathlib import Path
7
8def load_model(path="SajayR/Triad", device="cpu"):
9 model_path = hf_hub_download(repo_id=path, filename="model.safetensors")
10 model_config = hf_hub_download(repo_id=path, filename="config.json")
11 model_arch = hf_hub_download(repo_id=path, filename="hf_model.py")
12
13 sys.path.append(str(Path(model_arch).parent))
14 from hf_model import Triad
15
16 model = Triad(**json.load(open(model_config)))
17 weights = load_file(model_path)
18 model.load_state_dict(weights)
19 return model.to(device)
20
21# Initialize model
22model = load_model() # Use load_model(device="cuda") for GPU1# From file path
2output = model(image="path/to/image.jpg")
3output['visual_feats'].shape # torch.Size([1, 256, 512])
4
5# From tensor (already pre-processed)
6from torchvision import transforms
7from PIL import Image
8
9# Load and preprocess image
10image = Image.open("path/to/image.jpg").convert('RGB')
11transform = transforms.Compose([
12 transforms.Resize((224, 224)),
13 transforms.ToTensor(),
14 transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
15])
16image_tensor = transform(image) # Shape: [3, 224, 224]
17
18# Pass to model
19output = model(image=image_tensor)
20output['visual_feats'].shape # torch.Size([1, 256, 512])1# Audio only - returns audio features (B, N_segments, D)
2# Currently is trained for audio features of 1 seconds each. Longer audio sequences could have worse performance
3audio = torch.randn(1, 16331) # Raw audio waveform
4output = model(audio=audio)
5output['audio_feats'].shape # torch.Size([1, 50, 512])1# Text only - returns text features (B, N_tokens, D)
2text_list = ["a man riding a bicycle"]
3output = model(text_list=text_list)
4output['text_feats'].shape # torch.Size([1, 5, 512])1# Process a batch of image paths
2image_paths = ["path/to/image1.jpg", "path/to/image2.jpg", "path/to/image3.jpg"]
3output = model(image=image_paths)
4output['visual_feats'].shape # torch.Size([3, 256, 512])1# Process a batch of image tensors
2import torch
3from torchvision import transforms
4from PIL import Image
5
6# Create a transform
7transform = transforms.Compose([
8 transforms.Resize((224, 224)),
9 transforms.ToTensor(),
10 transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
11])
12
13# Load and preprocess images
14images = []
15for path in ["image1.jpg", "image2.jpg", "image3.jpg"]:
16 img = Image.open(path).convert('RGB')
17 images.append(transform(img))
18
19# Stack into a batch
20batch = torch.stack(images) # Shape: [3, 3, 224, 224]
21
22# Process the batch
23output = model(image=batch)
24output['visual_feats'].shape # torch.Size([3, 256, 512])1# Process image and audio together
2output = model(
3 audio=audio,
4 image="path/to/image.jpg"
5)
6
7print(output.keys()) # dict_keys(['visual_feats', 'audio_feats', 'vis_audio_sim_matrix'])
8
9# Output shapes:
10# - audio_feats: [1, 50, 512] # (batch, audio_segments, features)
11# - visual_feats: [1, 256, 512] # (batch, image_patches, features)
12# - vis_audio_sim_matrix: [1, 50, 256] # (batch, audio_segments, image_patches)visual_feats: (B, 256, 512) # When you pass an imageaudio_feats: (B, 50, 512) # When you pass audiotext_feats: (B, N_tokens, 512) # When you pass textvis_text_sim_matrix: (B, N_tokens, 256) # When you pass both image and textvis_audio_sim_matrix: (B, 50, 256) # When you pass both image and audiotext_audio_sim_matrix: (B, N_tokens, 50) # When you pass both text and audio