Error using trainNetwork. Unable to read file.

I am trying to implement CNN on signal's Data. I have a database in which I have 10 folders(Each folder has 12 subfolders). Each file has dimensions 12x2000 which is a .mat file. While running CNN on the above data I am facing below attached error. Can someone help me out?
location = 'C:\Users\AKRA\Desktop\New folder (3)';
imds = imageDatastore(location, 'FileExtensions', '.mat', 'IncludeSubfolders',1, ...
'LabelSource','foldernames');
labelCount = countEachLabel(imds)
img = readimage(imds,1);
size(img)
numTrainFiles = 8;
[imdsTrain,imdsValidation] = splitEachLabel(imds,numTrainFiles,'randomize');
layers = [
imageInputLayer([12 2000 1])
convolution2dLayer(3,8,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(10)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.01, ...
'MaxEpochs',4, ...
'Shuffle','every-epoch', ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(imdsTrain,layers,options);
YPred = classify(net,imdsValidation);
YValidation = imdsValidation.Labels;
accuracy = sum(YPred == YValidation)/numel(YValidation)
Screenshot (9).png

3 件のコメント

Catalytic
Catalytic 2019 年 4 月 2 日
Is there a reason you are using an imagedatastore for what looks like non-image data?
Hassan Ashraf
Hassan Ashraf 2019 年 4 月 2 日
What could be other options?
Catalytic
Catalytic 2019 年 4 月 2 日
編集済み: Catalytic 2019 年 4 月 2 日

サインインしてコメントする。

 採用された回答

Catalytic
Catalytic 2019 年 4 月 2 日
編集済み: Catalytic 2019 年 4 月 2 日

0 投票

I think it is expecting more traditional types of image files like .jpg,.png, etc.. I think for .mat you need to specify a specialized ReadFcn. Maybe this?
imds = imageDatastore(location, 'FileExtensions', '.mat', 'IncludeSubfolders',1, ...
'LabelSource','foldernames',...
'ReadFcn',@(f) getfield(load(f)),___);

16 件のコメント

Hassan Ashraf
Hassan Ashraf 2019 年 4 月 2 日
How can I specify a specialized ReadFcn for my work?
Hassan Ashraf
Hassan Ashraf 2019 年 4 月 2 日
Tried this but still no luck :(
Catalytic
Catalytic 2019 年 4 月 2 日
What was the result?
Hassan Ashraf
Hassan Ashraf 2019 年 4 月 2 日
error.png
Catalytic
Catalytic 2019 年 4 月 2 日
編集済み: Catalytic 2019 年 4 月 2 日
You should debug your ReadFcn, testing it on an isolated file, before submitting it to imagedatastore.
Hassan Ashraf
Hassan Ashraf 2019 年 4 月 5 日
Thank you "Catalytic". It worked and helped me a lot.
Keep helping. God Bless you.
Toqa Am
Toqa Am 2019 年 12 月 26 日
Hello Mr. Hassan Ashraf,
Can you please tell me how you can solve this problem, because I have faced it for a long time and I cannot solve it. The same error appears.
Please tell me as soon as possible.
Toqa Am
Toqa Am 2019 年 12 月 26 日
Moreover, there are another error has been accure,
Error using trainNetwork (line 150)
Conversion to single from struct is not possible.
Error in classificationlayers (line 55)
net = trainNetwork(trainingset,layers,options);
Caused by:
Error using cast
Conversion to single from struct is not possible.
I would be grateful for your help.
Hassan Ashraf
Hassan Ashraf 2019 年 12 月 27 日
Hi Toqa Am..!
I resolved the error by just debugging my ReadFcn function. There was an error of name in the ReadFcn function.
Toqa Am
Toqa Am 2019 年 12 月 27 日
I've also skipped this step, but now the problem is with the second error,
Error using trainNetwork (line 150)
Conversion to single from struct is not possible.
Error in classificationlayers (line 55)
net = trainNetwork(trainingset,layers,options);
Caused by:
Error using cast
Conversion to single from struct is not possible.
can you solve this error???
Max
Max 2021 年 1 月 31 日
編集済み: Max 2021 年 1 月 31 日
imds = imageDatastore(location, 'FileExtensions', '.mat', 'IncludeSubfolders',1,...
'LabelSource','foldernames','ReadFcn',@matReader);
function S = matReader(filename)
load(filename);
end
Walter Roberson
Walter Roberson 2021 年 11 月 17 日
What are you returning from matReader() ?
When you load() a .mat and assign the result to a variable, then the variable will be a struct with one field for each variable loaded from the file. You would need to extract some particular variable to assign to S.
Lan  Anh
Lan Anh 2021 年 12 月 13 日
Can you please tell me how you can solve this problem in detail? I tried but I could not.
Thanks so much in advance
Walter Roberson
Walter Roberson 2021 年 12 月 13 日
You have .mat files with names such as p10_v16_s10.mat .
A .mat file may contain multiple variables, but you will need to pick a particular variable to return.
In your situation, is the name of the variable to read from the file always the same? Of is the name of the variable to read the same as the file name? Or are you certain that the file only contains a single variable?
Lan  Anh
Lan Anh 2021 年 12 月 14 日
The file only contains a single variable and the name of the variable to read from the file is always the same
Walter Roberson
Walter Roberson 2021 年 12 月 15 日
function S = matReader(filename)
data_struct = load(filename);
fields = fieldnames(data_struct);
S = data_struct.(fields{1});
end
This code will handle the case of exactly one variable in the file, no matter what the variable is named.

サインインしてコメントする。

その他の回答 (1 件)

Matt J
Matt J 2019 年 4 月 2 日

0 投票

Maybe it's a corrupt file. Are you able to open CNN1.mat simply by using load?

3 件のコメント

Hassan Ashraf
Hassan Ashraf 2019 年 4 月 2 日
Yes I am able to do that. I tried to run this code multiple times and every time the same error appears with a different file name. Sometimes it says unable to read CNN12.mat and another time it says unable to read CNN19.mat.
john karli
john karli 2021 年 11 月 17 日
hellow Hassan
I need your help. I am also trying to train model on .mat file. I do the same like you but i am facing some issue. I have tried the above code
imds = imageDatastore(location, 'FileExtensions', '.mat', 'IncludeSubfolders',1,...
'LabelSource','foldernames','ReadFcn',@matReader);
and my matReader is
function S = matReader(filename)
S = load(filename);
end
but when i run the following code
net = trainNetwork(imdsTrain,lgraph_1,options);
Error using trainNetwork (line 184)
Conversion to single from struct is not possible.
Caused by:
Error using cast
Conversion to single from struct is not possible.
Please assist i have also attached the image
Hassan Ashraf
Hassan Ashraf 2021 年 11 月 17 日
If its a struct file, then there must be different fields within that struct file. You need to specify which file (field) from that struct file to read. See my code to read and call the struct file
function I=readFcn1(filename)
I=load(filename);
I=I.S;
and on the other hand, this is how I am calling this function
location = 'C:\EMG\Test\Subject5\Day5';
test = imageDatastore(location, 'FileExtensions', '.mat', 'IncludeSubfolders',1, ...
'LabelSource','foldernames',...
'ReadFcn', @readFcn1);

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by