Anaconda中调用fit训练模型时出现Fatal Python error: Aborted的解决方法
问题描述
我是深度学习领域的新手学生,正在开展迁移学习研究任务。使用Anaconda 2.5.4调用模型的fit方法训练时,触发以下错误:
2024-04-21 18:33:42.681880: 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 To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2024-04-21 18:33:42.683001: I tensorflow/core/common_runtime/process_util.cc:146] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance. OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5 already initialized. OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see <a href="http://www.intel.com/software/products/support/" rel="nofollow noreferrer">http://www.intel.com/software/products/support/</a>. Fatal Python error: Aborted Main thread: Current thread 0x00001920 (most recent call first): File "C:\Anaconda\envs\tf\lib\site-packages\keras\preprocessing\image.py", line 2591 in apply_affine_transform File "C:\Anaconda\envs\tf\lib\site-packages\keras\preprocessing\image.py", line 2013 in apply_transform File "C:\Anaconda\envs\tf\lib\site-packages\keras\preprocessing\image.py", line 384 in _get_batches_of_transformed_samples File "C:\Anaconda\envs\tf\lib\site-packages\keras\preprocessing\image.py", line 116 in __getitem__ File "C:\Anaconda\envs\tf\lib\site-packages\keras\engine\data_adapter.py", line 1022 in _peek_and_restore File "C:\Anaconda\envs\tf\lib\site-packages\keras\engine\data_adapter.py", line 869 in __init__ File "C:\Anaconda\envs\tf\lib\site-packages\keras\engine\data_adapter.py", line 1010 in __init__ File "C:\Anaconda\envs\tf\lib\site-packages\keras\engine\data_adapter.py", line 1262 in __init__ File "C:\Anaconda\envs\tf\lib\site-packages\keras\engine\data_adapter.py", line 1582 in get_data_handler File "C:\Anaconda\envs\tf\lib\site-packages\keras\engine\training.py", line 1501 in fit File "C:\Anaconda\envs\tf\lib\site-packages\keras\utils\traceback_utils.py", line 65 in error_handler File "e:\doctorat\source azo 06-04-2024\litesrgan_mobilenetv3small_ssdlite_impl\litesrganmbnet.py", line 61 in File "C:\Anaconda\envs\tf\lib\site-packages\spyder_kernels\py3compat.py", line 356 in compat_exec File "C:\Anaconda\envs\tf\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 473 in exec_code File "C:\Anaconda\envs\tf\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 615 in _exec_file File "C:\Anaconda\envs\tf\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 528 in runfile File "C:\Users\UNIVERSITE TOAMASINA\AppData\Local\Temp\ipykernel_4132\677467036.py", line 1 in Redémarrage du noyau...
我已尝试重装TensorFlow,但问题未解决,希望得到错误解释及可行的解决方案。
错误解释
核心错误是OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5 already initialized,这说明你的Python环境中同时加载了多份OpenMP运行时库(libiomp5md.dll和libiomp5)。OpenMP是用于并行计算的工具库,重复加载会导致内存冲突、计算逻辑混乱,最终触发程序崩溃(即日志中的Fatal Python error: Aborted)。
TensorFlow本身依赖OpenMP做并行优化,而环境中其他库(如部分图像处理、科学计算库)也可能自带OpenMP运行时,两者的依赖版本或加载方式冲突就会引发该问题。重装TensorFlow无效,因为冲突根源不在TensorFlow本身,而是环境内的库依赖重复。
解决方案
临时应急方案(快速生效,不推荐长期使用)
- 设置环境变量
KMP_DUPLICATE_LIB_OK=TRUE,强制允许重复加载OpenMP库:- Windows系统:打开「系统属性-高级-环境变量」,新建系统变量,变量名填
KMP_DUPLICATE_LIB_OK,变量值填TRUE - Anaconda环境临时生效:在代码开头添加以下两行:
import os os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
注意:这是官方标注的不安全、无官方支持的临时 workaround,可能导致计算结果错误或隐性崩溃,仅适合临时测试验证。
- Windows系统:打开「系统属性-高级-环境变量」,新建系统变量,变量名填
根治方案(推荐)
- 清理环境中重复的OpenMP库:
- 激活你的Anaconda环境:
conda activate tf(假设你的环境名为tf) - 查找环境中所有关联OpenMP的库:
也可直接在环境的conda list | grep ompLib\site-packages和Library\bin目录下搜索libiomp*.dll文件 - 卸载自带OpenMP的冗余库:部分版本的
numpy、scikit-image等库可能自带OpenMP,尝试用conda uninstall卸载后,重新安装conda官方编译的版本(优先用conda install而非pip install,减少二进制包依赖冲突) - 重建干净环境:若上述方法无效,直接新建Anaconda环境,仅安装必要依赖(TensorFlow、Keras、图像处理库等),全程优先使用
conda管理依赖,从根源避免冲突
- 激活你的Anaconda环境:
内容的提问来源于stack exchange,提问作者Reflet
相关产品推荐
相关产品推荐

