ZhenYe234 commited on
Commit
e839d6e
·
verified ·
1 Parent(s): 4cb973f

Upload scripts/build_clean_dataset.py

Browse files
Files changed (1) hide show
  1. scripts/build_clean_dataset.py +164 -0
scripts/build_clean_dataset.py ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import csv
3
+ import errno
4
+ import os
5
+ import shutil
6
+ import time
7
+ from pathlib import Path
8
+ from collections import Counter
9
+
10
+ SRC_ROOT = Path('/aifs4su/yezhen/data')
11
+ SRC_CSV = SRC_ROOT / 'unified_all_dataset_more_filtered.csv'
12
+ OUT_ROOT = SRC_ROOT / 'Talker-T2AV-Data-clean'
13
+ META_DIR = OUT_ROOT / 'metadata'
14
+ OUT_CSV = META_DIR / 'train.csv'
15
+ REPORT = OUT_ROOT / 'build_report.txt'
16
+
17
+ PREFIXES = {
18
+ 'dh_motion': 'apdcephfs_gy2/share_302533218/zhenye/data/DH-FaceVid-1K-lia-x-features/',
19
+ 'dh_audio': 'apdcephfs_gy2/share_302533218/zhenye/data/DH-FaceVid-1K-processed_separated/',
20
+ 'dh_video_dir': 'DH-FaceVid-1K-processed',
21
+ 'ditto': 'apdcephfs_gy2/share_302533218/zhenye/ditto_data_v214/',
22
+ 'seamless_audio': 'apdcephfs_gy2/share_302533218/zhenye/data/seamless_emilia_v2/',
23
+ 'hallo3_audio': 'apdcephfs_gy2/share_302533218/zhenye/data/hallo3_training_data/hallo3/audios_separated/',
24
+ 'ravdess_audio': 'apdcephfs_gy2/share_302533218/zhenye/data/ravadess/',
25
+ 'mead_audio': 'apdcephfs_gy2/share_302533218/zhenye/data/mead/',
26
+ 'talkvid_audio': 'apdcephfs_gy4/share_302533218/zhenye/talkvid_data/',
27
+ }
28
+
29
+ def require_suffix(path: str, prefix: str) -> str:
30
+ if not path.startswith(prefix):
31
+ raise ValueError(f'path does not start with expected prefix: {path} prefix={prefix}')
32
+ return path[len(prefix):]
33
+
34
+ def map_motion(src: str, dataset: str) -> str:
35
+ if dataset == 'dh_facevid':
36
+ suffix = require_suffix(src, PREFIXES['dh_motion'])
37
+ else:
38
+ suffix = require_suffix(src, PREFIXES['ditto'] + dataset + '/')
39
+ return f'motion/{dataset}/{suffix}'
40
+
41
+ def map_audio(src: str, dataset: str) -> str:
42
+ if dataset == 'dh_facevid':
43
+ suffix = require_suffix(src, PREFIXES['dh_audio'])
44
+ elif dataset == 'seamless_emilia':
45
+ suffix = require_suffix(src, PREFIXES['seamless_audio'])
46
+ elif dataset == 'hallo3':
47
+ suffix = require_suffix(src, PREFIXES['hallo3_audio'])
48
+ elif dataset == 'ravdess':
49
+ suffix = require_suffix(src, PREFIXES['ravdess_audio'])
50
+ elif dataset == 'mead':
51
+ suffix = require_suffix(src, PREFIXES['mead_audio'])
52
+ elif dataset == 'talkvid':
53
+ suffix = require_suffix(src, PREFIXES['talkvid_audio'])
54
+ else:
55
+ raise ValueError(f'unknown dataset_source for audio: {dataset}')
56
+ return f'audio/{dataset}/{suffix}'
57
+
58
+ def map_video_source_and_clean(motion_src: str, dataset: str):
59
+ if dataset == 'dh_facevid':
60
+ stem = Path(motion_src).stem
61
+ video_src = f"{PREFIXES['dh_video_dir']}/{stem}.mp4"
62
+ video_clean = f'video/{dataset}/{stem}.mp4'
63
+ else:
64
+ video_src = str(Path(motion_src).with_suffix('.mp4'))
65
+ motion_clean = map_motion(motion_src, dataset).replace('/seamless_emilia/', '/seamless/')
66
+ video_clean = str(Path(motion_clean.replace('motion/', 'video/', 1)).with_suffix('.mp4'))
67
+ return video_src, video_clean
68
+
69
+ def link_file(src_rel: str, dst_rel: str, missing_counter: Counter, linked_counter: Counter, kind: str):
70
+ src = SRC_ROOT / src_rel
71
+ dst = OUT_ROOT / dst_rel
72
+ if not src.exists():
73
+ missing_counter[kind] += 1
74
+ return False
75
+ dst.parent.mkdir(parents=True, exist_ok=True)
76
+ if dst.exists():
77
+ linked_counter[f'{kind}_exists'] += 1
78
+ return True
79
+ try:
80
+ os.link(src, dst)
81
+ except OSError as e:
82
+ if e.errno == errno.EEXIST:
83
+ linked_counter[f'{kind}_exists'] += 1
84
+ return True
85
+ if e.errno == errno.EXDEV:
86
+ os.symlink(src, dst)
87
+ else:
88
+ raise
89
+ linked_counter[kind] += 1
90
+ return True
91
+
92
+ def main():
93
+ start = time.time()
94
+ META_DIR.mkdir(parents=True, exist_ok=True)
95
+ tmp_csv = OUT_CSV.with_suffix('.csv.tmp')
96
+ fields = [
97
+ 'sample_id', 'dataset_source', 'split', 'text',
98
+ 'wav_path', 'motion_pt_path', 'video_path',
99
+ 'pt_length', 'fps',
100
+ ]
101
+ counts = Counter()
102
+ missing = Counter()
103
+ linked = Counter()
104
+ errors = []
105
+
106
+ with SRC_CSV.open('r', newline='', encoding='utf-8') as fin, tmp_csv.open('w', newline='', encoding='utf-8') as fout:
107
+ reader = csv.DictReader(fin)
108
+ writer = csv.DictWriter(fout, fieldnames=fields)
109
+ writer.writeheader()
110
+ for idx, row in enumerate(reader):
111
+ dataset = row['dataset_source']
112
+ clean_dataset = 'seamless' if dataset == 'seamless_emilia' else dataset
113
+ counts[clean_dataset] += 1
114
+ try:
115
+ motion_src = row['motion_pt_path']
116
+ wav_src = row['wav_path']
117
+ motion_clean = map_motion(motion_src, dataset).replace('/seamless_emilia/', '/seamless/')
118
+ wav_clean = map_audio(wav_src, dataset).replace('/seamless_emilia/', '/seamless/')
119
+ video_src, video_clean = map_video_source_and_clean(motion_src, dataset)
120
+
121
+ ok_motion = link_file(motion_src, motion_clean, missing, linked, 'motion')
122
+ ok_wav = link_file(wav_src, wav_clean, missing, linked, 'audio')
123
+ ok_video = link_file(video_src, video_clean, missing, linked, 'video')
124
+
125
+ if not (ok_motion and ok_wav):
126
+ errors.append((idx, dataset, motion_src, wav_src, video_src))
127
+
128
+ writer.writerow({
129
+ 'sample_id': f'{clean_dataset}_{idx:09d}',
130
+ 'dataset_source': clean_dataset,
131
+ 'split': 'train',
132
+ 'text': row.get('text', ''),
133
+ 'wav_path': wav_clean,
134
+ 'motion_pt_path': motion_clean,
135
+ 'video_path': video_clean if ok_video else '',
136
+ 'pt_length': row.get('pt_length', ''),
137
+ 'fps': '25',
138
+ })
139
+ except Exception as e:
140
+ errors.append((idx, dataset, str(e)))
141
+ missing['row_errors'] += 1
142
+
143
+ if (idx + 1) % 10000 == 0:
144
+ elapsed = time.time() - start
145
+ print(f'processed={idx+1} elapsed={elapsed:.1f}s missing={dict(missing)} linked={sum(v for k,v in linked.items() if not k.endswith("_exists"))}', flush=True)
146
+
147
+ os.replace(tmp_csv, OUT_CSV)
148
+ elapsed = time.time() - start
149
+ with REPORT.open('w', encoding='utf-8') as f:
150
+ f.write(f'source_csv={SRC_CSV}\n')
151
+ f.write(f'output_csv={OUT_CSV}\n')
152
+ f.write(f'elapsed_sec={elapsed:.1f}\n')
153
+ f.write(f'counts={dict(counts)}\n')
154
+ f.write(f'missing={dict(missing)}\n')
155
+ f.write(f'linked={dict(linked)}\n')
156
+ f.write(f'errors_count={len(errors)}\n')
157
+ for err in errors[:100]:
158
+ f.write(f'error={err}\n')
159
+ print(f'DONE elapsed={elapsed:.1f}s counts={dict(counts)} missing={dict(missing)} errors={len(errors)} report={REPORT}', flush=True)
160
+ if missing.get('motion', 0) or missing.get('audio', 0) or missing.get('row_errors', 0):
161
+ raise SystemExit(2)
162
+
163
+ if __name__ == '__main__':
164
+ main()