Integrating uigetfile to CNN in general

1 view (last 30 days)
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

Answers (1)

Reshma Nerella
Reshma Nerella on 13 Dec 2021
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!

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!