Views
No views yet


transformers version can be verified with: pip list | grep transformers.flash_attn==2.5.8
numpy==1.24.4
Pillow==10.3.0
Requests==2.31.0
torch==2.3.0
torchvision==0.18.0
transformers==4.43.0
accelerate==0.30.01from PIL import Image
2
3
4def process_image(img):
5 # Phi-Ground-Anything uses a larger 5x3-tile canvas (1680 x 1008).
6 target_width, target_height = 336 * 5, 336 * 3
7
8 img_ratio = img.width / img.height
9 target_ratio = target_width / target_height
10
11 if img_ratio > target_ratio:
12 new_width = target_width
13 new_height = int(new_width / img_ratio)
14 else:
15 new_height = target_height
16 new_width = int(new_height * img_ratio)
17 reshape_ratio = new_width / img.width
18
19 img = img.resize((new_width, new_height), Image.LANCZOS)
20 new_img = Image.new("RGB", (target_width, target_height), (255, 255, 255))
21 paste_position = (0, 0)
22 new_img.paste(img, paste_position)
23 return new_img, reshape_ratio
24
25
26# Phi-Ground-Anything takes the user instruction directly (no "describe the
27# element" wrapper) and is trained to emit the click point as
28# <x>VALUE</x><y>VALUE</y>
29# where VALUE is a relative coordinate in [0, 10000] over the padded canvas
30# (i.e., divide by 10000 and multiply by target_width / target_height to get
31# pixel coords in the padded image, then divide by reshape_ratio to recover
32# coords in the ORIGINAL image).
33instruction = "<your instruction>"
34prompt = """<|user|>
35{instruction}<|image_1|>
36<|end|>
37<|assistant|>""".format(instruction=instruction)
38
39image_path = "<your image path>"
40original_image = Image.open(image_path).convert("RGB")
41image, reshape_ratio = process_image(original_image)
42
43
44# ---------------------------------------------------------------------------
45# Example: parse the model output and recover original-image coordinates.
46# ---------------------------------------------------------------------------
47import re
48
49target_width, target_height = 336 * 5, 336 * 3
50SCALE = 10000.0
51
52x_pattern = re.compile(r"<x>\s*(-?\d+(?:\.\d+)?)\s*</x>")
53y_pattern = re.compile(r"<y>\s*(-?\d+(?:\.\d+)?)\s*</y>")
54
55
56def parse_xy(model_output: str):
57 xs = [float(v) for v in x_pattern.findall(model_output)]
58 ys = [float(v) for v in y_pattern.findall(model_output)]
59 return list(zip(xs, ys))
60
61
62def to_original_pixel(rel_xy, reshape_ratio: float):
63 x_rel, y_rel = rel_xy
64 px = (x_rel / SCALE) * target_width / reshape_ratio
65 py = (y_rel / SCALE) * target_height / reshape_ratio
66 return px, py
67
68
69# model_output = "<x>4823</x><y>3120</y>"
70# point_orig = to_original_pixel(parse_xy(model_output)[0], reshape_ratio)
71