-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfault_datasets.py
155 lines (125 loc) · 4.53 KB
/
fault_datasets.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
"""
PyTorch dataset classes for CWRU and HST datasets.
"""
import torch
from scipy.io import loadmat
from torch.utils.data import Dataset
from torchvision import transforms
from preprocess_cwru import load_CWRU_dataset
from preprocess_hst import load_HST_dataset
from utils import extract_dict_data
from PIL import Image
import os
class CWRU(Dataset):
def __init__(self,
domain,
dir_path,
preprocess,
transform=None):
super(CWRU, self).__init__()
if domain not in [0, 1, 2, 3]:
raise ValueError('Argument "domain" must be 0, 1, 2 or 3.')
self.domain = domain
self.dir_path = dir_path
if preprocess != 'FFT':
self.img_dir = dir_path + "/{}_CWRU/Drive_end_".format(preprocess) + str(domain) + "/"
else:
self.img_dir = dir_path + "/CWRU/Drive_end_" + str(domain) + "/"
self.img_list = os.listdir(self.img_dir)
if transform is None:
self.transform = transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
else:
self.transform = transform
def __len__(self):
return len(self.img_list)
def __getitem__(self, index):
img_name = self.img_list[index]
label = torch.tensor(int(img_name.split('_')[0]), dtype=torch.int64)
img_path = self.img_dir + img_name
img = Image.open(img_path)
img = self.transform(img)
return img, label
class CWRU_FFT(Dataset):
def __init__(self,
domain,
dir_path,
fft=True):
super(CWRU_FFT, self).__init__()
self.root = dir_path
if domain not in [0, 1, 2, 3]:
raise ValueError('Argument "domain" must be 0, 1, 2 or 3.')
self.domain = domain
self.dataset = load_CWRU_dataset(domain, dir_path, raw=True, fft=fft)
self.data, self.labels = extract_dict_data(self.dataset)
def __len__(self):
return len(self.data)
def __getitem__(self, index):
sample = self.data[index]
label = self.labels[index]
sample = torch.from_numpy(sample).float()
label = torch.tensor(label)
return sample, label
class HST(Dataset):
def __init__(self,
domain,
dir_path,
preprocess,
transform=None):
super(HST, self).__init__()
if domain not in [0, 1, 2]:
raise ValueError('Argument "domain" must be 0, 1, or 2.')
self.domain = domain
self.dir_path = dir_path
if preprocess == 'STFT' or preprocess == 'WT':
self.img_dir = dir_path + "/{}_HST/".format(preprocess) + str(domain) + "/"
elif preprocess == 'FFT':
self.img_dir = dir_path + "/HST/".format(preprocess) + str(domain) + "/"
else:
raise ValueError('Invalid preprocess name.')
self.img_list = os.listdir(self.img_dir)
if transform is None:
self.transform = transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
else:
self.transform = transform
def __len__(self):
return len(self.img_list)
def __getitem__(self, index):
img_name = self.img_list[index]
label = torch.tensor(int(img_name.split('_')[0]), dtype=torch.int64)
img_path = self.img_dir + img_name
img = Image.open(img_path)
img = self.transform(img)
return img, label
class HST_FFT(Dataset):
def __init__(self,
domain,
dir_path,
fft=True):
super(HST_FFT, self).__init__()
self.root = dir_path
if domain not in [0, 1, 2]:
raise ValueError('Argument "domain" must be 0, 1 or 2')
self.domain = domain
self.dataset = load_HST_dataset(domain, dir_path, raw=True, fft=fft)
self.data, self.labels = extract_dict_data(self.dataset)
def __len__(self):
return len(self.data)
def __getitem__(self, index):
sample = self.data[index]
label = self.labels[index]
sample = torch.from_numpy(sample).float()
label = torch.tensor(label)
return sample, label
if __name__ == '__main__':
data = CWRU(1, './data')
data.__getitem__(0)
data = HST(1, './data')
print(data.__getitem__(0))