Step 8:
Now
check to see if the network has been trained properly by using the
validation data (this is the 30% of the images we set aside when we
split the original data into a testing set and a validation set).
clear;
clc;
close all;
mydata = imageDatastore('MerchData', ... % load data from folder named 'MerchData'
'IncludeSubfolders',true,
...
% Also include the subfolders (there are 5
of these for the 5 objects, cap, cube, ...)
'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 includes
% 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 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 trained network to classify validation images and calculate classification accuracy.
[YPred,probs] = classify(net,mydataResizedValidation);
accuracy = mean(YPred == mydataValidation.Labels)