-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluation.py
More file actions
202 lines (137 loc) · 5.87 KB
/
evaluation.py
File metadata and controls
202 lines (137 loc) · 5.87 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import librosa
import torch
from train import TextDecoder
from tqdm import tqdm
from data_handling.clotho_dataset import get_clotho_loader
from eval_metrics import evaluate_metrics
import argparse
import torch
from clap_model.ase_model import ASE
from ruamel import yaml
import librosa
import os
import yaml
from dotmap import DotMap
def get_config():
with open('settings/settings.yaml', 'r') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
config = DotMap(config)
with open("settings/inference.yaml", "r") as f:
clap_config = yaml.safe_load(f)
return config, clap_config
def argument_parser():
parser = argparse.ArgumentParser()
parser.add_argument('--model_path', default='trained_models/clotho_baseline/best_model.pt')
parser.add_argument('--clap_path', default='/home/theokouz/data/WavCaps/cnn14-bert.pt')
parser.add_argument('--dataset', default='clotho')
parser.add_argument('--eval_dir', default='/home/theokouz/data/clotho/wavforms/evaluation')
parser.add_argument('--method', default='ad')
parser.add_argument('--mem', default='data/clotho.json')
args = parser.parse_args()
return args
def Decoding(model,clip_features):
embedding_cat = model.clip_project(clip_features).reshape(1,1,-1)
entry_length = 30
temperature = 0.1
tokens = None
for i in range(entry_length):
outputs = model.decoder(inputs_embeds=embedding_cat)
logits = outputs.logits
logits = logits[:, -1, :] / (temperature if temperature > 0 else 1.0)
logits_max = logits.max()
logits = torch.nn.functional.softmax(logits)
next_token = torch.argmax(logits, -1).unsqueeze(0)
next_token_embed = model.decoder.transformer.wte(next_token)
if tokens is None:
tokens = next_token
else:
tokens = torch.cat((tokens, next_token), dim=1)
if next_token.item()==102:
break
embedding_cat = torch.cat((embedding_cat, next_token_embed), dim=1)
try:
output_list = list(tokens.squeeze().cpu().numpy())
output = clap.text_encoder.tokenizer.decode(torch.tensor(output_list[1:-1]))
except:
output = 'None'
return output
import json
def construct_support_memory(text_json, clap):
with open(text_json, 'r') as f:
data = json.load(f)
text_features = []
captions = []
batch_size = 1000
clap.eval()
for i in tqdm(range(0,len(data[:])//batch_size)):
texts = data[i*batch_size:(i+1)*batch_size]
with torch.no_grad():
text_feature = clap.encode_text(texts)
text_features.append(text_feature)
captions.extend(texts)
text_features = torch.cat(text_features,dim=0)
text_features /= text_features.norm(dim=-1,keepdim=True).float()
return text_features
def eval(test_data, model, clap, args, use_beam, mapper = None):
evaluation_dir = args.eval_dir
captions_gt = []
captions_pred = []
text_features = construct_support_memory(args.mem, clap)
text_features = text_features.to(device)
for _, eval_batch in tqdm(enumerate(test_data), total=len(test_data)):
_, target_dicts, file_names = eval_batch
for file_name, target in zip(file_names, target_dicts):
audio_path = os.path.join(evaluation_dir, file_name)
target['file_name'] = file_name
captions_gt.append(target)
with torch.no_grad():
audio_data, _ = librosa.load(audio_path, sr=32000)
audio_data = torch.tensor(audio_data).unsqueeze(0).to(device)
audio_embed = clap.encode_audio(audio_data)
audio_embed /= audio_embed.norm(dim=-1, keepdim=True)
# Decode with the audio embedding
if args.method == 'ad':
prefix_embedding = torch.tensor(audio_embed).to(device)
# Decode with the nearest neighbor
elif args.method == 'nnd':
audio_embed = torch.tensor(audio_embed).to(device)
sim = audio_embed@text_features.T.float()
sim = (sim*100).softmax(dim=-1)
nearest_neighbor = torch.argmax(sim).item()
prefix_embedding = text_features[nearest_neighbor].unsqueeze(0)
prefix_embedding /= prefix_embedding.norm(dim=-1,keepdim=True)
# Project audio embedding to Memory and decode with the projection
else:
audio_embed = torch.tensor(audio_embed).to(device)
sim = audio_embed@text_features.T.float()
sim = (sim*100).softmax(dim=-1)
prefix_embedding = sim@text_features.float()
prefix_embedding /= prefix_embedding.norm(dim=-1,keepdim=True)
prediction = Decoding(model, prefix_embedding)
captions_pred.append(
{
'file_name': file_name,
'caption_predicted': prediction
}
)
metrics = evaluate_metrics(prediction_file=captions_pred, reference_file= captions_gt)
for metric, values in metrics.items():
print(f'greddy: {metric:<7s}: {values["score"]:7.4f}')
return metrics
if __name__ == '__main__':
args = argument_parser()
config, clap_config = get_config()
clap = ASE(clap_config)
cp_path = args.clap_path
cp = torch.load(cp_path)
clap.load_state_dict(cp['model'])
clap.eval()
device = 'cuda:3'
clap.to(device)
model = TextDecoder(prefix_size=1024)
test_data = get_clotho_loader(config, dataset_name=args.dataset)
weights_path = args.model_path
model.load_state_dict(torch.load(weights_path,map_location= torch.device('cpu')))
model = model.to(device)
model = model.eval()
eval(test_data, model, clap, args, use_beam=False)