【脏数据 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 文本后便可正常训练!🏋️
相关推荐
火山引擎开发者社区2 小时前
TLS for DeepSeek Harness 可观测实践:从系统总览到会话复盘
人工智能
数字融合4 小时前
透明化地铁线视频孪生综合监控项目技术
大数据·人工智能·virtualenv
怕浪猫5 小时前
从 pre-execute 到 post-execute:AI Agent 调用工具时背后发生了什么
aigc·openai·ai编程
鼎艺创新科技5 小时前
不依赖 UE/Unity:我们如何从零搭建一套国产三维 GIS 渲染引擎
人工智能·算法·unity·游戏引擎·三维电子沙盘
十三画者5 小时前
【文献分享】ConfRetro:融合3D构象信息的逆合成预测Transformer框架
人工智能·深度学习·数据挖掘·数据分析·transformer·数据可视化
前沿在线5 小时前
百度文心助手推出任务引擎 2.0,日活用户同比增长 83%,日均对话轮次增长超 2 倍
人工智能·ai·大模型
zandy10115 小时前
AI办公工具选哪个?千问办公、百度搭子、WorkBuddy三款高阶智能体深度拆解
人工智能·ai办公工具
mengpp_1234565 小时前
AIoT平台 vs 普通IoT平台 核心区别
人工智能
canonical_entropy5 小时前
可逆不是逆向运行:DeepSeek Harness 架构的数学本质
人工智能·架构·agent
别动我齐刘海6 小时前
机器人运动控制学习2——基础进阶
c++·人工智能·神经网络·学习·目标检测·机器学习·机器人