【脏数据 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 文本后便可正常训练!🏋️
相关推荐
hao_wujing11 分钟前
人工智能视角下的安全:可视化如何塑造恶意软件检测
人工智能·安全
kyle~12 分钟前
计算机视觉---目标追踪(Object Tracking)概览
人工智能·深度学习·计算机视觉
ModelWhale13 分钟前
践行“科学智能”!和鲸打造 AI for Science 专属应用
人工智能·ai4s
白杨SEO营销15 分钟前
白杨SEO:不到7天,白杨SEO博客网站百度搜索显示和排名恢复正常!顺带说说上海线下GEO聚会分享和播客红利
人工智能·搜索引擎·百度
ywyy679827 分钟前
推客小程序系统开发:全栈式技术解决方案与行业赋能实践
大数据·人工智能·微信小程序·小程序·系统·推客系统·推客小程序
高工智能汽车39 分钟前
芯驰科技与安波福联合举办技术研讨会,深化智能汽车领域合作交流
人工智能·科技·汽车
计算机毕设源码分享88888839 分钟前
杭州创维智能科技有限公司偿债能力盈利提升方案
人工智能·microsoft
TDengine (老段)1 小时前
TDengine 在新能源领域的价值
java·大数据·数据库·人工智能·时序数据库·tdengine·涛思数据
智合同(小智)1 小时前
《告别低效签约!智合同如何用AI重构商业“契约时代”》——解析智能合约技术的爆发与行业变革
大数据·人工智能·重构·智能合约·合同管理·智合同·ai合同
智合同(小智)1 小时前
从纸质契约到智能契约:AI如何改写信任规则与商业效率?——从智能合约到监管科技,一场颠覆传统商业逻辑的技术革命
人工智能·科技·智能合约·法律·合同管理·智合同·效率革命