MATLAB: How to “read” all of the images to use them in a loop

Deep Learning Toolboximage processingneural networksreading imagesstring variables

I am trying to classify my data set of 25,000 images and produce an accuracy rate, but I need to modify alexnet's categories to be more generalized and include all dog and cat categories to compare match rates with the basic label of my images of dogs and cats.
The following code works for my images in the dataset, but it is only 1 image at a time.
% Load AlexNet
net = alexnet
% Read the image to classify
%I = imread('doge.jpg');
I = imread('doge.jpg');
% Adjust size of the image
I = imresize(I,[227 227]);
% Classify the image using AlexNet
label = classify(net, I);
new_label = char(label);
fid = fopen('dogs.txt');
while ~feof(fid)
tline = fgetl(fid);
k = contains(tline,char(label));
if k==true
new_label = 'dog';
break
end
end
fid = fopen('cats.txt');
while ~feof(fid)
tline = fgetl(fid);
k = contains(tline,char(label));
if k==true
new_label = 'cat';
break
end
end
% Show the image and the classification results
figure
imshow(I)
text(10,20,char(new_label),'Color','red')
I turned my data set into a data store and I received the error message below:
Error using contains (line 53)
Search term must be a string array, character vector, or cell array of character vectors.
line 53 is in regards to the k = contains(tline,char(label)); code within the loop in the code provided. The read function line modifies the image to be 227×227 and in color as required to use in alexnet.
convnet=alexnet;
Catdogs2ds = imageDatastore('./train/train','IncludeSubfolders',true,'LabelSource','foldernames');
Catdogs2ds.ReadFcn = @(filename)readAndPreprocessImage(filename);
label = classify(convnet, Catdogs2ds);
new_label = char(label);
fid = fopen('dogs.txt');
while ~feof(fid)
tline = fgetl(fid);
k = contains(tline,char(label));
if k==true
new_label = 'dog';
break
end
end
fid = fopen('cats.txt');
while ~feof(fid)
tline = fgetl(fid);
k = contains(tline,char(label));
if k==true
new_label = 'cat';
break
end
end
Why is my predicted variable (label) no longer a string when I use a datastore?

Best Answer

fgetl() returns a numeric value at end of file. while ~feof(fid) cannot be counted on to reliably predict that end of file is about to occur.