def train_model_CV(slices_images,slices_labels,slice_number,f):
'''
Training model using cross-validation passing the slices manually
Parameters
----------
slices_images: list of numpy.array
slices_labels: list of numpy.array
Output
----------
Write in a results file the mean of the accuracy in the test set
'''
#images,labels,list_of_images = reorderRandomly(images,labels,list_of_images)
'''
for i in range(len(labels)):
if labels[i] == "AD":
labels[i] = 0
else:
labels[i] = 1
slices_images = [images[i::5] for i in range(5)]
slices_list_of_images = [list_of_images[i::5] for i in range(5)]
slices_labels = [labels[i::5] for i in range(5)]
print(slices_list_of_images)
'''
values_acc = []
for i in range(5):
model = create_model()
X_test = slices_images[i]
Y_test = slices_labels[i]
X_train = [item
for s in slices_images if s is not X_test
for item in s]
Y_train = [item
for s in slices_labels if s is not Y_test
for item in s]
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)
Y_test = to_categorical(Y_test)
history = model.fit(X_train,Y_train,epochs=70,batch_size=5,verbose=0)
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)
f.write(("The mean of all the test values for the slice %g is: %g \n"%(slice_number,mean)))
train.py 文件源码
python
阅读 28
收藏 0
点赞 0
评论 0
评论列表
文章目录