-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiemocap-extraFeature-utt.py
More file actions
178 lines (126 loc) · 5.58 KB
/
Copy pathiemocap-extraFeature-utt.py
File metadata and controls
178 lines (126 loc) · 5.58 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
# coding:utf-8
from cmath import e
# import cv2
from PIL import Image
import os
import os.path as osp
import sys
import pandas as pd
import numpy as np
from glob import glob
from tqdm import tqdm
import torch
import json
import shutil
import pickle
import math
sys.path.append(os.path.join(os.getcwd(), "../.."))
from torchvision import transforms
from audio import Wav2VecExtractor
# from make_ref import make_text
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
os.environ["TOKENIZERS_PARALLELISM"] = "false"
def make_text(csv_path, set_name):
df = pd.read_csv(csv_path, encoding='utf8')
ret = {}
labels = []
dia_utt_list = []
audio_paths = []
label_index = {'neu': 0, 'fru': 1, 'ang': 2, 'sad': 3, 'hap': 4, 'exc': 5}
for _, row in df.iterrows():
dia_num = int(row['Dialogue_ID'])
utt_num = int(row['Utterance_ID'])
utt_id = f'{set_name}/dia{dia_num}_utt{utt_num}'
dia_utt_list.append(f'dia{dia_num}_utt{utt_num}')
text = row['Utterance']
ret[utt_id] = text
label = row['Emotion']
labels.append(label_index[label])
audio_path = row['Wav_Path']
audio_paths.append(audio_path)
return ret, dia_utt_list, labels, audio_paths
class IEMOCAP():
def __init__(self, modality, data_dir, pre_output_dir):
self.modality = modality
# data path
self.data_dir = data_dir #
self.pre_output_dir = pre_output_dir #
# text path
self.train_path = os.path.join(data_dir, 'IEMOCAP_train.csv')
self.val_path = os.path.join(data_dir, 'IEMOCAP_dev.csv')
self.test_path = os.path.join(data_dir, 'IEMOCAP_test.csv')
def __padding(self, feature, MAX_LEN):
length = feature.shape[0]
if length >= MAX_LEN:
return feature[:MAX_LEN, :], np.ones((MAX_LEN))
pad = np.zeros([MAX_LEN - length, feature.shape[-1]])
utt_pad = np.ones((length), dtype=int)
utt_mask = np.zeros((MAX_LEN - length), dtype=int)
feature = np.concatenate((feature, pad), axis=0)
utt_fina_mask = np.concatenate((utt_pad, utt_mask), axis=0)
return feature, utt_fina_mask
def __paddingSequence(self, sequences):
feature_dim = sequences[0].shape[-1]
lens = [s.shape[0] for s in sequences]
final_length = int(np.mean(lens) + 3 * np.std(lens))
# padding sequences to final_length
final_sequence = np.zeros([len(sequences), final_length, feature_dim])
final_utt_mask = np.zeros([len(sequences), final_length])
for i, s in enumerate(sequences):
final_sequence[i], final_utt_mask[i] = self.__padding(s, final_length)
return final_sequence, final_utt_mask
def preprocess_data(self, modality=None, pretrainedAudioPath=None, AudioModelChoice=None, face_or_frame=None,
pretrained_vision_dataset=None, add_prob_distribution=None):
self.data = {}
self.data['train'] = {}
self.data['test'] = {}
self.data['dev'] = {}
if modality == 'audio':
print('开始执行语音模态的初始化特征提取!')
pretrainedAudioPath = os.path.join(pretrainedAudioPath, AudioModelChoice)
if AudioModelChoice == 'data2vec':
extract_audio_feat = None # Data2VecExtractor(pretrainedAudioPath,0)
elif AudioModelChoice == 'wav2vec2.0':
extract_audio_feat = Wav2VecExtractor(pretrainedAudioPath, 0)
for set_name in ['dev', 'test']:
csv_path = osp.join(self.data_dir, 'IEMOCAP_' + set_name + '.csv')
_, int2name, labels, audio_paths = make_text(csv_path, set_name)
features_utt = []
label_Emotion = []
i = 0
j = 0
for utt_id in tqdm(int2name):
if utt_id == 'dia220_utt0':
continue
profile = []
emotion = np.array(labels[i])
if modality == 'audio':
audio_path = audio_paths[i]
embedding = extract_audio_feat(audio_path)
features_utt.append(embedding)
label_Emotion.append(emotion)
i += 1
feature_fina, utt_mask = self.__paddingSequence(
features_utt) # (num_utterance, max_utt_lens, featureExtractor_dim)
label_Emotion = np.array(label_Emotion)
if modality == 'audio':
self.data[set_name]['audio'] = feature_fina
self.data[set_name]['audio_utt_mask'] = utt_mask
if modality == 'audio':
save_path = 'preprocess_data/IEMOCAP_full_release/T+A/iemocap_{}_audio_utt.pkl'.format(set_name)
f = open(save_path, 'wb')
pickle.dump(self.data, f, protocol=4)
f.close()
if __name__ == '__main__':
data_dir = 'preprocess_data/IEMOCAP_full_release'
pre_output_dir = 'preprocess_data/IEMOCAP_full_release'
#############################################################################################
modality = 'audio' # audio, vision
# add_prob_distribution = False #是否为每帧图片给予一个初始化的重要性
#############################################################################################
iemocap = IEMOCAP(modality, data_dir, pre_output_dir)
if modality == 'audio':
'''语音模态预训练模型'''
pretrainedAudioPath = 'pretrained_model/audio/'
AudioModelChoice = 'wav2vec2.0'
iemocap.preprocess_data(modality, pretrainedAudioPath, AudioModelChoice, None, None, False)