Datasets:
Download code/dynamic_dataset/inventory.py from Heiheihaha17/MESH-Benchmark: direct link, hf CLI and curl.
- Browser
- Download file 4.57 kB
-
https://huggingface.co/datasets/Heiheihaha17/MESH-Benchmark/resolve/main/code/dynamic_dataset/inventory.py
- Command line
-
hf download hf://datasets/Heiheihaha17/MESH-Benchmark/code/dynamic_dataset/inventory.py
-
curl -L -o inventory.py https://huggingface.co/datasets/Heiheihaha17/MESH-Benchmark/resolve/main/code/dynamic_dataset/inventory.py
4.57 kB
| """Inspect held-out aligned real speech and static room/scene inventory.""" | |
| from __future__ import annotations | |
| import argparse,gzip,json | |
| from collections import Counter | |
| from pathlib import Path | |
| import numpy as np | |
| import soundfile as sf | |
| from dynamic_demo.common import SR,sha,speech_activity,write | |
| def rows(path): | |
| opener=gzip.open if path.suffix=='.gz' else open | |
| with opener(path,'rt') as f: | |
| for line in f:yield json.loads(line) | |
| def source_candidates(release,corpus): | |
| sources={r['sha256']:r for r in rows(corpus/'source_catalog.jsonl') | |
| if r['corpus']=='librispeech' and r['split']=='test'} | |
| alignments={} | |
| for row in rows(release/'alignment_crops.jsonl.gz'): | |
| r=row['crop'] | |
| if r['source_sha256'] in sources:alignments.setdefault(r['source_sha256'],r) | |
| candidates=[];file_checks=0 | |
| for digest,alignment in sorted(alignments.items()): | |
| source=sources[digest];words=alignment['words'];runs=[];lo=0 | |
| for i,w in enumerate(words): | |
| if w['mean_score']<.5: | |
| if i>lo:runs.append((lo,i)) | |
| lo=i+1 | |
| elif i and w['start_s']-words[i-1]['end_s']>.3: | |
| if i>lo:runs.append((lo,i)) | |
| lo=i | |
| if len(words)>lo:runs.append((lo,len(words))) | |
| runs=[(a,b) for a,b in runs if words[b-1]['end_s']-words[a]['start_s']>=3.] | |
| if not runs:continue | |
| # Catalog paths are author-local. Published candidates use corpus-relative paths. | |
| source_path=corpus/'extracted'/Path(source['path']).relative_to('soundspaces_demo/corpora/english_edge_v1/extracted') | |
| if sha(source_path)!=digest:raise ValueError('Original speech checksum mismatch') | |
| wave,sr=sf.read(source_path,dtype='float32') | |
| if sr!=SR or wave.ndim!=1:raise ValueError('Invalid original speech') | |
| file_checks+=1 | |
| for first,last in runs: | |
| # VAD runs are recomputed for each final chosen crop; these full-run | |
| # labels provide an early inventory filter only. | |
| a=max(0,round((words[first]['start_s']-.02)*SR)) | |
| b=min(len(wave),round((words[last-1]['end_s']+.025)*SR)) | |
| spans=speech_activity(wave[a:b],0) | |
| if not spans:continue | |
| longest=max(((y[0]-x[1])/SR for x,y in zip(spans,spans[1:])),default=0.) | |
| candidates.append(dict(source_id=source['source_id'],speaker_id=source['speaker_id'], | |
| source_sha256=digest,original_corpus_path=str(source_path.relative_to(corpus/'extracted')), | |
| original_samples=len(wave),words=words,run_word_indices=[first,last], | |
| aligned_run_duration_s=words[last-1]['end_s']-words[first]['start_s'], | |
| full_run_maximum_internal_silence_s=longest, | |
| aligner=alignment['aligner'],aligner_weights_sha256=alignment['aligner_weights_sha256'])) | |
| if file_checks%100==0:print('SOURCE INVENTORY',file_checks,len(candidates),flush=True) | |
| return candidates,file_checks | |
| def inspect(release,corpus,output): | |
| scenes=list(rows(release/'unified_test.jsonl.gz'));rooms=[r for r in rows(release/'rooms.jsonl.gz') if r['split']=='test'] | |
| people=[s for s in scenes if s['physical_scene']['humans']>=2] | |
| candidates,files=source_candidates(release,corpus) | |
| report=dict(static_scenes=len(scenes),multi_human_scenes=len(people),test_rooms=len(rooms), | |
| source_files_checked=files,aligned_runs=len(candidates), | |
| source_runs_by_minimum_duration_s={str(t):sum(c['aligned_run_duration_s']>=t and c['full_run_maximum_internal_silence_s']<=.35 for c in candidates) for t in [3.,4.,4.8,5.,6.,7.]}, | |
| eligible_parents_by_minimum_duration_s={str(t):dict(scenes=sum(s['frames']/SR>=t for s in people), | |
| rooms=len({s['room_id'] for s in people if s['frames']/SR>=t})) for t in [6.,6.5,7.,7.5,8.]}, | |
| source_speakers=sorted({c['speaker_id'] for c in candidates}), | |
| original_test_only=True,minimum_word_alignment_score=.5,maximum_aligned_word_gap_s=.3, | |
| source_catalog_sha256=sha(corpus/'source_catalog.jsonl'), | |
| frozen_release_sha256=sha(release/'release.json')) | |
| write(output/'inventory.json',report);write(output/'source_candidates.json',candidates) | |
| print(json.dumps(report,ensure_ascii=False,indent=2),flush=True) | |
| if __name__=='__main__': | |
| p=argparse.ArgumentParser();p.add_argument('--release',type=Path,required=True);p.add_argument('--corpus',type=Path,required=True);p.add_argument('--output',type=Path,required=True) | |
| a=p.parse_args();inspect(a.release,a.corpus,a.output) | |