-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzero_shot_classification.py
More file actions
167 lines (151 loc) · 7.1 KB
/
zero_shot_classification.py
File metadata and controls
167 lines (151 loc) · 7.1 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
# coding: utf-8
# @Author : Yiming Li @ ICT, CAS
# @E-mail : liyiming22s1@ict.ac.cn
import librosa
import numpy as np
import pandas as pd
import glob
import torch
import torchaudio
from ruamel import yaml
from sklearn.metrics import accuracy_score
from tqdm import tqdm
import json
from re import sub
from data_handling.text_transform import text_preprocess
from models.ase_model import ASE
import torch.nn.functional as F
import torchaudio.transforms as T
with open("settings/inference_cls.yaml", "r") as f:
config = yaml.safe_load(f)
device = config["device"]
model = ASE(config)
model.to(device)
cp_path = config["eval"]["ckpt"]
cp = torch.load(cp_path)
model.load_state_dict(cp['model'], strict=True)
model.eval()
print("Model weights loaded from {}".format(cp_path))
# ESC-50
df = pd.read_csv(config["eval"]["data_root_dir"] + 'ESC-50-master/meta/esc50.csv')
class_to_idx = {}
sorted_df = df.sort_values(by=['target'])
classes = [x.replace('_', ' ') for x in sorted_df['category'].unique()]
for i, category in enumerate(classes):
class_to_idx[category] = i
classes = [c + " can be heard" for c in classes]
pre_path = config["eval"]["data_root_dir"] + 'ESC-50-master/audio/'
with torch.no_grad():
_, word_embeds, attn_mask = model.encode_text(classes)
text_embeds = model.msc(word_embeds, model.codebook, attn_mask)
text_embeds = F.normalize(text_embeds, dim=-1)
fold_acc = []
for fold in range(1, 6):
fold_df = sorted_df[sorted_df['fold'] == fold]
y_preds, y_labels = [], []
for file_path, target in tqdm(zip(fold_df["filename"], fold_df["target"]), total=len(fold_df)):
audio_path = pre_path + file_path
one_hot_target = torch.zeros(len(classes)).scatter_(0, torch.tensor(target), 1).reshape(1, -1)
audio, sr = torchaudio.load(audio_path)
audio = audio.mean(dim=0).reshape(-1)
resampler = T.Resample(sr, 32000)
audio = resampler(audio)
audio = audio.to(device)
if audio.shape[-1] < 32000 * 10:
pad_length = 32000 * 10 - audio.shape[-1]
audio = F.pad(audio, [0, pad_length], "constant", 0.0)
audio = audio.reshape(1, -1)
_, frame_embeds = model.encode_audio(audio)
audio_embeds = model.msc(frame_embeds, model.codebook)
audio_embeds = F.normalize(audio_embeds, dim=-1)
similarity = audio_embeds @ text_embeds.t()
y_pred = F.softmax(similarity.detach().cpu(), dim=1).numpy()
y_preds.append(y_pred)
y_labels.append(one_hot_target.cpu().numpy())
y_labels, y_preds = np.concatenate(y_labels, axis=0), np.concatenate(y_preds, axis=0)
acc = accuracy_score(np.argmax(y_labels, axis=1), np.argmax(y_preds, axis=1))
print('Fold {} Accuracy {}'.format(fold, acc))
fold_acc.append(acc)
print('ESC-50 Accuracy {}'.format(np.mean(np.array(fold_acc))))
# UrbanSound 8K
df = pd.read_csv(config["eval"]["data_root_dir"] + 'UrbanSound8K/metadata/UrbanSound8K.csv')
class_to_idx = {}
sorted_df = df.sort_values(by=['classID'])
classes = [x.replace('_', ' ') for x in sorted_df['class'].unique()]
for i, category in enumerate(classes):
class_to_idx[category] = i
classes = [c for c in classes]
pre_path = config["eval"]["data_root_dir"] + 'UrbanSound8K/audio/'
with torch.no_grad():
_, word_embeds, attn_mask = model.encode_text(classes)
text_embeds = model.msc(word_embeds, model.codebook, attn_mask)
text_embeds = F.normalize(text_embeds, dim=-1)
fold_acc = []
for fold in range(1, 11):
fold_df = sorted_df[sorted_df['fold'] == fold]
y_preds, y_labels = [], []
for file_path, target in tqdm(zip(fold_df["slice_file_name"], fold_df["classID"]), total=len(fold_df)):
audio_path = pre_path + "fold" + str(fold) + "/" + file_path
one_hot_target = torch.zeros(len(classes)).scatter_(0, torch.tensor(target), 1).reshape(1, -1)
audio, sr = torchaudio.load(audio_path)
audio = audio.mean(dim=0).reshape(-1)
resampler = T.Resample(sr, 32000)
audio = resampler(audio)
audio = audio.to(device)
if audio.shape[-1] < 32000 * 10:
pad_length = 32000 * 10 - audio.shape[-1]
audio = F.pad(audio, [0, pad_length], "constant", 0.0)
audio = audio.reshape(1, -1)
_, frame_embeds = model.encode_audio(audio)
audio_embeds = model.msc(frame_embeds, model.codebook)
audio_embeds = F.normalize(audio_embeds, dim=-1)
similarity = audio_embeds @ text_embeds.t()
y_pred = F.softmax(similarity.detach().cpu(), dim=1).numpy()
y_preds.append(y_pred)
y_labels.append(one_hot_target.cpu().numpy())
y_labels, y_preds = np.concatenate(y_labels, axis=0), np.concatenate(y_preds, axis=0)
acc = accuracy_score(np.argmax(y_labels, axis=1), np.argmax(y_preds, axis=1))
print('Fold {} Accuracy {}'.format(fold, acc))
fold_acc.append(acc)
print('Urbansound8K Accuracy {}'.format(np.mean(np.array(fold_acc))))
# VggSound
df = pd.read_csv(config["eval"]["data_root_dir"] + 'VGGSound/metadata/vggsound_test.csv', names=['filename', 'class'])
class_to_idx = {}
sorted_df = df.sort_values(by=['class'])
classes = [x.replace('_', ' ') for x in sorted_df['class'].unique()]
for i, category in enumerate(classes):
class_to_idx[category] = i
classes = [c + " can be heard" for c in classes]
df['target'] = 0
for idx, row in df.iterrows():
df.at[idx, 'target'] = class_to_idx[df.at[idx, 'class']]
pre_path = config["eval"]["data_root_dir"] + 'VGGSound/audio/'
with torch.no_grad():
_, word_embeds, attn_mask = model.encode_text(classes)
text_embeds = model.msc(word_embeds, model.codebook, attn_mask)
text_embeds = F.normalize(text_embeds, dim=-1)
y_preds, y_labels = [], []
for file_path, target in tqdm(zip(df["filename"], df["target"]), total=len(df)):
audio_path = pre_path + file_path[:-4] + ".wav"
one_hot_target = torch.zeros(len(classes)).scatter_(0, torch.tensor(target), 1).reshape(1, -1)
audio, sr = torchaudio.load(audio_path)
audio = audio.mean(dim=0).reshape(-1)
resampler = T.Resample(sr, 32000)
audio = resampler(audio)
audio = audio.to(device)
if audio.shape[-1] < 32000 * 10:
pad_length = 32000 * 10 - audio.shape[-1]
audio = F.pad(audio, [0, pad_length], "constant", 0.0)
audio = audio.reshape(1, -1)
audio = audio[:, :32000 * 10]
_, frame_embeds = model.encode_audio(audio)
audio_embeds = model.msc(frame_embeds, model.codebook)
audio_embeds = F.normalize(audio_embeds, dim=-1)
similarity = audio_embeds @ text_embeds.t()
y_pred = F.softmax(similarity.detach().cpu(), dim=1).numpy()
y_preds.append(y_pred)
y_labels.append(one_hot_target.cpu().numpy())
y_labels, y_preds = np.concatenate(y_labels, axis=0), np.concatenate(y_preds, axis=0)
acc = accuracy_score(np.argmax(y_labels, axis=1), np.argmax(y_preds, axis=1))
print('VGGSound Accuracy {}'.format(acc))