How do I integrate a trained neural network into an application

16 ビュー (過去 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 件のコメント
Patrick Kuczwara
Patrick Kuczwara 2020 年 3 月 6 日
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 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 件)

カテゴリ

Help Center および File ExchangeGetting Started with Microsoft .NET についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by