Error "The Python model could not be loaded" in Custom Python Model Predict of Statistics and Machine Learning Toolbox
25 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I'm implementing a predict model by using this block of Statistics and Machine Learning Toolbox. Now I'm using free trial version of this Toolbox.
First, I would like to try a simple model.
Referring to the example on this linked page, I created a block diagram like this.
I executed it but this error happened.
An error occurred in the MATLAB System block
'slexCustomPythonModelPredictExample/Custom Python Model Predict/Custom Python Model Block' while calling the 'getOutputSizeImpl' method of 'internal.pycoexblks.CustomPythonModelBlock'. The error was thrown from
'C:\Program Files\MATLAB\R2024b\toolbox\shared\pycoexblks\+internal\+pycoexblks\CustomPythonModelBlock.p' at line 0
'C:\Program Files\MATLAB\R2024b\toolbox\shared\pycoexblks\+internal\+pycoexblks\PythonModelBlockBase.p' at line 0
'C:\Program Files\MATLAB\R2024b\toolbox\shared\pycoexblks\+internal\+pycoexblks\PythonModelBlockBase.p' at line 0
'C:\Program Files\MATLAB\R2024b\toolbox\simulink\ui\studio\config\m\+SLStudio\StartPauseContinue.p' at line 0
'C:\Program Files\MATLAB\R2024b\toolbox\simulink\ui\studio\config\m\+SLStudio\ToolBars.p' at line 0'
Cause:
The call to the custom function load_model() failed. The call from MATLAB to Python was 'py.predict.load_model(1)'.
Please check the [load_model() arguments] field in the [Model Specification] tab of the block interface.
The Python model could not be loaded. The Python error message was: 'Python error: TypeError: load_model() takes 0 positional arguments but 1 was given'.。
I can't figure out how to solve this. I wonder where I'm mistaking.
The below is the details. I'd be happy if you could give me how to revise.
I set the workspace input signals using the variables generated by the following MATLAB code.
data = readtable('Input_data.csv');
V_TGT_Vehicle = data.V_TGT_Vehicle;
P_DCDC_PNT_W = data.P_DCDC_PNT_W;
P_HVAC_PNT_W = data.P_HVAC_PNT_W;
SOC_BT_Hi_PNT_per = data.SOC_BT_Hi_PNT_per;
open_accel_Driver_per = data.open_accel_Driver_per;
open_break_Driver_per = data.open_break_Driver_per;
w_MG_PNT_radps = data.w_MG_PNT_radps;
% Generate time columns
time = (0:0.025:(size(data, 1)-1)*0.025)';
% Merges each data column and time sequence to create a 2-dimensional array
V_TGT_Vehicle_with_time = [time, V_TGT_Vehicle];
P_DCDC_PNT_W_with_time = [time, P_DCDC_PNT_W];
P_HVAC_PNT_W_with_time = [time, P_HVAC_PNT_W];
SOC_BT_Hi_PNT_per_with_time = [time, SOC_BT_Hi_PNT_per];
open_accel_Driver_per_with_time = [time, open_accel_Driver_per];
open_break_Driver_per_with_time = [time, open_break_Driver_per];
w_MG_PNT_radps_with_time = [time, w_MG_PNT_radps];
Block Parameter of Custom Python Model Predict is set like this.
Arguments to load_model() is empty.
Here is predict.py.
Also, "surrogate_model.pkl" is my original model I made by scikit-learn in python.
The surrogate_model.pkl is a regression model trained on the CSV data (about 4,200 rows) to predict two output signals based on the following seven input signals.
Input Signals:
- V_TGT_Vehicle
- P_DCDC_PNT_W
- P_HVAC_PNT_W
- SOC_BT_Hi_PNT_per
- open_accel_Driver_per
- open_break_Driver_per
- w_MG_PNT_radps
Output Signals:
- trq_MG2_tgtCalc1
- trq_MG2_tgtCalc2
import pickle
import numpy as np
def load_model():
with open('surrogate_model.pkl', 'rb') as file:
model = pickle.load(file)
return model
def predict(model, inputList):
inputArray = np.array(inputList)
predictions = model.predict(inputArray)
return predictions.tolist()
Here is 「Input」 tab and 「Output」tab of that Block parameter .
If you need any additional information to provide your appropriate response, just let me know!
4 Commenti
Zuber Khan
il 25 Set 2024
Hi,
The issue is reproducible at my end. If you compare your 'predict.py' script with 'custom.py' script of example, the "load_model" function is designed to take the model weights/coefficients as input arguments in the example, while the "predict" function has the model prediction workflow implementation. However, here you are doing transfer learning.
Since this would need further debugging and exact details of "Custom Python Model Predict" block implementation, I would recommend you to reach out to MathWorks Technical Support for faster resolution.
You can reach out using the following link:
Regards,
Zuber
Risposta accettata
Karen
il 8 Ott 2024
Modificato: John Kelly
il 18 Nov 2024 alle 15:01
hank you for sharing your experience with the Custom Python Model Predict in the Statistics and Machine Learning Toolbox. It sounds like you're encountering a common issue related to the function call in your code. The error message suggests that the load_model() function is being called with an unexpected argument.
To resolve this, please ensure that your load_model() function is defined to accept arguments, or modify your block settings to call the function correctly without any parameters. You might also want to check that the paths to your model files are correct and that the model was trained properly in Python.
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Call Python from MATLAB in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!