-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_predictions.py
More file actions
112 lines (93 loc) · 3.7 KB
/
Copy pathcreate_predictions.py
File metadata and controls
112 lines (93 loc) · 3.7 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
#!/usr/bin/env python3
import argparse
import json
from pathlib import Path
import sys
import torch
def iter_json_records(p: Path):
"""Yield JSON objects from either .jsonl (one per line) or a single .json file."""
try:
text = p.read_text(encoding="utf-8").strip()
except Exception as e:
print(f"[WARN] Could not read {p}: {e}", file=sys.stderr)
return
# Try single JSON first
try:
obj = json.loads(text)
if isinstance(obj, dict):
yield obj
return
elif isinstance(obj, list):
# If it's a list of records, yield them
for rec in obj:
if isinstance(rec, dict):
yield rec
return
except Exception:
pass
# Fall back to JSON Lines
for i, line in enumerate(text.splitlines(), 1):
line = line.strip()
if not line:
continue
try:
rec = json.loads(line)
if isinstance(rec, dict):
yield rec
else:
print(f"[WARN] {p}:{i} not an object; skipping.", file=sys.stderr)
except Exception as e:
print(f"[WARN] Failed to parse {p}:{i} as JSON: {e}", file=sys.stderr)
def to_torch_tensor(x):
"""Convert nested lists/numbers to a torch.Tensor on CPU."""
# If it's already a tensor, detach->cpu
if isinstance(x, torch.Tensor):
return x.detach().cpu()
# If it’s a numpy array or list/scalar, let torch handle it
try:
return torch.tensor(x)
except Exception as e:
raise TypeError(f"Cannot convert to torch.Tensor: {e}")
def main():
ap = argparse.ArgumentParser(description="Convert GPT JSON records to .pt tensors.")
ap.add_argument("--src", type=str, default="gpt_vision", help="Directory with JSON/JSONL files.")
ap.add_argument("--dst", type=str, default="gpt_predictions", help="Output directory for .pt files.")
ap.add_argument("--overwrite", action="store_true", help="Overwrite existing .pt files.")
args = ap.parse_args()
src_dir = Path(args.src)
dst_dir = Path(args.dst)
dst_dir.mkdir(parents=True, exist_ok=True)
if not src_dir.exists():
print(f"[ERROR] Source directory does not exist: {src_dir}", file=sys.stderr)
sys.exit(1)
files = sorted(list(src_dir.rglob("*.json"))) + sorted(list(src_dir.rglob("*.jsonl")))
if not files:
print(f"[WARN] No .json/.jsonl files found in {src_dir}", file=sys.stderr)
seen = 0
written = 0
for jf in files:
for rec in iter_json_records(jf):
seen += 1
image_path = rec.get("image_path")
tensor_payload = rec.get("tensor")
if image_path is None or tensor_payload is None:
print(f"[WARN] {jf} missing 'image_path' or 'tensor'; skipping record.", file=sys.stderr)
continue
try:
tensor = to_torch_tensor(tensor_payload)
except Exception as e:
print(f"[WARN] Failed to convert tensor for {image_path}: {e}", file=sys.stderr)
continue
out_name = Path(image_path).stem + ".pt"
out_path = dst_dir / out_name
if out_path.exists() and not args.overwrite:
print(f"[INFO] Skipping existing {out_path} (use --overwrite to replace).", file=sys.stderr)
continue
try:
torch.save(tensor, out_path)
written += 1
except Exception as e:
print(f"[WARN] Failed to save {out_path}: {e}", file=sys.stderr)
print(f"[DONE] Processed records: {seen}. Saved tensors: {written}. Output dir: {dst_dir}")
if __name__ == "__main__":
main()