def train_LOO_pacient(images,labels,names):
'''
Training model using LOO validation by pacient
Parameters
----------
images: list of numpy.array. images of the pacients
labels: list of numpy.array. labels of the different images
names: list of numpy.array. name of the pacients
Output
----------
Print the mean of the LOO validation
'''
print("Training LOO with pacient name")
values_acc = []
already_tested = []
for i in range(len(labels)):
if labels[i] == "AD":
labels[i] = 0
else:
labels[i] = 1
print("The lenght of images is "+str(len(images)))
for i in range(len(images)):
model = create_model()
X_test = []
Y_test = []
# If we haven't tested that pacient already
# we insert it to the test set and also
# all the images with the same name
if(not(names[i] in already_tested)):
already_tested.append(names[i])
X_test.append(images[i])
Y_test.append(labels[i])
for j in range(len(images)):
if j!=i and names[j] == names[i]:
X_test.append(images[j])
Y_test.append(labels[j])
X_train = []
Y_train = []
for j in range(len(images)):
if names[j] != names[i]:
X_train.append(images[j])
Y_train.append(labels[j])
X_train = np.array(X_train)
Y_train = np.array(Y_train)
X_test = np.array(X_test)
Y_test = np.array(Y_test)
from keras.utils.np_utils import to_categorical
Y_train = to_categorical(Y_train,2)
Y_test = to_categorical(Y_test,2)
print("The len of the training set is: "+str(len(X_train)))
print("The len of the test set is: "+str(len(X_test)))
history = model.fit(X_train,Y_train,epochs=70,batch_size=10)
test_loss = model.evaluate(X_test,Y_test)
print("Loss and accuracy in the test set: Loss %g, Accuracy %g"%(test_loss[0],test_loss[1]))
values_acc.append(test_loss[1])
mean = calculate_mean(values_acc)
print("The mean of all the test values is: %g"%mean)
train.py 文件源码
python
阅读 27
收藏 0
点赞 0
评论 0
评论列表
文章目录