Access Python Library importing Keras

조회 수: 3 (최근 30일)
Lukas Scheunemann
Lukas Scheunemann 2018년 11월 12일
댓글: LEONARDO NUTI 2022년 4월 3일
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