Python Matlab integration error - Error using numpy_ops>init thinc.backends.numpy_ops

17 visualizaciones (últimos 30 días)
I want to call my python script from the Matlab. I received the error Error using numpy_ops>init thinc.backends.numpy_ops
Python Error: ValueError: numpy.dtype size changed, may indicate binary incompatibility. Expected 96 from C header, got 88 from PyObject.
The Python script is as follows
import spacy
def text_recognizer(model_path, text):
try:
# Load the trained model
nlp = spacy.load(model_path)
print("Model loaded successfully.")
# Process the given text
doc = nlp(text)
ent_labels = [(ent.text, ent.label_) for ent in doc.ents]
return ent_labels
The Matlab script is as follows
% Set up the Python environment
pe = pyenv;
py.importlib.import_module('final_output');
% Add the directory containing the Python script to the Python path
path_add = fileparts(which('final_output.py'));
if count(py.sys.path, path_add) == 0
insert(py.sys.path, int64(0), path_add);
end
% Define model path and text to process
model_path = 'D:\trained_model\\output\\model-best';
text = 'Roses are red';
% Call the Python function
pyOut = py.final_output.text_recognizer(model_path, text);
% Convert the output to a MATLAB cell array
entity_labels = cell(pyOut);
disp(entity_labels);
I found one solution to update numpy, what I did, but nothing changed.
How can I fix the issue?

Respuesta aceptada

Gayatri
Gayatri el 18 de Jun. de 2024
Hi Saswati,
The error message you are encountering arises due to a mismatch between the version of numpy expected by one of the Python libraries you are using, such as spacy and the installed version of numpy in your environment.
1) Update "spacy" and all the other related libraries to their latest versions.
2) Consider using a Python virtual environment. This can help ensure that the Python libraries used by your MATLAB project don't interfere with system-wide Python installations or libraries used by other projects. Here's how you could set it up and use it within MATLAB:
  • Create a virtual environment:
python -m venv myenv
  • Activate the environment and Install the necessary libraries within the activated environment:
pip install "name_of_library"
  • Point MATLAB to use the Python executable within this virtual environment:
pyenv('Version', 'path/to/myenv/bin/python'); % Use path\to\myenv\Scripts\python.exe on Windows
I hope it helps!
  1 comentario
Eddy
Eddy el 21 de Jun. de 2024
Solution is to downgrade numpy to 1.26.4 for now:
https://stackoverflow.com/questions/78650222/valueerror-numpy-dtype-size-changed-may-indicate-binary-incompatibility-expec

Iniciar sesión para comentar.

Más respuestas (1)

Eddy
Eddy el 21 de Jun. de 2024
https://stackoverflow.com/questions/78650222/valueerror-numpy-dtype-size-changed-may-indicate-binary-incompatibility-expec

Categorías

Más información sobre Python Package Integration en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by