Can not know how to use minibatchqueue for a deep learning network that takes input as 4-D numbers and output 3 numbers through fully-connected layer.

There is a MATLAB example that uses minibatchqueue for input date as 4-D (image) and output as categorical. What I need is to update this example to accept output to be three numberical values (through a 3-fully connected layer).
The MATLAB example is:
[XTrain,YTrain] = digitTrain4DArrayData;
dsX = arrayDatastore(XTrain,IterationDimension=4);
dsY = arrayDatastore(YTrain);
dsTrain = combine(dsX,dsY);
classes = categories(YTrain);
numClasses = numel(classes);
net = dlnetwork;
layers = [
imageInputLayer([28 28 1],Mean=mean(XTrain,4))
convolution2dLayer(5,20)
reluLayer
convolution2dLayer(3,20,Padding=1)
reluLayer
convolution2dLayer(3,20,Padding=1)
reluLayer
fullyConnectedLayer(numClasses)
softmaxLayer];
net = addLayers(net,layers);
net = initialize(net);
miniBatchSize = 128;
mbq = minibatchqueue(dsTrain,...
MiniBatchSize=miniBatchSize,...
PartialMiniBatch="discard",...
MiniBatchFcn=@preprocessMiniBatch,...
MiniBatchFormat=["SSCB",""]);
function [X,Y] = preprocessMiniBatch(XCell,YCell)
% Extract image data from the cell array and concatenate over fourth
% dimension to add a third singleton dimension, as the channel
% dimension.
X = cat(4,XCell{:});
% Extract label data from cell and concatenate.
Y = cat(2,YCell{:});
% One-hot encode labels.
Y = onehotencode(Y,1);
end
Again, what I need is to know how to modify the code to accept three regression values at fully connected output layer.
Actually, I tried alot and alot without success. I think the main trick is the update that should be done inside this function: preprocessMiniBatch (defined above).
Thanks

5 Comments

Hi Nader,
You will need to adjust the network layers to accommodate regression output instead of classification. Replace the fullyConnectedLayer(numClasses) and softmaxLayer with layers suitable for regression tasks. In this case, you can use a fully connected layer with three output nodes for regression. Then, adjust the preprocessing function to handle regression labels instead of one-hot encoded categorical labels. Modify the preprocessMiniBatch function to concatenate the regression labels properly. Remember to adjust the loss function and evaluation metrics accordingly for regression tasks. Now, if you follow these steps, you should be able to figure out by making modifications to your code to accomplish your goal.

Hi Nader,

In case, you could not figure out, I am providing code snippets for network architecture modification and update preprocessing function

Network Architecture Modification

layers = [

    imageInputLayer([28 28 1], 'Mean', mean(XTrain, 4))
    convolution2dLayer(5, 20)
    reluLayer
    convolution2dLayer(3, 20, 'Padding', 1)
    reluLayer
    convolution2dLayer(3, 20, 'Padding', 1)
    reluLayer
    fullyConnectedLayer(3) % Three output nodes for regression
    regressionLayer]; % Use regressionLayer for regression tasks

Update Preprocessing Function

function [X, Y] = preprocessMiniBatch(XCell, YCell)

    % Extract image data from the cell array and concatenate over the fourth dimension
    X = cat(4, XCell{:});
    % Extract and concatenate regression labels
    Y = cat(2, YCell{:});

end

Goodluck!

Thank you Umar, but there still exists an issue and the code can not run as attached in the figure.
Also, after some research I found that. the regressionLayer is not supported in dlnetwork, please refer to this question's answer:
I still have issue :D, if you find the solution please reply, waiting for your help, thanks alot.
Hi Umar,
I have solved the issue. As I mentioned, the only issue with your solution is regressionLayer. If you remove it, all will be fine. I obtained this idea from this simulation:
where classification task is converted to regression one.
Have a nice day and thanks alot for your time.
I apologize Nader, attending meeting with software consultants for a large scale project. Glad to know your problem is resolved.

Sign in to comment.

Answers (0)

Asked:

on 21 Jul 2024

Commented:

on 23 Jul 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!