clear all;
close all;
clc;

mydata = imageDatastore('MerchData', ...   %load data from folder 'MerchData'
'IncludeSubfolders',true, ...
'LabelSource','foldernames');  %the name of the subfolders supply the correct answer labels
[mydataTrain,mydataValidation] = splitEachLabel(mydata,0.7);

%Resize images in the image datastores to match the pretrained network GoogLeNet.
mydataResizedTrain = augmentedImageDatastore([224 224],mydataTrain,'colorpreprocessing','gray2rgb');  
mydataResizedValidation = augmentedImageDatastore([224 224],mydataValidation,'colorpreprocessing','gray2rgb');
%colorpreprocessing option above makes sure that if your data set incluedes
%both color and  grayscale images there won't be a problem loading them to the datastore.
%Color images usually have a dimension of mxnx3 where as grayscales are mxnx1

load MerchNewLayers  %network created for transfer learning using deepNetworkDesigner & GoogleNet

%SET OPTIONS FOR TRANSFER LEARNING
%Set InitialLearnRate to a small value to slow down learning in the transferred layers
% Specify a small number of epochs. An epoch is a full training cycle on
% the entire training data set. For transfer learning, you do not need to train for as many epochs.
% Shuffle the data every epoch.
% Specify the mini-batch size, that is, how many images to use in each iteration
% Specify validation data and validation frequency
% Turn on the training plot to monitor progress while you train

options = trainingOptions('sgdm', ...
'MiniBatchSize',10,...  
'MaxEpochs',6,...
'InitialLearnRate',1e-4,...   %this is 0.0001
'Shuffle','every-epoch',...
'ValidationData',mydataResizedValidation,...
'ValidationFrequency',6,...
'Verbose',false,...
'Plots','training-progress');

%TRAIN OUR NEW FINE-TUNED NETWORK
%To train the network, supply the layers you exported from the app (lgraph_1), and your resized 
%images, as well as training options to the trainNetwork function.
net = trainNetwork(mydataResizedTrain,NewLayers,options);

% Use fine-tuned network to classify validation images and calculate classification accuracy.
[YPred,probs] = classify(net,mydataResizedValidation);
accuracy = mean(YPred == mydataValidation.Labels)

%Plot a few of the images and their label predicted by the new network
figure
for i = 1:4
subplot(2,2,i)
a=randi(20);  %randomly pick one of theimages (there are 20 validation images out of the 75 total)
I = readimage(mydataValidation,a);
imshow(I)
label = YPred(a);  %The network's prediction (label for this image)
title(string(label) + ", " + num2str(100*max(probs(a,:)),3) + "%");
end




