Datasets:
WIP: create splits for dataset and dataset itself
Browse files- create_splits.py +218 -0
create_splits.py
ADDED
@@ -0,0 +1,218 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import pandas as pd
|
3 |
+
import torch
|
4 |
+
from torch.utils.data import Dataset, DataLoader
|
5 |
+
from pathlib import Path
|
6 |
+
import json
|
7 |
+
from typing import List, Dict, Optional
|
8 |
+
import numpy as np
|
9 |
+
|
10 |
+
def create_dataset_splits(
|
11 |
+
data_path: str,
|
12 |
+
metadata_path: str,
|
13 |
+
output_dir: str,
|
14 |
+
train_ratio: float = 0.8,
|
15 |
+
val_ratio: float = 0.1,
|
16 |
+
seed: int = 42
|
17 |
+
):
|
18 |
+
"""
|
19 |
+
Create and save train/val/test splits to disk.
|
20 |
+
|
21 |
+
Args:
|
22 |
+
data_path: Path to the JSON file containing voice mappings
|
23 |
+
metadata_path: Path to the metadata CSV file
|
24 |
+
output_dir: Directory to save the split CSV files
|
25 |
+
train_ratio: Ratio of data to use for training
|
26 |
+
val_ratio: Ratio of data to use for validation
|
27 |
+
seed: Random seed for reproducibility
|
28 |
+
"""
|
29 |
+
# Load raw data
|
30 |
+
with open(data_path, 'r') as f:
|
31 |
+
raw_data = json.load(f)
|
32 |
+
|
33 |
+
df_metadata = pd.read_csv(metadata_path)
|
34 |
+
|
35 |
+
# Convert to DataFrame
|
36 |
+
records = []
|
37 |
+
for path, available_models in raw_data.items():
|
38 |
+
df_metadata_row = df_metadata[df_metadata['path'] == path]
|
39 |
+
for model in available_models:
|
40 |
+
records.append({
|
41 |
+
'path': path,
|
42 |
+
'model': model,
|
43 |
+
'is_cloned_voice': model != 'commonvoice',
|
44 |
+
'age': df_metadata_row['age'].values[0],
|
45 |
+
'gender': df_metadata_row['gender'].values[0],
|
46 |
+
'accents': df_metadata_row['accents'].values[0],
|
47 |
+
'sentence': df_metadata_row['sentence'].values[0],
|
48 |
+
})
|
49 |
+
|
50 |
+
df = pd.DataFrame.from_records(records)
|
51 |
+
|
52 |
+
# Create deterministic split based on path
|
53 |
+
np.random.seed(seed)
|
54 |
+
unique_paths = df['path'].unique()
|
55 |
+
np.random.shuffle(unique_paths)
|
56 |
+
|
57 |
+
n_samples = len(unique_paths)
|
58 |
+
train_idx = int(n_samples * train_ratio)
|
59 |
+
val_idx = int(n_samples * (train_ratio + val_ratio))
|
60 |
+
|
61 |
+
# Create split DataFrames
|
62 |
+
splits = {
|
63 |
+
'train': df[df['path'].isin(unique_paths[:train_idx])],
|
64 |
+
'val': df[df['path'].isin(unique_paths[train_idx:val_idx])],
|
65 |
+
'test': df[df['path'].isin(unique_paths[val_idx:])]
|
66 |
+
}
|
67 |
+
|
68 |
+
# Save splits
|
69 |
+
output_dir = Path(output_dir)
|
70 |
+
output_dir.mkdir(exist_ok=True, parents=True)
|
71 |
+
|
72 |
+
# Save individual splits
|
73 |
+
for split_name, split_df in splits.items():
|
74 |
+
split_df.to_csv(output_dir / f'{split_name}.csv', index=False)
|
75 |
+
|
76 |
+
# Save split info
|
77 |
+
split_info = {
|
78 |
+
'num_samples': {
|
79 |
+
split_name: len(paths)
|
80 |
+
for split_name, paths in {
|
81 |
+
'train': unique_paths[:train_idx],
|
82 |
+
'val': unique_paths[train_idx:val_idx],
|
83 |
+
'test': unique_paths[val_idx:]
|
84 |
+
}.items()
|
85 |
+
},
|
86 |
+
'train_ratio': train_ratio,
|
87 |
+
'val_ratio': val_ratio,
|
88 |
+
'seed': seed
|
89 |
+
}
|
90 |
+
|
91 |
+
with open(output_dir / 'split_info.json', 'w') as f:
|
92 |
+
json.dump(split_info, f, indent=2)
|
93 |
+
|
94 |
+
class VoiceDataset(Dataset):
|
95 |
+
def __init__(
|
96 |
+
self,
|
97 |
+
split_path: str,
|
98 |
+
clips_dir: str,
|
99 |
+
models: Optional[List[str]] = None
|
100 |
+
):
|
101 |
+
"""
|
102 |
+
Args:
|
103 |
+
split_path: Path to the CSV file containing the split data
|
104 |
+
models: List of model names to include. If None, includes all models
|
105 |
+
"""
|
106 |
+
self.data = pd.read_csv(split_path)
|
107 |
+
self.clips_dir = clips_dir
|
108 |
+
|
109 |
+
# Filter models if specified
|
110 |
+
if models is not None:
|
111 |
+
self.data = self.data[self.data['model'].isin(models)]
|
112 |
+
|
113 |
+
# Create path to index mapping
|
114 |
+
self.path_to_idx = {path: idx for idx, path in enumerate(self.data['path'].unique())}
|
115 |
+
self.split_name = Path(split_path).stem # Get split name from file path
|
116 |
+
self.original_models = [model for model in self.data['model'].unique() if model != 'commonvoice']
|
117 |
+
|
118 |
+
|
119 |
+
|
120 |
+
def __len__(self):
|
121 |
+
return len(self.data)
|
122 |
+
|
123 |
+
def __getitem__(self, idx):
|
124 |
+
row = self.data.iloc[idx]
|
125 |
+
path = row['path']
|
126 |
+
model = row['model']
|
127 |
+
rel_path = os.path.join(model, path)
|
128 |
+
abs_path = os.path.join(self.clips_dir, rel_path)
|
129 |
+
return {
|
130 |
+
'path': abs_path,
|
131 |
+
'model': model,
|
132 |
+
'is_cloned_voice': row['is_cloned_voice'],
|
133 |
+
}
|
134 |
+
|
135 |
+
def summary(self) -> Dict:
|
136 |
+
"""
|
137 |
+
Generate a comprehensive summary of the dataset.
|
138 |
+
|
139 |
+
Returns:
|
140 |
+
Dictionary containing summary statistics
|
141 |
+
"""
|
142 |
+
summary = {
|
143 |
+
'split': self.split_name,
|
144 |
+
'total_samples': len(self.data),
|
145 |
+
'cloned_samples': len(self.data[self.data['is_cloned_voice']]),
|
146 |
+
'original_samples': len(self.data[~self.data['is_cloned_voice']]),
|
147 |
+
'unique_voices': len(self.path_to_idx),
|
148 |
+
'models': {
|
149 |
+
'available': list(self.original_models),
|
150 |
+
'selected': list(self.data['model'].unique()),
|
151 |
+
},
|
152 |
+
'samples_per_model': self.data['model'].value_counts().to_dict(),
|
153 |
+
'voices_per_model': self.data.groupby('model')['path'].nunique().to_dict(),
|
154 |
+
}
|
155 |
+
|
156 |
+
return summary
|
157 |
+
|
158 |
+
|
159 |
+
def print_summary(self):
|
160 |
+
"""
|
161 |
+
Print a formatted summary of the dataset.
|
162 |
+
|
163 |
+
Args:
|
164 |
+
include_metadata: Whether to include metadata statistics in the summary
|
165 |
+
"""
|
166 |
+
summary = self.summary()
|
167 |
+
|
168 |
+
print(f"\n=== Dataset Summary ({summary['split'].upper()} split) ===")
|
169 |
+
print(f"Total samples: {summary['total_samples']}")
|
170 |
+
print(f"Cloned samples: {summary['cloned_samples']}")
|
171 |
+
print(f"Original samples: {summary['original_samples']}")
|
172 |
+
|
173 |
+
print("\nModels:")
|
174 |
+
print(f"Available: {', '.join(summary['models']['available'])}")
|
175 |
+
print(f"Selected: {', '.join(summary['models']['selected'])}")
|
176 |
+
|
177 |
+
print("\nSamples per model:")
|
178 |
+
for model, count in summary['samples_per_model'].items():
|
179 |
+
print(f" {model}: {count}")
|
180 |
+
|
181 |
+
print("\nUnique voices per model:")
|
182 |
+
for model, count in summary['voices_per_model'].items():
|
183 |
+
print(f" {model}: {count}")
|
184 |
+
|
185 |
+
|
186 |
+
|
187 |
+
# Example usage:
|
188 |
+
if __name__ == "__main__":
|
189 |
+
json_file = 'files.json'
|
190 |
+
metadata_file = 'metadata-balanced.csv'
|
191 |
+
clips_dir = '.'
|
192 |
+
output_dir = 'splits'
|
193 |
+
|
194 |
+
# Create splits
|
195 |
+
create_dataset_splits(json_file, metadata_file, output_dir=output_dir)
|
196 |
+
|
197 |
+
|
198 |
+
# Create datasets for each split
|
199 |
+
train_dataset = VoiceDataset(
|
200 |
+
'splits/train.csv',
|
201 |
+
clips_dir=clips_dir,
|
202 |
+
models=['commonvoice', 'metavoice', 'xttsv2']
|
203 |
+
)
|
204 |
+
|
205 |
+
val_dataset = VoiceDataset(
|
206 |
+
'splits/val.csv',
|
207 |
+
clips_dir=clips_dir,
|
208 |
+
models=['commonvoice', 'metavoice', 'xttsv2']
|
209 |
+
)
|
210 |
+
|
211 |
+
test_dataset = VoiceDataset(
|
212 |
+
'splits/test.csv',
|
213 |
+
clips_dir=clips_dir,
|
214 |
+
models=['commonvoice', 'metavoice', 'xttsv2']
|
215 |
+
)
|
216 |
+
|
217 |
+
# Create DataLoader
|
218 |
+
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
|