clc 
clear all
close all


%%  
pathToImages= 'Cats_DogsDataset\Cats&Dogs100'
imds= imageDatastore(pathToImages, "IncludeSubfolders",true,"LabelSource","foldernames");
[trainImgs, testImgs]= splitEachLabel(imds,0.95)

%% Visualize the Image
close all
net= alexnet
img = imread(imds.Files{1}); 
figure(1)
imshow(img)
pippo= size(img)
actvn = activations(net,img,'conv1'); 
figure(2)
imshow(mat2gray(actvn(1:92,1:123,90)))
figure(3)
montage(mat2gray(actvn))

%% 
% SizeArray_=[];
% for i = 23919:length(imds.Files)
%     img = imread(imds.Files{i});
%     
%     Size_= size(img);
%     if (length(Size_)<3)
%         nome= imds.Files{i} 
%         imshow(img);        
%         delete(imds.Files{i} );
%     end
%     i
%     %%SizeArray_ = [SizeArray_ , Size_(3)];
% end
% SizeArray_
% mean(SizeArray_)
% 
% a=0

%%
%montage(imds)  %Create a collage of the available photos
pathToImages= 'Cats_DogsDataset\Cats&Dogs'
imds= imageDatastore(pathToImages, "IncludeSubfolders",true,"LabelSource","foldernames");
[trainImgs, testImgs]= splitEachLabel(imds,0.95)
trainds= augmentedImageDatastore([28 28] ,trainImgs) 
testds= augmentedImageDatastore([28 28] ,testImgs)
%%
length(testImgs.Labels)
a=0
sum(testImgs.Labels=='Dog')
testImgs.Labels=='Dog'

%%


% net = alexnet 
% layers= net.Layers
layers = [
            imageInputLayer([28 28 3])
            convolution2dLayer(3,16,'Padding',1)
            batchNormalizationLayer()
            reluLayer()
            maxPooling2dLayer(2,'Stride',2)
            fullyConnectedLayer(2)
            softmaxLayer()
            classificationLayer()
         ]

%net.Layers(1).InputSize=   [227 227 1];

layers(end-2)=fullyConnectedLayer(2) 
layers(end)=classificationLayer()

%%options= trainingOptions('adam',"InitialLearnRate",0.0001)  %0.03)  %0.0001)

options=trainingOptions("adam", "MaxEpochs",20, "InitialLearnRate",0.005, "Plots","training-progress")

  %% options=trainingOptions("adam", "MaxEpochs",100, "InitialLearnRate",0.5, "Plots","training-progress", "GradientThreshold",1, "SequenceLength",100, "LearnRateSchedule","piecewise","LearnRateDropFactor",0.97)

net = trainNetwork(trainds,layers,options)


%% Evaluate the Network
testPreds = classify(net,testds);

acc = nnz(testPreds == testImgs.Labels)/length(testPreds)


confusionmat(testImgs.Labels,testPreds)

testPreds


acc 

%%
close all 
img = imread(imds.Files{20000});
imshow(img)
imgn = imresize(img,[28 28])
size(imgn)

[a, score]=classify(net,imgn)


