How do I integrate a trained neural network into an application

12 views (last 30 days)
I am trying to compare an uploaded image to a trained neural network in an application. I have been ablet o upload an image using a push button, however, I am not able to have that image analyzed by the trained neural network. Below is the code for the application and I am unsure where the failure is occuring. Please let me know what you think would be the best way to go about this. Thank You
methods (Access = private)
function results = startupfunc(app)
x_net = load("xraycat.mat");
x_net = app.net.net;
end
end
methods (Access = private)
% Button pushed function: UploadImageButton
function UploadImageButtonPushed(app, event)
[File_Name, Path_Name] = uigetfile('PATHNAME');
imshow([Path_Name,File_Name],'Parent',app.UIAxes);
end
% Button pushed function: AnalyzeImageButton
function AnalyzeImageButtonPushed(app, event)
[YPred, Scores] = predict(app.net, [File_name, Path_Name]);
imshow([YPred, Scores],'Parent',app.UIAxes2);
end
end

Accepted Answer

Kojiro Saito
Kojiro Saito on 4 Mar 2020
You need add a property to pass that variable between functions.
In Code Browser panel in Code View, click Properties and click plus icon.
In properties, you can add a property, for example, variable name is filepath.
properties (Access = private)
filepath % file path
net % Trained Neural Network
end
After that, change your code as below.
function startupfunc(app)
app.net = load("xraycat.mat");
app.net = app.net.net;
end
% Button pushed function: UploadImageButton
function UploadImageButtonPushed(app, event)
[File_Name, Path_Name] = uigetfile('PATHNAME');
app.filepath = fullfile(Path_Name,File_Name);
imshow(app.filepath,'Parent',app.UIAxes);
end
% Button pushed function: AnalyzeImageButton
function AnalyzeImageButtonPushed(app, event)
imds = imageDatastore(app.filepath);
[YPred, Scores] = classify(app.net, imds);
% or,
% YPred = predict(app.net, imds);
% or, for SVM classification
% im = imread(app.filepath);
% featureLayer = 'fc7'; % For AlexNet
% imageFeatures = activations(app.net, im, featureLayer);
% [YPred, Scores] = predict(app.net, imageFeatures);
imshow([YPred, Scores],'Parent', app.UIAxes2);
end
I'm not sure which predict function you're using because there are some functions in MATLAB such as
But none of them can allow image's file path, so I changed your second input argument to imageDatastore in the above code.
  12 Comments
Patrick Kuczwara
Patrick Kuczwara on 6 Mar 2020
Now since this works, I would like to output the top 5 proabable matches with their categorical lable. If possible, I would like to output standard images for each one that is matched. Please let me know if this is possible. Thanks
Malina Balint
Malina Balint on 22 Apr 2021
I also had the exact same problem with my application and I followed the steps above, but I don't understand how can I create an interface with buttons that works, because from what I've understood so far if I put my function in app compiler it creates a application without an interface an so I still need an interface for my application. Thanks in advance

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!