esp32/audioset: update as_extract_part.py
Browse files- as_extract_part.py +108 -0
as_extract_part.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# requires-python = ">=3.11"
|
| 3 |
+
# dependencies = ["datasets>=4.0", "huggingface_hub>=0.30", "pyarrow", "soundfile", "numpy", "scipy"]
|
| 4 |
+
# ///
|
| 5 |
+
"""Part-split version of as_extract.py: --part i --nparts N scans files where index % N == i
|
| 6 |
+
and pushes to config clips_p{i} of Tonic/raw-audioset-explosion. Run N jobs in parallel."""
|
| 7 |
+
import argparse
|
| 8 |
+
ap = argparse.ArgumentParser()
|
| 9 |
+
ap.add_argument("--part", type=int, required=True)
|
| 10 |
+
ap.add_argument("--nparts", type=int, default=4)
|
| 11 |
+
ap.add_argument("--cap-expl", type=int, default=1500)
|
| 12 |
+
ap.add_argument("--cap-veh", type=int, default=1500)
|
| 13 |
+
ap.add_argument("--config", default=None)
|
| 14 |
+
args = ap.parse_args()
|
| 15 |
+
CONFIG = args.config or f"clips_p{args.part}"
|
| 16 |
+
import io, os
|
| 17 |
+
import numpy as np
|
| 18 |
+
import soundfile as sf
|
| 19 |
+
import pyarrow.parquet as pq
|
| 20 |
+
from huggingface_hub import HfApi, HfFileSystem
|
| 21 |
+
from datasets import Dataset, Features, Audio, Value, Sequence
|
| 22 |
+
|
| 23 |
+
EXPL = {"Explosion", "Gunshot, gunfire", "Fireworks", "Burst, pop", "Eruption", "Boom"}
|
| 24 |
+
VEH = {"Vehicle", "Engine", "Motor vehicle (on road)", "Car", "Truck", "Motorcycle",
|
| 25 |
+
"Aircraft", "Helicopter", "Train", "Motorboat", "Ship", "Emergency vehicle",
|
| 26 |
+
"Car passing by", "Race car, auto racing", "Airplane, aeroplane"}
|
| 27 |
+
CAP_EXPL, CAP_VEH = args.cap_expl, args.cap_veh
|
| 28 |
+
TARGET_SR = 16000
|
| 29 |
+
|
| 30 |
+
api = HfApi(token=os.environ["HF_TOKEN"])
|
| 31 |
+
fs = HfFileSystem(token=os.environ["HF_TOKEN"])
|
| 32 |
+
REPO = "agkphysics/AudioSet"
|
| 33 |
+
SPLITS = ("bal_train", "eval", "unbal_train")
|
| 34 |
+
|
| 35 |
+
files_all = [f for f in api.list_repo_files(REPO, repo_type="dataset")
|
| 36 |
+
if f.startswith("data/") and f.endswith(".parquet")
|
| 37 |
+
and f.split("/")[1] in SPLITS]
|
| 38 |
+
files = [f for i, f in enumerate(files_all) if i % args.nparts == args.part]
|
| 39 |
+
print(f"{len(files)} parquet shards", flush=True)
|
| 40 |
+
|
| 41 |
+
rows = []
|
| 42 |
+
n_expl = n_veh = 0
|
| 43 |
+
|
| 44 |
+
def encode_wav(raw):
|
| 45 |
+
arr, sr = sf.read(io.BytesIO(raw), dtype="float32", always_2d=False)
|
| 46 |
+
if arr.ndim > 1:
|
| 47 |
+
arr = arr.mean(axis=1)
|
| 48 |
+
if sr != TARGET_SR:
|
| 49 |
+
g = np.gcd(sr, TARGET_SR)
|
| 50 |
+
from scipy.signal import resample_poly
|
| 51 |
+
arr = resample_poly(arr, TARGET_SR // g, sr // g)
|
| 52 |
+
buf = io.BytesIO()
|
| 53 |
+
sf.write(buf, arr.astype(np.float32), TARGET_SR, format="WAV", subtype="PCM_16")
|
| 54 |
+
return buf.getvalue()
|
| 55 |
+
|
| 56 |
+
for fi, f in enumerate(files):
|
| 57 |
+
if n_expl >= CAP_EXPL and n_veh >= CAP_VEH:
|
| 58 |
+
break
|
| 59 |
+
with fs.open(f"datasets/{REPO}/{f}", "rb") as fh:
|
| 60 |
+
pf = pq.ParquetFile(fh)
|
| 61 |
+
for rg in range(pf.num_row_groups):
|
| 62 |
+
lab = pf.read_row_group(rg, columns=["video_id", "human_labels", "labels"])
|
| 63 |
+
vids = lab.column("video_id").to_pylist()
|
| 64 |
+
hls = lab.column("human_labels").to_pylist()
|
| 65 |
+
lbs = lab.column("labels").to_pylist()
|
| 66 |
+
match = []
|
| 67 |
+
for i, hl in enumerate(hls):
|
| 68 |
+
hl = set(hl or [])
|
| 69 |
+
if n_expl < CAP_EXPL and hl & EXPL:
|
| 70 |
+
match.append((i, "explosion"))
|
| 71 |
+
elif n_veh < CAP_VEH and hl & VEH:
|
| 72 |
+
match.append((i, "vehicle_mechanical"))
|
| 73 |
+
if not match:
|
| 74 |
+
continue
|
| 75 |
+
full = pf.read_row_group(rg) # all columns; struct audio reads fine here
|
| 76 |
+
acol = full.column("audio").combine_chunks()
|
| 77 |
+
for i, cls in match:
|
| 78 |
+
raw = acol[i].as_py()["bytes"]
|
| 79 |
+
hl = hls[i] or []
|
| 80 |
+
rows.append({
|
| 81 |
+
"video_id": vids[i],
|
| 82 |
+
"labels": lbs[i] or [],
|
| 83 |
+
"human_labels": list(hl),
|
| 84 |
+
"sound_class": cls,
|
| 85 |
+
"non_drone_source": "audioset:" + ",".join(sorted(hl)),
|
| 86 |
+
"audio": {"bytes": encode_wav(raw), "path": f"{vids[i]}.wav"},
|
| 87 |
+
})
|
| 88 |
+
if cls == "explosion":
|
| 89 |
+
n_expl += 1
|
| 90 |
+
else:
|
| 91 |
+
n_veh += 1
|
| 92 |
+
print(f"[{fi}/{len(files)}] {f} rg{rg}: {len(match)} matches (expl={n_expl} veh={n_veh})", flush=True)
|
| 93 |
+
if fi % 50 == 0:
|
| 94 |
+
print(f"progress: shards={fi} expl={n_expl} veh={n_veh} rows={len(rows)}", flush=True)
|
| 95 |
+
|
| 96 |
+
print(f"DONE scan: expl={n_expl} veh={n_veh} rows={len(rows)}", flush=True)
|
| 97 |
+
feats = Features({
|
| 98 |
+
"video_id": Value("string"),
|
| 99 |
+
"labels": Sequence(Value("string")),
|
| 100 |
+
"human_labels": Sequence(Value("string")),
|
| 101 |
+
"sound_class": Value("string"),
|
| 102 |
+
"non_drone_source": Value("string"),
|
| 103 |
+
"audio": Audio(sampling_rate=TARGET_SR),
|
| 104 |
+
})
|
| 105 |
+
out = Dataset.from_list(rows, features=feats)
|
| 106 |
+
out.push_to_hub("Tonic/raw-audioset-explosion", config_name=CONFIG, token=os.environ["HF_TOKEN"])
|
| 107 |
+
api.update_repo_settings(repo_id="Tonic/raw-audioset-explosion", repo_type="dataset", gated="manual")
|
| 108 |
+
print(f"PUSHED Tonic/raw-audioset-explosion config={CONFIG} (gated)", flush=True)
|