-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbrainextractionT1.m
More file actions
339 lines (265 loc) · 12.4 KB
/
Copy pathbrainextractionT1.m
File metadata and controls
339 lines (265 loc) · 12.4 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
%% 3-D Brain Extraction from MRI
% Train and cross validate a 3-D U-net for brain extraction on T1 image
%% load nifti data
% manu -
% load nifti data from /rsrch1/ip/egates1/NFBS\ Skull\ Strip/NFBSFilepaths.csv
% into matlab data structure
% Setting up the code: fresh start
clc
clear all
close all
fname = 'brainTest.json';
jsonText = fileread(fname);
jsonData = jsondecode(jsonText);
% Read file pathways into table
fullFileName = jsonData.fullFileName;
% enter: = /rsrch1/ip/egates1/NFBS Skull Strip/NFBSFilepaths.csv
delimiter = jsonData.delimiter;
% enter: ,
T = readtable(fullFileName, 'Delimiter', delimiter);
A = table2array(T);
volCol = jsonData.volCol;
% enter: 4
lblCol = jsonData.lblCol;
% enter: 5
volLoc = A(:,volCol);
lblLoc = A(:,lblCol);
stoFoldername = jsonData.stoFoldername;
% for user-defined: destination = input("Please enter the file pathway for folder to store training, validation, and test sets: ", 's')
destination = fullfile(tempdir,stoFoldername, 'preprocessedDataset');
% define readers
maskReader = @(x) (niftiread(x)>0);
volReader = @(x) niftiread(x);
%read data into datastores
volds = imageDatastore(volLoc, ...
'FileExtensions','.gz','ReadFcn',volReader);
classNames = ["background","brain"];
pixelLabelID = [0 1];
% read data intp pixelLabeldatastore
pxds = pixelLabelDatastore(lblLoc,classNames, pixelLabelID, ...
'FileExtensions','.gz','ReadFcn',maskReader);
reset(volds);
reset(pxds);
% create directories to store data sets
mkdir(fullfile(destination,'imagesMain'));
mkdir(fullfile(destination,'labelsMain'));
imDir = fullfile(destination, 'imagesMain', stoFoldername);
labelDir = fullfile(destination, 'labelsMain', stoFoldername);
%% Crop relevant region
NumFiles = length(pxds.Files);
id = 1;
while hasdata(pxds)
outL = readNumeric(pxds);
outV = read(volds);
temp = outL>0;
sz = size(outL);
reg = regionprops3(temp,'BoundingBox');
tol = 64;
ROI = ceil(reg.BoundingBox(1,:));
ROIst = ROI(1:3) - tol;
ROIend = ROI(1:3) + ROI(4:6) + tol;
ROIst(ROIst<1)=1;
ROIend(ROIend>sz)=sz(ROIend>sz);
tumorRows = ROIst(2):ROIend(2);
tumorCols = ROIst(1):ROIend(1);
tumorPlanes = ROIst(3):ROIend(3);
tcropVol = outV(tumorRows,tumorCols, tumorPlanes);
tcropLabel = outL(tumorRows,tumorCols, tumorPlanes);
% Data set with a valid size for 3-D U-Net (multiple of 8)
ind = floor(size(tcropVol)/8)*8;
incropVol = tcropVol(1:ind(1),1:ind(2),1:ind(3));
mask = incropVol == 0;
%%%%%%%% channelWisePreProcess
% As input has 4 channels (modalities), remove the mean and divide by the
% standard deviation of each modality independently.
incropVol1=single(incropVol);
chn_Mean = mean(incropVol1,[1 2 3]);
chn_Std = std(incropVol1,0,[1 2 3]);
cropVol1 = (incropVol1 - chn_Mean)./chn_Std;
rangeMin = -5;
rangeMax = 5;
% Remove outliers
cropVol1(cropVol1 > rangeMax) = rangeMax;
cropVol1(cropVol1 < rangeMin) = rangeMin;
% Rescale the data to the range [0, 1]
cropVol1 = (cropVol1 - rangeMin) / (rangeMax - rangeMin);
%%%%%%%%
% Set the nonbrain region to 0
cropVol1(mask) = 0;
cropLabel = tcropLabel(1:ind(1),1:ind(2),1:ind(3));
% save preprocessed data to folders
save([imDir num2str(id,'%.3d') '.mat'],'cropVol1');
save([labelDir num2str(id,'%.3d') '.mat'],'cropLabel');
%outDim{id} = size(cropVol1);
id=id+1;
end
%% create datastores for processed labels and images
% procvolds stores processed T1 volumetric data
procvolReader = @(x) matRead(x);
procvolLoc = fullfile(destination,'imagesMain');
procvolds = imageDatastore(procvolLoc, ...
'FileExtensions','.mat','ReadFcn',procvolReader);
labelDirProc = fullfile(destination, 'labelsMain');
% proclblfs stores processed mask file info
proclblfs = matlab.io.datastore.DsFileSet(labelDirProc);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Do the k-fold partition
patients = A(:,1);% Extract the patient ids in the filepaths table
partition = cvpartition(patients,'k',5);
err = zeros(partition.NumTestSets,1);
for i = 1:partition.NumTestSets
trIdx = partition.training(i);
teIdx = partition.test(i);
trData = subset(procvolds, trIdx);
trMask = subset(proclblfs, trIdx);
% Training, validation, and test data for each fold
trainData = subset(trData, [13:.8*length(patients)]);
trainMask = subset(trMask, [13:.8*length(patients)]);
valData = subset(trData, [1:12]);
valMask = subset(trMask, [1:12]);
testData = subset(procvolds, teIdx);
testMask = subset(proclblfs, teIdx);
% write file pathways of mask sets from dsfileset to table
trmaskinfo = resolve(trainMask);
valmaskinfo = resolve(valMask);
testmaskinfo = resolve(testMask);
% convert tables to arrays
trmaskfullArr = table2array(trmaskinfo);
valmaskfullArr = table2array(valmaskinfo);
testmaskfullArr = table2array(testmaskinfo);
% read file pathways into string arrays
trmaskArr = trmaskfullArr(:,1);
valmaskArr = valmaskfullArr(:,1);
testmaskArr = testmaskfullArr(:,1);
% convert string to char arrays
trmaskChar = convertStringsToChars(trmaskArr);
valmaskChar = convertStringsToChars(valmaskArr);
testmaskChar = convertStringsToChars(testmaskArr);
% read these into pixellabeldatastores
proclblReader = @(x) matRead(x);
classNames = ["background","tumor"];
pixelLabelID = [0 1];
trmaskpxds = pixelLabelDatastore(trmaskChar,classNames,pixelLabelID, ...
'FileExtensions','.mat','ReadFcn',procvolReader);
valmaskpxds = pixelLabelDatastore(valmaskChar,classNames,pixelLabelID, ...
'FileExtensions','.mat','ReadFcn',procvolReader);
tsmaskpxds = pixelLabelDatastore(testmaskChar,classNames,pixelLabelID, ...
'FileExtensions','.mat','ReadFcn',procvolReader);
% Compute Dice(general concept, might be a more code-friendly way to do it)
%{
p = networkPrediction.*correctPrediction
s = 2*sum(p, 'all')
err(i) = s/(sum(networkPrediction,'all')+sum(correctPrediction, 'all'))
%}
end
% Average Loss Function Error for all folds
%cvErr = sum(err)/sum(partition.TestSize);
% Need Random Patch Extraction on testing and validation Data
patchSize = [64 64 64];
patchPerImage = 16;
miniBatchSize = 8;
%training patch datastore
trpatchds = randomPatchExtractionDatastore(trainData,trmaskpxds,patchSize, ...
'PatchesPerImage',patchPerImage);
trpatchds.MiniBatchSize = miniBatchSize;
%validation patch datastore
dsVal = randomPatchExtractionDatastore(valData,valmaskpxds,patchSize, ...
'PatchesPerImage',patchPerImage);
dsVal.MiniBatchSize = miniBatchSize;
% before starting, need to define "n" which is the number of channels.
n = 1;
lgraph = layerGraph();
tempLayers = [
image3dInputLayer([64 64 64 n],"Name","input","Normalization","none")
convolution3dLayer([3 3 3],32,"Name","conv_Module1_Level1","Padding","same","WeightsInitializer","narrow-normal")
batchNormalizationLayer("Name","BN_Module1_Level1")
reluLayer("Name","relu_Module1_Level1")
convolution3dLayer([3 3 3],64,"Name","conv_Module1_Level2","Padding","same","WeightsInitializer","narrow-normal")
reluLayer("Name","relu_Module1_Level2")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
maxPooling3dLayer([2 2 2],"Name","maxpool_Module1","Padding","same","Stride",[2 2 2])
convolution3dLayer([3 3 3],64,"Name","conv_Module2_Level1","Padding","same","WeightsInitializer","narrow-normal")
batchNormalizationLayer("Name","BN_Module2_Level1")
reluLayer("Name","relu_Module2_Level1")
convolution3dLayer([3 3 3],128,"Name","conv_Module2_Level2","Padding","same","WeightsInitializer","narrow-normal")
reluLayer("Name","relu_Module2_Level2")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
maxPooling3dLayer([2 2 2],"Name","maxpool_Module2","Padding","same","Stride",[2 2 2])
convolution3dLayer([3 3 3],128,"Name","conv_Module3_Level1","Padding","same","WeightsInitializer","narrow-normal")
batchNormalizationLayer("Name","BN_Module3_Level1")
reluLayer("Name","relu_Module3_Level1")
convolution3dLayer([3 3 3],256,"Name","conv_Module3_Level2","Padding","same","WeightsInitializer","narrow-normal")
reluLayer("Name","relu_Module3_Level2")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
maxPooling3dLayer([2 2 2],"Name","maxpool_Module3","Padding","same","Stride",[2 2 2])
convolution3dLayer([3 3 3],256,"Name","conv_Module4_Level1","Padding","same","WeightsInitializer","narrow-normal")
reluLayer("Name","relu_Module4_Level1")
convolution3dLayer([3 3 3],512,"Name","conv_Module4_Level2","Padding","same","WeightsInitializer","narrow-normal")
reluLayer("Name","relu_Module4_Level2")
transposedConv3dLayer([2 2 2],512,"Name","transConv_Module4","Stride",[2 2 2])];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
concatenationLayer(4,2,"Name","concat3")
convolution3dLayer([3 3 3],256,"Name","conv_Module5_Level1","Padding","same","WeightsInitializer","narrow-normal")
reluLayer("Name","relu_Module5_Level1")
convolution3dLayer([3 3 3],256,"Name","conv_Module5_Level2","Padding","same","WeightsInitializer","narrow-normal")
reluLayer("Name","relu_Module5_Level2")
transposedConv3dLayer([2 2 2],256,"Name","transConv_Module5","Stride",[2 2 2])];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
concatenationLayer(4,2,"Name","concat2")
convolution3dLayer([3 3 3],128,"Name","conv_Module6_Level1","Padding","same","WeightsInitializer","narrow-normal")
reluLayer("Name","relu_Module6_Level1")
convolution3dLayer([3 3 3],128,"Name","conv_Module6_Level2","Padding","same","WeightsInitializer","narrow-normal")
reluLayer("Name","relu_Module6_Level2")
transposedConv3dLayer([2 2 2],128,"Name","transConv_Module6","Stride",[2 2 2])];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
concatenationLayer(4,2,"Name","concat1")
convolution3dLayer([3 3 3],64,"Name","conv_Module7_Level1","Padding","same")
reluLayer("Name","relu_Module7_Level1")
convolution3dLayer([3 3 3],64,"Name","conv_Module7_Level2","Padding","same")
reluLayer("Name","relu_Module7_Level2")
convolution3dLayer([1 1 1],2,"Name","ConvLast_Module7")
softmaxLayer("Name","softmax")
dicePixelClassification3dLayer("output")];
% helperDicePixelClassification3dLayer("output",1e-08,categorical(["background";"tumor"]));
lgraph = addLayers(lgraph,tempLayers);
% clean up helper variable
clear tempLayers;
lgraph = connectLayers(lgraph,"relu_Module1_Level2","maxpool_Module1");
lgraph = connectLayers(lgraph,"relu_Module1_Level2","concat1/in1");
lgraph = connectLayers(lgraph,"relu_Module2_Level2","maxpool_Module2");
lgraph = connectLayers(lgraph,"relu_Module2_Level2","concat2/in1");
lgraph = connectLayers(lgraph,"relu_Module3_Level2","maxpool_Module3");
lgraph = connectLayers(lgraph,"relu_Module3_Level2","concat3/in1");
lgraph = connectLayers(lgraph,"transConv_Module4","concat3/in2");
lgraph = connectLayers(lgraph,"transConv_Module5","concat2/in2");
lgraph = connectLayers(lgraph,"transConv_Module6","concat1/in2");
plot(lgraph);
%% train the model on the training set for each fold in the k-fold
% Need to Train the network using training and validation data
options = trainingOptions('adam', ...
'MaxEpochs',50, ...
'InitialLearnRate',5e-4, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',5, ...
'LearnRateDropFactor',0.95, ...
'ValidationData',dsVal, ...
'ValidationFrequency',400, ...
'Plots','training-progress', ...
'Verbose',false, ...
'MiniBatchSize',miniBatchSize);
modelDateTime = datestr(now,'dd-mmm-yyyy-HH-MM-SS');
[net,info] = trainNetwork(trpatchds,lgraph,options);
save(['trained3DUNet-' modelDateTime '-Epoch-' num2str(maxEpochs) '.mat'],'net');
%% evaluate the average dice similarity
%% train the model on the training set for each fold in the k-fold
%% evaluate the average dice similarity
% manu - output nifti files of the test set predictions
% priya - visualize differences in itksnap
%% compare to MONSTR
% @egates1 - do you have MONSTR predictions on this dataset ? are there any papers that have already done this ?