%% train_1transfer googlenet SqueezeNet aynı parametreler save %% imdsTrain = imageDatastore('800', 'IncludeSubfolders',true, 'LabelSource','foldernames'); % [imdsTrain,imdsValidation] = splitEachLabel(imds,0.9,'randomized'); % save data.mat imds imdsTrain imdsValidation % load data.mat; %test net=netTransfer; timds = imageDatastore('test800', 'IncludeSubfolders',true, 'LabelSource','foldernames'); inputSize = net.Layers(1).InputSize; ttimds = augmentedImageDatastore(inputSize(1:2),timds); [YPred,scores] = classify(netTransfer,ttimds);imdsTrain = imageDatastore('800', 'IncludeSubfolders',true, 'LabelSource','foldernames'); confMat = confusionmat(timds.Labels, YPred) cm = confusionchart(confMat); fig = figure; cm = confusionchart(confMat,'ColumnSummary','column-normalized'); writematrix(info.TrainingAccuracy,'info.xls','Sheet','shufflenet','Range','B6');% excel writematrix(info.TrainingLoss,'info.xls','Sheet','shufflenet','Range','B7');% excel %% NET %% imdsTrain=imds; % net = inceptionv3; % net=resnet18; % net=vgg19; % net=inceptionv3; % net=mobilenetv2; % net=densenet201; % net=resnet101; % net=inceptionresnetv2; % net=efficientnetb0; % net=shufflenet; lgraph = layerGraph(net); [learnableLayer,classLayer] = findLayersToReplace(lgraph); numClasses = numel(categories(imdsTrain.Labels)); if isa(learnableLayer,'nnet.cnn.layer.FullyConnectedLayer') newLearnableLayer = fullyConnectedLayer(numClasses, ... 'Name','new_fc', ... 'WeightLearnRateFactor',10, ... 'BiasLearnRateFactor',10); elseif isa(learnableLayer,'nnet.cnn.layer.Convolution2DLayer') newLearnableLayer = convolution2dLayer(1,numClasses, ... 'Name','new_conv', ... 'WeightLearnRateFactor',10, ... 'BiasLearnRateFactor',10); end lgraph = replaceLayer(lgraph,learnableLayer.Name,newLearnableLayer); newClassLayer = classificationLayer('Name','new_classoutput'); lgraph = replaceLayer(lgraph,classLayer.Name,newClassLayer); %% train %% no auq options = trainingOptions('adam', ... 'MiniBatchSize',64, ... % 256 2nin katı 4 veya 8 16 2, 4, 8, 16, 32, … 512 vb. 'MaxEpochs',2, ... 'InitialLearnRate',1e-4, ... 'Shuffle','every-epoch', ... 'Verbose',false, ... 'Plots','training-progress',... 'LearnRateSchedule', "piecewise"); % 'LearnRateDropPeriod', 6 inputSize = net.Layers(1).InputSize; augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain); tic; [netTransfer,info]=trainNetwork(augimdsTrain,lgraph,options); toc % input image 224 224 ayarlamak için augmentedImageDatastore % imdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation); % save resnet101epoch2size64.mat netTransfer info % save shufflenetepoch100size64.mat netTransfer info epoch2 32minesize 1 saat %% %% %% %% imageAugmenter %% inputSize = net.Layers(1).InputSize; pixelRange = [-30 30]; imageAugmenter = imageDataAugmenter( ... 'RandXReflection',true, ... 'RandXTranslation',pixelRange, ... 'RandYTranslation',pixelRange); augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ... 'DataAugmentation',imageAugmenter); augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation); %% first googlenet fullyConnectedLayer ******************************************* %% net = googlenet; %deepNetworkDesigner(net) inputSize = net.Layers(1).InputSize; lgraph = layerGraph(net); numClasses = numel(categories(imdsTrain.Labels)); newLearnableLayer = fullyConnectedLayer(numClasses, ... 'Name','new_fc', ... 'WeightLearnRateFactor',10, ... 'BiasLearnRateFactor',10); lgraph = replaceLayer(lgraph,'loss3-classifier',newLearnableLayer); newClassLayer = classificationLayer('Name','new_classoutput'); lgraph = replaceLayer(lgraph,'output',newClassLayer); pixelRange = [-30 30]; imageAugmenter = imageDataAugmenter( ... 'RandXReflection',true, ... 'RandXTranslation',pixelRange, ... 'RandYTranslation',pixelRange); augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ... 'DataAugmentation',imageAugmenter); augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation); options = trainingOptions('adam', ... 'MiniBatchSize',128, ... % 256 2nin katı 4 veya 8 16 2, 4, 8, 16, 32, … 512 vb. 'MaxEpochs',8, ... 'InitialLearnRate',1e-4, ... 'Shuffle','every-epoch', ... 'ValidationData',augimdsValidation, ... 'ValidationFrequency',64, ... % 30 yap 'Verbose',false, ... 'Plots','training-progress'); %netTransfer = trainNetwork(augimdsTrain,lgraph,options); [netTransfer,info]=trainNetwork(augimdsTrain,lgraph,options); % save netgooglepoch8size128.mat netTransfer info %% ************************************************ %% %% To retrain SqueezeNet conv10' and 'ClassificationLayer_predictions' %% load data.mat; net = squeezenet; inputSize = net.Layers(1).InputSize; lgraph = layerGraph(net); numClasses = numel(categories(imdsTrain.Labels)); newConvLayer = convolution2dLayer([1, 1],numClasses,'WeightLearnRateFactor',10,... 'BiasLearnRateFactor',10,... "Name",'new_conv'); lgraph = replaceLayer(lgraph,'conv10',newConvLayer); newClassificatonLayer = classificationLayer('Name','new_classoutput'); lgraph = replaceLayer(lgraph,'ClassificationLayer_predictions',newClassificatonLayer); pixelRange = [-30 30]; imageAugmenter = imageDataAugmenter( ... 'RandXReflection',true, ... 'RandXTranslation',pixelRange, ... 'RandYTranslation',pixelRange); augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ... 'DataAugmentation',imageAugmenter); augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation); options = trainingOptions('adam', ... 'MiniBatchSize',128, ... % 256 2nin katı 4 veya 8 16 2, 4, 8, 16, 32, … 512 vb. 'MaxEpochs',10, ... 'InitialLearnRate',1e-4, ... 'Shuffle','every-epoch', ... 'ValidationData',augimdsValidation, ... 'ValidationFrequency',30, ... % 30 yap 'Verbose',false, ... 'Plots','training-progress'); netTransfer = trainNetwork(augimdsTrain,lgraph,options); [netTransfer,info]=trainNetwork(augimdsTrain,lgraph,options); % save netsqueezenetepoch8size128.mat netTransfer info %% % miniBatchSize = 128; % learnRate = 0.1*miniBatchSize/128; options = trainingOptions("sgdm", ... 'InitialLearnRate', 1e-4,... 'MaxEpochs', 60, ...% 30 'MiniBatchSize', 128, ... % 256 2nin katı 4 veya 8 16 2, 4, 8, 16, 32, … 512 vb. 'ValidationFrequency',30, ... % 30 yap 'Shuffle','every-epoch', ... 'Plots','training-progress',... 'Verbose',false, ... 'ValidationData',augimdsValidation, ... 'LearnRateSchedule', "piecewise"); [netTransfer,info]=trainNetwork(augimdsTrain,lgraph,options); % save netsqueezenet.mat netTransfer info