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

ImageDataGenerator数据增强报错求助:RuntimeError边界模式不支持

问题解决:图像增强代码循环报错

问题描述

代码在for循环执行前能正常读取并打印图像数组,但执行循环时抛出RuntimeError: boundary mode not supported错误,需求是将输入的jpg图像生成增强图像并保存到指定目录。

用户代码

import keras 
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator

data_gen = tf.keras.preprocessing.image.ImageDataGenerator(
    rotation_range=45,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode='contrast',
    cval=125
)

x = io.imread('mona.jpg')
x = x.reshape((1, ) + x.shape)  #Array with shape (1, 256, 256, 3)

i = 0
for batch in data_gen.flow(x, batch_size=16, save_to_dir='/Users/ghad/Desktop', 
                          save_prefix='aug', 
                          save_format='jpg'):
    i += 1
    if i > 20: 

报错信息

RuntimeError                              Traceback (most recent call last)
Input In [14], in <cell line: 31>()
     28 x = x.reshape((1, ) + x.shape)  #Array with shape (1, 256, 256, 3)
     30 i = 0
---> 31 for batch in data_gen.flow(x, batch_size=16,  
     32                           save_to_dir='/Users/ghadahalhabib/Desktop', 
     33                           save_prefix='aug', 
     34                           save_format='jpg'):
     35     i += 1
     36     if i > 20:

File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/keras/preprocessing/image.py:148, in Iterator.__next__(self, *args, **kwargs)
    147 def __next__(self, *args, **kwargs):
--> 148   return self.next(*args, **kwargs)

File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/keras/preprocessing/image.py:160, in Iterator.next(self)
    157   index_array = next(self.index_generator)
    158 # The transformation of images is not under thread lock
    159 # so it can be done in parallel
--> 160 return self._get_batches_of_transformed_samples(index_array)

File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/keras/preprocessing/image.py:709, in NumpyArrayIterator._get_batches_of_transformed_samples(self, index_array)
    707 x = self.x[j]
    708 params = self.image_data_generator.get_random_transform(x.shape)
--> 709 x = self.image_data_generator.apply_transform(
    710     x.astype(self.dtype), params)
    711 x = self.image_data_generator.standardize(x)
    712 batch_x[i] = x

File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/keras/preprocessing/image.py:1800, in ImageDataGenerator.apply_transform(self, x, transform_parameters)
   1797 img_col_axis = self.col_axis - 1
   1798 img_channel_axis = self.channel_axis - 1
-> 1800 x = apply_affine_transform(
   1801     x,
   1802     transform_parameters.get('theta', 0),
   1803     transform_parameters.get('tx', 0),
   1804     transform_parameters.get('ty', 0),
   1805     transform_parameters.get('shear', 0),
   1806     transform_parameters.get('zx', 1),
   1807     transform_parameters.get('zy', 1),
   1808     row_axis=img_row_axis,
   1809     col_axis=img_col_axis,
   1810     channel_axis=img_channel_axis,
   1811     fill_mode=self.fill_mode,
   1812     cval=self.cval,
   1813     order=self.interpolation_order)
   1815 if transform_parameters.get('channel_shift_intensity') is not None:
   1816   x = apply_channel_shift(x,
   1817                           transform_parameters['channel_shift_intensity'],
   1818                           img_channel_axis)

File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/keras/preprocessing/image.py:2324, in apply_affine_transform(x, theta, tx, ty, shear, zx, zy, row_axis, col_axis, channel_axis, fill_mode, cval, order)
   2321 final_affine_matrix = transform_matrix[:2, :2]
   2322 final_offset = transform_matrix[:2, 2]
-> 2324 channel_images = [ndimage.interpolation.affine_transform(  # pylint: disable=g-complex-comprehension
   2325     x_channel,
   2326     final_affine_matrix,
   2327     final_offset,
   2328     order=order,
   2329     mode=fill_mode,
   2330     cval=cval) for x_channel in x]
   2331 x = np.stack(channel_images, axis=0)
   2332 x = np.rollaxis(x, 0, channel_axis + 1)

File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/keras/preprocessing/image.py:2324, in <listcomp>(.0)
   2321 final_affine_matrix = transform_matrix[:2, :2]
   2322 final_offset = transform_matrix[:2, 2]
-> 2324 channel_images = [ndimage.interpolation.affine_transform(  # pylint: disable=g-complex-comprehension
   2325     x_channel,
   2326     final_affine_matrix,
   2327     final_offset,
   2328     order=order,
   2329     mode=fill_mode,
   2330     cval=cval) for x_channel in x]
   2331 x = np.stack(channel_images, axis=0)
   2332 x = np.rollaxis(x, 0, channel_axis + 1)

File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/scipy/ndimage/interpolation.py:574, in affine_transform(input, matrix, offset, output_shape, output, order, mode, cval, prefilter)
    572     npad = 0
    573     filtered = input
--> 574 mode = _ni_support._extend_mode_to_code(mode)
    575 matrix = numpy.asarray(matrix, dtype=numpy.float64)
    576 if matrix.ndim not in [1, 2] or matrix.shape[0] < 1:

File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/scipy/ndimage/_ni_support.py:54, in _extend_mode_to_code(mode)
     52     return 6
     53 else:
--> 54     raise RuntimeError('boundary mode not supported')

RuntimeError: boundary mode not supported

错误原因及修复方案

1. 无效的fill_mode参数

报错核心是你设置的fill_mode='contrast'不被底层依赖的scipy.ndimage支持。ImageDataGenerator的fill_mode仅支持以下有效值:

  • constant:用cval指定的值填充边界
  • nearest:用最近邻像素填充
  • reflect:镜像反射填充
  • wrap:环绕填充
  • edge:用边缘像素填充

2. 其他代码问题修复

  • 缺少io模块导入:需要添加from skimage import io来读取图像
  • 循环未终止:if i > 20后缺少break语句,会导致循环无限执行

修复后的完整代码

import keras 
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator
from skimage import io  # 补充导入io模块

data_gen = tf.keras.preprocessing.image.ImageDataGenerator(
    rotation_range=45,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode='constant',  # 替换为支持的填充模式
    cval=125
)

x = io.imread('mona.jpg')
x = x.reshape((1, ) + x.shape)  #Array with shape (1, 256, 256, 3)

i = 0
for batch in data_gen.flow(x, batch_size=16, save_to_dir='/Users/ghad/Desktop', 
                          save_prefix='aug', 
                          save_format='jpg'):
    i += 1
    if i > 20: 
        break  # 添加break终止循环

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 04:05:40