【脏数据 bug 解决】ValueError: mean must have 1 elements if it is an iterable, got 3

问题描述:

  1. 在训练模型的过程中,出现 clip_image_processor 无法处理数据的问题,说明数据集中很可能出现了脏数据。
  2. 本文使用的数据为 LAION-Aesthetics-V2-6.5plus,从 https://dagshub.com/DagsHub-Datasets/LAION-Aesthetics-V2-6.5plus 上下载的。
python 复制代码
Traceback (most recent call last):
...
  File "/xxx/check_train_data.py", line 69, in __getitem__
    raise e  # Re-raise the exception to halt the training process
    ^^^^^^^
  File "/xxx/check_train_data.py", line 64, in __getitem__
    clip_image = self.clip_image_processor(images=raw_image, return_tensors="pt").pixel_values
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/xxx/lib/python3.12/site-packages/transformers/image_processing_utils.py", line 41, in __call__
    return self.preprocess(images, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/xxx/lib/python3.12/site-packages/transformers/models/clip/image_processing_clip.py", line 341, in preprocess
    self.normalize(image=image, mean=image_mean, std=image_std, input_data_format=input_data_format)
  File "/xxx/lib/python3.12/site-packages/transformers/image_processing_utils.py", line 111, in normalize
    return normalize(
           ^^^^^^^^^^
  File "/xxx/lib/python3.12/site-packages/transformers/image_transforms.py", line 392, in normalize
    raise ValueError(f"mean must have {num_channels} elements if it is an iterable, got {len(mean)}")
ValueError: mean must have 1 elements if it is an iterable, got 3

解决方案:

  1. 将原代码的 clip_image = self.clip_image_processor 修改为 try、except 来找到导致报错的图片。
  2. 将加载数据的代码部分拎出,并遍历一遍。
python 复制代码
 # read image
 raw_image = Image.open(os.path.join(self.image_root_path, image_file))
 image = self.transform(raw_image.convert("RGB"))
 # clip_image = self.clip_image_processor(images=raw_image, return_tensors="pt").pixel_values
 try:
     clip_image = self.clip_image_processor(images=raw_image, return_tensors="pt").pixel_values
     print(f'image_file_{idx} processed with clip_image_processor: {image_file}')
 except Exception as e:
     print(f'Error processing image_file_{idx}: {image_file}')
     print(e)
     raise e  # Re-raise the exception to halt the training process
  1. 最终卡在 4235 附近的图片,通过肉眼观察,发现 4236 是图片空的😂
  2. 手动删除 4236 图片以及对应的 json 文本后便可正常训练!🏋️
相关推荐
LiFileHub几秒前
神经网络全栈指南:从经典架构到NL范式落地(附12套工程化模板)
人工智能
AI_56787 分钟前
智慧交通:基于边缘计算的信号灯智能调度系统
人工智能·边缘计算
min18112345611 分钟前
因果推理在机器学习中的集成路径
人工智能
小鸡吃米…16 分钟前
机器学习——生态系统
人工智能·机器学习
说私域23 分钟前
基于开源AI大模型、AI智能名片与商城小程序的购物中心“人货场车”全面数字化解决方案研究
人工智能·小程序·开源
丝斯201124 分钟前
AI学习笔记整理(38)——自然语言处理的‌基于深度学习的语言模型
人工智能·学习·自然语言处理
小毅&Nora26 分钟前
【人工智能】【大模型】大语言模型最新进展:2025年技术演进与实用指南
人工智能·语言模型·自然语言处理
Codebee26 分钟前
惊了!ooder-org藏提示词彩蛋|AI驱动工程典范,1小时焕新DSM全靠A2UI
人工智能·编程语言·全栈
小小工匠29 分钟前
AIGC - 使用 Nano Banana Pro 生成卡通信息图的完整指南
aigc
Coder_Boy_29 分钟前
基于SpringAI的智能平台基座开发-(二)
java·人工智能·springboot·aiops·langchain4j