Error using trainNetwork. Unable to read file.
3 次查看(过去 30 天)
显示 更早的评论
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)
3 个评论
采纳的回答
Catalytic
2019-4-2
编辑:Catalytic
2019-4-2
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 个评论
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
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
2019-4-2
Maybe it's a corrupt file. Are you able to open CNN1.mat simply by using load?
3 个评论
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!