运行TFLite Model Maker代码时遇array_record_module导入错误
问题描述
运行TensorFlow Lite Model Maker官方图像分类示例代码时触发ImportError,错误提示array_record_module.so不是mach-o格式文件(macOS系统环境)。
运行的代码
from tflite_model_maker import image_classifier from tflite_model_maker.image_classifier import DataLoader # 加载设备端ML应用的输入数据 data = DataLoader.from_folder('flower_photos/') train_data, test_data = data.split(0.9) # 定制TensorFlow模型 model = image_classifier.create(train_data) # 评估模型 loss, accuracy = model.evaluate(test_data) # 导出TensorFlow Lite模型和标签文件到指定目录 model.export(export_dir='/tmp/')
错误日志
2023-05-14 13:43:07.639151: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. /usr/local/lib/python3.9/site-packages/tensorflow_addons/utils/tfa_eol_msg.py:23: UserWarning: TensorFlow Addons (TFA) has ended development and introduction of new features. TFA has entered a minimal maintenance and release mode until a planned end of life in May 2024. Please modify downstream libraries to take dependencies from other repositories in our TensorFlow community (e.g. Keras, Keras-CV, and Keras-NLP). For more information see: https://github.com/tensorflow/addons/issues/2807 warnings.warn( Traceback (most recent call last): File "/Users/apple/Documents/modelmakersrc/maketflitemodel.py", line 3, in <module> from tflite_model_maker import model_spec File "/usr/local/lib/python3.9/site-packages/tflite_model_maker/__init__.py", line 44, in <module> from tflite_model_maker import audio_classifier File "/usr/local/lib/python3.9/site-packages/tflite_model_maker/audio_classifier/__init__.py", line 24, in <module> from tensorflow_examples.lite.model_maker.core.data_util.audio_dataloader import DataLoader File "/usr/local/lib/python3.9/site-packages/tensorflow_examples/lite/model_maker/core/data_util/audio_dataloader.py", line 27, in <module> from tensorflow_examples.lite.model_maker.core.task.model_spec import audio_spec File "/usr/local/lib/python3.9/site-packages/tensorflow_examples/lite/model_maker/core/task/model_spec/__init__.py", line 24, in <module> from tensorflow_examples.lite.model_maker.core.task.model_spec import text_spec File "/usr/local/lib/python3.9/site-packages/tensorflow_examples/lite/model_maker/core/task/model_spec/text_spec.py", line 44, in <module> from official.nlp.data import classifier_data_lib File "/usr/local/lib/python3.9/site-packages/official/nlp/data/classifier_data_lib.py", line 28, in <module> import tensorflow_datasets as tfds File "/usr/local/lib/python3.9/site-packages/tensorflow_datasets/__init__.py", line 43, in <module> import tensorflow_datasets.core.logging as _tfds_logging File "/usr/local/lib/python3.9/site-packages/tensorflow_datasets/core/__init__.py", line 22, in <module> from tensorflow_datasets.core import community File "/usr/local/lib/python3.9/site-packages/tensorflow_datasets/core/community/__init__.py", line 18, in <module> from tensorflow_datasets.core.community.huggingface_wrapper import mock_builtin_to_use_gfile File "/usr/local/lib/python3.9/site-packages/tensorflow_datasets/core/community/huggingface_wrapper.py", line 31, in <module> from tensorflow_datasets.core import dataset_builder File "/usr/local/lib/python3.9/site-packages/tensorflow_datasets/core/dataset_builder.py", line 34, in <module> from tensorflow_datasets.core import dataset_info File "/usr/local/lib/python3.9/site-packages/tensorflow_datasets/core/dataset_info.py", line 47, in <module> from tensorflow_datasets.core import file_adapters File "/usr/local/lib/python3.9/site-packages/tensorflow_datasets/core/file_adapters.py", line 29, in <module> from array_record.python import array_record_module ImportError: dlopen(/usr/local/lib/python3.9/site-packages/array_record/python/array_record_module.so, 0x0002): tried: '/usr/local/lib/python3.9/site-packages/array_record/python/array_record_module.so' (not a mach-o file)
关键依赖包版本
- array-record: 0.2.0
- tensorflow: 2.12.0
- tensorflow-datasets: 4.9.1
- tflite-support: 0.1.0a1
解决方案
错误根源是array-record包安装了Linux平台的二进制版本(.so格式),而macOS需要mach-o格式的文件。按以下步骤修复:
- 卸载不兼容的array-record包
pip uninstall -y array-record
- 安装适配macOS的版本
降级到兼容的版本,或安装支持macOS架构的包:
pip install array-record==0.1.1
若上述命令无效,尝试用conda安装(需激活conda环境):
conda install -c conda-forge array-record
- 验证安装有效性
运行以下代码确认无导入错误:
from array_record.python import array_record_module
- 可选:重新安装tensorflow-datasets
若问题仍存在,重新安装tensorflow-datasets确保依赖匹配:
pip uninstall -y tensorflow-datasets pip install tensorflow-datasets==4.9.1
内容的提问来源于stack exchange,提问作者Charwaka
相关产品推荐
相关产品推荐

