-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathtensorflow_input_data.py
209 lines (159 loc) · 5.6 KB
/
tensorflow_input_data.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
# coding:utf-8
import os.path
import sys
import re
import os
import json
import tensorflow as tf
import numpy as np
from sklearn import preprocessing
import pickle as pickle #python pkl 文件读写
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from Controller import Algorithm_CWT
from Model.Seg import SegFile
def myJsonLoad(filePath):
'''把文件打开从字符串转换成数据类型'''
with open(filePath,'rb') as load_file:
load_dict = json.load(load_file)
return load_dict
def MyLabels(Labels):
returnLabels = []
returnLabels_oneHot =[]
# print(type(Labels))
# print(Labels)
for i in range(1,4):
# print(Labels[str(i)])
# print(Labels[i-1][0], Labels[i-1][1])
for j in range(Labels[str(i)][0], Labels[str(i)][1]):
returnLabels.append(i)
tag = [0,0,0,0]
tag[i-1] = 1
returnLabels_oneHot.append(tag)
return returnLabels, returnLabels_oneHot
def opeanFile(fileName):
fileName = fileName
segFile = SegFile()
reply = segFile.loadFile(fileName)
if(reply != 0):
print('error!')
else:
# print(len(segFile.dataList[1].data))
cwtmatr = []
for i in range(segFile.tapeNum):
cwtmatr.append(Algorithm_CWT.MyWavelets( segFile.dataList[i].data, 128) )
return cwtmatr
def MyPlot(cwtmatr):
''' 绘图 '''
# print(type(cwtmatr))
# print(len(cwtmatr))
# print(len(cwtmatr[0]))
# plt.plot(cwtmatr[1])
# plt.plot(cwtmatr[10])
# plt.plot(cwtmatr[200])
# plt.plot(cwtmatr[300])
# plt.plot(cwtmatr[400])
# plt.plot(cwtmatr[500])
# plt.plot(cwtmatr[600])
# plt.plot(cwtmatr[700])
# plt.plot(cwtmatr[1200])
# plt.plot(cwtmatr[1210])
# plt.plot(cwtmatr[1300])
# plt.plot(cwtmatr[1400])
# plt.plot(cwtmatr[1500])
# plt.plot(cwtmatr[1800])
# plt.plot(cwtmatr[1850])
# plt.plot(cwtmatr[1900])
# plt.plot(cwtmatr[1950])
# plt.plot(cwtmatr[2000])
# plt.plot(cwtmatr[2100])
# plt.plot(cwtmatr[2300])
# plt.plot(cwtmatr[2500])
# plt.matshow(cwtmatr)
plt.show()
def saveData(all_cwtmatr, all_labels_oneHot):
pass
# import os
filePath_data = 'tf_model_lstm/train_seg_data.plk'
if os.path.exists(filePath_data): #删除文件,可使用以下两种方法。
os.remove(filePath_data) #os.unlink(my_file)
with open(filePath_data,'wb') as f:
pickle.dump(all_cwtmatr, f)
filePath_labels = 'tf_model_lstm/train_seg_labels.plk'
if os.path.exists(filePath_labels): #删除文件,可使用以下两种方法。
os.remove(filePath_labels) #os.unlink(my_file)
with open(filePath_labels,'wb') as f:
pickle.dump(all_labels_oneHot, f)
def inputData(firstFileNo, lastFileNo):
# print('目前系统的编码为:',sys.getdefaultencoding())
''' 训练数据生成 '''
path = 'C:/锚索测量数据库/LSTMs训练数据/'
path_TagJson = 'C:/锚索测量数据库/LSTMs训练数据/tag.json'
TagDict = myJsonLoad(path_TagJson)
all_cwtmatr = []
all_labels_oneHot = []
for number in range(firstFileNo, lastFileNo+1):
# number = 5 # 1~5
cwtmatr_cwt = opeanFile(path + str(number)+'.seg')
for i in range(len(cwtmatr_cwt)):
cwtmatr = np.array( cwtmatr_cwt[i] )
# 逐行 Z-Score 标准化
cwtmatr = preprocessing.scale(cwtmatr, axis =1)
all_cwtmatr.append(cwtmatr)
# MyPlot(cwtmatr)
labels = TagDict[number-1]['items']
labels, labels_oneHot = MyLabels( labels )
all_labels_oneHot.append( np.array(labels_oneHot) )
# plt.plot(labels)
# plt.show()
all_cwtmatr = np.array(all_cwtmatr )
all_labels_oneHot = np.array(all_labels_oneHot )
# print(all_cwtmatr.shape)
# print(all_labels_oneHot.shape)
# print(all_labels_oneHot[0].shape)
return all_cwtmatr, all_labels_oneHot
if __name__ == "__main__":
all_cwtmatr, all_labels_oneHot = inputData(4, 5) # number: 1~5
saveData(all_cwtmatr, all_labels_oneHot)
print('\ndone!')
''' *******************
class MyData():
def __init__(self):
self.data_filePath = []
self.data_fileName = []
self.data_tpye = []
self.data = []
self.labels = []
# 遍历指定目录,显示目录下的所有文件名
def eachFile(filepath):
pathDir = os.listdir(filepath)
data = MyData()
for allDir in pathDir:
child = os.path.join('%s/%s' % (filepath, allDir))
if os.path.isfile(child):
data.data_filePath.append(child)
data.data_fileName.append(allDir)
theTpye = re.split('\.',allDir)[0]
# print(theTpye)
data.data_tpye.append( theTpye )
# # 显示
# for i in array:
# print(i)
return data
def saveData(py_data, filePath_data, filePath_labels):
pass
# with tf.Session() as sess:
# train_data =tf.convert_to_tensor(np.array( trainData.data ) )
data = np.array( py_data.data )
labels = py_data.labels
# import os
if os.path.exists(filePath_data): #删除文件,可使用以下两种方法。
os.remove(filePath_data) #os.unlink(my_file)
if os.path.exists(filePath_labels): #删除文件,可使用以下两种方法。
os.remove(filePath_labels) #os.unlink(my_file)
with open(filePath_data,'wb') as f:
pickle.dump(data, f)
with open(filePath_labels,'wb') as f:
pickle.dump(labels, f)
print('\ndone!')
'''