MATLAB Answers

How do I integrate a trained neural network into an application

조회 수: 53(최근 30일)
Patrick Kuczwara
Patrick Kuczwara 2020년 3월 3일
댓글: Malina Balint 2021년 4월 22일
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

채택된 답변

Kojiro Saito
Kojiro Saito 2020년 3월 4일
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
Malina Balint
Malina Balint 2021년 4월 22일
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

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by