-
Notifications
You must be signed in to change notification settings - Fork 0
/
t1w_custom.m
124 lines (110 loc) · 4.27 KB
/
t1w_custom.m
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
%% Program by Sreevalsan S. Menon (sm2hm@mst.edu)
function t1w_custom(XTrain,YTrain,XTest,YTest,crossfold)
dlXTest = dlarray(XTest,'SSSCB'); % create deep learning array
classes = categories(YTrain); % retrieve the class information with the type of categorical
numClasses = numel(classes); % Retrieve numbe rof classes
dlnet1=createLayer_1(XTrain); % Create deep learning layer
velocity1 = [];velocity2 = [];velocity3 = [];
numEpochs = 50;
miniBatchSize = 32;
numObservations = numel(YTrain);
numIterationsPerEpoch = floor(numObservations./miniBatchSize);
averageSqGrad1=[];
averageSqGrad2=[];
averageSqGrad3=[];
averageGrad1=[];
averageGrad2=[];
averageGrad3=[];
epsilon=0.001;
learnRate = 0.001;
GradDecay=0.9;
sqGradDecay= 0.9;
executionEnvironment = "gpu";
figure
lineLossTrain = animatedline;
xlabel("Iteration")
ylabel("Loss")
accu = 0;
iteration = 0;
start = tic;
% Loop over epochs.
patience_ini = 0;
for epoch = 1:numEpochs
% Shuffle data.
idx = randperm(numel(YTrain));
XTrain = XTrain(:,:,:,:,idx);
YTrain = YTrain(idx);
% Loop over mini-batches.
for i = 1:numIterationsPerEpoch
iteration = iteration + 1;
% Read mini-batch of data and convert the labels to dummy
% variables.
idx = (i-1)*miniBatchSize+1:i*miniBatchSize;
X1 = XTrain(:,:,:,:,idx);
% convert the label into one-hot vector to calculate the loss
Y = zeros(numClasses, miniBatchSize, 'single');
for c = 1:numClasses
Y(c,YTrain(idx)==classes(c)) = 1;
end
% Convert mini-batch of data to dlarray.
dlX1 = dlarray(single(X1),'SSSCB');
% If training on a GPU, then convert data to gpuArray.
if (executionEnvironment == "auto" && canUseGPU) || executionEnvironment == "gpu"
dlX1 = gpuArray(dlX1);
end
%the traning loss and the gradients after the backpropagation were
%calculated using the helper function modelGradients_demo
[gradients1,loss] = dlfeval(@modelGradients_demo,dlnet1,dlX1,dlarray(Y));
% Update the network parameters using the RMSProp optimizer.
% Update the parameters in dlnet1 to 3 sequentially
[dlnet1,averageGrad1, averageSqGrad1] = adamupdate(dlnet1, gradients1,averageGrad1, averageSqGrad1,iteration,learnRate,GradDecay,sqGradDecay,epsilon);
% Display the training progress.
D = duration(0,0,toc(start),'Format','hh:mm:ss');
addpoints(lineLossTrain,iteration,double(gather(extractdata(loss))))
title("Epoch: " + epoch + ", Elapsed: " + string(D))
drawnow
dlYPred = predict(dlnet1,dlXTest);
[~,idx] = max(extractdata(dlYPred),[],1);
YPred = classes(idx);
accuracy = sum(categorical(str2double(YPred))==YTest')/size(dlXTest,5);
if accuracy >= accu
save(['dwi_' num2str(crossfold) '_test.mat'],'dlnet1','YPred','YTest')
patience_ini = patience_ini + (accu = accuracy);
accu = accuracy;
end
if patience_ini>19
epoch = numEpochs;
break;
end
end
if patience_ini>19
epoch
disp('exit')
epoch = numEpochs;
break;
end
end
function dlnet=createLayer_1(XTrain)
layers = [
image3dInputLayer([91 109 91 3],'Name','fmri_input',"Mean",mean(XTrain,5))
convolution3dLayer(3,4,'Padding','same','Name','conv_f1')
reluLayer('Name','relu_f1')
maxPooling3dLayer(2,'Stride',2, 'Name','maxpool_f1')
convolution3dLayer(3,8,'Padding','same','Name','conv_f2')
reluLayer('Name','relu_f2')
maxPooling3dLayer(2,'Stride',2, 'Name','maxpool_f2')
dropoutLayer(0.5,'Name','dp_1')
fullyConnectedLayer(64,"Name","fc_1")
dropoutLayer(0.8,'Name','dp_2')
fullyConnectedLayer(4,"Name","fc_2")
softmaxLayer('Name','softmax')
];
lgraph = layerGraph(layers);
dlnet = dlnetwork(lgraph);
end
function [gradients1, loss] = modelGradients_demo(dlnet1,dlX1,Y)
dlYPred = forward(dlnet1,dlX1);
loss = crossentropy(dlYPred,Y);
gradients1 = dlgradient(loss,dlnet1.Learnables);
end
end