%% Import Keras Network Layers and Train Network
% Specify the network file to import.

modelfile = 'digitsDAGnet.h5';

% Import network layers.

layers = importKerasLayers(modelfile)

% Load a data set for training a classifier to recognize new digits.

folder = fullfile(toolboxdir('nnet'),'nndemos','nndatasets','DigitDataset');
imds = imageDatastore(folder, ...
    'IncludeSubfolders',true, ...
    'LabelSource','foldernames');

% Partition the dataset into training and test sets.

numTrainFiles = 750;
[imdsTrain,imdsTest] = splitEachLabel(imds,numTrainFiles,'randomize');

% Set the training options.

options = trainingOptions('sgdm', ...
    'MaxEpochs',10, ...
    'InitialLearnRate',0.001);

% Train network using training data.

net = trainNetwork(imdsTrain,layers,options);

%  Run the trained network on the test set that was not used to train the network and predict the image labels (digits).

YPred = classify(net,imdsTest);
YTest = imdsTest.Labels;

% Calculate the accuracy.

accuracy = sum(YPred == YTest)/numel(YTest)

% Copyright 2018 The MathWorks, Inc.