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

使用Keras ImageDataGenerator实现数据增强并可视化样本时遭遇RuntimeError问题求助

解决Keras ImageDataGenerator的"RuntimeError: boundary mode not supported"错误

嘿,我一眼就揪出问题啦——你在ImageDataGenerator里把fill_mode参数拼错了!你写的是"nearset",但正确拼写应该是"nearest"。这个参数用来指定图像变换后填充空白区域的方式,拼写错误会导致Keras识别不了这个边界模式,直接抛出了运行时错误。

咱们把这个小失误修正过来就行,下面是调整后的完整代码:

datagen = ImageDataGenerator(
    rotation_range=20,
    width_shift_range=0.1,
    height_shift_range=0.1,
    shear_range=0.1,
    zoom_range=0.1,
    horizontal_flip=True,
    fill_mode = "nearest"  # 这里修正了拼写错误
)
from tensorflow.keras.preprocessing import image
import os
import matplotlib.pyplot as plt

# 确保你已经正确定义了train_cats_dir变量
fnames = sorted([os.path.join(train_cats_dir, fname) for fname in os.listdir(train_cats_dir)])
img_path = fnames[3]
img = image.load_img(img_path, target_size=(150, 150))
x = image.img_to_array(img) # (150, 150, 3)
x = x.reshape((1,) + x.shape) # (1, 150, 150, 3)
i = 0
for batch in datagen.flow(x, batch_size=1):
    plt.figure(i)
    imgplot = plt.imshow(image.array_to_img(batch[0]))
    i += 1
    if i % 4 == 0:
        break
plt.show()

另外我还补上了可能缺失的import os和import matplotlib.pyplot as plt语句(如果你的代码里没加的话),防止出现导入错误。

修正后,ImageDataGenerator就能正确识别填充模式,你想要的数据增强样本应该就能正常生成并展示啦!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 17:02:39