深度学习——划分自定义数据集

深度学习------划分自定义数据集

以人脸表情数据集raf_db为例,初始目录如下:

需要经过处理后返回

train_images, train_label, val_images, val_label

定义 read_split_data(root: str, val_rate: float = 0.2) 方法来解决,代码如下:

python 复制代码
# root:数据集所在路径
# val_rate:划分测试集的比例

def read_split_data(root: str, val_rate: float = 0.2):

    random.seed(0)  # 保证随机结果可复现
    assert os.path.exists(root), "dataset root: {} does not exist.".format(root)

    # 遍历文件夹,一个文件夹对应一个类别
    file_class = [cla for cla in os.listdir(root) if os.path.isdir(os.path.join(root, cla))]
    # 排序,保证各平台顺序一致
    file_class.sort()
    # 生成类别名称以及对应的数字索引
    class_indices = dict((k, v) for v, k in enumerate(file_class))
    json_str = json.dumps(dict((val, key) for key, val in class_indices.items()), indent=4)
    with open('class_indices.json', 'w') as json_file:
        json_file.write(json_str)

    train_images = []  # 存储训练集的所有图片路径
    train_label = []  # 存储训练集图片对应索引信息
    val_images = []  # 存储验证集的所有图片路径
    val_label = []  # 存储验证集图片对应索引信息
    every_class_num = []  # 存储每个类别的样本总数
    supported = [".jpg", ".JPG", ".png", ".PNG"]  # 支持的文件后缀类型

    # 遍历每个文件夹下的文件
    for cla in file_class:
        cla_path = os.path.join(root, cla)
        # 遍历获取supported支持的所有文件路径
        images = [os.path.join(root, cla, i) for i in os.listdir(cla_path)
                  if os.path.splitext(i)[-1] in supported]
        # 排序,保证各平台顺序一致
        images.sort()
        # 获取该类别对应的索引
        image_class = class_indices[cla]
        # 记录该类别的样本数量
        every_class_num.append(len(images))
        # 按比例随机采样验证样本
        val_path = random.sample(images, k=int(len(images) * val_rate))

        for img_path in images:
            if img_path in val_path:  # 如果该路径在采样的验证集样本中则存入验证集
                val_images.append(img_path)
                val_label.append(image_class)
            else:  # 否则存入训练集
                train_images.append(img_path)
                train_label.append(image_class)

    print("{} images were found in the dataset.".format(sum(every_class_num)))
    print("{} images for training.".format(len(train_images)))
    print("{} images for validation.".format(len(val_images)))
    assert len(train_images) > 0, "number of training images must greater than 0."
    assert len(val_images) > 0, "number of validation images must greater than 0."

    return train_images, train_label, val_images, val_label

此时可通过以下代码获得训练集和测试集数据:

python 复制代码
train_images, train_label, val_images, val_label = read_split_data(data_path)

完结撒花。

相关推荐
AI人工智能+9 分钟前
发票识别技术:结合OCR与AI技术,实现纸质票据高效数字化,推动企业智能化转型
人工智能·nlp·ocr·发票识别
机器学习之心12 分钟前
198种组合算法+优化CNN卷积神经网络+SHAP分析+新数据预测+多输出!深度学习可解释分析,强烈安利,粉丝必备!
深度学习·shap分析·优化cnn卷积神经网络
用户51914958484516 分钟前
Aniyomi扩展开发指南与Google Drive集成方案
人工智能·aigc
ezl1fe18 分钟前
第零篇:把 Agent 跑起来的最小闭环
人工智能·后端·agent
说私域21 分钟前
开源链动2+1模式AI智能名片S2B2C商城小程序在竞争激烈的中低端面膜服装行业中的应用与策略
大数据·人工智能·小程序
佛喜酱的AI实践23 分钟前
Claude Code配置魔法:从单人编程到专属AI团队协作
人工智能·claude
文心快码BaiduComate25 分钟前
文心快码Comate3.5S更新,用多智能体协同做个健康管理应用
前端·人工智能·后端
叶楊27 分钟前
PEFT适配器加载
人工智能·深度学习·机器学习
Tezign_space33 分钟前
AI用户洞察新纪元:atypica.AI如何重塑商业决策逻辑
人工智能·ai智能体·atypica
却道天凉_好个秋34 分钟前
OpenCV(十一):色彩空间转换
人工智能·opencv·计算机视觉