FaceNet-pytorch(fixing data imbalance-CASIA)

# ------------------------------------------------#
#   进行训练前需要利用这个文件生成cls_train.txt
# ------------------------------------------------#
import os
import random


def txt_annotation(datasets_path, select_num):
    '''
    :param datasets_path: 数据集根目录
    :param select_num: 每一个folder选择的文件数目
    :return: cls_train.txt
    '''

    types_name = os.listdir (datasets_path)

    types_name = sorted (types_name)

    # print (types_name)
    list_file = open ('cls_train.txt', 'w')
    for cls_id, type_name in enumerate (types_name):
        photos_path = os.path.join (datasets_path, type_name)
        if not os.path.isdir (photos_path) or select_num > len (os.listdir (photos_path)):
            print (
                'pass folder {} with {} numbers , required {} '.format (type_name, len (os.listdir (photos_path)),
                                                                        select_num))
            continue

        photos_name = os.listdir (photos_path)
        photos_name = random.sample (photos_name, select_num)
        # print(photos_name)

        for photo_name in photos_name:
            # print (cls_id)
            list_file.write (
                str (cls_id) + ";" + '%s' % (os.path.join (os.path.abspath (datasets_path), type_name, photo_name)))
            list_file.write ('\n')
    list_file.close ()


if __name__ == "__main__":
    dataset_path = r'test_dataset'
    select_num = 32

    txt_annotation (dataset_path, select_num)

效果如下:
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值