MATLAB Answers

Error using trainNetwork. Unable to read file.

조회 수: 12(최근 30일)
Hassan Ashraf
Hassan Ashraf 2019년 4월 2일
편집: Max 2021년 1월 31일
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일
편집: 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)),___);
  댓글 수: 11
Max
Max 2021년 1월 31일
imds = imageDatastore(location, 'FileExtensions', '.mat', 'IncludeSubfolders',1,...
'LabelSource','foldernames','ReadFcn',@matReader);
function S = matReader(filename)
load(filename);
end

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Matt J
Matt J 2019년 4월 2일
Maybe it's a corrupt file. Are you able to open CNN1.mat simply by using load?
  댓글 수: 1
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.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by