-
Notifications
You must be signed in to change notification settings - Fork 6
/
NeuroTalkDataset.py
166 lines (125 loc) · 5.21 KB
/
NeuroTalkDataset.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
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
import csv
import os
import numpy as np
import torch
from torch.utils.data import Dataset
epsilon = np.finfo(float).eps
class myDataset(Dataset):
def __init__(self, mode, data="./", task = "SpokenEEG", recon="Y_mel"):
self.sample_rate = 8000
self.n_classes = 13
self.mode = mode
self.iter = iter
self.savedata = data
self.task = task
self.recon = recon
self.max_audio = 32768.0
self.lenth = len(os.listdir(self.savedata + '/train/Y/')) #780 # the number data
self.lenthtest = len(os.listdir(self.savedata + '/test/Y/')) #260
self.lenthval = len(os.listdir(self.savedata + '/val/Y/')) #260
def __len__(self):
if self.mode == 2:
return self.lenthval
elif self.mode == 1:
return self.lenthtest
else:
return self.lenth
def __getitem__(self, idx):
'''
:param idx:
:return:
'''
if self.mode == 2:
forder_name = self.savedata + '/val/'
elif self.mode == 1:
forder_name = self.savedata + '/test/'
else:
forder_name = self.savedata + '/train/'
# tasks
allFileList = os.listdir(forder_name + self.task + "/")
allFileList.sort()
file_name = forder_name + self.task + '/' + allFileList[idx]
# if self.task.find('vec') != -1: # embedding vector
# input, avg_input, std_input = self.read_vector_data(file_name)
if self.task.find('mel') != -1:
input, avg_input, std_input = self.read_data(file_name)
elif self.task.find('Voice') != -1: # voice
input, avg_input, std_input = self.read_voice_data(file_name)
else: # EEG
input, avg_input, std_input = self.read_data(file_name)
# recon target
allFileList = os.listdir(forder_name + self.recon + "/")
allFileList.sort()
file_name = forder_name + self.recon + '/' + allFileList[idx]
# if self.recon.find('vec') != -1: # embedding vector
# target, avg_target, std_target = self.read_vector_data(file_name)
if self.recon.find('mel') != -1:
target, avg_target, std_target = self.read_data(file_name)
elif self.recon.find('Voice') != -1: # voice
target, avg_target, std_target = self.read_voice_data(file_name)
else: # EEG
target, avg_target, std_target = self.read_data(file_name)
# voice
allFileList = os.listdir(forder_name + "Voice/")
allFileList.sort()
file_name = forder_name + "Voice/"+ allFileList[idx]
voice, _, _ = self.read_voice_data(file_name)
# voice=[]
# target label
allFileList = os.listdir(forder_name + "Y/")
allFileList.sort()
file_name = forder_name + 'Y/' + allFileList[idx]
target_cl,_,_ = self.read_raw_data(file_name)
target_cl = np.squeeze(target_cl)
# to tensor
input = torch.tensor(input, dtype=torch.float32)
target = torch.tensor(target, dtype=torch.float32)
return input, target, target_cl, voice, (avg_target, std_target, avg_input, std_input)
def read_vector_data(self, file_name,n_classes):
with open(file_name, 'r', newline='') as f:
lines = csv.reader(f)
data = []
for line in lines:
data.append(line)
data = np.array(data).astype(np.float32)
(r,c) = data.shape
data = np.reshape(data,(n_classes,r//n_classes,c))
max_ = np.max(data).astype(np.float32)
min_ = np.min(data).astype(np.float32)
avg = (max_ + min_) / 2
std = (max_ - min_) / 2
data = np.array((data - avg) / std).astype(np.float32)
return data, avg, std
def read_voice_data(self, file_name):
with open(file_name, 'r', newline='') as f:
lines = csv.reader(f)
data = []
for line in lines:
data.append(line)
data = np.array(data).astype(np.float32)
data = np.array(data / self.max_audio).astype(np.float32)
avg = np.array([0]).astype(np.float32)
return data, avg, self.max_audio
def read_data(self, file_name):
with open(file_name, 'r', newline='') as f:
lines = csv.reader(f)
data = []
for line in lines:
data.append(line)
data = np.array(data).astype(np.float32)
max_ = np.max(data).astype(np.float32)
min_ = np.min(data).astype(np.float32)
avg = (max_ + min_) / 2
std = (max_ - min_) / 2
data = np.array((data - avg) / std).astype(np.float32)
return data, avg, std
def read_raw_data(self, file_name):
with open(file_name, 'r', newline='') as f:
lines = csv.reader(f)
data = []
for line in lines:
data.append(line)
data = np.array(data).astype(np.float32)
avg = np.array([0]).astype(np.float32)
std = np.array([1]).astype(np.float32)
return data, avg, std