%double click on deeplearning_couse_files
flowerds = imageDatastore("Flowers","IncludeSubfolders",true,"LabelSource","foldernames");
%Split into training and testing sets
[trainImgs,testImgs] = splitEachLabel(flowerds,0.6,'randomized');
%Determine the number of flower species
numClasses = numel(categories(flowerds.Labels));
%Get the layers from GoogLeNet
net = googlenet;
lgraph = layerGraph(net);
%Modify the classification and output layers
newFc = fullyConnectedLayer(12,"Name","new_fc");
lgraph = replaceLayer(lgraph,"loss3-classifier",newFc);
newOut = classificationLayer("Name","new_out");
lgraph = replaceLayer(lgraph,"output",newOut);
%Lower the learning rate for transfer learning
options = trainingOptions("sgdm","InitialLearnRate", 0.001);
%perform training 30 epochs 3min 11sec on calipso
[flowernet,info] = trainNetwork(trainImgs, lgraph, options);
%Use the trained network to classify test images
testpreds = classify(flowernet,testImgs);
%Calculate the accuracy
accuracy=nnz(testpreds == testImgs.Labels)/numel(testpreds)
%Visualize the confusion matrix
confusionchart(testImgs.Labels,testpreds);