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