-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathdataset.py
100 lines (78 loc) · 3.01 KB
/
dataset.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
import numpy as np
import glob
import scipy.io as sio
import torch
from torch.utils.data import Dataset, DataLoader
def UT_HAR_dataset(root_dir):
data_list = glob.glob(root_dir+'/UT_HAR/data/*.csv')
label_list = glob.glob(root_dir+'/UT_HAR/label/*.csv')
WiFi_data = {}
for data_dir in data_list:
data_name = data_dir.split('/')[-1].split('.')[0]
with open(data_dir, 'rb') as f:
data = np.load(f)
data = data.reshape(len(data),1,250,90)
data_norm = (data - np.min(data)) / (np.max(data) - np.min(data))
WiFi_data[data_name] = torch.Tensor(data_norm)
for label_dir in label_list:
label_name = label_dir.split('/')[-1].split('.')[0]
with open(label_dir, 'rb') as f:
label = np.load(f)
WiFi_data[label_name] = torch.Tensor(label)
return WiFi_data
# dataset: /class_name/xx.mat
class CSI_Dataset(Dataset):
"""CSI dataset."""
def __init__(self, root_dir, modal='CSIamp', transform=None, few_shot=False, k=5, single_trace=True):
"""
Args:
root_dir (string): Directory with all the images.
modal (CSIamp/CSIphase): CSI data modal
transform (callable, optional): Optional transform to be applied
on a sample.
"""
self.root_dir = root_dir
self.modal=modal
self.transform = transform
self.data_list = glob.glob(root_dir+'/*/*.mat')
self.folder = glob.glob(root_dir+'/*/')
self.category = {self.folder[i].split('/')[-2]:i for i in range(len(self.folder))}
def __len__(self):
return len(self.data_list)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
sample_dir = self.data_list[idx]
y = self.category[sample_dir.split('/')[-2]]
x = sio.loadmat(sample_dir)[self.modal]
# normalize
x = (x - 42.3199)/4.9802
# sampling: 2000 -> 500
x = x[:,::4]
x = x.reshape(3, 114, 500)
if self.transform:
x = self.transform(x)
x = torch.FloatTensor(x)
return x,y
class Widar_Dataset(Dataset):
def __init__(self,root_dir):
self.root_dir = root_dir
self.data_list = glob.glob(root_dir+'/*/*.csv')
self.folder = glob.glob(root_dir+'/*/')
self.category = {self.folder[i].split('/')[-2]:i for i in range(len(self.folder))}
def __len__(self):
return len(self.data_list)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
sample_dir = self.data_list[idx]
y = self.category[sample_dir.split('/')[-2]]
x = np.genfromtxt(sample_dir, delimiter=',')
# normalize
x = (x - 0.0025)/0.0119
# reshape: 22,400 -> 22,20,20
x = x.reshape(22,20,20)
# interpolate from 20x20 to 32x32
# x = self.reshape(x)
x = torch.FloatTensor(x)
return x,y