图像文本联合训练时的数据清洗流程设计
在多模态大模型训练中,数据质量直接影响模型性能。本文将详细介绍图像-文本联合训练中的数据清洗流程。
数据预处理管道
import pandas as pd
import cv2
import numpy as np
from PIL import Image
import os
class MultimodalDataCleaner:
def __init__(self, min_text_len=10, max_text_len=500):
self.min_text_len = min_text_len
self.max_text_len = max_text_len
def clean_image_data(self, image_path):
# 1. 图像质量检测
try:
img = cv2.imread(image_path)
if img is None:
return False, "图像读取失败"
# 检查图像尺寸
height, width = img.shape[:2]
if width < 64 or height < 64:
return False, "图像过小"
# 检查图像清晰度
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.Laplacian(gray, cv2.CV_64F).var()
if blur < 100:
return False, "图像模糊"
return True, "图像质量合格"
except Exception as e:
return False, f"图像处理异常: {str(e)}"
def clean_text_data(self, text):
# 2. 文本质量检测
if not isinstance(text, str):
return False, "文本格式错误"
if len(text) < self.min_text_len or len(text) > self.max_text_len:
return False, "文本长度异常"
# 检查文本内容
text = text.strip()
if not text or text.isspace():
return False, "文本为空"
# 检查是否包含无效字符
invalid_chars = ['<', '>', '&', '"']
for char in invalid_chars:
if char in text:
return False, "包含非法字符"
return True, "文本质量合格"
def clean_pair_data(self, image_path, text):
# 3. 联合数据验证
img_valid, img_msg = self.clean_image_data(image_path)
txt_valid, txt_msg = self.clean_text_data(text)
if not img_valid:
return False, f"图像问题: {img_msg}"
if not txt_valid:
return False, f"文本问题: {txt_msg}"
return True, "数据对合格"
数据清洗流程步骤
- 数据导入:读取包含图片路径和文本描述的CSV文件
- 图像验证:检查图像是否存在、尺寸是否合理、清晰度是否达标
- 文本验证:验证文本长度、格式、内容合法性
- 联合过滤:确保每对图像-文本数据都符合要求
可复现代码示例
# 使用示例
cleaner = MultimodalDataCleaner()
# 读取数据
df = pd.read_csv('multimodal_data.csv')
cleaned_data = []
for idx, row in df.iterrows():
image_path = row['image_path']
text = row['caption']
is_valid, message = cleaner.clean_pair_data(image_path, text)
if is_valid:
cleaned_data.append(row)
else:
print(f"跳过数据行{idx}: {message}")
# 保存清洗后数据
cleaned_df = pd.DataFrame(cleaned_data)
cleaned_df.to_csv('cleaned_multimodal_data.csv', index=False)```
该流程确保了训练数据的一致性和可靠性,为后续模型训练奠定基础。
讨论