%% Train Network for Image Regression
% Load the sample data, which consists of synthetic images of handwritten digits. The third output contains the corresponding angles in degrees by which each image has been rotated.
% Load the training images as 4-D arrays using digitTrain4DArrayData. The output XTrain is a 28-by-28-by-1-by-5000 array, where:
% 28 is the height and width of the images.
% 1 is the number of channels.
% 5000 is the number of synthetic images of handwritten digits.
% YTrain contains the rotation angles in degrees.

[XTrain,~,YTrain] = digitTrain4DArrayData;

% Display 20 random training images using imshow.

figure
numTrainImages = numel(YTrain);
idx = randperm(numTrainImages,20);
for i = 1:numel(idx)
    subplot(4,5,i)    
    imshow(XTrain(:,:,:,idx(i)))
    drawnow;
end

% Specify the convolutional neural network architecture. For regression problems, include a regression layer at the end of the network.

layers = [ ...
    imageInputLayer([28 28 1])
    convolution2dLayer(12,25)
    reluLayer
    fullyConnectedLayer(1)
    regressionLayer];

% Specify the network training options. Set the initial learn rate to 0.001.

options = trainingOptions('sgdm', ...
    'InitialLearnRate',0.001, ...
    'Verbose',false, ...
    'Plots','training-progress');

% Train the network.

net = trainNetwork(XTrain,YTrain,layers,options);

% Test the performance of the network by evaluating the prediction accuracy of the test data. Use predict to predict the angles of rotation of the validation images.

[XTest,~,YTest] = digitTest4DArrayData;
YPred = predict(net,XTest);

% Evaluate the performance of the model by calculating the root-mean-square error (RMSE) of the predicted and actual angles of rotation.

rmse = sqrt(mean((YTest - YPred).^2))

%
% Copyright 2012 The MathWorks, Inc.