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

为何仅x_train出现setting an array element with a sequence错误及重塑失败?

图像数据集处理异常问题

处理图像数据集时,仅对x_train执行操作出现异常:

  • 将其转换为numpy数组并归一化时抛出ValueError: setting an array element with a sequence错误,注释x_train相关代码后无问题。
  • 改用dtype = object后,执行reshape操作又抛出ValueError: cannot reshape array of size 1374 into shape (224,224,3)错误,但x_val和x_test无此类问题。

初始错误信息

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
TypeError: only size-1 arrays can be converted to Python scalars

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
<ipython-input-19-86f84f4d44b9> in <cell line: 1>()
----> 1 x_train = np.array(x_train, dtype = np.float16) / 255
      2 x_val = np.array(x_val, dtype = np.float16) / 255.0
      3 x_test = np.array(x_test, dtype = np.float16) / 255.0
      4 x_train = x_train.reshape(-1, 224, 224, 3)
      5 x_val = x_val.reshape(-1, 224, 224, 3)

ValueError: setting an array element with a sequence.

相关代码

def convert_image_to_array(image_dir):
  try:
    image = cv2.imread(image_dir)
    if image is not None:
      image = cv2.resize(image, (224,224))
      return img_to_array(image)
    else:
      return np.array([])
  except Exception as e:
    print(f"Error : {e}")
    return None

image_list_train, label_list_train = [], []

all_labels = [
    'Healthy Potato',
    'Potato early blight',
    'Rice neck blast',
    'Wheat leaf septoria',
    'Healthy Rice',
    'Potato late blight',
    'Tomato Early blight leaf',
    'Wheat leaf stripe rust',
    'Healthy Tomato',
    'Rice brown spot',
    'Tomato leaf late blight',
    'Healthy Wheat',
    'Rice leaf blast',
    'Wheat brown rust'
]

binary_labels = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

temp = -1

for directory in all_labels:
    plant_image_list = listdir(f"{dir_train}/{directory}")
    temp += 1
    for files in plant_image_list:
      image_path = f"{dir_train}/{directory}/{files}"
      image_list_train.append(convert_image_to_array(image_path))
      label_list_train.append(binary_labels[temp])

x_train, x_val, y_train, y_val = train_test_split(image_list_train, label_list_train, test_size = 0.2)

x_train = np.array(x_train, dtype = np.float16) / 255
x_val = np.array(x_val, dtype = np.float16) / 255.0
x_test = np.array(x_test, dtype = np.float16) / 255.0
x_train = x_train.reshape(-1, 224, 224, 3)
x_val = x_val.reshape(-1, 224, 224, 3)
x_test = x_test.reshape(-1, 224, 224, 3)

改用object dtype后的错误信息

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-20-3411c6f1375e> in <cell line: 4>()
      2 x_val = np.array(x_val, dtype = object) / 255.0
      3 x_test = np.array(x_test, dtype = object) / 255.0
----> 4 x_train = x_train.reshape(-1, 224, 224, 3)
      5 x_val = x_val.reshape(-1, 224, 224, 3)
      6 x_test = x_test.reshape(-1, 224, 224, 3)

ValueError: cannot reshape array of size 1374 into shape (224,224,3)

数组形状输出

x train shape: (1374,)
x val shape: (344, 224, 224, 3)
x test shape: (141, 224, 224, 3)

问题原因及解决方法

核心原因

image_list_train中存在空数组或None值:

  • 当cv2.imread读取失败返回None时,函数返回np.array([]);当读取发生异常时返回None。这些异常值混入正常的(224,224,3)数组中,导致x_train无法被转换为规整的多维数组,只能变成形状为(1374,)的object数组。

解决步骤

  1. 过滤异常数据:构建训练集时只保留有效图像数组,同步过滤对应标签:

    for files in plant_image_list:
      image_path = f"{dir_train}/{directory}/{files}"
      img_arr = convert_image_to_array(image_path)
      # 仅保留形状符合要求的有效数组
      if img_arr is not None and img_arr.shape == (224,224,3):
        image_list_train.append(img_arr)
        label_list_train.append(binary_labels[temp])
    
  2. 统一异常返回值:修改图像转换函数,避免混合空数组和None:

    def convert_image_to_array(image_dir):
      try:
        image = cv2.imread(image_dir)
        if image is not None:
          image = cv2.resize(image, (224,224))
          return img_to_array(image)
        # 读取失败统一返回None
        return None
      except Exception as e:
        print(f"处理图像失败 {image_dir}: {e}")
        return None
    
  3. 验证数据有效性:转换为numpy数组前检查异常元素:

    # 统计训练集中的无效图像数量
    invalid_count = sum(1 for arr in image_list_train if arr is None or arr.shape != (224,224,3))
    print(f"训练集中发现 {invalid_count} 张无效图像")
    
  4. 重新转换数据:过滤后再执行归一化和reshape,此时x_train会自动形成规整的(N,224,224,3)形状:

    x_train = np.array(image_list_train, dtype=np.float16) / 255.0
    print(f"过滤后x_train形状: {x_train.shape}")
    x_train = x_train.reshape(-1, 224, 224, 3)
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 02:22:04