-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathdataset2.py
250 lines (191 loc) · 7.47 KB
/
dataset2.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import os
import glob
from sklearn.utils import shuffle
import numpy as np
import scipy.signal as signal
def load_train(train_path, classes):
samples = []
labels = []
sample_names = []
cls = []
for fields in classes:
index = classes.index(fields)
path = os.path.join(train_path, fields, '*.npy')
files = glob.glob(path)
for fl in files:
iq_samples = np.load(fl)
real = np.real(iq_samples)
imag = np.imag(iq_samples)
# I values, then Q values...
# iq_samples = np.concatenate((real, imag))
# iq_samples = np.reshape(iq_samples, (-1, 2, 12500))
# I,Q,I,Q...
iq_samples = np.ravel(np.column_stack((real, imag)))
iq_samples = iq_samples[:24576] # truncate some samples
iq_samples = np.reshape(iq_samples, (-1, 128, 2))
samples.append(iq_samples)
label = np.zeros(len(classes))
label[index] = 1.0
labels.append(label)
flbase = os.path.basename(fl)
sample_names.append(flbase)
cls.append(fields)
samples = np.array(samples)
labels = np.array(labels)
sample_names = np.array(sample_names)
cls = np.array(cls)
return samples, labels, sample_names, cls
def load_train2(train_path, classes):
samples = []
labels = []
sample_names = []
cls = []
for fields in classes:
index = classes.index(fields)
path = os.path.join(train_path, fields, '*.npy')
files = glob.glob(path)
for fl in files:
# raw decimated samples
iq_samples = np.load(fl)
# print(iq_samples)
real = np.real(iq_samples)
imag = np.imag(iq_samples)
# I array, then Q array
# iq_samples = np.concatenate((real, imag))
# iq_samples = np.reshape(iq_samples, (-1, 25000))
# concatenate I, Q, I, Q...
iq_samples = np.ravel(np.column_stack((real, imag)))
multiple = True
if multiple:
iq_samples1 = iq_samples[:1568]
iq_samples1 = iq_samples1.reshape(28, 28, 2)
iq_samples2 = iq_samples[1568:3136]
iq_samples2 = iq_samples2.reshape(28, 28, 2)
iq_samples3 = iq_samples[3136:4704]
iq_samples3 = iq_samples3.reshape(28, 28, 2)
iq_samples4 = iq_samples[4704:6272]
iq_samples4 = iq_samples4.reshape(28, 28, 2)
samples.append(iq_samples1)
samples.append(iq_samples2)
samples.append(iq_samples3)
samples.append(iq_samples4)
flbase = os.path.basename(fl)
label = np.zeros(len(classes))
label[index] = 1.0
labels.append(label)
labels.append(label)
labels.append(label)
labels.append(label)
sample_names.append(flbase)
sample_names.append(flbase)
sample_names.append(flbase)
sample_names.append(flbase)
cls.append(fields)
cls.append(fields)
cls.append(fields)
cls.append(fields)
else:
iq_samples = iq_samples[:1568]
# print(iq_samples)
iq_samples = iq_samples.reshape(28, 28, 2)
# print(iq_samples)
# exit(-1)
samples.append(iq_samples)
label = np.zeros(len(classes))
label[index] = 1.0
labels.append(label)
flbase = os.path.basename(fl)
sample_names.append(flbase)
cls.append(fields)
samples = np.array(samples)
labels = np.array(labels)
sample_names = np.array(sample_names)
cls = np.array(cls)
'''
np.save("save\samples.npy", samples)
np.save("save\labels.npy", labels)
np.save("save\sample_names.npy", sample_names)
np.save("save\cls.npy", cls)
'''
return samples, labels, sample_names, cls
def load_train3(train_path, classes):
samples = np.load("save\samples.npy")
labels = np.load("save\labels.npy")
sample_names = np.load("save\sample_names.npy")
cls = np.load("save\cls.npy")
return samples, labels, sample_names, cls
class DataSet2(object):
def __init__(self, images, labels, img_names, cls):
self._num_examples = images.shape[0]
self._images = images
self._labels = labels
self._img_names = img_names
self._cls = cls
self._epochs_done = 0
self._index_in_epoch = 0
@property
def images(self):
return self._images
@property
def labels(self):
return self._labels
@property
def img_names(self):
return self._img_names
@property
def cls(self):
return self._cls
@property
def num_examples(self):
return self._num_examples
@property
def epochs_done(self):
return self._epochs_done
def next_batch(self, batch_size):
start = self._index_in_epoch
self._index_in_epoch += batch_size
if self._index_in_epoch > self._num_examples:
self._epochs_done += 1
start = 0
self._index_in_epoch = batch_size
assert batch_size <= self._num_examples
end = self._index_in_epoch
return self._images[start:end], self._labels[start:end], self._img_names[start:end], self._cls[start:end]
def read_train_sets(train_path, classes, validation_size):
class DataSets(object):
pass
data_sets = DataSets()
images, labels, img_names, cls = load_train(train_path, classes)
images, labels, img_names, cls = shuffle(images, labels, img_names, cls)
if isinstance(validation_size, float):
validation_size = int(validation_size * images.shape[0])
validation_images = images[:validation_size]
validation_labels = labels[:validation_size]
validation_img_names = img_names[:validation_size]
validation_cls = cls[:validation_size]
train_images = images[validation_size:]
train_labels = labels[validation_size:]
train_img_names = img_names[validation_size:]
train_cls = cls[validation_size:]
data_sets.train = DataSet2(train_images, train_labels, train_img_names, train_cls)
data_sets.valid = DataSet2(validation_images, validation_labels, validation_img_names, validation_cls)
return data_sets
def read_train_sets2(train_path, classes, validation_size):
class DataSets(object):
pass
data_sets = DataSets()
images, labels, img_names, cls = load_train2(train_path, classes) # 2 calculating, 3 loading
images, labels, img_names, cls = shuffle(images, labels, img_names, cls)
if isinstance(validation_size, float):
validation_size = int(validation_size * images.shape[0])
validation_images = images[:validation_size]
validation_labels = labels[:validation_size]
validation_img_names = img_names[:validation_size]
validation_cls = cls[:validation_size]
train_images = images[validation_size:]
train_labels = labels[validation_size:]
train_img_names = img_names[validation_size:]
train_cls = cls[validation_size:]
data_sets.train = DataSet2(train_images, train_labels, train_img_names, train_cls)
data_sets.valid = DataSet2(validation_images, validation_labels, validation_img_names, validation_cls)
return data_sets