how to do model prediction using python in matlab
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Eugenia Angela Salsabillah
le 20 Juil 2021
Commenté : Eugenia Angela Salsabillah
le 23 Juil 2021
this is the python code, and it works so well in python
import keras
from keras.models import load_model
from keras.preprocessing import image
import json
import numpy as np
filepath = 'model2.h5'
test_img = 'falling14145853.png'
model = keras.models.load_model(filepath)
img = image.load_img(test_img, target_size=(150, 150))
x = image.img_to_array(img)
x = np.expand_dims(x, axis = 0)
images = np.vstack([x])
classes = model.predict(images, batch_size = 10)
_mapping = ["falling", "sitting", "standing"]
a = np.concatenate(classes, axis=None).astype(int).tolist()
idx = np.argmax(a[0])
prediction = _mapping[idx]
response_json = {"Prediction" : prediction}
data_in_json = json.dumps(response_json)
with open("prediction.json", "w") as out:
out.write(data_in_json)
and this is in matlab (I'm using R2019b)
system('test_model.py'); %doing prediction
this is the matlab output and i've tried to run pyhton to make a capture using cv and works so well in matlab but when I tried to run python code which contain of keras, its showing this error
>> bismillahbisaaaa
Traceback (most recent call last):
File "E:\angie\test_model.py", line 1, in <module>
import keras
ImportError: No module named keras
2 commentaires
Mudit Chaturvedi
le 21 Juil 2021
Hello!
I understand you are trying to do model prediction using python in Matlab and are facing an error while trying to import keras module.
The import statement does not have the same functionality in Matlab as in Python. Please refer to the Understanding Python and MATLAB import Commands documentation for more details.
To call Python libraries from MATLAB, install a supported version of the reference implementation (CPython) for Python. MATLAB supports versions 2.7, 3.6, and 3.7. Refer to this R2019b documentation link for more information.
Réponses (1)
Peter O
le 22 Juil 2021
So it looks like it's launching into Python fine. A couple things to try:
- Standard Python or Anaconda? Sometimes Anaconda doesn't play so well. Make sure you're launching matlab from the Anaconda prompt if you go this route, and sometimes the python libraries that depend on dll's will still have some trouble. If OpenCV is playing nicely in a python call through MATLAB, then chances are you're OK there.
- Is it a case of environments? Is the activated environment missing keras? Is it a module import problem? Does numpy or another library import without issue if you do that first?
- Try calling the python file through the system call as 'python test_model.py'. This should be no different than calling the file, but stranger things have happened.
3 commentaires
Peter O
le 23 Juil 2021
Are you using a GPU component for them? My hunch is that some hook file is getting activated with the interpreter's startup routine (maybe an environment variable pointing to a required DLL or it using a different environment) when you use
system("python test_model.py")
which doesn't happen when you just pass it the file directly using
system("test_model.py")
which relies on the formulation from the shell command. Maybe the python run from the shell has a different permission set than the full interpreter, particularly if it gets called from inside MATLAB? Keras stores a configuration file in your home directory -- maybe it fails quietly with insufficient privileges?
This is about the extent of my knowledge on Python environment/startup configuration.
Is there any reason the former invocation doesn't work for your purposes? Seems like it would occupy the same amount of code and overhead in the m-file, and there's no difference in the output returned. If you need to send it multiple files you can always concatenate them at run-time:
files_to_process = ["a.py", "b.py"];
for ix=1:numel(files_to_process)
my_cmd = "python " + files_to_process(ix);
system(my_cmd)
end
Voir également
Catégories
En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!