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

Keras运行数据增强代码报WinError 126找不到指定模块如何解决

错误成因
  1. 环境依赖故障:从报错栈可定位错误触发在scipy库初始化阶段,加载依赖的dll动态链接库失败触发Win126错误,通常由scipy安装包损坏、版本与当前Python/系统不兼容、缺少Windows VC++运行库三类原因导致。
  2. 代码语法错误:编写的代码存在多处拼写、大小写类语法错误,即便环境正常也无法运行。
修复步骤
  • 修复scipy依赖:
    激活你的Anaconda环境myenv,先执行卸载命令pip uninstall scipy清除损坏版本,再执行conda install scipy重装兼容版本。如果重装后仍然报错,安装最新版Visual C++ Redistributable x64运行库后重启设备即可。
  • 修正代码问题:
    • 导入语句的Image改为小写image
    • 实例化时imageDataGenerator首字母大写改为ImageDataGenerator
    • 修正参数拼写错误,widht_shift_range改为width_shift_range
    • 提前手动创建保存增强结果的preview文件夹,避免路径不存在报错
修正后可运行代码
## Data Augmentation using python and keras

## What is data Augmentation??
## In data augmentation we create more images from one image

from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
datagen = ImageDataGenerator(
    rotation_range=40,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode='nearest')

img = load_img('Deep learning/CNN Work/Data Augmentation using python and keras/2.jpg') ## This is a PIL Image
x = img_to_array(img) ## this is a numpy array with shape(3,150,150)
x = x.reshape((1,) + x.shape) ## this is a numpy array with shape(1,3,150,150)


## The .flow() command below generates batches of randomly transformed images
## and save the result to the 'preview/' directory 
i=0
for batch in datagen.flow(x, batch_size=1,
                        save_to_dir='preview', save_prefix='dog',
                        save_format='jpeg'):
    i+=1
    if i>20:
        break # otherwise the generator loop would indefinitely

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.09 06:12:02