Saved classifier forgets its predict function

4 views (last 30 days)
In a project I'm training several classifiers in Matlab 2020b, new data though should be processed in python 3.8. To do so I save the trained classifiers as .mat files (simply with "save(classifier_obj)").
In order to allow multiple users (without a matlab licence but with the runtime v9.9 installed) to use the classifiers I store these files as blob in a mysql DB (I know its not the best practice as stated here: https://de.mathworks.com/matlabcentral/answers/84154-storing-a-mat-file-in-mysql-database.).
When accessing, I download the blob file, store it temporaly on disc with these 2 lines in python and pass the file_name to the compiled matlab function (described below)
tmp = open(file_name, "wb")
tmp.write(mat)
In order to classify new data elements I've written this little function and compiled it as python code.
function label=new_classifier_predict(file_name,data)
load(file_name);
label=predict(classifier,data);
My problem now is that the classifier seems to forget its class method "predict"...
File C:\Users\steenbuck\AppData\Local\Temp\steenbuck\mcrCache9.9\new_cl0\new_classifi\classificators\new_classifier_predict.m, line 4, in new_classifier_predict
Method 'predict' is not defined for class 'ClassificationKNN' or is removed from MATLAB's search path.
I'm guessing that the object can't be reconstructed completly and therefore fails.. Strangely this error only occours when running the compiled python file. Matlab 2020b can handle all files as expected.
Is there a way to solve this or to debug and see whats happening the the matlab runtime environment?
  1 Comment
Johannes Steenbuck
Johannes Steenbuck on 12 Jan 2021
Edited: Johannes Steenbuck on 12 Jan 2021
Another error in this context arises if my loading function gets more complex. I've implemented some own classifiers (as well with a predict method).
Loading the self-developed classes works but when I try to load the classifiers from the toolbox I get the error on the bottom.
The compiled and called function is:
function label=matlab_classifier_predict(file_name,data)
if contains(file_name,"PKK-Line")
%load user_written class PKK
classifier_obj= PKK.load(file_name);
elseif contains(file_name,"PKK-Ellipse")
%load user_written class PKK_ellipse
classifier_obj=PKK_ellipse.load(file_name);
else
%load matlab classifiers
load(file_name);
label=predict(classifier,data);
return
end
label=predict(classifier_obj,data);
end
An error occurred when evaluating the result from a function. Details: File C:\Program Files\MATLAB\MATLAB Runtime\v99\mcr\toolbox\stats\classreg\+classreg\+learning\+internal\makeClassificationModelAdapter.m, line 3, in @(x)istall(x) File C:\Program Files\MATLAB\MATLAB Runtime\v99\mcr\toolbox\stats\classreg\+classreg\+learning\+internal\makeClassificationModelAdapter.m, line 3, in makeClassificationModelAdapter File C:\Program Files\MATLAB\MATLAB Runtime\v99\mcr\toolbox\stats\classreg\ClassificationKNN.m, line 689, in ClassificationKNN.predict File C:\Users\steenbuck\AppData\Local\Temp\steenbuck\mcrCache9.9\matlab4\matlab_class\classificators\matlab_classifier_predict.m, line 11, in matlab_classifier_predict Undefined function 'istall' for input arguments of type 'double'.
File "C:\Program Files\MATLAB\MATLAB Runtime\v99\toolbox\compiler_sdk\pysdk_py\matlab_pysdk\runtime\futureresult.py", line 135, in result raise e File "C:\Program Files\MATLAB\MATLAB Runtime\v99\toolbox\compiler_sdk\pysdk_py\matlab_pysdk\runtime\futureresult.py", line 135, in result raise e File "C:\Program Files\MATLAB\MATLAB Runtime\v99\toolbox\compiler_sdk\pysdk_py\matlab_pysdk\runtime\futureresult.py", line 135, in result raise e File "C:\Program Files\MATLAB\MATLAB Runtime\v99\toolbox\compiler_sdk\pysdk_py\matlab_pysdk\runtime\deployablefunc.py", line 79, in __call__ ret = futureresult.FutureResult(self._cppext_handle, future_tuple, nlhs,

Sign in to comment.

Accepted Answer

Johannes Steenbuck
Johannes Steenbuck on 18 Jan 2021
Matlab support got the soluition:
For MATLAB Compiler deployed component to be able to correctly load a pretrained classifier from a MAT-file, and for all methods of that classifier to then be available, the correct classifier class definitions actually need to be "available" in the deployed component. For your custom classifier classes, you probably noticed that your custom classes actually need to be compiled into the component at compile time for them to be available at runtime. For built-in classifiers something similar has to be done, we actually need to know at compile time already that you want to work with them such that we can then properly make them available at runtime.
One way to make the compiler aware of these things at compile time is to use %#function pragmas in your code:
So, to make sure that the ClassificationKNN class from the error message is properly available, add:
%#function ClassificationKNN to the code before compilation.
function label=new_classifier_predict(file_name,data)
%#function ClassificationKNN
load(file_name);
label=predict(classifier,data);

More Answers (0)

Categories

Find more on Python Package Integration in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!