之前所用的代码是在tensorflow的基础上强改的,还是存在一些问题,比如样本个数过少之类缺点。最近自己在网上缝缝补补,总算是拼凑出一个勉强可以一用的代码。供大家参考,如果有不足之处,请大家批评指正,欢迎讨论交流。
import numpy as np
from scipy.io import loadmat
name = ["105.mat","118.mat","130.mat","169.mat","185.mat","197.mat","209.mat","222.mat","234.mat","97.mat"]
# For 12k Drive End Bearing Fault Data
data = []
lab = []
label = 1
for axisname in name:
stride=512
signal_size=1024
filename=r'F:\data\cwru\12k Drive End Bearing Fault Data'+'\\'+axisname
axis = ["_DE_time", "_FE_time", "_BA_time"]
datanumber = axisname.split(".") #将105.mat分割为105与mat取105
if eval(datanumber[0]) < 100:
realaxis = "X0" + datanumber[0] + axis[0] #可组成X105_DE_time
else:
realaxis = "X" + datanumber[0] + axis[0]
fl = loadmat(filename)[realaxis]
n=(int)((fl.size-signal_size)/stride+1)
start, end = 0, 0
#贴标签,使用重叠采样,采样数量为 (点数-采样窗口大小)/步长+1
for i in range(n):
start = i * stride
end = signal_size + i * stride
data.append(fl[start:end])
lab.append(label)
label = label + 1
data = np.array(data)
data = data.reshape(-1,signal_size)
lab = np.array(lab)
lab = lab.reshape(-1,1)
print(lab)
smpaledata = np.concatenate((data,lab),axis=1)
Tips:这里的label不能从0开始,否则进网络的话,交叉熵计算会出现问题,好像和torch的底层代码有关系,我是没搞懂的。
欢迎大家交流讨论