Upload demo_usage.py with huggingface_hub
Browse files- demo_usage.py +43 -4
demo_usage.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import torch
|
| 2 |
from termcolor import colored
|
| 3 |
-
from modeling_tara import TARA, read_frames_decord
|
| 4 |
|
| 5 |
|
| 6 |
-
def main():
|
| 7 |
print(colored("="*60, 'yellow'))
|
| 8 |
print(colored("TARA Model Demo", 'yellow', attrs=['bold']))
|
| 9 |
print(colored("="*60, 'yellow'))
|
|
@@ -11,7 +11,7 @@ def main():
|
|
| 11 |
# Load model from current directory
|
| 12 |
print(colored("\n[1/3] Loading model...", 'cyan'))
|
| 13 |
model = TARA.from_pretrained(
|
| 14 |
-
|
| 15 |
device_map='auto',
|
| 16 |
torch_dtype=torch.bfloat16,
|
| 17 |
)
|
|
@@ -63,6 +63,40 @@ def main():
|
|
| 63 |
print(colored("✓ Similarities computed!", 'green'))
|
| 64 |
for i, txt in enumerate(text):
|
| 65 |
print(f" '{txt}': {similarities[0, i].item():.4f}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
print(colored("\n" + "="*60, 'yellow'))
|
| 68 |
print(colored("Demo completed successfully! 🎉", 'green', attrs=['bold']))
|
|
@@ -70,4 +104,9 @@ def main():
|
|
| 70 |
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
from termcolor import colored
|
| 3 |
+
from modeling_tara import TARA, read_frames_decord, read_images_decord
|
| 4 |
|
| 5 |
|
| 6 |
+
def main(model_path: str = "."):
|
| 7 |
print(colored("="*60, 'yellow'))
|
| 8 |
print(colored("TARA Model Demo", 'yellow', attrs=['bold']))
|
| 9 |
print(colored("="*60, 'yellow'))
|
|
|
|
| 11 |
# Load model from current directory
|
| 12 |
print(colored("\n[1/3] Loading model...", 'cyan'))
|
| 13 |
model = TARA.from_pretrained(
|
| 14 |
+
model_path, # Load from current directory
|
| 15 |
device_map='auto',
|
| 16 |
torch_dtype=torch.bfloat16,
|
| 17 |
)
|
|
|
|
| 63 |
print(colored("✓ Similarities computed!", 'green'))
|
| 64 |
for i, txt in enumerate(text):
|
| 65 |
print(f" '{txt}': {similarities[0, i].item():.4f}")
|
| 66 |
+
print("-" * 100)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
# Negation example: a negation in text query should result
|
| 70 |
+
# in retrieval of images without the neg. object in the query
|
| 71 |
+
image_paths = [
|
| 72 |
+
'./assets/cat.png',
|
| 73 |
+
'./assets/dog+cat.png',
|
| 74 |
+
]
|
| 75 |
+
image_tensors = read_images_decord(image_paths)
|
| 76 |
+
with torch.no_grad():
|
| 77 |
+
image_embs = model.encode_vision(image_tensors.to(model.model.device)).cpu().float()
|
| 78 |
+
image_embs = torch.nn.functional.normalize(image_embs, dim=-1)
|
| 79 |
+
print(f"Image embedding shape: {image_embs.shape}")
|
| 80 |
+
|
| 81 |
+
texts = ['an image of a cat but there is no dog in it']
|
| 82 |
+
with torch.no_grad():
|
| 83 |
+
text_embs = model.encode_text(texts).cpu().float()
|
| 84 |
+
text_embs = torch.nn.functional.normalize(text_embs, dim=-1)
|
| 85 |
+
print("Text query: ", texts)
|
| 86 |
+
sim = text_embs @ image_embs.t()
|
| 87 |
+
print(f"Text-Image similarity: {sim}")
|
| 88 |
+
print("-" * 100)
|
| 89 |
+
|
| 90 |
+
texts = ['an image of a cat and a dog together']
|
| 91 |
+
with torch.no_grad():
|
| 92 |
+
text_embs = model.encode_text(texts).cpu().float()
|
| 93 |
+
text_embs = torch.nn.functional.normalize(text_embs, dim=-1)
|
| 94 |
+
print("Text query: ", texts)
|
| 95 |
+
sim = text_embs @ image_embs.t()
|
| 96 |
+
print(f"Text-Image similarity: {sim}")
|
| 97 |
+
print("-" * 100)
|
| 98 |
+
import ipdb; ipdb.set_trace()
|
| 99 |
+
|
| 100 |
|
| 101 |
print(colored("\n" + "="*60, 'yellow'))
|
| 102 |
print(colored("Demo completed successfully! 🎉", 'green', attrs=['bold']))
|
|
|
|
| 104 |
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|
| 107 |
+
import argparse
|
| 108 |
+
parser = argparse.ArgumentParser()
|
| 109 |
+
parser.add_argument("--model_path", type=str, default=".")
|
| 110 |
+
args = parser.parse_args()
|
| 111 |
+
|
| 112 |
+
main(args.model_path)
|