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

19 次查看(过去 30 天)
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?

采纳的回答

Gayatri
Gayatri 2024-6-18
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 个评论
Eddy
Eddy 2024-6-21
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

请先登录,再进行评论。

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Python Package Integration 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by