【脏数据 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 文本后便可正常训练!🏋️
相关推荐
敲代码的小霖3 分钟前
novanovastudio 开源漫剧短剧无限画布项目发布v0.1.9版本 3D导演台开源
ai·aigc·开源软件·无限画布·3d导演台
峥嵘life5 分钟前
Android16 Camera2录像中拍照后预览模糊问题分析与解决总结
人工智能·数码相机·wireshark
漫步人生走在路上9 分钟前
用小语种SEO突破外贸B2B天花板:法语、德语市场的实测数据
开发语言·前端·javascript·人工智能·搜索引擎·ecmascript
二川bro16 分钟前
签到自动化
人工智能
白日放歌须纵久21 分钟前
Agent 工具契约测试工程:适配层隔离与 Mock 回放,让接口变更不再静默击穿你的工作流
人工智能
火山引擎开发者社区22 分钟前
一句话,做出一个应用:豆包背后的数据库底座是什么?
人工智能
humors22124 分钟前
如何识别网络假信息
网络·人工智能·ai·互联网·识别·谣言·假消息
技灵AI25 分钟前
AI 奶茶广告分镜提示词怎么写:从参考图到芒果奶茶广告片(附全文)
人工智能·prompt·aigc
阿乔外贸日记28 分钟前
美国买家资产包装套路与应收款尽调框架,580万应收账款案例解析
大数据·服务器·人工智能·microsoft·云计算
面壁的熊猫29 分钟前
Jev-LiteMAS 论文解读:把多智能体工作流里的「决策」与「生成」拆开
人工智能