Question about imageDatastore input
古いコメントを表示
Hi, I am using matlab neural network toolbox to do image classification. But what we have as train data now is .mat files instead of .jpg files. Every .mat file contains a 3D double matrix.
One way I am doing now is that for a .mat file I use imwrite function to transfer it into an image.And then use the standard way to input the data
trainImages = imageDatastore('DataImage\224M225\TrJPG','IncludeSubfolders',true,'LabelSource','foldernames');
trainImages.ReadFcn = @(loc)imresize(imread(loc),[224,224]);
But eventually what is used in the neural network is just a 3D matrix, is there any way I can directly input my .mat files without saving it as an image first? (because imwrite function makes the matrix different from the original one, lots of information is lost)
4 件のコメント
Rui Xiang
2018 年 1 月 12 日
Rui Xiang
2018 年 1 月 12 日
Krishna Bindumadhavan
2018 年 1 月 18 日
編集済み: Krishna Bindumadhavan
2018 年 1 月 18 日
Did you try using the nnstart command by typing
>> nnstart
at the MATLAB command line?
In case you are trying to solve a classification problem, you can select Pattern recognition and import data from a variety of formats including .mat files.
When you say your input data is a three dimensional matrix of doubles, could you clarify exactly how the data is structured , i.e how are the predictors referenced in this case?
Rui Xiang
2018 年 1 月 22 日
回答 (1 件)
Raphael Ruschel
2018 年 10 月 5 日
Hello, I had a similar problem and solved it like this:
train_images = imageDatastore(train_path,'IncludeSubfolders',true,'FileExtensions','.mat','ReadFcn',@loadmydata);
function data = loadmydata(filename)
load(filename)
end
On my case, I had my matrices saved as 'data' inside my .mat so I only needed to call load(filename)
9 件のコメント
Sean de Wolski
2018 年 10 月 5 日
編集済み: Sean de Wolski
2018 年 10 月 5 日
S = load(filename);
data = S.data;
Would recommend this because it's more explicit.
onur erdem korkmaz
2019 年 2 月 9 日
Hi Raphael Ruschel
I had the same problem and solved thanks to you
I have another problem. I have lots of .mat file in imageDatastore and i want to create a CNN network. How can i do it ?
In matlab CNN exercise (Create Simple Deep Learning Network for Classification), imageDatastore have .jpeg data so it gives an error when using .mat format.
How to create CNN architecture with mat files ?
Is there an example?
Raphael Ruschel
2019 年 2 月 9 日
Hi,
From my experience, it is the same thing as on that example exercise but the only difference is that you will be using a custom ReadFcn to read your data from the .mat files instead of .jpg.
For example, you would be doing simething like this:
imds = imageDatastore(digitDatasetPath, ...
'IncludeSubfolders',true,'LabelSource','foldernames','FileExtensions','.mat','ReadFcn',@loadmydata);
Then you define your layers and trainingOptions just like you normally would and proceed to train your network.
You just have to make sure that your InputImageLayer is the same size of the data you are reading from your .mat files
onur erdem korkmaz
2019 年 2 月 10 日
編集済み: onur erdem korkmaz
2019 年 2 月 10 日
Thank you for your answer
My code is in here;
DatasetPath = fullfile(matlabroot,'toolbox','mydata3');
imds = imageDatastore(DatasetPath,'IncludeSubfolders',true,'FileExtensions','.mat','LabelSource','foldernames');
numTrainFiles = 2000;
[imdsTrain,imdsValidation] = splitEachLabel(imds,numTrainFiles,'randomize');
layers = [
imageInputLayer([10 11 1]) % my data size 10x11
convolution2dLayer(3,3,'Padding',1)
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(2,2,'Padding',1)
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,2,'Padding',1)
batchNormalizationLayer
reluLayer
fullyConnectedLayer(2)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'MaxEpochs',10, ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(imdsTrain,layers,options);
on the last line I get the following error
Error using trainNetwork (line 154)
Unable to read file: '/Applications/MATLAB_R2018a.app/toolbox/mydata3/0/nop300_100.mat'.
Error in my_cnn (line 73)
net = trainNetwork(imdsTrain,layers,options);
Caused by:
Error using matlab.io.datastore.ImageDatastore/read (line 75)
Unable to read file: '/Applications/MATLAB_R2018a.app/toolbox/mydata3/0/nop300_100.mat'.
Error using matlab.io.datastore.splitreader.WholeFileCustomReadSplitReader>iErrorOnReadFcn (line 161)
Error using ReadFcn @readDatastoreImage function handle for file
/Applications/MATLAB_R2018a.app/toolbox/mydata3/0/nop300_100.mat.
Error using imread>get_format_info (line 491)
Unable to determine the file format.
Error in imread (line 354)
fmt_s = get_format_info(fullname);
Error in readDatastoreImage (line 12)
data = imread(filename);
What should I do ?
onur erdem korkmaz
2019 年 2 月 10 日
i save your loadmydata function on my current folder
and when i run your code i get the following error;
Error using trainNetwork (line 154)
Output argument "data" (and maybe others) not assigned during call to "loadmydata".
Caused by:
Error using matlab.io.datastore.ImageDatastore/read (line 75)
Error using ReadFcn @loadmydata function handle for file
/Applications/MATLAB_R2018a.app/toolbox/mydata3/0/nop300_10019.mat.
Output argument "data" (and maybe others) not assigned during call to "loadmydata".
Raphael Ruschel
2019 年 2 月 10 日
You forgot to pass the 'ReadFcn' argumento to your call to ImageDatastore, it should be
imds = imageDatastore(DatasetPath,'IncludeSubfolders',true,'FileExtensions','.mat','LabelSource','foldernames','ReadFcn',@loadmydata);
The other error using the loadmydata function, it seems that the output variable 'data' never gets assigned. What is the name of the variable saved inside your '.mat' files? That code I poster will only work if the name is 'data'.
I recommend you changing that function as Sean de Wolski suggested
S = load(filename);
data = S.varNameInsideMatFiles;
onur erdem korkmaz
2019 年 2 月 10 日
ohh sory i add 'ReadFcn' in my code but i didn't add it here in my code.
i have a two folders. The name of the mat files in the first folder is respectively p300_1, p300_2,... and second folders mat file names nop300_1, nop300_2,...
What is the name of the variable saved inside your '.mat' files? : chance p300_1, p300_2, ..... p300_1020 and second folder nop300_1, nop300_2,..... nop300_1020
Raphael Ruschel
2019 年 2 月 11 日
I recommend you rename the variables inside your .mat in a way that everyone has the same name, as this would make it easier to load the variables using the loadmydata function
khadicha amonova
2019 年 3 月 19 日
I used so this code as you corrected, but error occured
Error using training option
ImageDatastore has no labels.
My mat file is consist of 1x1struct with Data 125 x60000 double and Labels 1x125 cell.
Did i make any error?
please help
カテゴリ
ヘルプ センター および File Exchange で Deep Learning Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!