rlaif-v_formatted
收藏魔搭社区2025-12-05 更新2025-02-15 收录
下载链接:
https://modelscope.cn/datasets/HuggingFaceH4/rlaif-v_formatted
下载链接
链接失效反馈官方服务:
资源简介:
```python
from datasets import load_dataset, features
def format(examples):
"""
Convert prompt from "xxx" to [{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": "xxx"}]}]
and chosen and rejected from "xxx" to [{"role": "assistant", "content": [{"type": "text", "text": "xxx"}]}].
Images are wrapped in a list.
"""
output = {"images": [], "prompt": [], "chosen": [], "rejected": []}
for image, question, chosen, rejected in zip(examples["image"], examples["question"], examples["chosen"], examples["rejected"]):
prompt = [{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": question}]}]
chosen = [{"role": "assistant", "content": [{"type": "text", "text": chosen}]}]
rejected = [{"role": "assistant", "content": [{"type": "text", "text": rejected}]}]
output["images"].append([image])
output["prompt"].append(prompt)
output["chosen"].append(chosen)
output["rejected"].append(rejected)
return output
dataset = load_dataset("openbmb/RLAIF-V-Dataset", split="train")
cols = dataset.column_names
dataset = dataset.map(format, batched=True, writer_batch_size=4, batch_size=4, remove_columns=cols)
f = dataset.features
f["images"] = features.Sequence(features.Image(decode=True)) # to avoid bytes
dataset = dataset.cast(f)
dataset = dataset.train_test_split(test_size=0.05)
dataset.push_to_hub("HuggingFaceH4/rlaif-v_formatted")
```
本脚本用于多模态对话数据集的格式化与预处理,具体流程如下:1. 导入依赖:从Hugging Face datasets库中导入load_dataset函数与features模块,用于数据集加载与特征定义。2. 定义格式化预处理函数format:该函数接收批量样本作为输入,将其转换为适配多模态对话模型的格式:将用户提示转换为包含图像与文本的用户角色对话消息结构,格式为[{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": "xxx"}]}];将选中的优质回复与被拒绝的劣质回复分别转换为助手角色的文本对话消息结构,格式为[{"role": "assistant", "content": [{"type": "text", "text": "xxx"}]}],其中图像将被封装为列表形式。函数内部初始化包含images、prompt、chosen、rejected四个字段的输出字典,遍历输入样本的图像、问题、选中回复与被拒绝回复字段,为每个样本构建符合格式的内容并追加至输出字典,最终返回格式化后的批量样本。3. 加载数据集:加载openbmb/RLAIF-V-Dataset数据集的训练划分。4. 应用预处理:获取数据集的原始列名,使用map方法以批处理模式调用format函数进行格式化,设置批大小与写入批大小均为4,并移除原始数据集的所有列以仅保留格式化后的字段。5. 更新特征结构:获取数据集的原始特征定义,将images字段重新定义为序列类型,其元素为支持解码的图像特征,以避免存储原始字节流形式的图像数据,随后将数据集转换为更新后的特征结构。6. 数据集划分:将格式化后的数据集按95%训练集、5%测试集的比例划分为训练子集与测试子集。7. 推送数据集:将处理完成的数据集推送至Hugging Face Hub的HuggingFaceH4/rlaif-v_formatted仓库,完成公开共享。
提供机构:
maas
创建时间:
2025-02-10



