Access Python Library importing Keras

3 Ansichten (letzte 30 Tage)
Lukas Scheunemann
Lukas Scheunemann am 12 Nov. 2018
Kommentiert: LEONARDO NUTI am 3 Apr. 2022
Hi all,
I am triyng to call python scripts i allready wrote in matlab.
In specific i am trying call i python function which imports keras models i created and uses them to predict the input data like this:
from keras.models import load_model
from euclid_dist import euclid_dist
import numpy as np
class nn_handler:
def __init__(self, model_location='current_checkpoint_lstm_2'):
self.model = load_model(model_location,custom_objects={'euclid_dist':euclid_dist})
def prediction(self,X):
pred=self.model.predict(X)
return pred.reshape(pred.shape[0],9,3)
However i noticed that matlab is (amoungst others) not accepting python scripts importing keras.
For exmple if i write two scripts:
test.py:
import numpy
def foo():
return 'bar'
and test2.py: (i only change the import and are not even using it)
import keras
def foo():
return 'bar'
And call them in matlab, setting the version to my anaconda enviroment:
pyversion('C:\Users\lukas.scheunemann\AppData\Local\Continuum\miniconda3\envs\nnenv\pythonw.exe')
py.test.foo()
py.test2.foo()
The first one returns 'bar'. But the secound an error 'Undefined variable "py" or class "py.test2.foo".
Did i make a misstake installing keras so it can not be used?
Or is only a limited selection off python libraries supported unlike the official limitations , because it is not desired that people use python instead off aquivilent matlab packages. E.g. i am supposed to buy the deep learning toolbox.
  1 Kommentar
ARUN
ARUN am 23 Feb. 2021
I am facing the same probelm, you got this resolved? I am trying to call the python script which contains the keras model imported through load_model as yours. I am getting the same undefined function error.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Lukas Scheunemann
Lukas Scheunemann am 15 Nov. 2018
I was able to solve the task by writting the arguments into a .mat file and then start the python script via the systems() command. Which in turn loads from the .mat and the model, then makes a prediction and returns the result again in a .mat file, which is loaded in matlab again.
It is slow and a should have given a warning to everybody valueing their sanity bevor make them even read this ... but it works.
  1 Kommentar
LEONARDO NUTI
LEONARDO NUTI am 3 Apr. 2022
Hey i'm facing same issue can you elaborate on how you saved arguments in .mat file? I've xgboost and nn models written in Python that I need to run thorugh MATLAB

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by