function [trainedNet testPredictionsPerFile] = network_main_v6()
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here
ads = audioDatastore('onlineDataset_v5.1.1','IncludeSubfolders',true,'LabelSource','foldernames');
labelTable = countEachLabel(ads);
overlapPercentage=75;
numClasses = size(labelTable,1);
numAudios = length(ads.Labels);
num_folds=5;
for fold_idx=1:num_folds
    fprintf('processing %d among %d folds \n',fold_idx,num_folds);
    test_idx=fold_idx:num_folds:numAudios;
    adsTest=subset(ads,test_idx);
    [testFeatures,testLabels,segmentsPerFile] = vggishPreprocess(adsTest,overlapPercentage)
    
    train_idx=setdiff(1:length(ads.Files),test_idx);
    adsTrainVlidation=subset(ads,train_idx);
    [adsTrain, adsValidation] = splitEachLabel(adsTrainVlidation,0.9,'randomized');
    [trainFeatures,trainLabels] = vggishPreprocess(adsTrain,overlapPercentage);
    [validationFeatures,validationLabels] = vggishPreprocess(adsValidation,overlapPercentage);
    
     clear nets;
     clear net;
     nets = load('VGGishToTestData5.mat');
     net =nets.trainedNet;
     lgraph = layerGraph(net.Layers);
     lgraph = removeLayers(lgraph,'classOut');
    lgraph.Layers(end);
    lgraph = removeLayers(lgraph,'softmax');
     lgraph = removeLayers(lgraph,'FCFinal');
    lgraph.Layers(end);
    layers = lgraph.Layers
   for i = 1:8
        if isprop(layers(i),'WeightLearnRateFactor')
            layers(i).WeightLearnRateFactor = 0;
        end
        if isprop(layers(i),'WeightL2Factor')
            layers(i).WeightL2Factor = 0;
        end
        if isprop(layers(i),'BiasLearnRateFactor')
            layers(i).BiasLearnRateFactor = 0;
        end
        if isprop(layers(i),'BiasL2Factor')
            layers(i).BiasL2Factor = 0;
        end
   end
    lgraph = layerGraph(layers);
    lgraph = layerGraph(layers);
    lgraph = addLayers(lgraph,fullyConnectedLayer(numClasses,'Name','FFFCFinal'));
    lgraph = addLayers(lgraph,softmaxLayer('Name','softmax'));
    lgraph = addLayers(lgraph,classificationLayer('Name','classOut'));

    lgraph = connectLayers(lgraph,'EmbeddingBatch','FFFCFinal');
    lgraph = connectLayers(lgraph,'FFFCFinal','softmax');
    lgraph = connectLayers(lgraph,'softmax','classOut');
     miniBatchSize = 32;
     options = trainingOptions('adam',...
        'MaxEpochs',20,'MiniBatchSize',miniBatchSize,...
        'Shuffle','every-epoch', ...
         'ValidationData',{validationFeatures,validationLabels}, ...
        'Verbose',false, ...
        'Plots','training-progress',...
        'ValidationPatience',7,'ExecutionEnvironment','gpu','InitialLearnRate',0.0001);
    
   [trainedNet, netInfo] = trainNetwork(trainFeatures,trainLabels,lgraph,options);
   [testPredictions scores]  = classify(trainedNet,testFeatures);
    save(['BW-VGGish',num2str(fold_idx),'.mat'],'trainedNet');
    save(['info',num2str(fold_idx),'.mat'],'netInfo');
   idx = 1;
    for ii = 1:numel(adsTest.Files)
        testPredictionsPerFile(1,test_idx(ii)) = mode(testPredictions(idx:idx+segmentsPerFile(ii)-1));
         scoresPerFile(test_idx(ii),:) = mode(scores(idx:idx+segmentsPerFile(ii)-1,:));
        idx = idx + segmentsPerFile(ii);
    end
end
actual_labels=ads.Labels;

% Confusion Matrix
figure;
plotconfusion(actual_labels,testPredictionsPerFile')
title('Confusion Matrix:');


figure;
[X1,Y1,T,AUC1]=perfcurve(actual_labels,scoresPerFile(:,1),'alpha');
plot(X1,Y1);
hold on

[X2,Y2,T,AUC2]=perfcurve(actual_labels,scoresPerFile(:,2),'beta');
plot(X2,Y2);
hold on

[X3,Y3,T,AUC3]=perfcurve(actual_labels,scoresPerFile(:,3),'delta');
plot(X3,Y3);
hold on

[X4,Y4,T,AUC4]=perfcurve(actual_labels,scoresPerFile(:,6),'theta');
plot(X4,Y4);
hold on
[X5,Y5,T,AUC5]=perfcurve(actual_labels,scoresPerFile(:,4),'gamma');
plot(X5,Y5);
hold off
AUC = (AUC1+AUC2+AUC3+AUC4+AUC5)/5
legend('alpha','beta','delta','theta','gamma','Location','SE');
xlabel('False positive rate') 
ylabel('True positive rate')
title('ROC Curve')
end

