The learning source is an official example from Mathworks, which is used for personal learning and can be used to implement CNN regression calculations based on the requirement change data set on individual projects

The method to generate data sets can be reference: blog.csdn.net/qingfengxd1…

The %% data set contains a composite image of handwritten numbers, and the corresponding Angle (in degrees) of rotation for each image. %% Use digitTrain4DArrayData and digitTest4DArrayData to load the training and validation images as a 4D array. %% prints YTrain and YValidation as rotations in angles. Each training and validation dataset contains 5000 images. [XTrain, ~, Ytrain] = digitTrain4DArrayData; [XValidation, ~, YValidation] = digitTest4DArrayData; NumTrainImages = numel(YTrrain); figure; idx = randperm(numTrainImages, 20); for i = 1 : numel(idx) subplot(4, 5, i); imshow(XTrain(:, :, :, idx(i))) drawnow endCopy the code

When training neural networks, it is often helpful to ensure that your data is standardized at all stages of the network. %% normalization helps to use gradient descent to stabilize and speed up network training. %% If your data size is too small, the loss may become NaN, and network parameters may diverge during training. Common methods for standardizing data include recalibrating the data to range [0,1] or to have a mean of 0 and a standard deviation of 1. You can standardize the following data: 1, input data. Normalize predictors before they are input to the network. In this case, the input image has been normalized to the [0,1] range. 2. Layer output. You can normalize the output of each convolution and full join layer using the batch normalization layer. 3. Response. If the layer output at the end of the network is normalized using a batch normalization layer, the prediction of the network is normalized at the beginning of training. If the size of the response is very different from these predictions, then network training may fail to converge. If your answers don’t expand well, try to standardize them and see if online training improves. If the response is normalized before training, the prediction of the training network must be transformed to obtain the prediction of the original response. %}

In general, data need not be completely standardized. %% However, if in this case the network is trained to predict 100*YTrain or YTrain+500 instead of YTrain, then the loss becomes NaN, and %% the network parameters diverge when the training begins. These results would still occur even though the only difference between the network prediction aY + b and the network prediction Y is the re-weighting and bias of the final fully connected layer. Non-linear transformations can also be performed (for example, taking logarithms) if the distribution of inputs or responses is very uneven or skewed

%% Plot response distribution: In classification problems, the output is class probability, and class probability is always normalized. figure; histogram(YTrain) axis tight ylabel('Counts') xlabel('Rotation Angle')Copy the code

In general, data need not be fully normalized. However, if the network had been trained in this example to predict 100*YTrain or YTrain+500 instead of YTrain, the loss would have become NaN, and the network parameters would have deviated at the start of the training. These results occur even if the only difference between a network that predicts aY + b and one that predicts Y is a simple re-scaling of the weights and biases of the final fully connected layer.

If the distribution of inputs or responses is very uneven or skewed, you can also perform a nonlinear transformation of the data (for example, take its logarithm) before training the network.

%% Create network layer %% The first layer defines the size and type of the input data. The input image size is 28×28×1. Create an image input layer of the same size as the training image. The %% network middle tier defines the core architecture of the network, where most computing and learning takes place. The last layer defines the size and type of the output data. For regression problems, the full connection layer must precede the regression layer at the end of the network. layers = [ imageInputLayer([28 28 1]) batchNormalizationLayer reluLayer averagePooling2dLayer(2, 'Stride', 2) convolution2dLayer(3, 16, 'Padding', 'same') batchNormalizationLayer reluLayer averagePooling2dLayer(2, 'Stride', 2) convolution2dLayer(3, 32, 'Padding', 'same') batchNormalizationLayer reluLayer concolution2dLayer(3, 32, 'Padding', BatchNormalizationLayer reluLayer dropoutLayer(0.2) fullyConnectedLayer(1) regressionLayer]; %% training network -- Options %% Train for 30 EPOchs 0.001 decreased learning rate after 20 epochs. Monitor network accuracy during training by specifying validation data and frequency. Train the network according to the training data, and calculate the accuracy of the verification data regularly in the training process. %% validation data is not used to update network weights. Open the training progress chart and close the command window output. miniBatchSize = 128; validationFrequency = floor(numel(YTrain) / miniBatchSize); options = trainingOptions('sgdm', ... 'MiniBatchSize', miniBatchSize, ... 'MaxEpochs', 30, ... 'InitialLearnRate', 1e-3, ... 'LearnRateSchedule', 'piecewise', ... 'LearnRateDropFactor, 0.1,... 'LearnRateDropPeriod', 20, ... 'Shuffle', 'every-epoch', ... 'ValidationData', {XValidation, YValidation}, ... 'ValidationFrequency', validationFrequency, ... 'Plots', 'training-progress', ... 'Verbose', false); net = trainNetwork(XTrain, YTrain, layer, options)Copy the code

Create a network using trainNetwork. If a compatible GPU exists, this command uses the GPU. Otherwise, trainNetwork will use CPU. Training on a GPU requires a CUDA-enabled NVIDIA® GPU with 3.0 or higher computing power.

Check the Layers property of NET for details of the network architecture.

net.Layers
Copy the code

Test network performance based on validation data evaluation accuracy. Predict was used to verify the rotation Angle of the image.

YPredicted = predict(net,XValidation);
Copy the code

Evaluating performance

Evaluate model performance by calculating the following values:

  1. The percentage of the predicted value within the acceptable margin of error
  2. Root mean Square Error of Predicted rotation Angle and actual Rotation Angle (RMSE)

The prediction error between the predicted rotation Angle and the actual rotation Angle is calculated.

predictionError = YValidation - YPredicted;
Copy the code

Calculate the number of predicted values within the acceptable error bounds of the actual Angle. Set the threshold to 10 degrees. Calculate the percentage of predicted values within this threshold range.

thr = 10;
numCorrect = sum(abs(predictionError) < thr);
numValidationImages = numel(YValidation);

accuracy = numCorrect/numValidationImages
Copy the code

The root mean square error (RMSE) was used to measure the difference between the predicted and actual rotation angles.

squares = predictionError.^2;
rmse = sqrt(mean(squares))
Copy the code

Displays a residual boxplot for each numeric class

The boxplot function requires a matrix in which the columns correspond to the residuals of each numeric class.

The validation data were grouped into digital categories 0-9, with each group containing 500 samples. 0 Use 0 to group residuals by number type.

ResidualMatrix = reshape (predictionError, 500, 10);Copy the code

Each column of the residualMatrix corresponds to the residual of each number. Use BoxPlot (Statistics and Machine Learning Toolbox) to create residual boxplots for each number.

figure
boxplot(residualMatrix,...
    'Labels',{'0','1','2','3','4','5','6','7','8','9'})
xlabel('Digit Class')
ylabel('Degrees Error')
title('Residuals')
Copy the code

The number classes with the highest accuracy have mean values close to zero and small variances.

You can use the functions in the Image Processing Toolbox to align numbers and display them together. Use imRotate (Image Processing Toolbox) to rotate 49 sample numbers based on the predicted rotation Angle.

idx = randperm(numValidationImages,49);
for i = 1:numel(idx)
    image = XValidation(:,:,:,idx(i));
    predictedAngle = YPredicted(idx(i));  
    imagesRotated(:,:,:,i) = imrotate(image,predictedAngle,'bicubic','crop');
end
Copy the code

Displays raw numbers and corrected rotated numbers. You can use the Montage (Image Processing Toolbox) to display numbers on the same Image.

Figure subplot(1,2,1) montage(XValidation(:,:,:,idx)) title('Original') subplot(1,2,2) montage(imagesRotated) title('Corrected')Copy the code