vladmandic commited on
Commit
83b9bf2
·
verified ·
1 Parent(s): 70985d0

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +49 -0
README.md CHANGED
@@ -7,3 +7,52 @@ tags:
7
  - locateanything,
8
  ---
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  - locateanything,
8
  ---
9
 
10
+ # LocateAnything
11
+
12
+ Full reimplementation of <https://huggingface.co/nvidia/LocateAnything-3B> for compatibility with `transformers>=5`
13
+
14
+ Example code:
15
+
16
+ ```py
17
+ import argparse
18
+ from pathlib import Path
19
+
20
+ import torch
21
+ from PIL import Image
22
+ from transformers import AutoModel, AutoProcessor
23
+
24
+ repo_id = 'SD-Next/LocateAnything-3B'
25
+ parser = argparse.ArgumentParser()
26
+ parser.add_argument("--image", required=True, type=Path)
27
+ parser.add_argument("--prompt", required=True)
28
+ args = parser.parse_args()
29
+
30
+ processor = AutoProcessor.from_pretrained(repo_id, trust_remote_code=True)
31
+ model = AutoModel.from_pretrained(repo_id, trust_remote_code=True, torch_dtype="auto", device_map="auto").eval()
32
+
33
+ image = Image.open(args.image).convert("RGB")
34
+ messages = [{"role": "user", "content": [
35
+ {"type": "image", "image": image},
36
+ {"type": "text", "text": args.prompt}
37
+ ]}]
38
+ text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
39
+ inputs = processor(images=image, text=text, return_tensors="pt").to(model.device)
40
+
41
+ with torch.inference_mode():
42
+ output = model.generate(**inputs, max_new_tokens=128, do_sample=False)
43
+ decoded = processor.batch_decode(output[:, inputs["input_ids"].shape[1]:], skip_special_tokens=False)
44
+ print(decoded[0])
45
+ ```
46
+
47
+ Example output:
48
+
49
+ > python test-locateanything.py --image samples/cartoon.png --prompt "locate hands and face"
50
+
51
+ ```xml
52
+ <ref>hands</ref>'
53
+ <box><312><754><357><888></box>
54
+ <box><568><778><623><917></box>
55
+ <ref>face</ref>
56
+ <box><438><81><536><255></box>
57
+ <|im_end|>
58
+ ```