qq_42519463 2021-05-06 14:42 采纳率: 62.5%
浏览 65
已采纳

深度学习代码运行时数据集文件出现问题?

我根据视频分类之UCF-101上的CNN方法详解 - 知乎 (zhihu.com)介绍的方法下载了源代码和数据集,第一个py是移动文件代码。按此链接要求:

把数据集文件放在了data下。

其中ucfTrainTestlist文件夹是这样的:

数据集文件夹是这样的:

源代码中,我把两个文件路径改了:

"""
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
import os.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 = 'E:/Graduation Project/UCF-101_video_classification-master/data/ucfTrainTestlist/testlist' + version + '.txt'
    train_file = 'E:/Graduation Project/UCF-101_video_classification-master/data/ucfTrainTestlist/trainlist' + version + '.txt'

    # 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:

            # Get the parts.
            parts = video.split('/')
            classname = parts[0]
            filename = parts[1]

            # Check if this class exists.
            if not os.path.exists(group + '/' + classname):
                print("Creating folder for %s/%s" % (group, classname))
                os.makedirs(group + '/' + classname)

            # Check if we have already moved this file, or at least that it
            # exists to move.
            if not os.path.exists(filename):
                print("Can't find %s to move. Skipping." % (filename))
                continue

            # Move it.
            dest = group + '/' + classname + '/' + filename
            print("Moving %s to %s" % (filename, dest))
            os.rename(filename, dest)

    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()

    # Move the files.
    move_files(group_lists)

if __name__ == '__main__':
    main()

但是运行会出现这样:

请问这是为啥?

  • 写回答

4条回答 默认 最新

  • GitCode 官方 企业官方账号 2021-05-07 17:56
    关注

    第一步先下载数据集,然后解压

    打开某个视频类别文件夹,如下图所示

    接下来看看1_move_files.py这个python文件

    首先看下原始仓库代码的目录结构,如下图所示

    运行1_move_files.py之后,产生如下图所示的结果

    我修改过1_move_files.py代码了,我贴上来

    """
    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
    import os.path
    
    origin_data_url = 'C:/Users/My104/Downloads/data/UCF101/UCF-101/'
    
    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 = './ucfTrainTestlist/testlist' + version + '.txt'
        train_file = './ucfTrainTestlist/trainlist' + version + '.txt'
    
        # 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:
    
                # Get the parts.
                parts = video.split('/')
                classname = parts[0]
                filename = parts[1]
    
                # Check if this class exists.
                if not os.path.exists(group + '/' + classname):
                    print("Creating folder for %s/%s" % (group, classname))
                    os.makedirs(group + '/' + classname)
    
                # Move it.
                dest = group + '/' + classname + '/' + filename
                print("Moving %s to %s" % (origin_data_url + classname + '/' + filename, dest))
                os.rename(origin_data_url + classname + '/' + filename, dest)
    
        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()
    
        # Move the files.
        move_files(group_lists)
    
    if __name__ == '__main__':
        main()
    

    第一处更改是

    origin_data_url = 'C:/Users/My104/Downloads/data/UCF101/UCF-101/'

    这个是你的原始数据集下载解压后的位置(根据你的本地数据集位置,自己动手修改)

    第二处更改是删除了下面这部分代码

    if not os.path.exists(filename):
        print("Can't find %s to move. Skipping." % (filename))
        continue

    第三处修改是下面这部分代码

    dest = group + '/' + classname + '/' + filename
    print("Moving %s to %s" % (origin_data_url + classname + '/' + filename, dest))
    os.rename(origin_data_url + classname + '/' + filename, dest)

    os.rename的第一个参数是原始视频的位置,这个原代码没写对

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b