-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprepare_training_data.py
More file actions
178 lines (136 loc) · 5.23 KB
/
Copy pathprepare_training_data.py
File metadata and controls
178 lines (136 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python3
import argparse
import json
import tarfile
from pathlib import Path
from huggingface_hub import snapshot_download
from tqdm import tqdm
REPO_ID = "xiaorui638/FINER-Tuning-data"
def rewrite_image_path(old_path: str, data_root: Path) -> str:
p = Path(old_path)
shard = p.parent.name
filename = p.name
return str(data_root / "images_extracted" / shard / filename)
def extract_tars(images_dir: Path, out_dir: Path):
out_dir.mkdir(parents=True, exist_ok=True)
tar_files = sorted(images_dir.glob("*.tar"))
print(f"Found {len(tar_files)} tar files.")
for tar_path in tqdm(tar_files, desc="Extracting image tars"):
shard_name = tar_path.stem
shard_out = out_dir / shard_name
if shard_out.exists() and any(shard_out.iterdir()):
continue
shard_out.mkdir(parents=True, exist_ok=True)
with tarfile.open(tar_path, "r") as tar:
tar.extractall(shard_out)
def rewrite_jsonl_file(path: Path, data_root: Path):
tmp_path = path.with_suffix(path.suffix + ".tmp")
n = 0
with path.open("r", encoding="utf-8") as fin, tmp_path.open("w", encoding="utf-8") as fout:
for line in fin:
if not line.strip():
continue
sample = json.loads(line)
if "images" in sample:
sample["images"] = [
rewrite_image_path(img_path, data_root)
for img_path in sample["images"]
]
fout.write(json.dumps(sample, ensure_ascii=False) + "\n")
n += 1
tmp_path.replace(path)
print(f"Rewrote {n} samples in place: {path}")
def rewrite_all_jsonl(data_root: Path):
for split_dir in ["sampled_annotations"]:
src_root = data_root / split_dir
if not src_root.exists():
continue
for src_path in sorted(src_root.rglob("*.jsonl")):
rewrite_jsonl_file(src_path, data_root)
def update_llamafactory_dataset_info(
llamafactory_dir: Path,
dataset_name: str,
jsonl_path: Path,
):
dataset_info_path = llamafactory_dir / "data" / "dataset_info.json"
if not dataset_info_path.exists():
raise FileNotFoundError(f"dataset_info.json not found: {dataset_info_path}")
with dataset_info_path.open("r", encoding="utf-8") as f:
dataset_info = json.load(f)
if dataset_name not in dataset_info:
raise KeyError(f"Dataset key '{dataset_name}' not found in {dataset_info_path}")
dataset_info[dataset_name]["file_name"] = str(jsonl_path.resolve())
with dataset_info_path.open("w", encoding="utf-8") as f:
json.dump(dataset_info, f, indent=2, ensure_ascii=False)
f.write("\n")
print(f"Updated LlamaFactory dataset_info.json:")
print(f" dataset key : {dataset_name}")
print(f" file_name : {jsonl_path.resolve()}")
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--output_dir",
type=str,
required=True,
help="Where to download and prepare FINER-Tuning-data.",
)
parser.add_argument(
"--skip_extract",
action="store_true",
help="Skip tar extraction if images are already extracted.",
)
parser.add_argument(
"--llamafactory_dir",
type=str,
default=None,
help="Path to local LlamaFactory root. If provided, dataset_info.json will be updated.",
)
parser.add_argument(
"--dataset_name",
type=str,
default="pixmo_dpo_220k_pon",
help="Dataset key in LlamaFactory/data/dataset_info.json to update.",
)
parser.add_argument(
"--jsonl_relpath",
type=str,
default="sampled_annotations/main_experiments/first6_pon.jsonl",
help="Path relative to output_dir for the JSONL file to register in LlamaFactory.",
)
args = parser.parse_args()
output_dir = Path(args.output_dir).expanduser().resolve()
output_dir.mkdir(parents=True, exist_ok=True)
print(f"Downloading dataset to: {output_dir}")
snapshot_download(
repo_id=REPO_ID,
repo_type="dataset",
local_dir=str(output_dir),
allow_patterns=[
"images/*.tar",
"sampled_annotations/**/*.jsonl",
"full_annotations/**/*.jsonl",
"README.md",
],
)
if not args.skip_extract:
extract_tars(
images_dir=output_dir / "images",
out_dir=output_dir / "images_extracted",
)
rewrite_all_jsonl(output_dir)
if args.llamafactory_dir is not None:
llamafactory_dir = Path(args.llamafactory_dir).expanduser().resolve()
jsonl_path = output_dir / args.jsonl_relpath
if not jsonl_path.exists():
raise FileNotFoundError(f"JSONL file not found: {jsonl_path}")
update_llamafactory_dataset_info(
llamafactory_dir=llamafactory_dir,
dataset_name=args.dataset_name,
jsonl_path=jsonl_path,
)
print("\nDone.")
print(f"Updated annotation files in place under: {output_dir / 'sampled_annotations'}")
print(f"Updated annotation files in place under: {output_dir / 'full_annotations'}")
print(f"Images are under: {output_dir / 'images_extracted'}")
if __name__ == "__main__":
main()