提交 4048b3be 编写于 作者: L LiuChaoXD

add refined tsn model

上级 f5e723e7
import argparse
import os
import glob
import fnmatch
import random
def parse_directory(path,
key_func=lambda x: x[-11:],
rgb_prefix='img_',
level=1):
"""
Parse directories holding extracted frames from standard benchmarks
"""
print('parse frames under folder {}'.format(path))
if level == 1:
frame_folders = glob.glob(os.path.join(path, '*'))
elif level == 2:
frame_folders = glob.glob(os.path.join(path, '*', '*'))
else:
raise ValueError('level can be only 1 or 2')
def count_files(directory, prefix_list):
lst = os.listdir(directory)
cnt_list = [len(fnmatch.filter(lst, x + '*')) for x in prefix_list]
return cnt_list
# check RGB
frame_dict = {}
for i, f in enumerate(frame_folders):
all_cnt = count_files(f, (rgb_prefix))
k = key_func(f)
x_cnt = all_cnt[1]
y_cnt = all_cnt[2]
if x_cnt != y_cnt:
raise ValueError('x and y direction have different number '
'of flow images. video: ' + f)
if i % 200 == 0:
print('{} videos parsed'.format(i))
frame_dict[k] = (f, all_cnt[0], x_cnt)
print('frame folder analysis done')
return frame_dict
def build_split_list(split, frame_info, shuffle=False):
def build_set_list(set_list):
rgb_list = list()
for item in set_list:
if item[0] not in frame_info:
# print("item:", item)
continue
elif frame_info[item[0]][1] > 0:
rgb_cnt = frame_info[item[0]][1]
rgb_list.append('{} {} {}\n'.format(item[0], rgb_cnt, item[1]))
else:
rgb_list.append('{} {}\n'.format(item[0], item[1]))
if shuffle:
random.shuffle(rgb_list)
return rgb_list
train_rgb_list = build_set_list(split[0])
test_rgb_list = build_set_list(split[1])
return (train_rgb_list, test_rgb_list)
def parse_ucf101_splits(level):
class_ind = [x.strip().split() for x in open('./annotations/classInd.txt')]
class_mapping = {x[1]: int(x[0]) - 1 for x in class_ind}
def line2rec(line):
items = line.strip().split(' ')
vid = items[0].split('.')[0]
vid = '/'.join(vid.split('/')[-level:])
label = class_mapping[items[0].split('/')[0]]
return vid, label
splits = []
for i in range(1, 4):
train_list = [
line2rec(x)
for x in open('./annotations/trainlist{:02d}.txt'.format(i))
]
test_list = [
line2rec(x)
for x in open('./annotations/testlist{:02d}.txt'.format(i))
]
splits.append((train_list, test_list))
return splits
def parse_args():
parser = argparse.ArgumentParser(description='Build file list')
parser.add_argument(
'frame_path', type=str, help='root directory for the frames')
parser.add_argument('--rgb_prefix', type=str, default='img_')
parser.add_argument('--num_split', type=int, default=3)
parser.add_argument(
'--subset', type=str, default='train',
choices=['train', 'val', 'test'])
parser.add_argument('--level', type=int, default=2, choices=[1, 2])
parser.add_argument(
'--format',
type=str,
default='rawframes',
choices=['rawframes', 'videos'])
parser.add_argument('--out_list_path', type=str, default='./')
parser.add_argument('--shuffle', action='store_true', default=False)
args = parser.parse_args()
return args
def main():
args = parse_args()
if args.level == 2:
def key_func(x):
return '/'.join(x.split('/')[-2:])
else:
def key_func(x):
return x.split('/')[-1]
if args.format == 'rawframes':
frame_info = parse_directory(
args.frame_path,
key_func=key_func,
rgb_prefix=args.rgb_prefix,
level=args.level)
elif args.format == 'videos':
if args.level == 1:
video_list = glob.glob(os.path.join(args.frame_path, '*'))
elif args.level == 2:
video_list = glob.glob(os.path.join(args.frame_path, '*', '*'))
frame_info = {
os.path.relpath(x.split('.')[0], args.frame_path): (x, -1, -1)
for x in video_list
}
split_tp = parse_ucf101_splits(args.level)
assert len(split_tp) == args.num_split
out_path = args.out_list_path
if len(split_tp) > 1:
for i, split in enumerate(split_tp):
lists = build_split_list(
split_tp[i], frame_info, shuffle=args.shuffle)
filename = 'ucf101_train_split_{}_{}.txt'.format(i + 1, args.format)
with open(os.path.join(out_path, filename), 'w') as f:
f.writelines(lists[0])
filename = 'ucf101_val_split_{}_{}.txt'.format(i + 1, args.format)
with open(os.path.join(out_path, filename), 'w') as f:
f.writelines(lists[1])
else:
lists = build_split_list(split_tp[0], frame_info, shuffle=args.shuffle)
filename = '{}_{}_list_{}.txt'.format(args.dataset, args.subset,
args.format)
if args.subset == 'train':
ind = 0
elif args.subset == 'val':
ind = 1
elif args.subset == 'test':
ind = 2
with open(os.path.join(out_path, filename), 'w') as f:
f.writelines(lists[0][ind])
if __name__ == "__main__":
main()
import argparse
import sys
import os
import os.path as osp
import glob
from pipes import quote
from multiprocessing import Pool, current_process
import cv2
def dump_frames(vid_item):
full_path, vid_path, vid_id = vid_item
vid_name = vid_path.split('.')[0]
out_full_path = osp.join(args.out_dir, vid_name)
try:
os.mkdir(out_full_path)
except OSError:
pass
vr = cv2.VideoCapture(full_path)
videolen = int(vr.get(cv2.CAP_PROP_FRAME_COUNT))
for i in range(videolen):
ret, frame = vr.read()
if ret == False:
continue
img = frame[:, :, ::-1]
# covert the BGR img into RGB img
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
if img is not None:
cv2.imwrite('{}/img_{:05d}.jpg'.format(out_full_path, i + 1), img)
else:
print('[Warning] length inconsistent!'
'Early stop with {} out of {} frames'.format(i + 1, videolen))
break
print('{} done with {} frames'.format(vid_name, videolen))
sys.stdout.flush()
return True
def parse_args():
parser = argparse.ArgumentParser(description='extract optical flows')
parser.add_argument('src_dir', type=str)
parser.add_argument('out_dir', type=str)
parser.add_argument('--level', type=int, choices=[1, 2], default=2)
parser.add_argument('--num_worker', type=int, default=8)
parser.add_argument(
"--out_format",
type=str,
default='dir',
choices=['dir', 'zip'],
help='output format')
parser.add_argument(
"--ext",
type=str,
default='avi',
choices=['avi', 'mp4'],
help='video file extensions')
parser.add_argument(
"--new_width", type=int, default=0, help='resize image width')
parser.add_argument(
"--new_height", type=int, default=0, help='resize image height')
parser.add_argument(
"--resume",
action='store_true',
default=False,
help='resume optical flow extraction '
'instead of overwriting')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_args()
if not osp.isdir(args.out_dir):
print('Creating folder: {}'.format(args.out_dir))
os.makedirs(args.out_dir)
if args.level == 2:
classes = os.listdir(args.src_dir)
for classname in classes:
new_dir = osp.join(args.out_dir, classname)
if not osp.isdir(new_dir):
print('Creating folder: {}'.format(new_dir))
os.makedirs(new_dir)
print('Reading videos from folder: ', args.src_dir)
print('Extension of videos: ', args.ext)
if args.level == 2:
fullpath_list = glob.glob(args.src_dir + '/*/*.' + args.ext)
done_fullpath_list = glob.glob(args.out_dir + '/*/*')
elif args.level == 1:
fullpath_list = glob.glob(args.src_dir + '/*.' + args.ext)
done_fullpath_list = glob.glob(args.out_dir + '/*')
print('Total number of videos found: ', len(fullpath_list))
if args.resume:
fullpath_list = set(fullpath_list).difference(set(done_fullpath_list))
fullpath_list = list(fullpath_list)
print('Resuming. number of videos to be done: ', len(fullpath_list))
if args.level == 2:
vid_list = list(
map(lambda p: osp.join('/'.join(p.split('/')[-2:])), fullpath_list))
elif args.level == 1:
vid_list = list(map(lambda p: p.split('/')[-1], fullpath_list))
pool = Pool(args.num_worker)
pool.map(dump_frames, zip(fullpath_list, vid_list, range(len(vid_list))))
# extract_rawframes_opencv.py
## 应用说明
### 对于kinetics400数据
运行脚本的命令如下 `python extract_rawframes_opencv.py ./video/ ./rawframes/ --level 2 --ext mp4` 或者`python extract_rawframes_opencv.py ./video/ ./rawframes/ --level 2 --ext mp4`
### 参数说明
`./video/` : 这个参数表示视频目录的地址
`./rawframes` : 提取出的frames的存放目录
`--level 1 or 2`
level 1,表示video的存储方式为
------ video
|------ xajhljklk.mp4
|------ jjkjlljjk.mp4
....
level 2, 表示video的存储方式为
------ video
|------ class1
|-------- xajhljklk.mp4
|-------- jjkjlljjk.mp4
....
`--ext 4` : 表示视频文件的格式。
......@@ -6,9 +6,7 @@ use_gpu=True
use_data_parallel=True
export CUDA_VISIBLE_DEVICES=0,1,2,3
export FLAGS_fast_eager_deletion_mode=1
export FLAGS_eager_delete_tensor_gb=0.0
export FLAGS_fraction_of_gpu_memory_to_use=0.98
echo $mode "TSN" $configs $resume $pretrain
if [ "$resume"x != ""x ]; then
......
......@@ -3,9 +3,7 @@ use_gpu=True
use_data_parallel=False
export CUDA_VISIBLE_DEVICES=0
export FLAGS_fast_eager_deletion_mode=1
export FLAGS_eager_delete_tensor_gb=0.0
export FLAGS_fraction_of_gpu_memory_to_use=0.98
echo $mode $configs $weights
......
......@@ -7,9 +7,7 @@ use_data_parallel=False
weights="" #set the path of weights to enable eval and predicut, just ignore this when training
export CUDA_VISIBLE_DEVICES=0
export FLAGS_fast_eager_deletion_mode=1
export FLAGS_eager_delete_tensor_gb=0.0
export FLAGS_fraction_of_gpu_memory_to_use=0.98
echo $mode "TSN" $configs $resume $pretrain
if [ "$resume"x != ""x ]; then
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册