How can I run a trained CNN for classification of a batch of images?
3 次查看(过去 30 天)
显示 更早的评论
G'day,
I've trained an inception_V3 network using deepNetworkDesigner to classify my images into 2 classes. I've exported this trained network to the workspace and want to now run it on a batch of images.
For these new test images I loaded an entire folder on 100 images onto an imageDatastore object and resized them to 299x299x3 using an augmentedImageDatastore. I needed my newly trained inception_V3 to run on this datastore of 100 images and provide me with labels for each image. I'm guessing the 'predict' or 'classify' commands need to be used but am not able to figure out how to apply these to a datastore.
Also is it possible to know which test image received which prediction label? Is there a way to store the predictions so that the test images can later be separated according to the predictions by the CNN?
Would like to know the syntax/method needed for such batch classification.
Happy to provide more context if required. Thanks!
0 个评论
采纳的回答
V Sairam Reddy
2022-9-9
I understand that you are trying to predict all the test images at once and access their corresponding labels.
Please follow the below mentioned code :
imdsTest = imageDatastore(DatasetPath,IncludeSubfolders=true);
% YTest contains all the predicted labels
YTest = classify(trained_net,imdsTest);
% Accessing Image present at 1st index
index = 1;
% Accessing one Image and its Corresponding label
Image_Data = imread(imdsTest.Files{index});
Predicted_Label = YTest(index);
% Accessing all the Images and corresponding labels
All_Images = readall(imdsTest)
Image_at_Index = All_Images{index};
Corresponding_Label = YTest(index);
Please refer to "Classify Images using Trained Convolutional Neural Network" subsection to know more about "Classify" function and "Specify Images to read" subsection to know more about "Image Datastore" object
3 个评论
V Sairam Reddy
2022-9-14
Hey Rishi,
I understand that you are trying to separate the images into two separate folders depending on their prediction labels. Please follow the below mentioned code :
Total_No_Images = numel(auimds.Files);
Total_No_Classes = 2;
% Make 2 folders named 'PredictedAsClass0' and 'PredictedAsClass1'
for i = 0:Total_No_Classes-1
mkdir(strcat('PredictedAsClass',num2str(i)));
end
% Get the working directory path
working_directory = cd;
% For every image
for i = 1:Total_No_Images
% If prediction is 0, save the image in 'PredictedAsClass0' folder
if string(YTest(i)) == "0"
% Get the image name
[filepath,name,ext] = fileparts(auimds.Files{i});
% Read the image
image = imread(auimds.Files{i});
% Save the image with same name in required folder
imwrite(image,strcat(working_directory, '\', 'PredictedAsClass', string(YTest(i)), '\', name, ext));
end
% Similarly for class 1
if string(YTest(i)) == "1"
[filepath,name,ext] = fileparts(auimds.Files{i});
image = imread(auimds.Files{i});
imwrite(image,strcat(working_directory, '\', 'PredictedAsClass', string(YTest(i)), '\', name, ext));
end
end
After the code execution, 2 folders named 'PredictedAsClass0' and 'PredictedAsClass1' are created in the working directory. 'PredictedAsClass0' folder contains all the images whose prediction label is class 0 and vice versa.
更多回答(0 个)
另请参阅
类别
在 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!