CHB-MIT波士顿儿童医院癫痫EEG脑电数据处理-癫痫发作预测(六)

CHB-MIT波士顿儿童医院癫痫EEG脑电数据处理-癫痫发作预测(六)

  • 导入需要的包
  • 各功能模块
  • 数据集导入
  • 模型训练
  • 训练结果
  • 保存模型
  • 测试结果

导入需要的包

import numpy as np
import matplotlib.pyplot as plt 
from tensorflow import keras
import tensorflow as tf
import pandas as pd
from sklearn.preprocessing import StandardScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Dropout,Activation,Flatten
from tensorflow.keras.layers import Conv2D,MaxPooling2D,ZeroPadding2D
from tensorflow.keras.layers import Reshape
from tensorflow.python.keras.utils.multi_gpu_utils import multi_gpu_model
from sklearn.metrics import confusion_matrix
import os

各功能模块

def choose_channel(data, drop=set()):
    return data[:, :, list(channels), :]

def train_test_split(data, label):
    return train_data, train_label, test_data, test_label

def get_model(n, channel_numbers):
    model = keras.models.Sequential([
        # 1. define matrix:[ input_dim, output_dim]
        # 2. input:[None, input_length, 1(one_hout:vocab_size)]  return:[None, input_length, embedding_dim]
        # keras.layers.Embedding(input_dim=vocab_size, output_dim=embedding_dim, input_length=max_length),
        # return : [None, embedding_dim]
        keras.layers.Conv2D(filters=32, kernel_size=(1,channel_numbers), input_shape=(n,channel_numbers,5), activation='relu', padding='same'),
        keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu', padding='same'),
        keras.layers.MaxPool2D(pool_size=(2,2)),
        keras.layers.Conv2D(filters=64, kernel_size=(3,9), activation='relu', padding='same'),
        keras.layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu', padding='same'),
        keras.layers.MaxPooling2D(pool_size=(2,2)),
        keras.layers.Conv2D(filters=128, kernel_size=(3,3), activation='relu', padding='same'),
        keras.layers.Conv2D(filters=128, kernel_size=(3,3), activation='relu', padding='same'),
        keras.layers.MaxPooling2D(pool_size=(2,2)),
        keras.layers.Conv2D(filters=256, kernel_size=(3,3), activation='relu', padding='same'),
        keras.layers.Conv2D(filters=256, kernel_size=(3,3), activation='relu', padding='same'),
    #     keras.layers.MaxPooling2D(pool_size=(2,2)),
        keras.layers.Conv2D(filters=512, kernel_size=(3,3), activation='relu', padding='same'),
        keras.layers.Conv2D(filters=512, kernel_size=(3,3), activation='relu', padding='same'),
        keras.layers.Reshape(( -1, 512)),
        keras.layers.Bidirectional(keras.layers.LSTM(units=512, return_sequences=True)),
        keras.layers.Bidirectional(keras.layers.LSTM(units=512, return_sequences=True)),
        keras.layers.Bidirectional(keras.layers.LSTM(units=1024, return_sequences=False)),
        keras.layers.Flatten(),
        keras.layers.Dropout(0.5),
        keras.layers.Dense(128, activation='relu'),
        keras.layers.Dense(64, activation='relu'),
        keras.layers.Dense(2, activation='softmax')
    ])
    return model

数据集导入

path_d = "test_data/data/"
path_l = "test_data/lab/"
d = np.load(path_d+"chb01.npy")
l = np.load(path_l+"chb01.npy")
d = d[:8000]
l = l[:8000]
print(d.shape,l.shape)

模型训练

def get_score(data, label, drop=set(), batch_size=80, epochs=30):
    
    data_droped = choose_channel(data, drop=drop)
    train_data, train_label, test_data, test_label = train_test_split(data_droped, l)
    
    data_train = tf.data.Dataset.from_tensor_slices((train_data,train_label)).shuffle(10).batch(batch_size, drop_remainder=True)
    data_test = tf.data.Dataset.from_tensor_slices((test_data,test_label)).shuffle(10).batch(batch_size, drop_remainder=True)
    model = get_model(data_droped.shape[1],data_droped.shape[2])
#     parallel_model = multi_gpu_model(model, gpus=8)
#     parallel_model.compile(
#             optimizer=tf.keras.optimizers.SGD(learning_rate=1e-4, momentum=0.9),
#     #         optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4),
#             loss=tf.keras.losses.sparse_categorical_crossentropy,
#     #         loss=tf.keras.losses.binary_crossentropy,
#     #         metrics=[tf.keras.metrics.sparse_categorical_accuracy], 
#             metrics=["accuracy"], 
#     #         loss_weights=[1, 5, 20, 1],
#     #         metrics=METRICS
#     )
#     history = parallel_model.fit(data_train, epochs=epochs, batch_size=batch_size, validation_data=data_test,validation_freq=1)
#     score = parallel_model.evaluate(data_test)
    model.compile(
            optimizer=tf.keras.optimizers.SGD(learning_rate=1e-4, momentum=0.9),
    #         optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4),
            loss=tf.keras.losses.sparse_categorical_crossentropy,
            metrics=["accuracy"], 
    )
    history = model.fit(data_train, epochs=epochs, batch_size=batch_size, validation_data=data_test,validation_freq=1)
#     score = model.evaluate(data_test)
#     del model, data_train, data_test, train_data, train_label, test_data, test_label, data_droped
    return model, history

model.summary()

训练结果

model, history = get_score(d, l)

CHB-MIT波士顿儿童医院癫痫EEG脑电数据处理-癫痫发作预测(六)_第1张图片

保存模型

model.save('model/chb##')

测试结果

train_len = int(len(d) * 0.8)
test_data = d[train_len:]
test_label = l[train_len:]

predict_y = model.predict(test_data, batch_size=256)
TN, FP, FN, TP = confusion_matrix(predict_y[:, 1]>0.5, test_label).ravel()
# Sensitivity, hit rate, recall, or true positive rate
TPR = TP/(TP+FN)
# Specificity or true negative rate
TNR = TN/(TN+FP) 
# Precision or positive predictive value
PPV = TP/(TP+FP)
# Negative predictive value
NPV = TN/(TN+FN)
# Fall out or false positive rate
FPR = FP/(FP+TN)
# False negative rate
FNR = FN/(TP+FN)
# False discovery rate
FDR = FP/(TP+FP)

precision = TP / (TP+FP)  # 查准率
recall = TP / (TP+FN)  # 查全率

print(TPR, TNR, precision, recall, FDR, FPR)

CHB-MIT波士顿儿童医院癫痫EEG脑电数据处理-癫痫发作预测(六)_第2张图片
CHB-MIT波士顿儿童医院癫痫EEG脑电数据处理-癫痫发作预测(六)_第3张图片

你可能感兴趣的:(eeg脑电研究,tensorflow,深度学习,python)