You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TensorFlow CNN数据增强后出现scipy.ndimage.interpolation属性错误

报错原因

该报错是scipy版本过高导致的API变动问题:scipy 1.12及以上版本移除了scipy.ndimage.interpolation子模块,原子模块下的所有函数都直接合并到scipy.ndimage根命名空间下。你使用的ImageDataGenerator依赖的旧版keras-preprocessing库没有适配这个API变动,仍然调用了已被移除的子模块路径,因此触发报错。
未做数据增强时不会触发仿射变换逻辑,自然不会调用到这部分scipy代码,所以运行正常。

解决方案
  • 方案1:降级scipy版本
    直接安装1.11.x分支的scipy即可适配旧版keras-preprocessing的调用逻辑:
    pip install scipy==1.11.4
    
  • 方案2:替换为TensorFlow官方推荐的预处理层(无需降级依赖)
    目前TensorFlow官方已不推荐使用ImageDataGenerator做数据增强,建议使用tf.keras.layers下的预处理层实现,性能更优且无依赖冲突,示例实现:
    # 定义数据增强流水线
    data_augmentation = tf.keras.Sequential([
      tf.keras.layers.Rescaling(1./255),
      tf.keras.layers.RandomFlip("horizontal"),
      tf.keras.layers.RandomRotation(0.2),
      tf.keras.layers.RandomZoom(0.2),
      tf.keras.layers.RandomTranslation(height_factor=0.2, width_factor=0.2)
    ])
    
    # 加载数据集时使用该流水线处理,也可以直接嵌入到模型输入层之后
    train_ds = tf.keras.utils.image_dataset_from_directory(
      train_dir,
      image_size=(224, 224),
      batch_size=32,
      label_mode='categorical'
    )
    train_data_augmented = train_ds.map(lambda x, y: (data_augmentation(x, training=True), y))
    
  • 方案3:临时补丁修复(无需改业务代码/无需降级)
    在导入所有依赖的最开头添加两行别名映射代码,让旧调用路径指向新的模块位置即可:
    import scipy.ndimage
    scipy.ndimage.interpolation = scipy.ndimage
    # 后面再导入其他依赖即可正常运行
    

内容的提问来源于stack exchange,提问作者BestSithInEU

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 20:15:08