Clear Filters
Clear Filters

Apply non-image data to CNN

15 views (last 30 days)
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.

Accepted Answer

Shivam Gupta
Shivam Gupta on 4 Mar 2019
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:
Shivam Gupta
Shivam Gupta on 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 on 5 Mar 2019
I got it now. I will try it and let you know. Thanks Sir for your time and help.

Sign in to comment.

More Answers (3)

Zia Uddin Ahmed Zihan
Zia Uddin Ahmed Zihan on 21 Jun 2019
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?

Abdullah Bittar
Abdullah Bittar on 25 May 2020
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 on 20 Apr 2021
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 Comment
HayderMU on 2 Nov 2022
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!