Access Python Library importing Keras

2 次查看(过去 30 天)
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 个评论
ARUN
ARUN 2021-2-23
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.

请先登录,再进行评论。

回答(1 个)

Lukas Scheunemann
Lukas Scheunemann 2018-11-15
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 个评论
LEONARDO NUTI
LEONARDO NUTI 2022-4-3
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by