MATLAB Answers

How do I integrate a trained neural network into an application

30 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

  0 Comments

Sign in to comment.

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.

  11 Comments

Show 8 older comments
Patrick Kuczwara
Patrick Kuczwara on 5 Mar 2020
So I loaded all of the functions into a script that were contained within the app, minust the startup functions, button pushes, etc. As below:
%% Loading Network
app.net = load("xraycat.mat");
app.net = DAGNetwork.loadobj(app.net.net);
% msgbox(sprintf("Network is %s", class(app.net))) % Commented out after proper loading was seen.
%% Retrieving Image
[File_Name, Path_Name] = uigetfile('PATHNAME');
app.filepath = fullfile(Path_Name,File_Name);
%% Analyzing Image
im = imread(app.filepath);
im = imresize(im, [224 224]); % Resize image to GoogleNetwork input size
[YPred, Scores] = classify(app.net, im);
sMax = max(Scores);
frame = insertText(im, [0 5], [char(YPred) ':' num2str(sMax)]); % Insert label text to image
imshow(frame)
When this was run everything worked well and the frame was shown with the score and name on the image. Therefore we know that everything works, however, there is just something that does not add up within the application itself. As I would like to be able to share this with other non-MATLAB computers, how else could a user interface be built?
Kojiro Saito
Kojiro Saito on 5 Mar 2020
A simple way is this:
1. Change the script to function (Insert function at the top) and name the m file as the same as function name.
For example,
myFunc.m
function myFunc()
%% Loading Network
app.net = load("xraycat.mat");
app.net = DAGNetwork.loadobj(app.net.net);
% msgbox(sprintf("Network is %s", class(app.net))) % Commented out after proper loading was seen.
%% Retrieving Image
[File_Name, Path_Name] = uigetfile('PATHNAME');
app.filepath = fullfile(Path_Name,File_Name);
%% Analyzing Image
im = imread(app.filepath);
im = imresize(im, [224 224]); % Resize image to GoogleNetwork input size
[YPred, Scores] = classify(app.net, im);
sMax = max(Scores);
frame = insertText(im, [0 5], [char(YPred) ':' num2str(sMax)]); % Insert label text to image
imshow(frame)
end
Then, from Apps, click Application Compiler (installation and license of MATLAB Compiler are needed) and convert the myFunc.m to exe file which runs with MATLAB Runtime in non-MATLAB users machine.
If the exe file works, it your App Designer app will also work fine, so you can convert your app file (.mlapp) to Standalone application (.exe) or Web Apps (.ctf).
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

Sign in to comment.

More Answers (0)


Translated by