-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
201 lines (168 loc) · 7.02 KB
/
utils.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import logging
import os
import torch
import pywt
import logging
import h5py
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import stft
from PIL import Image
def setup_logger(log_path, experiment_title):
logger = logging.getLogger()
logger.setLevel(logging.INFO)
formatter = logging.Formatter("[%(asctime)s] %(message)s",
datefmt="%Y-%m-%d %H:%M:%S")
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
ch.setFormatter(formatter)
fh = logging.FileHandler(os.path.join(log_path,
experiment_title + '.log'))
fh.setLevel(logging.INFO)
fh.setFormatter(formatter)
logger.addHandler(ch)
logger.addHandler(fh)
def accuracy(predictions, targets):
predictions = predictions.argmax(dim=1).view(targets.shape)
return (predictions == targets).sum().float() / targets.size(0)
def fast_adapt(batch, learner, loss, adaptation_steps, shots, ways, device):
data, labels = batch
data, labels = data.to(device), labels.to(device)
# Separate data into adaptation/evalutation sets
adaptation_indices = np.zeros(data.size(0), dtype=bool)
adaptation_indices[np.arange(shots*ways) * 2] = True
evaluation_indices = torch.from_numpy(~adaptation_indices)
adaptation_indices = torch.from_numpy(adaptation_indices)
adaptation_data, adaptation_labels = data[adaptation_indices], labels[adaptation_indices]
evaluation_data, evaluation_labels = data[evaluation_indices], labels[evaluation_indices]
# Adapt the model
for step in range(adaptation_steps):
train_error = loss(learner(adaptation_data), adaptation_labels)
learner.adapt(train_error)
# Evaluate the adapted model
predictions = learner(evaluation_data)
valid_error = loss(predictions, evaluation_labels)
valid_accuracy = accuracy(predictions, evaluation_labels)
return valid_error, valid_accuracy
def pairwise_distances_logits(a, b):
n = a.shape[0]
m = b.shape[0]
logits = -((a.unsqueeze(1).expand(n, m, -1) -
b.unsqueeze(0).expand(n, m, -1))**2).sum(dim=2)
return logits
def print_logs(iteration, meta_train_error, meta_train_accuracy, meta_test_error, meta_test_accuracy):
logging.info('Iteration {}:'.format(iteration))
logging.info('Meta Train Results:')
logging.info('Meta Train Error: {}.'.format(meta_train_error))
logging.info('Meta Train Accuracy: {}.'.format(meta_train_accuracy))
logging.info('Meta Test Results:')
logging.info('Meta Test Error: {}.'.format(meta_test_error))
logging.info('Meta Test Accuracy: {}.\n'.format(meta_test_accuracy))
def normalize(data):
return (data-min(data)) / (max(data)-min(data))
def make_time_frequency_image_STFT(dataset_name,
dataset,
img_size,
window_size,
overlap,
img_path):
overlap_samples = int(window_size * overlap)
frequency, time, magnitude = stft(dataset, nperseg=window_size, noverlap=overlap_samples)
if dataset_name == 'HST':
magnitude = np.log10(np.abs(magnitude) + 1e-10)
else:
magnitude = np.abs(magnitude)
# Image Plotting
plt.pcolormesh(time, frequency, magnitude, shading='gouraud')
plt.axis('off')
plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
plt.margins(0, 0)
plt.gcf().set_size_inches(img_size/100, img_size/100)
plt.savefig(img_path, dpi=100)
plt.clf()
plt.close()
def make_time_frequency_image_WT(dataset_name,
data,
img_size,
img_path):
# Data Length
sampling_length = len(data)
# Wavelet Transform Parameters Setting
if dataset_name == 'CWRU':
sampling_period = 1.0 / 12000
total_scale = 128
wavelet = 'cmor100-1'
elif dataset_name == 'HST':
sampling_period = 4e-6
total_scale = 16
wavelet = 'morl'
else:
raise ValueError("Invalid dataset name")
fc = pywt.central_frequency(wavelet)
cparam = 2 * fc * total_scale
scales = cparam / np.arange(total_scale, 0, -1)
# Conduct Wavelet Transform
coefficients, frequencies = pywt.cwt(data, scales, wavelet, sampling_period)
amplitude = abs(coefficients)
if dataset_name == 'HST':
amplitude = np.log10(amplitude + 1e-4)
# Image Plotting
t = np.linspace(0, sampling_period, sampling_length, endpoint=False)
plt.contourf(t, frequencies, amplitude, cmap='jet')
plt.axis('off')
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
plt.margins(0, 0)
plt.gcf().set_size_inches(img_size/100, img_size/100)
plt.savefig(img_path, dpi=100)
plt.clf()
plt.close()
def generate_time_frequency_image_dataset(dataset_name,
algorithm,
dataset,
labels,
img_size,
window_size,
overlap,
img_dir):
for index in range(len(labels)):
count = 0
for i, data in enumerate(dataset[labels[index]]):
os.makedirs(img_dir, exist_ok=True)
img_path = img_dir + str(index) + "_" + str(count)
if algorithm == 'STFT':
make_time_frequency_image_STFT(dataset_name,
data,
img_size,
window_size,
overlap,
img_path)
elif algorithm == 'WT':
make_time_frequency_image_WT(dataset_name,
data,
img_size,
img_path)
else:
raise ValueError("Invalid algorithm name")
count += 1
image_list = os.listdir(img_dir)
for image_name in image_list:
image_path = os.path.join(img_dir, image_name)
img = Image.open(image_path)
img = img.convert('RGB')
img.save(image_path)
def loadmat_v73(data_path, realaxis, channel):
with h5py.File(data_path, 'r') as f:
mat_data = f[f[realaxis]['Y']['Data'][channel][0]]
return mat_data[:].reshape(-1)
def extract_dict_data(dataset):
x = np.concatenate([dataset[key] for key in dataset.keys()])
y = []
for i, key in enumerate(dataset.keys()):
number = len(dataset[key])
y.append(np.tile(i, number))
y = np.concatenate(y)
return x, y
if __name__ == '__main__':
pass