Convolutional neural networ for classification problem with matrices instead of images

3 views (last 30 days)
Hi all. I want to do something similar to this example (you can find the example here: https://it.mathworks.com/help/deeplearning/gs/create-simple-deep-learning-classification-network.html ), a image classification problem using cnn, but with matrices instead of images. This is the example's code:
digitDatasetPath = fullfile(matlabroot,'toolbox','nnet','nndemos', ...
'nndatasets','DigitDataset');
imds = imageDatastore(digitDatasetPath, ...
'IncludeSubfolders',true,'LabelSource','foldernames');
numTrainFiles = 750;
[imdsTrain,imdsValidation] = splitEachLabel(imds,numTrainFiles,'randomize');
inputSize = [28 28 1];
numClasses = 10;
layers = [
imageInputLayer(inputSize)
convolution2dLayer(5,20)
batchNormalizationLayer
reluLayer
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'MaxEpochs',4, ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(imdsTrain,layers,options);
In my case I have 4x4 matrices of complex scalars saved in .csv files (each file contains one 4x4 matrix) store in more folders (folder1 has 10 .csv files, folder2 has 10 .csv files and so on...).
Since I'm dealing with .csv files i have to use the "tabularTextDatastore" command to create the datastore. The problem here is that with this command i can't assign category/label to my matrices (with the command "imageDatastore" this assignment is possible thanks to the command "...'LabelSource','foldernames');".
How can i solve this problem? How can I assign a single category/label to a whole 4x4 matrix?
I also post the code I've writtend so far that gives me this error due to the problem I've just explained. The error:
Error using trainNetwork.
Invalid training data. For image, sequence-to-label, and feature classification tasks, responses must be categorical.
Error in cnn_matrix_script (line 112)
net = trainNetwork(data_train, layers, options)
My code:
data_store = tabularTextDatastore(folder_path,'IncludeSubfolders',true); % Create the data_store with the .csv files contained in the subfolders
data_train = partition(data_store,2,1); % data_train contains the 50% of the files in data_store
data_validation = partition(data_store,4,3); % data_validation contains the 25% of the files in data_store
data_test = partition(data_store,4,4); % data_test contains the last 25% of the files in data_store
layers = [
imageInputLayer([4 4 1],'SplitComplexInputs',true)
convolution2dLayer(2,10,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',1)
convolution2dLayer(2,10,'Padding','same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(7)
softmaxLayer
classificationLayer
];
options = trainingOptions("adam", ...
'InitialLearnRate',0.01, ...
'MaxEpochs',100, ...
'Shuffle','every-epoch', ...
'ValidationData',data_validation, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(data_train, layers, options);
Sorry for the long post and thanks for the answers.

Answers (1)

Matt J
Matt J on 17 May 2023
Since I'm dealing with .csv files i have to use the "tabularTextDatastore" command to create the datastore.
No, just use an imageDataStore, but set the ReadFcn property to something that can read in your .csv data and re-organize it as an image.

Community Treasure Hunt

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

Start Hunting!