clc
clear
close all
%% parameters
%parametr ILR
InitLearnRate1 = [0.00001]

n = length(InitLearnRate1);
correctgraf = zeros(1,n);
NOKcorrectgraph = zeros(1,n);
OKcorrectgraph = zeros(1,n);
traintimegraf = zeros(1,n);
testtimegraf = zeros(1,n);
 
outputFolder = fullfile('MDSkusy');  %cesta
    
categories = {'KulatOK','KulatNOK'};          %vyber kategorii
imds = imageDatastore(fullfile(outputFolder,categories),'LabelSource','foldernames');


%% Rozmnozeni dat pred rozdelenim
imds1 = imageDatastore(cat(1,imds.Files,imds.Files,imds.Files,imds.Files,imds.Files,imds.Files))
imds1.Labels = cat(1,imds.Labels,imds.Labels,imds.Labels,imds.Labels,imds.Labels,imds.Labels)

imds = imageDatastore(cat(1,imds1.Files,imds1.Files,imds1.Files,imds1.Files))
imds.Labels = cat(1,imds1.Labels,imds1.Labels,imds1.Labels,imds1.Labels)

imds1 = imageDatastore(cat(1,imds.Files,imds.Files))
imds1.Labels = cat(1,imds.Labels,imds.Labels)
miniBatchSize = 30;
imds1.ReadSize = miniBatchSize;
%%
for i = 1:n  %loop na zkouseni vic hodnot parametru
    
load BasicNet.mat     %nacteni site  
tic
%Rozdeleni dat
[trainingSet,testSet] = splitEachLabel(imds1, 0.8, 'randomize') 
[trainingSet,validationSet] = splitEachLabel(trainingSet, 0.8, 'randomize') 

imageSize = layers_1(1).InputSize   %zjisteni potrebne velikosti obrazku

%Uprava dat pred ucenim, 
    dsTrain = transform(trainingSet,@Augmentation,'IncludeInfo',true)
    dsValidate = transform(validationSet,@Augmentation,'IncludeInfo',true)
    dsTest = transform(testSet,@Augmentation,'IncludeInfo',true)

Actuallabels = testSet.Labels;      %popisky snimku, pro pozdejsi test

%nastaveni trenovacich parametru
options = trainingOptions("sgdm","InitialLearnRate", InitLearnRate1(i),"Minibatchsize",30,...           
    "MaxEpochs",5,"ValidationFrequency",10,"ValidationData",dsValidate,"Plots","training-progress")

[basicnet,info] = trainNetwork(dsTrain,layers_1,options)     %trenovani site
    
    trainingtime = toc          %doba trenovani
    
    tic
testPreds = classify(basicnet,dsTest);    %test site
    testtime = toc                  %doba testovani

    numcorect = nnz(Actuallabels == testPreds);     %Pocet spravne klasifikovanych
    fracCorrect = numcorect/numel(testPreds)        %celkova presnost

cm = confusionchart(Actuallabels,testPreds)     %chybova matice
    NOKcorrect = cm.NormalizedValues(1,1)/(cm.NormalizedValues(1,1)+cm.NormalizedValues(1,2))   %presnost u NOK kusu
    OKcorrect = cm.NormalizedValues(2,2)/(cm.NormalizedValues(2,1)+cm.NormalizedValues(2,2))    %presnost u OK kusu  

%ukladani pozadovanych hodnot
correctgraf(i) = fracCorrect
NOKcorrectgraph(i) = NOKcorrect
traintimegraf(i) = trainingtime
testtimegraf(i) = testtime
OKcorrectgraph(i) = OKcorrect


end

%% zapsani do tabulky
accuracy = correctgraf.';
NOKaccuracy = NOKcorrectgraph.';
OKaccuracy = OKcorrectgraph.';
trainTime = traintimegraf.';
testTime = testtimegraf.';
initialLearningRate = InitLearnRate1.';

T = table(initialLearningRate,accuracy,OKaccuracy,NOKaccuracy,trainTime,testTime)     %vytvoreni tabulky

%zapsani do excelu
% filename = 'datafinal.xlsx';
% writetable(T,filename,'Sheet',1,'Range','A1')






