mirror of
https://github.com/This-weiyuqing/ECG_by_Diffenert_CNN_Test.git
synced 2026-10-18 03:13:39 +08:00
load two layer of cnn
This commit is contained in:
1 parent
815bd40d40
commit
5f757ad939
11 files changed
+158
-126
No files matched your search
BIN
Binary file not shown.
@@ -1,92 +0,0 @@
|
|||||||
import numpy as np
|
|
||||||
import pywt
|
|
||||||
import wfdb
|
|
||||||
import tensorflow as tf
|
|
||||||
|
|
||||||
ECGDATAPATH= "mit-bih-arrhythmia-database-1.0.0/"
|
|
||||||
|
|
||||||
|
|
||||||
def denoise(date):
|
|
||||||
# Wavelet db5 level9
|
|
||||||
coeffs = pywt.wavedec(data=date, wavelet='db5', level=9)
|
|
||||||
cA9, cD9, cD8, cD7, cD6, cD5, cD4, cD3, cD2, cD1 = coeffs
|
|
||||||
# noice del
|
|
||||||
threshold = (np.median(np.abs(cD1)) / 0.6745) * (np.sqrt(2 * np.log(len(cD1))))
|
|
||||||
# take cD 1,2 turn 0 to denoise
|
|
||||||
cD1.fill(0)
|
|
||||||
cD2.fill(0)
|
|
||||||
for i in range(1, len(coeffs) - 2):
|
|
||||||
coeffs[i] = pywt.threshold(coeffs[i], threshold)
|
|
||||||
# get signal that before wavelet
|
|
||||||
rdata = pywt.waverec(coeffs=coeffs, wavelet='db5')
|
|
||||||
return rdata
|
|
||||||
|
|
||||||
|
|
||||||
# get ECG data,date type and denoise
|
|
||||||
# this function without return
|
|
||||||
# use append take return data to pass data
|
|
||||||
def getDataSet(number, X_data, Y_data):
|
|
||||||
ecgClassSet = ['N', 'A', 'V', 'l', 'R']
|
|
||||||
print('get' + number + ' ECG data')
|
|
||||||
record = wfdb.rdrecord(ECGDATAPATH + number, channel_names=['MLII'])
|
|
||||||
data = record.p_signal.flatten()
|
|
||||||
# use def denoise
|
|
||||||
rdata = denoise(date=data)
|
|
||||||
|
|
||||||
# get type of ECG data
|
|
||||||
annotation = wfdb.rdann(ECGDATAPATH + number, 'atr')
|
|
||||||
Rlocation = annotation.sample # annotation.sample:get R
|
|
||||||
Rclass = annotation.symbol
|
|
||||||
|
|
||||||
# delete stable date near R
|
|
||||||
start = 10
|
|
||||||
end = 5
|
|
||||||
i = start
|
|
||||||
j = len(annotation.symbol) - end
|
|
||||||
|
|
||||||
# get NAVLR type data
|
|
||||||
while i < j:
|
|
||||||
try:
|
|
||||||
lable = ecgClassSet.index(Rclass[i])
|
|
||||||
x_train = rdata[Rlocation[i] - 99:Rlocation[i] + 201]
|
|
||||||
X_data.append(x_train)
|
|
||||||
Y_data.append(lable)
|
|
||||||
i += 1
|
|
||||||
except ValueError:
|
|
||||||
i += 1
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
# RATIO=The ratio of the training set to the test set,usually is 0.3
|
|
||||||
def loadData(RATIO):
|
|
||||||
numberSet = [
|
|
||||||
'100', '101', '103', '105', '106', '107', '108', '109', '111', '112', '113', '114', '115', '116', '117', '119',
|
|
||||||
'121', '122', '123', '124', '200', '201', '202', '203', '205', '208', '210', '212', '213', '214', '215', '217',
|
|
||||||
'219', '220', '221', '222', '223', '228', '230', '231', '232', '233', '234'
|
|
||||||
]
|
|
||||||
dataSet = []
|
|
||||||
labelSet = []
|
|
||||||
for n in numberSet:
|
|
||||||
getDataSet(n, dataSet, labelSet)
|
|
||||||
|
|
||||||
dataSet = np.array(dataSet).reshape(-1, 300)
|
|
||||||
labelSet = np.array(labelSet).reshape(-1, 1)
|
|
||||||
train_ds = np.hstack((dataSet, labelSet))
|
|
||||||
np.random.shuffle(train_ds)
|
|
||||||
|
|
||||||
X = train_ds[:, :300].reshape(-1, 300, 1)
|
|
||||||
Y = train_ds[:, 300]
|
|
||||||
# cut test and train data from ECG data
|
|
||||||
shuffle_index = np.random.permutation(len(X))
|
|
||||||
test_length = int(RATIO * len(shuffle_index))
|
|
||||||
test_index = shuffle_index[:test_length]
|
|
||||||
train_index = shuffle_index[test_index:]
|
|
||||||
X_test, Y_test = X[test_index:], Y[test_index]
|
|
||||||
X_train, Y_train = X[train_index], Y[train_index]
|
|
||||||
return X_train, Y_test, X_test, Y_train
|
|
||||||
|
|
||||||
|
|
||||||
class CNN_model_level_one():
|
|
||||||
def __init__(self):
|
|
||||||
super().__init__()
|
|
||||||
self.conv1 = tf.layers.Conv1d(filter=4)
|
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
import datetime
|
||||||
|
import os
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import pywt
|
||||||
|
import wfdb
|
||||||
|
import tensorflow as tf
|
||||||
|
from tensorflow import keras
|
||||||
|
from utils import loadData, plot_history_tf, plot_heat_map
|
||||||
|
|
||||||
|
#from ECG_read_weiyuqing_without_wfdb import ECGDATAPATH
|
||||||
|
#PATH of this test
|
||||||
|
Project_PATH = "./Number-Of-CNN-Layers/Two/"
|
||||||
|
#PICTUREPATH= Project_PATH + "picture/"
|
||||||
|
log_dir = Project_PATH + "logs/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
|
||||||
|
model_Path_one = Project_PATH + "model/ecg_model_one_layer.h5"
|
||||||
|
#model_Path_one = Project_PATH + "ecg_model_one_layer.h5"
|
||||||
|
|
||||||
|
RATIO = 0.3
|
||||||
|
RANDOM_SEED = 42
|
||||||
|
BATCH_SIZE = 128
|
||||||
|
NUM_EPOCHS = 30
|
||||||
|
|
||||||
|
|
||||||
|
def CNN_model_level_one():
|
||||||
|
leavlOneModel = tf.keras.models.Sequential([
|
||||||
|
|
||||||
|
# #four CNN layers test
|
||||||
|
# tf.keras.layers.InputLayer(input_shape=(300,)),
|
||||||
|
# # reshape the tensor with shape (batch_size, 300) to (batch_size, 300, 1)
|
||||||
|
# tf.keras.layers.Reshape(target_shape=(300, 1)),
|
||||||
|
# # the first convolution layer, 4 21x1 convolution kernels, output shape (batch_size, 300, 4)
|
||||||
|
# tf.keras.layers.Conv1D(filters=4, kernel_size=21, strides=1, padding='SAME', activation='relu'),
|
||||||
|
# # the first pooling layer, max pooling, pooling size=3 , stride=2, output shape (batch_size, 150, 4)
|
||||||
|
# tf.keras.layers.MaxPool1D(pool_size=3, strides=2, padding='SAME'),
|
||||||
|
# # the second convolution layer, 16 23x1 convolution kernels, output shape (batch_size, 150, 16)
|
||||||
|
# tf.keras.layers.Conv1D(filters=16, kernel_size=23, strides=1, padding='SAME', activation='relu'),
|
||||||
|
# # the second pooling layer, max pooling, pooling size=3, stride=2, output shape (batch_size, 75, 16)
|
||||||
|
# tf.keras.layers.MaxPool1D(pool_size=3, strides=2, padding='SAME'),
|
||||||
|
# # the third convolution layer, 32 25x1 convolution kernels, output shape (batch_size, 75, 32)
|
||||||
|
# tf.keras.layers.Conv1D(filters=32, kernel_size=25, strides=1, padding='SAME', activation='relu'),
|
||||||
|
# # the third pooling layer, average pooling, pooling size=3, stride=2, output shape (batch_size, 38, 32)
|
||||||
|
# tf.keras.layers.AvgPool1D(pool_size=3, strides=2, padding='SAME'),
|
||||||
|
# # the fourth convolution layer, 64 27x1 convolution kernels, output shape (batch_size, 38, 64)
|
||||||
|
# tf.keras.layers.Conv1D(filters=64, kernel_size=27, strides=1, padding='SAME', activation='relu'),
|
||||||
|
# # flatten layer, for the next fully connected layer, output shape (batch_size, 38*64)
|
||||||
|
# tf.keras.layers.Flatten(),
|
||||||
|
# # fully connected layer, 128 nodes, output shape (batch_size, 128)
|
||||||
|
# tf.keras.layers.Dense(128, activation='relu'),
|
||||||
|
# # Dropout layer, dropout rate = 0.2
|
||||||
|
# tf.keras.layers.Dropout(rate=0.2),
|
||||||
|
# # fully connected layer, 5 nodes (number of classes), output shape (batch_size, 5)
|
||||||
|
# tf.keras.layers.Dense(5, activation='softmax')
|
||||||
|
|
||||||
|
#take test for one CNN layers model
|
||||||
|
|
||||||
|
tf.keras.layers.InputLayer(input_shape=(300, 1)),
|
||||||
|
|
||||||
|
# ECG data cant add 0 in edge, will broken data ,take false answer. So we need padding same data in edge.
|
||||||
|
|
||||||
|
# take four 21*1 conv excitation function used RElu
|
||||||
|
tf.keras.layers.Conv1D(filters=4, kernel_size=21, strides=1, padding='same', activation='relu'),
|
||||||
|
# take four 3*1 pool,take strids 2.
|
||||||
|
tf.keras.layers.MaxPool1D(pool_size=3, strides=2, padding='same'),
|
||||||
|
|
||||||
|
tf.keras.layers.Conv1D(filters=4, kernel_size=21, strides=1, padding='same', activation='relu'),
|
||||||
|
tf.keras.layers.MaxPool1D(pool_size=3, strides=2, padding='same'),
|
||||||
|
# tf.keras.layers.Conv1D(filters=4, kernel_size=21, strides=1, padding='same', activation='relu'),
|
||||||
|
# tf.keras.layers.MaxPool1D(pool_size=3, strides=2, padding='same'),
|
||||||
|
|
||||||
|
tf.keras.layers.Flatten(),
|
||||||
|
tf.keras.layers.Dense(128, activation='relu'),
|
||||||
|
tf.keras.layers.Dropout(rate=0.2),
|
||||||
|
tf.keras.layers.Dense(5, activation='softmax')
|
||||||
|
])
|
||||||
|
return leavlOneModel
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
# get traxin test
|
||||||
|
X_train, X_test, y_train, y_test = loadData(RATIO, RANDOM_SEED)
|
||||||
|
|
||||||
|
# get model or create model
|
||||||
|
if os.path.exists(model_Path_one):
|
||||||
|
print(' get model in h5 file')
|
||||||
|
model = tf.keras.models.load_model(filepath=model_Path_one)
|
||||||
|
else:
|
||||||
|
# create new model(if model unexists)
|
||||||
|
model = CNN_model_level_one()
|
||||||
|
model.compile(optimizer='adam',
|
||||||
|
loss='sparse_categorical_crossentropy',
|
||||||
|
metrics=['accuracy'])
|
||||||
|
model.summary()
|
||||||
|
# TB make
|
||||||
|
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir,
|
||||||
|
histogram_freq=1,
|
||||||
|
)
|
||||||
|
# Training and validation
|
||||||
|
history = model.fit(X_train, y_train, epochs=NUM_EPOCHS,
|
||||||
|
batch_size=BATCH_SIZE,
|
||||||
|
validation_data=(X_test, y_test),
|
||||||
|
callbacks=[tensorboard_callback])
|
||||||
|
# validation_split=RATIO,
|
||||||
|
model.save(filepath=model_Path_one)
|
||||||
|
plot_history_tf(history)
|
||||||
|
|
||||||
|
y_pred = np.argmax(model.predict(X_test), axis=-1)
|
||||||
|
plot_heat_map(y_test, y_pred)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
||||||
|
main()
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
import numpy as np
|
|
||||||
import pywt
|
|
||||||
import wfdb
|
|
||||||
from matplotlib import pyplot as plt
|
|
||||||
|
|
||||||
#file error
|
|
||||||
|
|
||||||
#use db5 ,level9 do deviose
|
|
||||||
from ECG_read_weiyuqing_without_wfdb import ECGDATAPATH
|
|
||||||
|
|
||||||
#from ECG_read_weiyuqing_without_wfdb import PATH
|
|
||||||
|
|
||||||
record=wfdb.rdrecord(ECGDATAPATH + 100, channel_names=['MLII'])
|
|
||||||
data=record.p_signal.flatten()
|
|
||||||
|
|
||||||
coeffs=pywt.wavedec(data=data,wavelet='db5',level=9)
|
|
||||||
cA9,cD9, cD8, cD7, cD6, cD5, cD4, cD3, cD2, cD1 = coeffs
|
|
||||||
|
|
||||||
threshold=(np.median(np.abs(cD1))/0.6745)*(np.sqrt(2*np.log(len(cD1))))
|
|
||||||
cD1.fill(0)
|
|
||||||
cD2.fill(0)
|
|
||||||
for i in range(1,len(coeffs)-2):
|
|
||||||
coeffs[i]=pywt.threshold(coeffs[i],threshold)
|
|
||||||
|
|
||||||
rdata=pywt.waverec(coeffs=coeffs,wavelet='db5')
|
|
||||||
|
|
||||||
plt.figure(figsize=(20,4))
|
|
||||||
plt.plot(data)
|
|
||||||
plt.show()
|
|
||||||
plt.plot(rdata)
|
|
||||||
plt.show()
|
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
matplotlib==3.7.0
|
||||||
|
numpy==1.23.5
|
||||||
|
opencv_python==4.7.0.72
|
||||||
|
Pillow==9.4.0
|
||||||
|
PyWavelets==1.4.1
|
||||||
|
scikit_learn==1.2.1
|
||||||
|
seaborn==0.12.2
|
||||||
|
tensorflow==2.10.0
|
||||||
|
tensorflow_intel==2.11.0
|
||||||
|
torch==2.0.0+cu117
|
||||||
|
wfdb==4.1.0
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import numpy as np
|
||||||
|
import pywt
|
||||||
|
import wfdb
|
||||||
|
from matplotlib import pyplot as plt
|
||||||
|
|
||||||
|
# file error
|
||||||
|
|
||||||
|
# use db5 ,level9 do deviose
|
||||||
|
ECGDATAPATH = './MIT-BIT/'
|
||||||
|
|
||||||
|
# from ECG_read_weiyuqing_without_wfdb import PATH
|
||||||
|
# record = wfdb.rdrecord(ECGDATAPATH + number, channel_names=['MLII'])
|
||||||
|
record = wfdb.rdrecord(ECGDATAPATH + '100', channel_names=['MLII'])
|
||||||
|
data = record.p_signal.flatten()
|
||||||
|
|
||||||
|
coeffs = pywt.wavedec(data=data, wavelet='db5', level=9)
|
||||||
|
cA9, cD9, cD8, cD7, cD6, cD5, cD4, cD3, cD2, cD1 = coeffs
|
||||||
|
|
||||||
|
threshold = (np.median(np.abs(cD1)) / 0.6745) * (np.sqrt(2 * np.log(len(cD1))))
|
||||||
|
cD1.fill(0)
|
||||||
|
cD2.fill(0)
|
||||||
|
for i in range(1, len(coeffs) - 2):
|
||||||
|
coeffs[i] = pywt.threshold(coeffs[i], threshold)
|
||||||
|
|
||||||
|
rdata = pywt.waverec(coeffs=coeffs, wavelet='db5')
|
||||||
|
|
||||||
|
plt.figure(figsize=(20, 4))
|
||||||
|
plt.plot(data)
|
||||||
|
plt.show()
|
||||||
|
plt.plot(rdata)
|
||||||
|
plt.show()
|
||||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -4,14 +4,14 @@ import numpy as np
|
|||||||
import wfdb
|
import wfdb
|
||||||
import matplotlib.pyplot
|
import matplotlib.pyplot
|
||||||
|
|
||||||
PATH = './mit-bih-arrhythmia-database-1.0.0/'
|
PATH = '../MIT-BIT/'
|
||||||
|
|
||||||
|
|
||||||
# file error with read_ecg_data(data) parameter data false
|
# file error with read_ecg_data(data) parameter data false
|
||||||
|
|
||||||
def read_ecg_data(data):
|
def read_ecg_data(data):
|
||||||
# get the data from the ecg data file and set begin and end points
|
# get the data from the ecg data file and set begin and end points
|
||||||
record = wfdb.rdrecord(PATH + data, sampfrom=0, sampto=1500)
|
record = wfdb.rdrecord(PATH + 'data', sampfrom=0, sampto=1500)
|
||||||
# return type record
|
# return type record
|
||||||
print(type(record))
|
print(type(record))
|
||||||
print(dir(record))
|
print(dir(record))
|
||||||
@@ -10,7 +10,7 @@ from sklearn.metrics import confusion_matrix
|
|||||||
from sklearn.model_selection import train_test_split
|
from sklearn.model_selection import train_test_split
|
||||||
|
|
||||||
# from _mian_tensorflow import Project_PATH
|
# from _mian_tensorflow import Project_PATH
|
||||||
Project_PATH = "./Number-Of-CNN-Layers/One/"
|
Project_PATH = "./Number-Of-CNN-Layers/Two/"
|
||||||
|
|
||||||
PICTUREPATH= Project_PATH +'picture/'
|
PICTUREPATH= Project_PATH +'picture/'
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user