diff --git a/.idea/ECG_by_Diffenert_CNN_Test.iml b/.idea/ECG_by_Diffenert_CNN_Test.iml index b9284b0..cdf704d 100644 --- a/.idea/ECG_by_Diffenert_CNN_Test.iml +++ b/.idea/ECG_by_Diffenert_CNN_Test.iml @@ -3,6 +3,7 @@ + diff --git a/some-test/Auto_create_code.py b/Auto_create_code.py similarity index 100% rename from some-test/Auto_create_code.py rename to Auto_create_code.py diff --git a/The_last_model/The_CNN_of_ECG.py b/The_last_model/The_CNN_of_ECG.py index e69de29..c1d6ee4 100644 --- a/The_last_model/The_CNN_of_ECG.py +++ b/The_last_model/The_CNN_of_ECG.py @@ -0,0 +1,88 @@ +import datetime +import os + +import numpy as np +import tensorflow as tf +from utils_CNN_of_ECG import loadData, plot_history_tf, plot_heat_map + +#from ECG_read_weiyuqing_without_wfdb import ECGDATAPATH +#PATH of this test +Project_PATH = "./" +log_dir = Project_PATH + "logs/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S") +model_Path_one = Project_PATH + "model/ecg_model_three_filters.h5" + +RATIO = 0.3 +RANDOM_SEED = 42 +BATCH_SIZE = 128 +#迭代次数 +NUM_EPOCHS = 50 + + +def CNN_model_level_one(): + leavlOneModel = tf.keras.models.Sequential([ + + #take test for one CNN layers model + + tf.keras.layers.InputLayer(input_shape=(300,)), + tf.keras.layers.Reshape(target_shape=(300, 1)), + + tf.keras.layers.Conv1D(filters=2, kernel_size=19, 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=19, strides=1, padding='same', activation='relu'), + tf.keras.layers.MaxPool1D(pool_size=3, strides=2, padding='same'), + tf.keras.layers.Conv1D(filters=16, kernel_size=19, strides=1, padding='same', activation='relu'), + tf.keras.layers.MaxPool1D(pool_size=3, strides=2, padding='same'), + tf.keras.layers.Conv1D(filters=32, kernel_size=19, strides=1, padding='same', activation='relu'), + tf.keras.layers.MaxPool1D(pool_size=3, strides=2, padding='same'), + tf.keras.layers.Conv1D(filters=64, kernel_size=19, strides=1, padding='same', activation='relu'), + tf.keras.layers.MaxPool1D(pool_size=3, strides=2, padding='same'), + tf.keras.layers.Conv1D(filters=128, kernel_size=19, strides=1, padding='same', activation='relu'), + tf.keras.layers.MaxPool1D(pool_size=3, strides=2, padding='same'), + tf.keras.layers.Conv1D(filters=256, kernel_size=19, 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(512, activation='relu'), + # add dropout layer, dropout rate = 0.2, + 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], + shuffle=1) + # 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() diff --git a/The_last_model/logs/20230329-114321/train/events.out.tfevents.1680061405.WEIYUQING.13136.0.v2 b/The_last_model/logs/20230329-114321/train/events.out.tfevents.1680061405.WEIYUQING.13136.0.v2 new file mode 100644 index 0000000..abc0e52 Binary files /dev/null and b/The_last_model/logs/20230329-114321/train/events.out.tfevents.1680061405.WEIYUQING.13136.0.v2 differ diff --git a/The_last_model/logs/20230329-114402/train/events.out.tfevents.1680061446.WEIYUQING.21356.0.v2 b/The_last_model/logs/20230329-114402/train/events.out.tfevents.1680061446.WEIYUQING.21356.0.v2 new file mode 100644 index 0000000..5bbefce Binary files /dev/null and b/The_last_model/logs/20230329-114402/train/events.out.tfevents.1680061446.WEIYUQING.21356.0.v2 differ diff --git a/The_last_model/logs/20230329-121740/train/events.out.tfevents.1680063463.WEIYUQING.30616.0.v2 b/The_last_model/logs/20230329-121740/train/events.out.tfevents.1680063463.WEIYUQING.30616.0.v2 new file mode 100644 index 0000000..1c3719f Binary files /dev/null and b/The_last_model/logs/20230329-121740/train/events.out.tfevents.1680063463.WEIYUQING.30616.0.v2 differ diff --git a/The_last_model/logs/20230329-121824/train/events.out.tfevents.1680063508.WEIYUQING.27812.0.v2 b/The_last_model/logs/20230329-121824/train/events.out.tfevents.1680063508.WEIYUQING.27812.0.v2 new file mode 100644 index 0000000..a3683a4 Binary files /dev/null and b/The_last_model/logs/20230329-121824/train/events.out.tfevents.1680063508.WEIYUQING.27812.0.v2 differ diff --git a/The_last_model/logs/20230329-122255/train/events.out.tfevents.1680063778.WEIYUQING.5624.0.v2 b/The_last_model/logs/20230329-122255/train/events.out.tfevents.1680063778.WEIYUQING.5624.0.v2 new file mode 100644 index 0000000..323cd62 Binary files /dev/null and b/The_last_model/logs/20230329-122255/train/events.out.tfevents.1680063778.WEIYUQING.5624.0.v2 differ diff --git a/The_last_model/logs/20230329-122255/validation/events.out.tfevents.1680063785.WEIYUQING.5624.1.v2 b/The_last_model/logs/20230329-122255/validation/events.out.tfevents.1680063785.WEIYUQING.5624.1.v2 new file mode 100644 index 0000000..0522a3d Binary files /dev/null and b/The_last_model/logs/20230329-122255/validation/events.out.tfevents.1680063785.WEIYUQING.5624.1.v2 differ diff --git a/The_last_model/logs/20230331-174547/train/events.out.tfevents.1680255950.WEIYUQING.30704.0.v2 b/The_last_model/logs/20230331-174547/train/events.out.tfevents.1680255950.WEIYUQING.30704.0.v2 new file mode 100644 index 0000000..eb40c62 Binary files /dev/null and b/The_last_model/logs/20230331-174547/train/events.out.tfevents.1680255950.WEIYUQING.30704.0.v2 differ diff --git a/The_last_model/logs/20230331-174547/validation/events.out.tfevents.1680255957.WEIYUQING.30704.1.v2 b/The_last_model/logs/20230331-174547/validation/events.out.tfevents.1680255957.WEIYUQING.30704.1.v2 new file mode 100644 index 0000000..c544179 Binary files /dev/null and b/The_last_model/logs/20230331-174547/validation/events.out.tfevents.1680255957.WEIYUQING.30704.1.v2 differ diff --git a/The_last_model/logs/20230331-175703/train/events.out.tfevents.1680256626.WEIYUQING.29780.0.v2 b/The_last_model/logs/20230331-175703/train/events.out.tfevents.1680256626.WEIYUQING.29780.0.v2 new file mode 100644 index 0000000..a71535d Binary files /dev/null and b/The_last_model/logs/20230331-175703/train/events.out.tfevents.1680256626.WEIYUQING.29780.0.v2 differ diff --git a/The_last_model/logs/20230331-175703/validation/events.out.tfevents.1680256633.WEIYUQING.29780.1.v2 b/The_last_model/logs/20230331-175703/validation/events.out.tfevents.1680256633.WEIYUQING.29780.1.v2 new file mode 100644 index 0000000..eae1deb Binary files /dev/null and b/The_last_model/logs/20230331-175703/validation/events.out.tfevents.1680256633.WEIYUQING.29780.1.v2 differ diff --git a/The_last_model/logs/20230331-180109/train/events.out.tfevents.1680256872.WEIYUQING.12672.0.v2 b/The_last_model/logs/20230331-180109/train/events.out.tfevents.1680256872.WEIYUQING.12672.0.v2 new file mode 100644 index 0000000..17bb71b Binary files /dev/null and b/The_last_model/logs/20230331-180109/train/events.out.tfevents.1680256872.WEIYUQING.12672.0.v2 differ diff --git a/The_last_model/logs/20230331-180109/validation/events.out.tfevents.1680256879.WEIYUQING.12672.1.v2 b/The_last_model/logs/20230331-180109/validation/events.out.tfevents.1680256879.WEIYUQING.12672.1.v2 new file mode 100644 index 0000000..b28d0cb Binary files /dev/null and b/The_last_model/logs/20230331-180109/validation/events.out.tfevents.1680256879.WEIYUQING.12672.1.v2 differ diff --git a/The_last_model/logs/20230331-181201/train/events.out.tfevents.1680257524.WEIYUQING.24400.0.v2 b/The_last_model/logs/20230331-181201/train/events.out.tfevents.1680257524.WEIYUQING.24400.0.v2 new file mode 100644 index 0000000..80b2f93 Binary files /dev/null and b/The_last_model/logs/20230331-181201/train/events.out.tfevents.1680257524.WEIYUQING.24400.0.v2 differ diff --git a/The_last_model/logs/20230331-181201/validation/events.out.tfevents.1680257531.WEIYUQING.24400.1.v2 b/The_last_model/logs/20230331-181201/validation/events.out.tfevents.1680257531.WEIYUQING.24400.1.v2 new file mode 100644 index 0000000..2c15de6 Binary files /dev/null and b/The_last_model/logs/20230331-181201/validation/events.out.tfevents.1680257531.WEIYUQING.24400.1.v2 differ diff --git a/The_last_model/logs/20230331-181856/train/events.out.tfevents.1680257939.WEIYUQING.23688.0.v2 b/The_last_model/logs/20230331-181856/train/events.out.tfevents.1680257939.WEIYUQING.23688.0.v2 new file mode 100644 index 0000000..9c2f8a2 Binary files /dev/null and b/The_last_model/logs/20230331-181856/train/events.out.tfevents.1680257939.WEIYUQING.23688.0.v2 differ diff --git a/The_last_model/logs/20230331-181856/validation/events.out.tfevents.1680257946.WEIYUQING.23688.1.v2 b/The_last_model/logs/20230331-181856/validation/events.out.tfevents.1680257946.WEIYUQING.23688.1.v2 new file mode 100644 index 0000000..948c0ee Binary files /dev/null and b/The_last_model/logs/20230331-181856/validation/events.out.tfevents.1680257946.WEIYUQING.23688.1.v2 differ diff --git a/The_last_model/logs/20230401-151652/train/events.out.tfevents.1680333416.WEIYUQING.16452.0.v2 b/The_last_model/logs/20230401-151652/train/events.out.tfevents.1680333416.WEIYUQING.16452.0.v2 new file mode 100644 index 0000000..c93ba4e Binary files /dev/null and b/The_last_model/logs/20230401-151652/train/events.out.tfevents.1680333416.WEIYUQING.16452.0.v2 differ diff --git a/The_last_model/logs/20230401-151652/validation/events.out.tfevents.1680333430.WEIYUQING.16452.1.v2 b/The_last_model/logs/20230401-151652/validation/events.out.tfevents.1680333430.WEIYUQING.16452.1.v2 new file mode 100644 index 0000000..96047ba Binary files /dev/null and b/The_last_model/logs/20230401-151652/validation/events.out.tfevents.1680333430.WEIYUQING.16452.1.v2 differ diff --git a/The_last_model/model/ecg_model_three_filters.h5 b/The_last_model/model/ecg_model_three_filters.h5 new file mode 100644 index 0000000..0ce8edf Binary files /dev/null and b/The_last_model/model/ecg_model_three_filters.h5 differ diff --git a/The_last_model/model_softmax/ecg_model_three_filters.h5 b/The_last_model/model_softmax/ecg_model_three_filters.h5 new file mode 100644 index 0000000..111b3b1 Binary files /dev/null and b/The_last_model/model_softmax/ecg_model_three_filters.h5 differ diff --git a/The_last_model/model_withoutshuffle/ecg_model_three_filters.h5 b/The_last_model/model_withoutshuffle/ecg_model_three_filters.h5 new file mode 100644 index 0000000..15bbf35 Binary files /dev/null and b/The_last_model/model_withoutshuffle/ecg_model_three_filters.h5 differ diff --git a/The_last_model/picture/accuracy.png b/The_last_model/picture/accuracy.png new file mode 100644 index 0000000..fc0dc01 Binary files /dev/null and b/The_last_model/picture/accuracy.png differ diff --git a/The_last_model/picture/confusion_matrix.png b/The_last_model/picture/confusion_matrix.png new file mode 100644 index 0000000..a25dd6f Binary files /dev/null and b/The_last_model/picture/confusion_matrix.png differ diff --git a/The_last_model/picture/loss.png b/The_last_model/picture/loss.png new file mode 100644 index 0000000..5c4cc24 Binary files /dev/null and b/The_last_model/picture/loss.png differ diff --git a/The_last_model/picture_softmax/accuracy.png b/The_last_model/picture_softmax/accuracy.png new file mode 100644 index 0000000..98402fc Binary files /dev/null and b/The_last_model/picture_softmax/accuracy.png differ diff --git a/The_last_model/picture_softmax/confusion_matrix.png b/The_last_model/picture_softmax/confusion_matrix.png new file mode 100644 index 0000000..6b79bef Binary files /dev/null and b/The_last_model/picture_softmax/confusion_matrix.png differ diff --git a/The_last_model/picture_softmax/loss.png b/The_last_model/picture_softmax/loss.png new file mode 100644 index 0000000..fb07861 Binary files /dev/null and b/The_last_model/picture_softmax/loss.png differ diff --git a/The_last_model/picture_withoutsuffle/accuracy.png b/The_last_model/picture_withoutsuffle/accuracy.png new file mode 100644 index 0000000..74a7579 Binary files /dev/null and b/The_last_model/picture_withoutsuffle/accuracy.png differ diff --git a/The_last_model/picture_withoutsuffle/confusion_matrix.png b/The_last_model/picture_withoutsuffle/confusion_matrix.png new file mode 100644 index 0000000..01fc331 Binary files /dev/null and b/The_last_model/picture_withoutsuffle/confusion_matrix.png differ diff --git a/The_last_model/picture_withoutsuffle/loss.png b/The_last_model/picture_withoutsuffle/loss.png new file mode 100644 index 0000000..699ca99 Binary files /dev/null and b/The_last_model/picture_withoutsuffle/loss.png differ diff --git a/The_last_model/utils_CNN_of_ECG.py b/The_last_model/utils_CNN_of_ECG.py new file mode 100644 index 0000000..c946e22 --- /dev/null +++ b/The_last_model/utils_CNN_of_ECG.py @@ -0,0 +1,131 @@ +# this file give +import os + +import wfdb +import pywt +import seaborn +import numpy as np +import matplotlib.pyplot as plt +from sklearn.metrics import confusion_matrix +from sklearn.model_selection import train_test_split + +# from _mian_tensorflow import Project_PATH +Project_PATH = './' + +PICTUREPATH = Project_PATH + 'picture/' + +# from ECG_read_weiyuqing_without_wfdb import ECGDATAPATH +MITBITECGDATAPATH = '../MIT-BIT/' + + +# wavelet denoise preprocess using mallat algorithm + +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(MITBITECGDATAPATH + number, channel_names=['MLII']) + data = record.p_signal.flatten() + # use def denoise + rdata = denoise(date=data) + + # get type of ECG data + annotation = wfdb.rdann(MITBITECGDATAPATH + 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, random_seed): + 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) + + # reshape the data and split the dataset + dataSet = np.array(dataSet).reshape(-1, 300) + lableSet = np.array(labelSet).reshape(-1) + X_train, X_test, y_train, y_test = train_test_split(dataSet, lableSet, test_size=ratio, random_state=random_seed) + return X_train, X_test, y_train, y_test + + + +# confusion matrix +def plot_heat_map(y_test, y_pred): + con_mat = confusion_matrix(y_test, y_pred) + plt.figure(figsize=(8, 8)) + seaborn.heatmap(con_mat, annot=True, fmt='.20g', cmap='Blues') + plt.ylim(0, 5) + plt.xlabel('Predicted labels') + plt.ylabel('True labels') + plt.title('Confusion Matrix') + plt.savefig(PICTUREPATH + 'confusion_matrix.png') + plt.show() + + +def plot_history_tf(history): + # os.remove(PICTUREPATH+'accuracy.png') + # os.remove(PICTUREPATH+'loss.png') + + plt.figure(figsize=(8, 8)) + plt.plot(history.history['accuracy']) + plt.plot(history.history['val_accuracy']) + plt.title('Model Accuracy') + plt.ylabel('Accuracy') + plt.xlabel('Epoch') + plt.legend(['Train', 'Test'], loc='upper left') + plt.savefig(PICTUREPATH + 'accuracy.png') + plt.show() + + plt.figure(figsize=(8, 8)) + plt.plot(history.history['loss']) + plt.plot(history.history['val_loss']) + plt.title('Model Loss') + plt.ylabel('Loss') + plt.xlabel('Epoch') + plt.legend(['Train', 'Test'], loc='upper left') + plt.savefig(PICTUREPATH + 'loss.png') + plt.show() \ No newline at end of file diff --git a/some-test/test.py b/some-test/test.py deleted file mode 100644 index df835ff..0000000 --- a/some-test/test.py +++ /dev/null @@ -1,28 +0,0 @@ -import datetime -import os -import numpy as np -import tensorflow as tf -from utils_seventyOne_of_Filters import loadData, plot_history_tf, plot_heat_mapProject_PATH = "../some-test\def main(): - X_train, X_test, y_train, y_test = loadData(RATIO, RANDOM_SEED) - 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) \ No newline at end of file