-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_avhubert_feats.py
50 lines (42 loc) · 2.19 KB
/
extract_avhubert_feats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import argparse
import os
import tqdm
import numpy as np
import torch
from hubert_classification_dataset import AVHubertFeatureDataset
def main(args):
# Assuming you have already defined AVHubertFeatureDataset class
# Define paths for saving .npy files
os.makedirs(args.output_dir, exist_ok=True)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
train_dataset = AVHubertFeatureDataset(
ckpt_path=args.ckpt_path,
manifest_path=args.manifest_path,
label_path=args.label_path,
sample_rate=args.sample_rate,
modalities=args.modalities,
normalize=args.normalize,
device=device,
)
# Iterate over the dataset and save features, labels, and file IDs
for i in tqdm.tqdm(range(len(train_dataset))):
feature, label, fid = train_dataset[i]
feature_path = os.path.join(args.output_dir, f'feature_{fid}.npy')
label_path = os.path.join(args.output_dir, f'label_{fid}.npy')
fid_path = os.path.join(args.output_dir, f'fid_{fid}.npy')
np.save(feature_path, feature.cpu().detach().numpy())
np.save(label_path, np.array(label))
np.save(fid_path, np.array(fid))
print("Features, labels, and file IDs have been saved to .npy files.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Process arguments for dataset processing")
parser.add_argument("--ckpt_path", type=str, default="checkpoints/base_vox_iter5.pt", help="Path to the checkpoint")
parser.add_argument("--manifest_path", type=str, default="data/train.tsv", help="Path to the manifest file")
parser.add_argument("--label_path", type=str, default=None, help="Path to the label file")
parser.add_argument("--sample_rate", type=int, default=16000, help="Sample rate of the audio")
parser.add_argument("--modalities", nargs="+", type=str, default=["video", "audio"], help="Modalities to include")
parser.add_argument("--normalize", action="store_true", help="Normalize the features")
parser.add_argument("--output_dir", type=str, default="feats/train", help="Directory to save .npy files")
args = parser.parse_args()
main(args)