• 根据CSV文件生成ImageFolder格式数据集,并按比例划分训练集验证集


    根据csv文件分类,生成ImageFolder格式数据集

    import csv
    import shutil
    import os
    
    target_path = './train_split/'
    original_path = './train/'
    with open('train.csv', "rt", encoding="utf-8") as csvfile:
        reader = csv.reader(csvfile)
        rows = [row for row in reader]
        for row in rows:
            if os.path.exists(target_path + row[1]):
                full_path = original_path + row[0]
                shutil.move(full_path, target_path + row[1] + '/')
            else:
                os.makedirs(target_path + row[1])
                full_path = original_path + row[0]
                shutil.move(full_path, target_path + row[1] + '/')
    

    将ImageFolder格式数据集按比例划分训练集验证集

    import os
    import random
    import shutil
    
    dirct = './train/'
    dirList = []
    files = os.listdir(dirct)
    for f in files:
        if os.path.isdir(dirct + '/' + f):  #这里是绝对路径,该句判断目录是否是文件夹
            dirList.append(f)
    
    #print(dirList)
    
    org_dir = './train/'
    tar_dir = './train_proportion/'
    rate = 0.1
    if not os.path.exists(tar_dir):
        os.makedirs(tar_dir)
    for fdir in dirList:
        fileDir = org_dir + fdir + '/'
        tarDir = tar_dir + fdir + '/'
        pathDir = os.listdir(fileDir)  #scan
        filenumber = len(pathDir)
        picknumber = int(filenumber * rate)
        print('total {} pictures'.format(filenumber))
        print('moved {} pictures to {}'.format(picknumber, tarDir))
        if not os.path.exists(tarDir):
            os.mkdir(tarDir)
        sample = random.sample(pathDir, picknumber)
        for name in sample:
            shutil.move(fileDir + name, tarDir + name)
            print(name)
        print('succeed moved {} pictures from {} to {}'.format(
            picknumber, fileDir, tarDir))
    
    
  • 相关阅读:
    [NOI2008] [bzoj1061] 志愿者招募
    [bzoj1070] 修车
    [群内模拟4.8] 定点爆破 后宫着♂火 签到题
    初识费用流 模板(spfa+slf优化) 餐巾计划问题
    Chromium的无锁线程模型C++代码示例
    JavaScript的Date类的函数特殊处理导致的问题
    ssh免密码快速登录配置
    container-with-most-water(最大蓄水问题)
    死锁
    美团2018春招编程题第一题 字符串距离 O(n)解法
  • 原文地址:https://www.cnblogs.com/Arsene-W/p/14019838.html
Copyright © 2020-2023  润新知