如何把UCF101数据集分成训练集和测试集

“”"
After extracting the RAR, we run this to move all the files into
the appropriate train/test folders.

Should only run this file once!
“”"
import os
from os.path import exists
import shutil
from pathlib import Path

def get_train_test_lists(version=‘01’):
“”"
Using one of the train/test files (01, 02, or 03), get the filename
breakdowns we’ll later use to move everything.
“”"
# Get our files based on version.
test_file = os.path.join(‘ucfTrainTestlist’, ‘testlist’ + version + ‘.txt’)
# print(‘test_file路径:’,test_file)
train_file = os.path.join(‘ucfTrainTestlist’, ‘trainlist’ + version + ‘.txt’)
# print(‘train_file路径:’,train_file)

# Build the test list.
with open(test_file) as fin:
    test_list = [row.strip() for row in list(fin)]

# Build the train list. Extra step to remove the class index.
with open(train_file) as fin:
    train_list = [row.strip() for row in list(fin)]
    train_list = [row.split(' ')[0] for row in train_list]

# Set the groups in a dictionary.
file_groups = {
    'train': train_list,
    'test': test_list
}

return file_groups

def move_files(file_groups):
“”“This assumes all of our files are currently in this directory.
So move them to the appropriate spot. Only needs to happen once.
“””
# Do each of our groups.
for group, videos in file_groups.items():

    # Do each of our videos.
    for video in videos:

        # print('video:',video)
        # Get the parts.
        # parts = video.split(os.path.sep)
        parts = os.path.split(video)

        # print('不完整路径:',parts)
        classname = parts[0]
        # print('动作分类文件夹:'+classname)
        absolutepathname = ('D:/expressionDatabase/UCF101/')
        # print('绝对路径:',absolutepathname)
        filename = parts[1]
        print('文件名:' + filename)
        # print('源代码中要产生的路径是:',os.path.join(group, filename))
        # Check if this class exists.
        '''
        if not os.path.exists(os.path.join(group, filename)):
            print("Creating folder for %s/%s" % (group, filename))
            os.makedirs(os.path.join(group, filename))
        '''

        # os.path <module 'ntpath' from 'D:\\python37\\lib\\ntpath.py'>

        if not exists(absolutepathname + group + '/' + classname):
            # print("新建文件夹:", (absolutepathname + group+ '/' + classname))
            os.makedirs(absolutepathname + group + '/' + classname)

        # Check if we have already moved this file, or at least that it
        # exists to move.
        file_to_move = absolutepathname + 'UCF101/' + classname + '/' + filename
        if not os.path.exists(file_to_move):
            print("找不到要移动的文件.")
            continue

        # Move it.
        dest = Path(absolutepathname + group + '/' + classname + '/' + filename)
        print('目标路径:', dest)
        # os.rename(filename, dest)
        shutil.move(file_to_move, dest)
        print('move finished...')

print("Done.")

def main():
“”"
Go through each of our train/test text files and move the videos
to the right place.
“”"
# Get the videos in groups so we can move them.
group_lists = get_train_test_lists()
print(group_lists)
# Move the files.
move_files(group_lists)

if name == ‘main’:
main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值