function [netTransfer, info] = diatomnet(model_output_root, epoch_number, miniBatchSize, inputSize, numClasses, imdsTrain, imdsValidation, imdsTest) augimdsTrain = augmentedImageDatastore(inputSize, imdsTrain); augimdsValidation = augmentedImageDatastore(inputSize,imdsValidation); augimdsTest = augmentedImageDatastore(inputSize,imdsTest); lgraph = layerGraph(); tempLayers = [ imageInputLayer(inputSize,"Name","data") convolution2dLayer([7 7],64,"Name","conv1-7x7_s2","BiasLearnRateFactor",2,"Padding","same","Stride",[2 2]) reluLayer("Name","conv1-relu_7x7") maxPooling2dLayer([3 3],"Name","pool1-3x3_s2","Padding","same","Stride",[2 2]) crossChannelNormalizationLayer(5,"Name","pool1-norm1","K",1) convolution2dLayer([1 1],64,"Name","conv2-3x3_reduce","BiasLearnRateFactor",2,"Padding","same") reluLayer("Name","conv2-relu_3x3_reduce") convolution2dLayer([3 3],192,"Name","conv2-3x3","BiasLearnRateFactor",2,"Padding","same") reluLayer("Name","conv2-relu_3x3") crossChannelNormalizationLayer(5,"Name","conv2-norm2","K",1) maxPooling2dLayer([3 3],"Name","pool2-3x3_s2","Padding","same","Stride",[2 2])]; lgraph = addLayers(lgraph,tempLayers); tempLayers = [ convolution2dLayer([1 1],16,"Name","inception_3a-5x5_reduce","BiasLearnRateFactor",2,"Padding","same") reluLayer("Name","inception_3a-relu_5x5_reduce") convolution2dLayer([5 5],32,"Name","inception_3a-5x5","BiasLearnRateFactor",2,"Padding","same") reluLayer("Name","inception_3a-relu_5x5")]; lgraph = addLayers(lgraph,tempLayers); tempLayers = [ convolution2dLayer([1 1],96,"Name","inception_3a-3x3_reduce","BiasLearnRateFactor",2,"Padding","same") reluLayer("Name","inception_3a-relu_3x3_reduce") convolution2dLayer([3 3],128,"Name","inception_3a-3x3","BiasLearnRateFactor",2,"Padding","same") reluLayer("Name","inception_3a-relu_3x3")]; lgraph = addLayers(lgraph,tempLayers); tempLayers = [ convolution2dLayer([1 1],64,"Name","inception_3a-1x1","BiasLearnRateFactor",2,"Padding","same") reluLayer("Name","inception_3a-relu_1x1")]; lgraph = addLayers(lgraph,tempLayers); tempLayers = [ maxPooling2dLayer([3 3],"Name","inception_3a-pool","Padding","same") convolution2dLayer([1 1],32,"Name","inception_3a-pool_proj","BiasLearnRateFactor",2,"Padding","same") reluLayer("Name","inception_3a-relu_pool_proj")]; lgraph = addLayers(lgraph,tempLayers); tempLayers = [ depthConcatenationLayer(4,"Name","inception_3b-output") maxPooling2dLayer([3 3],"Name","pool3-3x3_s2","Padding","same","Stride",[2 2])]; lgraph = addLayers(lgraph,tempLayers); tempLayers = [ convolution2dLayer([1 1],96,"Name","inception_4a-3x3_reduce","BiasLearnRateFactor",2) reluLayer("Name","inception_4a-relu_3x3_reduce") convolution2dLayer([3 3],208,"Name","inception_4a-3x3","BiasLearnRateFactor",2,"Padding",[1 1 1 1]) reluLayer("Name","inception_4a-relu_3x3")]; lgraph = addLayers(lgraph,tempLayers); tempLayers = [ convolution2dLayer([1 1],16,"Name","inception_4a-5x5_reduce","BiasLearnRateFactor",2) reluLayer("Name","inception_4a-relu_5x5_reduce") convolution2dLayer([5 5],48,"Name","inception_4a-5x5","BiasLearnRateFactor",2,"Padding",[2 2 2 2]) reluLayer("Name","inception_4a-relu_5x5")]; lgraph = addLayers(lgraph,tempLayers); tempLayers = [ convolution2dLayer([1 1],192,"Name","inception_4a-1x1","BiasLearnRateFactor",2) reluLayer("Name","inception_4a-relu_1x1")]; lgraph = addLayers(lgraph,tempLayers); tempLayers = [ maxPooling2dLayer([3 3],"Name","inception_4a-pool","Padding",[1 1 1 1]) convolution2dLayer([1 1],64,"Name","inception_4a-pool_proj","BiasLearnRateFactor",2) reluLayer("Name","inception_4a-relu_pool_proj")]; lgraph = addLayers(lgraph,tempLayers); tempLayers = [ depthConcatenationLayer(4,"Name","inception_4e-output") maxPooling2dLayer([3 3],"Name","pool4-3x3_s2","Padding","same","Stride",[2 2])]; lgraph = addLayers(lgraph,tempLayers); tempLayers = [ convolution2dLayer([1 1],48,"Name","inception_5b-5x5_reduce","BiasLearnRateFactor",2) reluLayer("Name","inception_5b-relu_5x5_reduce") convolution2dLayer([5 5],128,"Name","inception_5b-5x5","BiasLearnRateFactor",2,"Padding",[2 2 2 2]) reluLayer("Name","inception_5b-relu_5x5")]; lgraph = addLayers(lgraph,tempLayers); tempLayers = [ maxPooling2dLayer([3 3],"Name","inception_5b-pool","Padding",[1 1 1 1]) convolution2dLayer([1 1],128,"Name","inception_5b-pool_proj","BiasLearnRateFactor",2) reluLayer("Name","inception_5b-relu_pool_proj")]; lgraph = addLayers(lgraph,tempLayers); tempLayers = [ convolution2dLayer([1 1],384,"Name","inception_5b-1x1","BiasLearnRateFactor",2) reluLayer("Name","inception_5b-relu_1x1")]; lgraph = addLayers(lgraph,tempLayers); tempLayers = [ convolution2dLayer([1 1],192,"Name","inception_5b-3x3_reduce","BiasLearnRateFactor",2) reluLayer("Name","inception_5b-relu_3x3_reduce") convolution2dLayer([3 3],384,"Name","inception_5b-3x3","BiasLearnRateFactor",2,"Padding",[1 1 1 1]) reluLayer("Name","inception_5b-relu_3x3")]; lgraph = addLayers(lgraph,tempLayers); tempLayers = [ depthConcatenationLayer(4,"Name","inception_5b-output") globalAveragePooling2dLayer("Name","pool5-7x7_s1") dropoutLayer(0.4,"Name","pool5-drop_7x7_s1") fullyConnectedLayer(numClasses,"Name","loss3-classifier","BiasLearnRateFactor",2) softmaxLayer("Name","prob") classificationLayer("Name","output")]; lgraph = addLayers(lgraph,tempLayers); lgraph = connectLayers(lgraph,"pool2-3x3_s2","inception_3a-5x5_reduce"); lgraph = connectLayers(lgraph,"pool2-3x3_s2","inception_3a-3x3_reduce"); lgraph = connectLayers(lgraph,"pool2-3x3_s2","inception_3a-1x1"); lgraph = connectLayers(lgraph,"pool2-3x3_s2","inception_3a-pool"); lgraph = connectLayers(lgraph,"inception_3a-relu_1x1","inception_3b-output/in2"); lgraph = connectLayers(lgraph,"inception_3a-relu_pool_proj","inception_3b-output/in1"); lgraph = connectLayers(lgraph,"inception_3a-relu_5x5","inception_3b-output/in3"); lgraph = connectLayers(lgraph,"inception_3a-relu_3x3","inception_3b-output/in4"); lgraph = connectLayers(lgraph,"pool3-3x3_s2","inception_4a-3x3_reduce"); lgraph = connectLayers(lgraph,"pool3-3x3_s2","inception_4a-5x5_reduce"); lgraph = connectLayers(lgraph,"pool3-3x3_s2","inception_4a-1x1"); lgraph = connectLayers(lgraph,"pool3-3x3_s2","inception_4a-pool"); lgraph = connectLayers(lgraph,"inception_4a-relu_3x3","inception_4e-output/in4"); lgraph = connectLayers(lgraph,"inception_4a-relu_1x1","inception_4e-output/in3"); lgraph = connectLayers(lgraph,"inception_4a-relu_pool_proj","inception_4e-output/in1"); lgraph = connectLayers(lgraph,"inception_4a-relu_5x5","inception_4e-output/in2"); lgraph = connectLayers(lgraph,"pool4-3x3_s2","inception_5b-5x5_reduce"); lgraph = connectLayers(lgraph,"pool4-3x3_s2","inception_5b-pool"); lgraph = connectLayers(lgraph,"pool4-3x3_s2","inception_5b-1x1"); lgraph = connectLayers(lgraph,"pool4-3x3_s2","inception_5b-3x3_reduce"); lgraph = connectLayers(lgraph,"inception_5b-relu_1x1","inception_5b-output/in1"); lgraph = connectLayers(lgraph,"inception_5b-relu_pool_proj","inception_5b-output/in4"); lgraph = connectLayers(lgraph,"inception_5b-relu_5x5","inception_5b-output/in3"); lgraph = connectLayers(lgraph,"inception_5b-relu_3x3","inception_5b-output/in2"); clear tempLayers; valFrequency = floor(numel(augimdsTrain.Files)/miniBatchSize); options = trainingOptions('sgdm', ... 'MiniBatchSize',miniBatchSize, ... 'MaxEpochs',1, ... 'InitialLearnRate',0.0001, ... 'Shuffle','every-epoch', ... 'ValidationData',augimdsValidation, ... 'ValidationFrequency',valFrequency, ... 'Verbose',false, ... 'Plots','none'... ); fileID = fopen(strcat(model_output_root, '/results.txt'),'w'); fprintf(fileID,'epoch#;TrainingLoss;TrainingAccuracy;FinalValidationLoss;FinalValidationAccuracy;TrainAccuracy;TestAccuracy\n'); maxTestAccuracy = -1; for i = 1:epoch_number [netTransfer, info] = trainNetwork(augimdsTrain,lgraph,options); lgraph = layerGraph(netTransfer); % train [YPredTrain, scoresTrain] = classify(netTransfer,augimdsTrain); YTrain = imdsTrain.Labels; accuracyTrain = mean(YPredTrain == YTrain); %test [YPredTest, scoresTest] = classify(netTransfer,augimdsTest); YTest = imdsTest.Labels; accuracyTest = mean(YPredTest == YTest); %validation [YPredValidation, scoresValidation] = classify(netTransfer,augimdsValidation); YVal = imdsValidation.Labels; accuracyValidation = mean(YPredValidation == YVal); sprintf('epoch#%d, train %3.4f, val %3.4f, test %3.4f, loss %3.4f'... ,i, accuracyTrain*100, info.FinalValidationAccuracy, accuracyTest*100, info.FinalValidationLoss) fprintf(fileID, 'epoch#%d;%3.4f;%3.4f;%3.4f;%3.4f;%3.4f;%3.4f\n'... ,i,... mean(info.TrainingLoss),... mean(info.TrainingAccuracy),... info.FinalValidationLoss,... accuracyValidation,... accuracyTrain * 100,... accuracyTest * 100); if accuracyTest > maxTestAccuracy %path = fullfile(model_output_root, strcat('model', int2str(i), '.mat')); %save (path, 'netTransfer', 'info', 'lgraph') maxTestAccuracy = accuracyTest; sprintf('saved %d', i); end path = fullfile(model_output_root, strcat('model', int2str(i), '.mat')); save (path, 'netTransfer', 'info', 'lgraph') end fclose(fileID); end