Integrating uigetfile to CNN in general

조회 수: 3 (최근 30일)
David Glendale Ardenaso
David Glendale Ardenaso 2021년 12월 2일
답변: Reshma Nerella 2021년 12월 13일
I'm trying to integrate uigetfile function for the given CNN example I found so it doesn't have to be user-specific and it doesn't need to be adjusted by different users but when I tried it, I get errors. Here's the code I attempted with it:
[fileInput, pathInput] = uigetfile('*.png','MultiSelect','on');
[fileValid, pathValid] = uigetfile('*.png','MultiSelect','on');
numTrainfiles =numel(fileInput);
numValidfiles = numel(fileValid);
for k = 1:length(numTrainfiles)
baseFileNameInput{k} = fileInput{k};
fullFileName{k} = fullfile(baseFileNameInput);
imageArrayInput(k,:,:,:) = imread(fullFileName{k});
[~, height{k}, width{k}, colors{k}] = size(imageArrayInput);
end
for l = 1:length(numValidfiles)
baseFileNameValid{l} = fileValid{l};
fullFileName2{l} = fullfile(baseFileNameValid);
imageArrayValid(l,:,:,:) = imread(fullFileNameValid{l});
[~, height{l}, width{l}, colors{l}] = size(imageArrayValid);
end
imageAugmenter = imageDataAugmenter( ...
'RandRotation', [-20,20], ...
'RandXTranslation',[-3,3], ...
'RandYTranslation',[-3,3]);
imageSize = [height{k} width{k} colors{k}];
augimds = augmentedImageDatastore(imageSize,numTrainfiles,'DataAugmentation',imageAugmenter);
[imdsTrain] = splitEachLabel(imds,numTrainfiles,'randomized');
[imdsValidation] = splitEachLabel(imds2,numValidfiles,'randomized');
augimds = augmentedImageDatastore(imageSize,imdsTrain,'DataAugmentation',imageAugmenter);
%definingarchitecture
numClasses = numel(categories(imdsTrain.Labels));
numClasses2 = numel(categories(imdsValidation.Labels));
layers = [
imageInputLayer(imageSize)
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(numClasses)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'MaxEpochs',6, ...
'Shuffle','every-epoch', ...
'MiniBatchSize',64,...
'ValidationData',imdsValidation, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(imdsTrain,layers,options);
YPred = classify(net,imdsValidation);
yvalidation = imdsValidation.Labels;
accuracy = mean(Ypred == yvalidation);
And this is the error that I get:
"Error using imread>parse_inputs (line 504)
The file name or URL argument must be a character vector or string scalar.
Error in imread (line 342)
[source, fmt_s, extraArgs, was_cached_fmt_used] = parse_inputs(cached_fmt, varargin{:});
Error in cnn3 (line 9)
imageArrayInput(k,:,:,:) = imread(fullFileName{k});"
Hoping that anyone could answer this so that I could also integrate uigetfile for my own CNN

답변 (1개)

Reshma Nerella
Reshma Nerella 2021년 12월 13일
Hi,
In this code
fullFileName{k} = fullfile(baseFileNameInput);
imageArrayInput(k,:,:,:) = imread(fullFileName{k});
fullFileName is a cell array of cell arrays.
Two possible fixes for the error:
1.Modify the fullFileName as cell array of character vectors
fullFileName = fullfile(baseFileNameInput);
imageArrayInput(k,:,:,:) = imread(fullFileName{k});
2.Refer the character vector properly
fullFileName{k} = fullfile(baseFileNameInput);
imageArrayInput(k,:,:,:) = imread(fullFileName{k}{1});
Hope this helps!

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by