Dear all,
while creating an app using app designer I am not able to compeltelly deploy my trainned CNN. My main issue is wiht the function classify which should be valid with:
[YPred]= classify(app.net,app.imds1);
Where app.net is the trainned CNN, app.imds1 is the imageDatastore where all the images that I want to classify are stored.
The error message that I got is:
Error using classify (line 123)
Requires at least three arguments.
Meaning that the function classify is using a 2nd options instead of the one that I want it to use. Is there a way to make it work? Could it be that I did not completelly unistall Matlab 2018b from my computer? I have 2019a running but it never occurred this error before...
The full code is the following one:
classdef ICapp < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
DistanceEvolution matlab.ui.control.UIAxes
UITable matlab.ui.control.Table
Load matlab.ui.control.StateButton
Predict matlab.ui.control.StateButton
FrameNSpinnerLabel matlab.ui.control.Label
FrameNSpinner matlab.ui.control.Spinner
Save matlab.ui.control.Button
DistanceextrantionappLabel matlab.ui.control.Label
Image matlab.ui.control.Image
Image_2 matlab.ui.control.Image
PLOT matlab.ui.control.UIAxes
DoneLampLabel matlab.ui.control.Label
DoneLamp matlab.ui.control.Lamp
DoneLamp_2Label matlab.ui.control.Label
DoneLamp_2 matlab.ui.control.Lamp
DoneLamp_3Label matlab.ui.control.Label
DoneLamp_3 matlab.ui.control.Lamp
end
properties (Access = private)
imds0;
table;
imds1;
NFrame;
frames;
Name;
net;
predictY;
Info;
% Description
end
methods (Access = private)
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app, NET, example)
app.net=load("net.mat");
example=imageDatastore('example.png');
img = readimage(example,1);
imshow(img,'Parent',app.PLOT);
app.table = readtable('Example.xlsx');
app.UITable.Data=app.table;
end
% Value changed function: Load
function LoadValueChanged(app, event)
DataLocation=uigetdir('Location of the data');
app.imds0 = imageDatastore(fullfile(DataLocation),'FileExtensions','.png');
app.imds1 = augmentedImageDatastore([224 224],app.imds0 );
% N° of frames
S=size(app.imds0.Files);
app.frames=S(1,1);
app.NFrame = 1:1:S(1,1);app.NFrame=app.NFrame';
app.NFrame;
app.table.FrameN_(1:S(1,1))=app.NFrame;
% Names of those frames
app.Name=app.imds0.Files;
app.table.ImageName(1:S(1,1))=app.Name;
% Load all the data
app.UITable.Data=app.table;
% Switch on lamp
app.DoneLamp.Color='g';
end
% Value changing function: FrameNSpinner
function FrameNSpinnerValueChanging(app, event)
end
% Value changed function: FrameNSpinner
function FrameNSpinnerValueChanged(app, event)
FrameN = app.FrameNSpinner.Value;
images1=readimage(app.imds0,FrameN);
imshow(images1,'Parent',app.PLOT)
end
% Value changed function: Predict
function PredictValueChanged(app, event)
app.net
app.imds1
group=18;
[YPred]= classify(app.net,app.imds1);
s=string(YPred);
app.predictY=double(s);
app.table.Distance(1:app.frames)=app.predictY;
app.DoneLamp_2.Color='g';
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 1170 742];
app.UIFigure.Name = 'UI Figure';
% Create DistanceEvolution
app.DistanceEvolution = uiaxes(app.UIFigure);
title(app.DistanceEvolution, 'Distance evolution ')
xlabel(app.DistanceEvolution, 'Frame N°')
ylabel(app.DistanceEvolution, 'Distance(pixels)')
app.DistanceEvolution.Position = [561 40 570 321];
% Create UITable
app.UITable = uitable(app.UIFigure);
app.UITable.ColumnName = {'Frame N°'; 'ImageName'; 'Distance'};
app.UITable.ColumnWidth = {'auto'};
app.UITable.RowName = {};
app.UITable.Position = [17 73 528 274];
% Create Load
app.Load = uibutton(app.UIFigure, 'state');
app.Load.ValueChangedFcn = createCallbackFcn(app, @LoadValueChanged, true);
app.Load.Text = 'LOAD';
app.Load.FontWeight = 'bold';
app.Load.Position = [91 611 100 22];
% Create Predict
app.Predict = uibutton(app.UIFigure, 'state');
app.Predict.ValueChangedFcn = createCallbackFcn(app, @PredictValueChanged, true);
app.Predict.Text = {'PREDICT'; ''};
app.Predict.FontWeight = 'bold';
app.Predict.Position = [91 581 100 22];
% Create FrameNSpinnerLabel
app.FrameNSpinnerLabel = uilabel(app.UIFigure);
app.FrameNSpinnerLabel.HorizontalAlignment = 'center';
app.FrameNSpinnerLabel.Position = [699 404 57 22];
app.FrameNSpinnerLabel.Text = 'Frame N°';
% Create FrameNSpinner
app.FrameNSpinner = uispinner(app.UIFigure);
app.FrameNSpinner.ValueChangingFcn = createCallbackFcn(app, @FrameNSpinnerValueChanging, true);
app.FrameNSpinner.ValueChangedFcn = createCallbackFcn(app, @FrameNSpinnerValueChanged, true);
app.FrameNSpinner.HorizontalAlignment = 'center';
app.FrameNSpinner.Position = [755 404 45 22];
app.FrameNSpinner.Value = 1;
% Create Save
app.Save = uibutton(app.UIFigure, 'push');
app.Save.FontWeight = 'bold';
app.Save.Position = [91 551 100 22];
app.Save.Text = {'SAVE'; ''};
% Create DistanceextrantionappLabel
app.DistanceextrantionappLabel = uilabel(app.UIFigure);
app.DistanceextrantionappLabel.FontSize = 40;
app.DistanceextrantionappLabel.Position = [111 685 434 48];
app.DistanceextrantionappLabel.Text = 'Distance extrantion app';
% Create Image
app.Image = uiimage(app.UIFigure);
app.Image.Position = [17 653 86 80];
app.Image.ImageSource = '0.jpg';
% Create Image_2
app.Image_2 = uiimage(app.UIFigure);
app.Image_2.Position = [544 653 86 80];
app.Image_2.ImageSource = 'ijm_logo.jpg';
% Create PLOT
app.PLOT = uiaxes(app.UIFigure);
title(app.PLOT, 'Preview')
xlabel(app.PLOT, '')
ylabel(app.PLOT, '')
app.PLOT.FontSize = 14;
app.PLOT.Position = [799 360 332 344];
% Create DoneLampLabel
app.DoneLampLabel = uilabel(app.UIFigure);
app.DoneLampLabel.HorizontalAlignment = 'right';
app.DoneLampLabel.Position = [209 611 34 22];
app.DoneLampLabel.Text = 'Done';
% Create DoneLamp
app.DoneLamp = uilamp(app.UIFigure);
app.DoneLamp.Position = [258 611 20 20];
app.DoneLamp.Color = [1 0 0];
% Create DoneLamp_2Label
app.DoneLamp_2Label = uilabel(app.UIFigure);
app.DoneLamp_2Label.HorizontalAlignment = 'right';
app.DoneLamp_2Label.Position = [209 581 34 22];
app.DoneLamp_2Label.Text = 'Done';
% Create DoneLamp_2
app.DoneLamp_2 = uilamp(app.UIFigure);
app.DoneLamp_2.Position = [258 581 20 20];
app.DoneLamp_2.Color = [1 0 0];
% Create DoneLamp_3Label
app.DoneLamp_3Label = uilabel(app.UIFigure);
app.DoneLamp_3Label.HorizontalAlignment = 'right';
app.DoneLamp_3Label.Position = [209 551 34 22];
app.DoneLamp_3Label.Text = 'Done';
% Create DoneLamp_3
app.DoneLamp_3 = uilamp(app.UIFigure);
app.DoneLamp_3.Position = [258 551 20 20];
app.DoneLamp_3.Color = [1 0 0];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = ICapp(varargin)
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @(app)startupFcn(app, varargin{:}))
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end

 Réponse acceptée

Kojiro Saito
Kojiro Saito le 11 Juin 2019

0 votes

Your classify function is treated as that of Statistics and Machine Learning Toolbox in the compiled application. In order to force compiled application to use CNN classify, there are two ways.
(1) Use SeriesNetwork.loadobj
function startupFcn(app, NET, example)
app.net = load("net.mat");
app.net = SeriesNetwork.loadobj(app.net.net);
% ... %
end
(2) User function progma (%#) in the first line of startupFcn
function startupFcn(app, NET, example)
%#function SeriesNetwork
app.net = load("net.mat");
app.net = app.net.net;
% ... %
end
Hope this helps..

39 commentaires

Pablo Salaverria
Pablo Salaverria le 11 Juin 2019
It worked!!!! Thank you very much!!!
Shawn Dante
Shawn Dante le 6 Avr 2021
i am having an error at app.net.net
it says "Reference to non existent field 'net'.
Kojiro Saito
Kojiro Saito le 6 Avr 2021
Does your mat file contain variable net? app.net.net should be app.net.a if your mat file contains variable a.
Shawn Dante
Shawn Dante le 7 Avr 2021
@Kojiro Saito Thank You Sir it worked i am also having an problem at label=classify(app.net,app.myimage); error:Undefined function classify for input argument of type unit 8
//app.myimage is a single test image file
Kojiro Saito
Kojiro Saito le 7 Avr 2021
What is your app.net model? SeriesNetwork, DAGNetwork or other?
Does classify run fine by command line?
Shawn Dante
Shawn Dante le 7 Avr 2021
Modifié(e) : Shawn Dante le 7 Avr 2021
its a CNN model, classify does not run in my command line
Have you installed Deep Learning Toolbox on your MATLAB?
Does ver command return Deep Learning Toolbox?
ver
Shawn Dante
Shawn Dante le 7 Avr 2021
yes it contains deep learning toolbox
OK, could you try the following? Please replace yourimage.jpg to your actual image file name and extension.
app.myimage = imageDatastore('yourimage.jpg');
label=classify(app.net,app.myimage);
Shawn Dante
Shawn Dante le 7 Avr 2021
again it gives the same error error:Undefined function classify for input argument of type matlab.io.datastore.ImageDatastore'.
Maybe you have another classify.
Does
which classify -all
returns properly toolbox\nnet\cnn\@DAGNetwork\classify.m?
Shawn Dante
Shawn Dante le 7 Avr 2021
yes it returns
So, the cause might be your mat file.
After loading the mat file,
whos
returns DAGNetwork or SeriesNetwork in the Class column?
Shawn Dante
Shawn Dante le 7 Avr 2021
its Series network
Kojiro Saito
Kojiro Saito le 7 Avr 2021
Modifié(e) : Kojiro Saito le 7 Avr 2021
OK, thank you. So it would work...
Have you tried other models, for example, AlexNet?
app.net = alexnet;
label=classify(app.net,app.myimage);
Shawn Dante
Shawn Dante le 7 Avr 2021
yes it works for alexnet
Kojiro Saito
Kojiro Saito le 7 Avr 2021
OK, so alexnet works. Could you upload or share your model (.mat)?
Thanks. I finally understand that this is due to the mat file.
For creating mat file which contains a deep learning model, I would do the following.
save('csnet.mat', 'csnet')
If so, csnet.mat only contains variable csnet.
But your mat file contains other variables like I and img.
To work with existing csnet.mat, you need the following.
(1) In your startupFcn function, app.net should be csnet variable.
function startupFcn(app)
app.net=load("csnet.mat");
%app.net=app.net.I;
app.net=app.net.csnet;
%...%
end
(2) In your ResultButtonPushed function, it's not possible to merge label and app.myimage, so try insertText instead.
function ResultButtonPushed(app, event)
label=classify(app.net,app.myimage);
%imshow([label,app.myimage],'Parent',app.UIAxes_2)
myimage2 = insertText(app.myimage, [0 5], string(label),'FontSize', 24);
imshow(myimage2, 'Parent',app.UIAxes_2)
end
I am trying to load my net.mat file, which I have created using nnstart command, but it is giving me error
Error using load
Unable to read file 'net.mat'. No such file or directory.
Can you explain what to do when the network I am trying to load into matlab is a DAGnetwork?
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app = load('net.mat');
app.net = app.net.net;
end
% Image clicked function: Image
function ImageClicked(app, event)
file = uigetfile("*.jpg");
test = imread(file);
img = imresize(test,[224 224]);
app.Image.ImageSource = img;
pred = classify(app.net,img);
app.Label.Labelsource = pred;
end
end
function startupFcn(app)
%#exclude net.mat
%#function DAGNetwork
app = load('net.mat');
...
end
For detail, please refer to these documents (function pragma and exclude pragma).
仲富 刘
仲富 刘 le 11 Mai 2022
i meet these problems too using matlab2020 and i just want to use it to classify my picture,what should i do
仲富 刘
仲富 刘 le 11 Mai 2022
can you give me a help ,i save my picture as a global data,code like this :
as start: app=load('net.mat');
app.net = Net;%Net is the name of the net data.
the classify function is:
global im; reim=imresize(im,[227 227]);
[result,score] = classify(app.net,reim);
there are a lot of problems about "net",what should i do:
仲富 刘
仲富 刘 le 11 Mai 2022
it says :The property 'net' of class 'app' is not recognized
仲富 刘
仲富 刘 le 13 Mai 2022
It solved by adding a property! Thank you!
Fatin Nasuha Bt Asrol
Fatin Nasuha Bt Asrol le 8 Juin 2022
Hi..can I get some help to look at my coding..I still get error for the classification result button..Hopefully someone can help me.
This is my coding:
Did you add function pragma (starts with %#) in startupFcn? This is not just a comment but a pragma.
function startupFcn(app)
%#function SeriesNetwork
app.net = load("net.mat");
app.net = app.net.net;
end
If this does not work, try DAGNetwork function pragma.
function startupFcn(app)
%#function DAGNetwork
app.net = load("net.mat");
app.net = app.net.net;
end
Fatin Nasuha Bt Asrol
Fatin Nasuha Bt Asrol le 9 Juin 2022
@Kojiro Saito Thank you so much for your reply. I already try DAGNetwork function pragma.However, I still get the same error.
Do you know what is the possible cause for this? will much appreaciated if you can assist me on this.Thank you.
It's better to check actual class.
Does the following return DAGNetwork in Class?
whos app.net
Fatin Nasuha Bt Asrol
Fatin Nasuha Bt Asrol le 9 Juin 2022
@Kojiro Saito No, it did not return DAGNetwork in Class.Did you know how to make it happen?
What variables are contained in your mat file?
The following example shows DAGNetwork.
net = googlenet;
save net
% Suppose reading mat file in AppDesigner
app.net = load('net.mat');
Fatin Nasuha Bt Asrol
Fatin Nasuha Bt Asrol le 10 Juin 2022
Modifié(e) : Fatin Nasuha Bt Asrol le 10 Juin 2022
@Kojiro Saito Thank you for your reply. However I still get the same error. Actually, I used result trainedNetwork _1 in deep network designer and save it as net.mat file.
As the image below:
This is in app designer:
And to classify the images I reused it in app designer as the image above. Is it my method correct to classify image in app designer?
You need to extract trainedNetwork_1 variable from net.mat file.
function startupFcn(app)
%#function DAGNetwork
app.net = load('net.mat');
app.net = app.net.trainedNetwork_1;
end
function ClassificationresultButtonPushed(app, event)
[YPred, probs] = classify(app.net, I);
end
Fatin Nasuha Bt Asrol
Fatin Nasuha Bt Asrol le 10 Juin 2022
@Kojiro Saito Thank you for your reply. I already tried your suggestion extract trainedNetwork_1 variable from net.mat file but I still get the same error [ Undefined function 'classify' for input arguments of type 'uint8']. Is it another way to solve it?
OK, apart from App Designer and MATLAB Compiler, let's execute classify in MATLAB command window.
What did you get by the following command?
load('net.mat')
img = imread('somefile.jpg');
imgRes = imresize(img, [244 244]);
[YPred, probs] = classify(trainedNetwork_1, imgRes);
Fatin Nasuha Bt Asrol
Fatin Nasuha Bt Asrol le 10 Juin 2022
@Kojiro Saito Thank you for reply, I got this error:
I copied your code, but the error says imresize to 244x244 is wrong.
Fix it by the following.
imgRes = imresize(img, [224 224]);
Fatin Nasuha Bt Asrol
Fatin Nasuha Bt Asrol le 11 Juin 2022
@Kojiro Saito Thank you for reply. I already tried change it according your coding imgRes = imresize (img, [244 244]); but I get the another error like this:
Kojiro Saito
Kojiro Saito le 11 Juin 2022
I'm a bit confused.
Is Deep Learning Toolbox installed and you have a valid license?
Is your trained model (trainedNetwork_1) allows imageInputLayer?
Your question is not related to App Designer any more, so if the above does not work, I think it's better if you would post a question in MATLAB Answers as a new question.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by