【脏数据 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 文本后便可正常训练!🏋️
相关推荐
醉卧考场君莫笑4 分钟前
NLP(正向,逆向,双向匹配法分词及代码实现)
人工智能·自然语言处理·easyui
拓朗工控11 分钟前
视觉革命:独立显卡工控机在医疗领域的深度应用
人工智能·智慧医疗·工控机
victory043114 分钟前
2026年4月22日 Malicious Finetuning for LLM via Steganography 解读 复现要点
人工智能
Python私教26 分钟前
Hermes Agent 技能系统:让 AI 学会自我进化
人工智能
小饕28 分钟前
RAG学习之- RAG 数据导入完整指南
人工智能·python·学习
黑客说30 分钟前
白日梦无限世界 各类型副本分析
人工智能·科技·游戏·娱乐
wearegogog12332 分钟前
基于和差波束法的单脉冲测角MATLAB实现
人工智能·算法·matlab
陈广亮41 分钟前
一个 CLAUDE.md 文件,一周涨了 44K Star:Karpathy 的 AI 编程四原则
人工智能
三秋树41 分钟前
豆包 Agent Harness 工程师入门 | 第 5 章 Skills 技能
人工智能·agent·ai编程
一线数智42 分钟前
从数字化到数智化: AI 赋能零售/餐饮高效运营
人工智能·零售