-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBraTs Dataset Train
More file actions
67 lines (52 loc) · 2.05 KB
/
Copy pathBraTs Dataset Train
File metadata and controls
67 lines (52 loc) · 2.05 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
%fresh start
clc
clear all
close all
gpuDevice(2)
% brats training and validation dataset locations
trImLoc = fullfile('/tmp/BraTS/preprocessedDataset/imagesTr');
valImLoc = fullfile('/tmp/BraTS/preprocessedDataset/imagesVal');
TrLblLoc = fullfile('/tmp/BraTS/preprocessedDataset/labelsTr');
valLblLoc = fullfile('/tmp/BraTS/preprocessedDataset/labelsVal');
% define readers
maskReader = @(x) (niftiread(x)>0);
volReader = @(x) niftiread(x);
procvolReader = @(x) matRead(x);
%%
% store in datastores
trImgDs = imageDatastore(trImLoc,'FileExtensions','.mat','ReadFcn',procvolReader)
trpxlDs = pixelLabelDatastore(TrLblLoc,["background","tumor"],[0 1],'FileExtensions','.mat', 'ReadFcn',procvolReader )
valImgDs = imageDatastore(valImLoc,'FileExtensions','.mat','ReadFcn',procvolReader)
valpxlDs = pixelLabelDatastore(valLblLoc,["background","tumor"],[0 1],'FileExtensions','.mat','ReadFcn',procvolReader)
%%
% Need Random Patch Extraction on testing and validation Data
patchSize = [57 57 57];
patchPerImage = 25;
miniBatchSize = 8;
%training patch datastore
trpatchds = randomPatchExtractionDatastore(trImgDs,trpxlDs,patchSize, ...
'PatchesPerImage',patchPerImage);
trpatchds.MiniBatchSize = miniBatchSize;
%validation patch datastore
dsVal = randomPatchExtractionDatastore(valImgDs,valpxlDs,patchSize, ...
'PatchesPerImage',patchPerImage);
dsVal.MiniBatchSize = miniBatchSize;
% before starting, need to define "n" which is the number of channels.
n = 4;
%% paste network layers
%% set training options
options = trainingOptions('adam', ...
'MaxEpochs',35, ...
'InitialLearnRate',5e-4, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',5, ...
'LearnRateDropFactor',0.95, ...
'ValidationData',dsVal, ...
'ValidationFrequency',400, ...
'Plots','training-progress', ...
'Verbose',false, ...
'MiniBatchSize',miniBatchSize);
%Train
modelDateTime = datestr(now,'dd-mmm-yyyy-HH-MM-SS');
[net,info] = trainNetwork(trpatchds,lgraph,options);
save(['trainedDeepMedic' modelDateTime '-Epoch-' num2str(maxEpochs) '.mat'],'net');