processImagesMNIST doesn't give the right data format for trainNetwork

6 views (last 30 days)
Loading mnist:
According to the dataset for DL,
I load MNIST by this:
filenameImagesTrain = 'train-images-idx3-ubyte.gz';
filenameLabelsTrain = 'train-labels-idx1-ubyte.gz';
XTrain = processImagesMNIST(filenameImagesTrain);
YTrain = processLabelsMNIST(filenameLabelsTrain);
By running whos XTrain and YTrain,
XTrain size: 28 28 1 6000, class: dlarray, YTrain size: 60000 1, class: categorical.
Training network
I define a layers as the Convolutional neural network.
When I run the training,
net = trainNetwork(XTrain, YTrain, layers, options);
It throws the error:
Invalid 2-D image training data. Specify image data as a 3-D numeric array containing a single image, a 4-D numeric array containing multiple images, a datastore, or a table containing image file paths or images in the first column.
But XTrain is indeed 4-d array.
What's the problem here?
  1 Comment
Runcong Kuang
Runcong Kuang on 31 Jul 2022
I use
XTrain = extractdata(XTrain)
to convert dlarray to numerical array.
And then it works.
But I believe dlarray is advanced for doing DL. If I convert it back to do the DL, I think it's not efficient.
What's the best way to make use of the dlarray directly? Any toolbox?
Thanks.

Sign in to comment.

Answers (1)

Aman
Aman on 12 Sep 2023
Hi Runcong,
It is my understanding that you are having issues while loading the data for model training.
The “processImagesMNIST” function returns a “dlarray”, and the “trainNetwork” method does not accept “dlarray” as an input parameter. In order to resolve the error, you need to convert the “dlarray” into a numerical array using the “extractdata” function and then pass the numerical array to the “trainNetwork” function.
You can convert the “dlarray” to a numerical array in the following manner:
XTrain = extractdata(XTrain);
Please refer to the following links to know more about the “trainNetwork” function and the list of the functions that support “dlarray”:
Hope this helps and resolves your error!
Regards,
Aman Mehta

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!