In [ ]:
import tensorflow as tf
#import tensorflow.contrib.learn.python.learn as learn
import tflearn
import scipy as sp
import numpy as np
import matplotlib.pyplot as plt
import six
from sklearn.metrics import roc_curve
import datetime
%matplotlib inline
In [ ]:
comData = np.load("./inData/3D-conv/comData.npy")
comClass = np.load("./inData/3D-conv/comClass.npy")
In [ ]:
def processClassData(classData):
"""
Process classData.
Returns a one-hot array of shape [len(classData), 2].
"""
# Convert label data to one-hot array
classDataOH = np.zeros((len(classData),2))
classDataOH[np.arange(len(classData)), classData] = 1
return classDataOH
comData = comData[..., np.newaxis]
comClassOH = processClassData(comClass)
In [ ]:
# kfold x-validation...
k = 3
kfoldData = np.array_split(comData, k)
kfoldLabelsOH = np.array_split(comClassOH, k)
kfoldLabels = np.array_split(comClass, k)
print(kfoldData[0].shape)
In [ ]:
spec = []
sens = []
roc = []
In [ ]:
for i in np.arange(0,k,1):
sess = tf.InteractiveSession()
tf.reset_default_graph()
tflearn.initializations.normal()
# Input layer:
net = tflearn.layers.core.input_data(shape=[None, 50, 19, 17, 1])
# First layer:
net = tflearn.layers.conv.conv_3d(net, 8, [5,5,5], activation="leaky_relu")
net = tflearn.layers.conv.max_pool_3d(net, 2, strides=2)
# Second layer:
net = tflearn.layers.conv.conv_3d(net, 16, [5,5,5], activation="leaky_relu")
net = tflearn.layers.conv.max_pool_3d(net, 2, strides=2)
# Fully connected layer
net = tflearn.layers.core.fully_connected(net, 1024, regularizer="L2", weight_decay=0.001, activation="leaky_relu")
#net = tflearn.layers.core.fully_connected(net, 1024, regularizer="L2", weight_decay=0.001, activation="leaky_relu")
# Dropout layer:
net = tflearn.layers.core.dropout(net, keep_prob=0.5)
# Output layer:
net = tflearn.layers.core.fully_connected(net, 2, activation="softmax")
net = tflearn.layers.estimator.regression(net, optimizer='adam', learning_rate=0.0001, loss='categorical_crossentropy')
model = tflearn.DNN(net, tensorboard_verbose=3)
dummyData = np.reshape(np.concatenate(kfoldData[:i] + kfoldData[i+1:], axis=0), [-1, 2000, 19, 17, 1])
dummyLabels = np.reshape(np.concatenate(kfoldLabelsOH[:i] + kfoldLabelsOH[i+1:], axis=0), [-1, 2])
model.fit(dummyData[:,::40], dummyLabels, batch_size=8, n_epoch=30, show_metric=True)
illTest = []
healthTest = []
for index, item in enumerate(kfoldLabels[i]):
if item == 1:
illTest.append(kfoldData[i][index])
if item == 0:
healthTest.append(kfoldData[i][index])
healthLabel = np.tile([1,0], (len(healthTest), 1))
illLabel = np.tile([0,1], (len(illTest), 1))
sens.append(model.evaluate(np.array(healthTest)[:,::40], healthLabel))
spec.append(model.evaluate(np.array(illTest)[:,::40], illLabel))
# Get roc curve data
predicted = np.array(model.predict(np.array(kfoldData[i])[:,::40]))
fpr, tpr, th = roc_curve(kfoldLabels[i], predicted[:,1])
roc.append([fpr, tpr])
In [ ]:
#model.save("./outData/models/3d_conv8-16-50.tflearn")
#model.load("./outData/models/3d_conv.tflearn")
In [ ]:
print("Specificity:", spec, "\nAvg:", np.mean(spec), "\nSensitivity:", sens, "\nAvg:", np.mean(sens))
In [ ]:
for i in np.arange(k):
plt.plot(roc[i][0], roc[i][1])
plt.title("ROC curve")
plt.xlabel("FPR")
plt.ylabel("TPR")
plt.savefig("./outData/MCG/mcgKfold3D.png")