【脏数据 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 文本后便可正常训练!🏋️
相关推荐
计育韬10 小时前
Prezi+AI创意玩法:无限手绘地图图解演示文稿
人工智能·新媒体运营·微信公众平台
我的世界洛天依10 小时前
【VFrame・云瑶 R1 国语声库公测】RVC1006Nvidia 训练全记录|私人闭源虚拟歌手养成
人工智能·电脑
AI医影跨模态组学10 小时前
如何将影像组学特征与非小细胞肺癌脑转移瘤免疫微环境中的干扰素通路及CD8+ T细胞浸润建立关联,并解释与预后、免疫治疗响应的机制联系
人工智能·论文·医学·医学影像·影像组学·医学科研
qq_4112624210 小时前
基于 ESP32-S3 + VB6824 的四博 A1 AI 智能拍学机方案:事件驱动架构、拍照识别与语音交互实现
人工智能·智能音箱
ting945200010 小时前
动手学深度学习(PyTorch版)深度详解(6):现代卷积神经网络-从经典模型到图像分类实战
人工智能·分类·cnn
@不误正业10 小时前
第12章-端侧AI操作系统概述
人工智能
AI技术增长10 小时前
Pytorch图像去噪实战(十二):DDPM图像去噪完整训练流程,构建可复现扩散模型工程
pytorch·python·深度学习
Maynor99610 小时前
Codex 中国站正式上线!
人工智能·gpt·macos·github
qq_4112624210 小时前
四博 CozyLife AI 中控方案:基于 ESP32-C5 双频 Wi-Fi + 4G 打造智能家居语音控制入口
人工智能·智能家居
Change is good10 小时前
桌面型软件(如UE)AI测试工具
人工智能