I am trying to train a CNN with a numerical data set. Once the input data is not an image format. Assuming my data has a size of 1850*11 matrix. With a 1850*1 label (for output). In libsvm I can use it directly and in the Neural Network Toolbox (nnstart) as well.
However, in case I want to use a CNN network. The only training function is "trainNetwork" it could only support image as an input. So how could I train the CNN with my data on Matlab?
Any simple example will be appreciated.
Thanks to all of in advance.
Gad

 Réponse acceptée

Shivam Gupta
Shivam Gupta le 4 Mar 2019

2 votes

You can read in your data using the function "imageDatastore" with a custom read function.
For example,
nonImageData = imageDatastore(pathToDataFiles, 'IncludeSubfolders', true, 'LabelSource', 'foldernames', 'FileExtensions', '', 'ReadFcn', @customreader);
You can then use 'nonImageData' as you would normally use an "imageDatastore" - for example, as input to "splitEachlabel" or "trainNetwork".
Note: Be careful to watch for negative numbers, however, as some layers (ex. ReLULayer) will convert all negative values to zero.
For more information, see:
For more Related MATLAB Answers see:

9 commentaires

Gadelhag M Omar Mohmed
Gadelhag M Omar Mohmed le 4 Mar 2019
Thanks for your time to answer my question. To clarify my question, I have two mat files (Training_F and Testing_F). I have 22 columns in each; the first 21 columns are my input and the last column (22nd) is my target, which contains 7 classes. Now, I want to use the function you mentioned in your answer to read the data in the Training_F.mat to be used as an input to my CNN.
Please find the attached two files (Training_F and Testing_F).
Thanks in advance.
Gad
Create a readFcn1.m file and add the following function
function I = readFcn1(filename)
% Load data and get matrices from the structure
I = load(filename);
I = I.data;
end
Copy Training_F.mat file first 21 columns to new file say 'signal.mat' and change the name of variable to 'data'. Add this file inside a directory. Update location as the absolute path to this directory.
Create another script file and add the following code to it
location = 'absolutepathtofile';
imds = imageDatastore(location, 'FileExtensions', '.mat', 'IncludeSubfolders',1, ...
'LabelSource','foldernames');
countEachLabel(imds);
trainingDS = imds;
% Convert labels to categoricals
trainingDS.Labels = categorical(trainingDS.Labels);
trainingDS.ReadFcn = @readFcn1;
layers = [ ...
imageInputLayer([210 21 1])
convolution2dLayer(5,20)
reluLayer
maxPooling2dLayer(2,'Stride',2)
fullyConnectedLayer(1)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'MaxEpochs',20,...
'InitialLearnRate',1e-4, ...
'Verbose',false, ...
'Plots','training-progress');
trainedNet = trainNetwork(imds,layers,options);
You can edit layers and options parameters for better train your data.
Gadelhag M Omar Mohmed
Gadelhag M Omar Mohmed le 5 Mar 2019
That is great, thank you very much for the given answer.
Just the last question, onec I created "signal.mat " that contains the 21 columns. What do you mean by "change the name of variable to 'data' ". Do I need to create a varible inside the signal.mat file and I named it (data)?
I am sorry for keep asking you questions, I am still new to Matlab. Thanks in advance Sir.
Gad
Shivam Gupta
Shivam Gupta le 5 Mar 2019
load signal.mat file in workspace. Inside the workspace rename the Training_F to data. This is required for getting data in variable I as we define I as I=I.data
I created a new script and named it as (prepair_data.m) and I put the code as :
clc, clear
load 'Training_F.mat';
signal= Training_F(:,1:21);
data= Training_F;
Then I save the data.mat in a directory as :
location = 'C:\Users\N0670029\Dropbox\Gadelhag\Matlab\CNN\try_ccn\Train';
And then, I created another script in the same directory with the (prepair_data.m) and named it as (save_mat_try.m) and put the code you mentioned it as :
location = 'C:\Users\N0670029\Dropbox\Gadelhag\Matlab\CNN\try_ccn\Train';
imds = imageDatastore(location, 'FileExtensions', '.mat', 'IncludeSubfolders',1, ...
'LabelSource','foldernames');
countEachLabel(imds);
trainingDS = imds;
% Convert labels to categoricals
trainingDS.Labels = categorical(trainingDS.Labels);
trainingDS.ReadFcn = @readFcn1;
layers = [ ...
imageInputLayer([210 21 1])
convolution2dLayer(5,20)
reluLayer
maxPooling2dLayer(2,'Stride',2)
fullyConnectedLayer(1)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'MaxEpochs',20,...
'InitialLearnRate',1e-4, ...
'Verbose',false, ...
'Plots','training-progress');
trainedNet = trainNetwork(imds,layers,options);
but, I getting this error :
Error using trainNetwork (line 154)
The training images are of size 210x22x1 but the input layer expects images of size 210x21x1.
Error in save_mat_try (line 22)
trainedNet = trainNetwork(imds,layers,options);
Caused by:
Error using nnet.internal.cnn.util.NetworkDataValidator/assertCorrectDataSizeForInputLayer (line 262)
The training images are of size 210x22x1 but the input layer expects images of size 210x21x1.
Any suggestions, please.
Thanks in advance
Gad
Shivam Gupta
Shivam Gupta le 5 Mar 2019
Modifié(e) : Shivam Gupta le 5 Mar 2019
Have you defined function 'readFcn1'?
Also check that the 'data' dimension is 210x21 as from the error it looks like trainNetwork is receiving input of size 210x22 but it's expected size is 210x21 which is defined here:imageInputLayer([210 21 1]).
Gadelhag M Omar Mohmed
Gadelhag M Omar Mohmed le 5 Mar 2019
Hi
Yes I did define the readFcn1. Regarding the size of "data" is 210*22 as I renamed the original file (Training_F.mat) file to be my "data".
Once I changed the imageInputLayer([210 21 1]) to be imageInputLayer([210 22 1]) instead.
It is working with no error. Is what I have done by chaninging the imageInputLayer to be ([210 22 1]) I am doing somthing wrong? as I gave the CNN the all inputs and the output as weel.
Shivam Gupta
Shivam Gupta le 5 Mar 2019
Hi Gad,
Have you followed this "Copy Training_F.mat file first 21 columns to new file say 'signal.mat' and change the name of variable to 'data'."
For your training only first 21 columns are required not the 22nd column.
Gadelhag M Omar Mohmed
Gadelhag M Omar Mohmed le 5 Mar 2019
Hi
I got it now. I will try it and let you know. Thanks Sir for your time and help.
Regards
Gad

Connectez-vous pour commenter.

Plus de réponses (3)

Zia Uddin Ahmed Zihan
Zia Uddin Ahmed Zihan le 21 Juin 2019

0 votes

Hi Shivam,
I am also struggling with the same. My problem is I want to run the numerical data alongside each image to improve image classification accuracy. I ran my code according to your suggestion. However, how am I supposed to know if MATLAB is taking the image and numerical data correctly simultaneously?
Thanks,
Zia
Abdullah Bittar
Abdullah Bittar le 25 Mai 2020

0 votes

Hi Guys,
I am also trying to train CNN using non image data. I followed the steps that were published by Shivam Gupta but was not seccussful. I am having the following problem.
Error using trainNetwork (line 150)
The training images are of size 1x1x1 but the input layer expects images of size 137x14x1.
Error in save_mat_try (line 22)
trainedNet = trainNetwork(imds,layers,options);
I sparated my input datafrom my output (target data) into two separate mat files. My input data has 14 colunms and 137 rows. Therefore it is the size of [137 14 1].
I have my imageInputLayer as imageInputLayer([137 14 1]
I tried searching for a solution for my probelm but i didnt find any solution. I would truley appreciate the help.
Thank you
Omar Almasarani
Omar Almasarani le 20 Avr 2021

0 votes

Hi,
I followed the answer of Shivam and it run but this in only input data without target. how do you define the target data?

1 commentaire

HayderMU
HayderMU le 2 Nov 2022
Exactly
I followed the code. The code NEVER LOADS THE CLASSES.
So it's training just on the fly. The last column which contains the target classes is comlpetely excluded. THe network is training on itself just to do nothing bcause no target classes are loaded.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by