yunfengwang commited on
Commit
e760eca
·
verified ·
1 Parent(s): 0551e23

Upload utils/special_tokens.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. utils/special_tokens.py +106 -0
utils/special_tokens.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Special tokens for visual primitives.
3
+
4
+ Paper format:
5
+ Box: <|ref|>TARGET<|/ref|><|box|>[[x1,y1,x2,y2],...]<|/box|>
6
+ Point: <|point|>[[x1,y1],[x2,y2],...]<|/point|>
7
+
8
+ Coordinates are normalized to discrete integers in [0, 999].
9
+ """
10
+
11
+ from typing import List, Tuple
12
+
13
+ # Special token strings
14
+ REF_START = "<|ref|>"
15
+ REF_END = "<|/ref|>"
16
+ BOX_START = "<|box|>"
17
+ BOX_END = "<|/box|>"
18
+ POINT_START = "<|point|>"
19
+ POINT_END = "<|/point|>"
20
+
21
+ SPECIAL_TOKENS = [
22
+ REF_START, REF_END,
23
+ BOX_START, BOX_END,
24
+ POINT_START, POINT_END,
25
+ ]
26
+
27
+ # For parsing
28
+ import re
29
+
30
+ BOX_PATTERN = re.compile(
31
+ r"<\|box\|>(.*?)<\|/box\|>", re.DOTALL
32
+ )
33
+ POINT_PATTERN = re.compile(
34
+ r"<\|point\|>(.*?)<\|/point\|>", re.DOTALL
35
+ )
36
+ REF_PATTERN = re.compile(
37
+ r"<\|ref\|>(.*?)<\|/ref\|>", re.DOTALL
38
+ )
39
+
40
+
41
+ def normalize_coordinate(val: float, image_size: int) -> int:
42
+ """Map a pixel coordinate to [0, 999]."""
43
+ return int(round(val / image_size * 999))
44
+
45
+
46
+ def denormalize_coordinate(val: int, image_size: int) -> int:
47
+ """Map a normalized coordinate [0, 999] back to pixel space."""
48
+ return int(round(val / 999 * image_size))
49
+
50
+
51
+ def format_box_token(boxes: List[Tuple[int, int, int, int]]) -> str:
52
+ """
53
+ boxes: list of (x1, y1, x2, y2) in normalized [0, 999] ints.
54
+ Returns: <|box|>[[x1,y1,x2,y2],...]<|/box|>
55
+ """
56
+ if not boxes:
57
+ return f"{BOX_START}[]{BOX_END}"
58
+ inner = ",".join(f"[{x1},{y1},{x2},{y2}]" for x1, y1, x2, y2 in boxes)
59
+ return f"{BOX_START}[{inner}]{BOX_END}"
60
+
61
+
62
+ def format_point_token(points: List[Tuple[int, int]]) -> str:
63
+ """
64
+ points: list of (x, y) in normalized [0, 999] ints.
65
+ Returns: <|point|>[[x1,y1],[x2,y2],...]<|/point|>
66
+ """
67
+ if not points:
68
+ return f"{POINT_START}[]{POINT_END}"
69
+ inner = ",".join(f"[{x},{y}]" for x, y in points)
70
+ return f"{POINT_START}[{inner}]{POINT_END}"
71
+
72
+
73
+ def parse_box_token(text: str) -> List[Tuple[int, int, int, int]]:
74
+ """Parse all box groups from text. Returns list of (x1,y1,x2,y2)."""
75
+ boxes = []
76
+ for m in BOX_PATTERN.finditer(text):
77
+ content = m.group(1).strip()
78
+ # Match [[x1,y1,x2,y2],[x3,y3,x4,y4],...]
79
+ for bm in re.finditer(r"\[(\d+),(\d+),(\d+),(\d+)\]", content):
80
+ boxes.append(tuple(int(bm.group(i)) for i in range(1, 5)))
81
+ return boxes
82
+
83
+
84
+ def parse_point_token(text: str) -> List[Tuple[int, int]]:
85
+ """Parse all point groups from text. Returns list of (x,y)."""
86
+ points = []
87
+ for m in POINT_PATTERN.finditer(text):
88
+ content = m.group(1).strip()
89
+ for pm in re.finditer(r"\[(\d+),(\d+)\]", content):
90
+ points.append((int(pm.group(1)), int(pm.group(2))))
91
+ return points
92
+
93
+
94
+ def parse_ref_text(text: str) -> List[str]:
95
+ """Parse all reference texts from text."""
96
+ refs = []
97
+ for m in REF_PATTERN.finditer(text):
98
+ refs.append(m.group(1).strip())
99
+ return refs
100
+
101
+
102
+ def add_special_tokens(tokenizer):
103
+ """Add visual primitive special tokens to a tokenizer."""
104
+ special = {"additional_special_tokens": SPECIAL_TOKENS}
105
+ tokenizer.add_special_tokens(special)
106
+ return tokenizer