/
run_12ECG_classifier.py
61 lines (50 loc) · 2.1 KB
/
run_12ECG_classifier.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
#!/usr/bin/env python
import numpy as np, os, sys
from numpy import array
from numpy import argmax
import joblib
import numpy as np, os, sys, joblib
from scipy.io import loadmat
from sklearn.impute import SimpleImputer
from get_12ECG_features import get_12ECG_features
# Import tensorflow and keras libraries
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Conv1D, LSTM, Dense, Dropout, TimeDistributed
from keras.optimizers import Adam
from keras.models import Sequential
from keras.layers import Conv1D, Flatten, Dense, Dropout
from keras.optimizers import Adam
import keras
# conver to hot-encode
from keras.utils import to_categorical
import matplotlib.pyplot as plt
def run_12ECG_classifier(data,header_data,model):
# Load Classes
classes = ['164884008', '164889003', '164909002', '164931005', '270492004', '284470004', '426783006', '429622005', '59118001']
imputer = joblib.load('./model/imputer.sav')
imputer = imputer['imputer']
features=np.asarray(get_12ECG_features(data,header_data))
# Normalize data between 0 and 1
features = tf.keras.utils.normalize(features)
feats_reshape = features.reshape(1, -1)
# Evaluate if this is really necesary
#imputer = SimpleImputer().fit(feats_reshape) # instead of this use the trained imputer
feats_reshape = imputer.transform(feats_reshape)
feats_reshape = np.expand_dims(feats_reshape, axis=2)
prediction = model.predict(feats_reshape)
prediction = np.float64(prediction)
prediction = np.argmax(prediction)
encoded = to_categorical(prediction)
current_label = encoded.astype(int)
# Suppose softmax model
probability_model = tf.keras.Sequential([model,tf.keras.layers.Softmax()])
current_score = model.predict(feats_reshape)
current_score = current_score.flatten()
# convert float32 to float 64 just in case
return current_label, current_score, classes
def load_12ECG_model(input_directory):
# Tensorflow & keras model saving mode
name = 'CNN_1.model'
model = tf.keras.models.load_model(input_directory)
return model