我根据视频分类之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()
但是运行会出现这样:
请问这是为啥?
第一步先下载数据集,然后解压
打开某个视频类别文件夹,如下图所示
接下来看看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的第一个参数是原始视频的位置,这个原代码没写对
应该是文件路径不对,你看看其他文件还有没有这些路径设置
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!
速戳参与调研>>>https://t.csdnimg.cn/Kf0y