【脏数据 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 文本后便可正常训练!🏋️
相关推荐
wuxinyan1235 小时前
大模型学习之路007:RAG 零基础入门教程(第四篇):生成侧核心技术与大模型集成
人工智能·学习·rag
亚鲁鲁5 小时前
02-启动流程
人工智能
kcuwu.5 小时前
机器学习入门:线性回归完全指南(含波士顿房价预测案例)
人工智能·机器学习·线性回归
幸运的大号暖贴6 小时前
解决Vibe Coding时Idea经常不自动git add问题
java·人工智能·git·intellij-idea·claudecode·opencode
MonkeyKing_sunyuhua6 小时前
什么是服务端 VAD 端点检测
人工智能·语音识别
ascarl20106 小时前
Linux.do 帖子整理:AI 调用 Chrome DevTools 调试前端页面
linux·前端·人工智能
qxq_sunshine6 小时前
从 CNN 到 Agent:给 DL 工程师的“智能体”入门黑话指南(概念篇)
人工智能·神经网络·cnn
郝学胜-神的一滴6 小时前
反向传播:神经网络的「灵魂」修炼法则
人工智能·pytorch·深度学习·神经网络·机器学习·数据挖掘
Tutankaaa6 小时前
知识竞赛软件SaaS版 vs 本地部署
人工智能·经验分享·笔记·学习
DanCheOo6 小时前
开源 | 我是怎么用 ai-memory 让 Cursor 每次开新对话都自动知道项目背景的
前端·人工智能·ai·ai编程