blkpst commited on
Commit
dbdf69a
·
1 Parent(s): ad6fd75

use JSONL manifest for HF streaming

Browse files
Files changed (3) hide show
  1. README.md +10 -8
  2. data/masks/train.jsonl +0 -0
  3. scripts/prepare_dataset.py +50 -3
README.md CHANGED
@@ -13,7 +13,7 @@ configs:
13
  - config_name: annotations
14
  data_files:
15
  - split: train
16
- path: data/masks/*.json
17
  ---
18
 
19
  # Chang'E-4 TCM Dataset
@@ -30,8 +30,8 @@ LabelMe terrain annotations for Chang'E-4 Yutu-2 rover imagery.
30
  # Install dependencies
31
  pip install -r requirements.txt
32
 
33
- # Strip embedded imageData before publishing raw annotations to Hugging Face
34
- python scripts/prepare_dataset.py --strip-only
35
 
36
  # Generate local mask PNGs and metadata from annotations
37
  python scripts/prepare_dataset.py
@@ -40,14 +40,15 @@ python scripts/prepare_dataset.py
40
  python scripts/convert_pds.py data/raw data/images
41
  ```
42
 
43
- For the Hugging Face dataset, publish only `data/masks/*.json`. Strip the
44
- embedded `imageData` field first so the annotation files stay small enough for
45
- the dataset viewer to stream reliably.
46
 
47
  ## Annotation Format
48
 
49
- Each dataset row is one LabelMe JSON annotation from `data/masks/*.json`. The
50
- `shapes` field contains polygon or rectangle regions with the labels below.
 
51
 
52
  ### Class Labels
53
 
@@ -117,6 +118,7 @@ python scripts/convert_pds.py data/raw data/images --flat
117
  ```
118
  ├── data/
119
  │ ├── masks/ # LabelMe annotation JSONs
 
120
  │ ├── masks_png/ # Indexed mask PNGs (generated locally)
121
  │ ├── metadata.jsonl # Local metadata for derived assets
122
  │ ├── class_labels.json
 
13
  - config_name: annotations
14
  data_files:
15
  - split: train
16
+ path: data/masks/train.jsonl
17
  ---
18
 
19
  # Chang'E-4 TCM Dataset
 
30
  # Install dependencies
31
  pip install -r requirements.txt
32
 
33
+ # Build the Hugging Face JSONL manifest from raw annotations
34
+ python scripts/prepare_dataset.py --hf-jsonl
35
 
36
  # Generate local mask PNGs and metadata from annotations
37
  python scripts/prepare_dataset.py
 
40
  python scripts/convert_pds.py data/raw data/images
41
  ```
42
 
43
+ For the Hugging Face dataset, publish only files under `data/masks/`. Generate
44
+ `data/masks/train.jsonl` from the stripped LabelMe annotations so the dataset
45
+ viewer can stream rows reliably.
46
 
47
  ## Annotation Format
48
 
49
+ Each dataset row comes from `data/masks/train.jsonl`, with one stripped
50
+ LabelMe annotation per line. The `shapes` field contains polygon or rectangle
51
+ regions with the labels below.
52
 
53
  ### Class Labels
54
 
 
118
  ```
119
  ├── data/
120
  │ ├── masks/ # LabelMe annotation JSONs
121
+ │ │ └── train.jsonl # HF streaming manifest generated from masks/
122
  │ ├── masks_png/ # Indexed mask PNGs (generated locally)
123
  │ ├── metadata.jsonl # Local metadata for derived assets
124
  │ ├── class_labels.json
data/masks/train.jsonl ADDED
The diff for this file is too large to render. See raw diff
 
scripts/prepare_dataset.py CHANGED
@@ -2,9 +2,8 @@
2
  """Prepare local derivatives from LabelMe JSON annotations.
3
 
4
  By default this script generates indexed mask PNGs and a metadata.jsonl file.
5
- It can also strip base64 imageData from the JSON files without generating any
6
- derived assets, which is useful when publishing only the raw annotations to
7
- Hugging Face.
8
 
9
  Class mapping (index -> label):
10
  0: background
@@ -21,6 +20,7 @@ Usage:
21
  python scripts/prepare_dataset.py
22
  python scripts/prepare_dataset.py --skip-strip # keep imageData in JSONs
23
  python scripts/prepare_dataset.py --strip-only # only strip imageData
 
24
  """
25
 
26
  import argparse
@@ -48,6 +48,7 @@ ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
48
  MASKS_DIR = os.path.join(ROOT, "data", "masks")
49
  MASKS_PNG_DIR = os.path.join(ROOT, "data", "masks_png")
50
  METADATA_PATH = os.path.join(ROOT, "data", "metadata.jsonl")
 
51
 
52
 
53
  def render_mask(annotation: dict) -> Image.Image:
@@ -91,6 +92,29 @@ def strip_image_data(json_path: str) -> None:
91
  json.dump(data, f, indent=2)
92
 
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  def main():
95
  parser = argparse.ArgumentParser(description="Prepare HF dataset from LabelMe annotations")
96
  parser.add_argument("--skip-strip", action="store_true", help="Don't strip imageData from JSONs")
@@ -99,6 +123,11 @@ def main():
99
  action="store_true",
100
  help="Only strip imageData from JSONs; don't generate PNGs or metadata",
101
  )
 
 
 
 
 
102
  args = parser.parse_args()
103
 
104
  if args.strip_only and args.skip_strip:
@@ -124,6 +153,24 @@ def main():
124
  print(f"\nDone: stripped imageData from {stripped_count}/{len(json_files)} JSON files")
125
  return
126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  os.makedirs(MASKS_PNG_DIR, exist_ok=True)
128
 
129
  metadata_entries = []
 
2
  """Prepare local derivatives from LabelMe JSON annotations.
3
 
4
  By default this script generates indexed mask PNGs and a metadata.jsonl file.
5
+ It can also strip base64 imageData from the JSON files and generate a JSONL
6
+ manifest for publishing the raw annotations to Hugging Face.
 
7
 
8
  Class mapping (index -> label):
9
  0: background
 
20
  python scripts/prepare_dataset.py
21
  python scripts/prepare_dataset.py --skip-strip # keep imageData in JSONs
22
  python scripts/prepare_dataset.py --strip-only # only strip imageData
23
+ python scripts/prepare_dataset.py --hf-jsonl # write data/masks/train.jsonl
24
  """
25
 
26
  import argparse
 
48
  MASKS_DIR = os.path.join(ROOT, "data", "masks")
49
  MASKS_PNG_DIR = os.path.join(ROOT, "data", "masks_png")
50
  METADATA_PATH = os.path.join(ROOT, "data", "metadata.jsonl")
51
+ HF_JSONL_PATH = os.path.join(MASKS_DIR, "train.jsonl")
52
 
53
 
54
  def render_mask(annotation: dict) -> Image.Image:
 
92
  json.dump(data, f, indent=2)
93
 
94
 
95
+ def build_hf_annotation_row(annotation: dict, json_path: str) -> dict:
96
+ """Return a JSON-serializable row for HF streaming from raw annotations."""
97
+ return {
98
+ "source_file": os.path.basename(json_path),
99
+ "version": annotation.get("version"),
100
+ "flags": annotation.get("flags", {}),
101
+ "shapes": annotation.get("shapes", []),
102
+ "imagePath": annotation.get("imagePath"),
103
+ "imageHeight": annotation.get("imageHeight"),
104
+ "imageWidth": annotation.get("imageWidth"),
105
+ }
106
+
107
+
108
+ def write_hf_jsonl(json_files: list[str]) -> None:
109
+ """Write a JSONL manifest that HF can stream reliably."""
110
+ with open(HF_JSONL_PATH, "w") as f:
111
+ for json_path in json_files:
112
+ with open(json_path, "r") as src:
113
+ annotation = json.load(src)
114
+ row = build_hf_annotation_row(annotation, json_path)
115
+ f.write(json.dumps(row, ensure_ascii=True) + "\n")
116
+
117
+
118
  def main():
119
  parser = argparse.ArgumentParser(description="Prepare HF dataset from LabelMe annotations")
120
  parser.add_argument("--skip-strip", action="store_true", help="Don't strip imageData from JSONs")
 
123
  action="store_true",
124
  help="Only strip imageData from JSONs; don't generate PNGs or metadata",
125
  )
126
+ parser.add_argument(
127
+ "--hf-jsonl",
128
+ action="store_true",
129
+ help="Write data/masks/train.jsonl for Hugging Face streaming",
130
+ )
131
  args = parser.parse_args()
132
 
133
  if args.strip_only and args.skip_strip:
 
153
  print(f"\nDone: stripped imageData from {stripped_count}/{len(json_files)} JSON files")
154
  return
155
 
156
+ if args.hf_jsonl:
157
+ stripped_count = 0
158
+ for i, json_path in enumerate(json_files):
159
+ with open(json_path, "r") as f:
160
+ annotation = json.load(f)
161
+ if not args.skip_strip and annotation.get("imageData") is not None:
162
+ strip_image_data(json_path)
163
+ stripped_count += 1
164
+
165
+ if (i + 1) % 50 == 0 or (i + 1) == len(json_files):
166
+ print(f" [{i + 1}/{len(json_files)}] processed")
167
+
168
+ write_hf_jsonl(json_files)
169
+ print(f"\nDone: wrote HF JSONL to {HF_JSONL_PATH}")
170
+ if not args.skip_strip:
171
+ print(f"Stripped imageData from {stripped_count}/{len(json_files)} JSON files")
172
+ return
173
+
174
  os.makedirs(MASKS_PNG_DIR, exist_ok=True)
175
 
176
  metadata_entries = []