Upload HMS_EXP_4_DATASET.py with huggingface_hub
Browse files- HMS_EXP_4_DATASET.py +90 -0
HMS_EXP_4_DATASET.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
class CustomDataset(Dataset):
|
2 |
+
def __init__(
|
3 |
+
self,
|
4 |
+
df : pd.DataFrame,
|
5 |
+
augment : bool = False,
|
6 |
+
mode : str = 'train',
|
7 |
+
specs : Dict[int, np.ndarray] = spectrograms,
|
8 |
+
eeg_specs: Dict[int, np.ndarray] = all_eegs
|
9 |
+
):
|
10 |
+
self.df = df
|
11 |
+
self.augment = augment
|
12 |
+
self.mode = mode
|
13 |
+
self.spectograms = spectrograms
|
14 |
+
self.eeg_spectograms = eeg_specs
|
15 |
+
|
16 |
+
def __len__(self):
|
17 |
+
"""
|
18 |
+
Denotes the number of batches per epoch.
|
19 |
+
"""
|
20 |
+
return len(self.df)
|
21 |
+
|
22 |
+
def __getitem__(self, index):
|
23 |
+
"""
|
24 |
+
Generate one batch of data.
|
25 |
+
"""
|
26 |
+
X, y = self.__data_generation(index)
|
27 |
+
if self.augment:
|
28 |
+
X = self.__transform(X)
|
29 |
+
return {"spectrogram":torch.tensor(X, dtype=torch.float32), "labels":torch.tensor(y, dtype=torch.float32)}
|
30 |
+
|
31 |
+
def __data_generation(self, index):
|
32 |
+
"""
|
33 |
+
Generates data containing batch_size samples.
|
34 |
+
"""
|
35 |
+
X = np.zeros((128, 256, 8), dtype='float32')
|
36 |
+
y = np.zeros(6, dtype='float32')
|
37 |
+
img = np.ones((128,256), dtype='float32')
|
38 |
+
row = self.df.iloc[index]
|
39 |
+
if self.mode=='test':
|
40 |
+
r = 0
|
41 |
+
else:
|
42 |
+
r = int(row['spectrogram_label_offset_seconds'] // 2)
|
43 |
+
|
44 |
+
for region in range(4):
|
45 |
+
img = self.spectograms[row.spectrogram_id][r:r+300, region*100:(region+1)*100].T
|
46 |
+
|
47 |
+
# Log transform spectogram
|
48 |
+
img = np.clip(img, np.exp(-4), np.exp(8))
|
49 |
+
img = np.log(img)
|
50 |
+
|
51 |
+
# Standarize per image
|
52 |
+
ep = 1e-6
|
53 |
+
mu = np.nanmean(img.flatten())
|
54 |
+
std = np.nanstd(img.flatten())
|
55 |
+
img = (img-mu)/(std+ep)
|
56 |
+
img = np.nan_to_num(img, nan=0.0)
|
57 |
+
X[14:-14, :, region] = img[:, 22:-22] / 2.0
|
58 |
+
img = self.eeg_spectograms[row.label_id]
|
59 |
+
X[:, :, 4:] = img
|
60 |
+
|
61 |
+
if self.mode != 'test':
|
62 |
+
y = row[TARGETS].values.astype(np.float32)
|
63 |
+
|
64 |
+
return X, y
|
65 |
+
|
66 |
+
def __transform(self, img):
|
67 |
+
params1 = {
|
68 |
+
"num_masks_x" : 1,
|
69 |
+
"mask_x_length": (0, 20), # This line changed from fixed to a range
|
70 |
+
"fill_value" : (0, 1, 2, 3, 4, 5, 6, 7),
|
71 |
+
}
|
72 |
+
params2 = {
|
73 |
+
"num_masks_y" : 1,
|
74 |
+
"mask_y_length": (0, 20),
|
75 |
+
"fill_value" : (0, 1, 2, 3, 4, 5, 6, 7),
|
76 |
+
}
|
77 |
+
params3 = {
|
78 |
+
"num_masks_x" : (2, 4),
|
79 |
+
"num_masks_y" : 5,
|
80 |
+
"mask_y_length": 8,
|
81 |
+
"mask_x_length": (10, 20),
|
82 |
+
"fill_value" : (0, 1, 2, 3, 4, 5, 6, 7),
|
83 |
+
}
|
84 |
+
|
85 |
+
transforms = A.Compose([
|
86 |
+
A.XYMasking(**params1, p=0.3),
|
87 |
+
A.XYMasking(**params2, p=0.3),
|
88 |
+
A.XYMasking(**params3, p=0.3),
|
89 |
+
])
|
90 |
+
return transforms(image=img)['image']
|