2023-06-19 16:39:27 +08:00
|
|
|
# 这个脚本提供mocap的基本运行接口
|
|
|
|
import os
|
|
|
|
from easymocap.config import Config, load_object
|
|
|
|
from tqdm import tqdm
|
|
|
|
|
2023-10-16 19:45:09 +08:00
|
|
|
def process(dataset, model, args):
|
2023-06-19 16:39:27 +08:00
|
|
|
ret_all = []
|
|
|
|
print('[Run] dataset has {} samples'.format(len(dataset)))
|
2023-10-16 19:45:09 +08:00
|
|
|
if args.num_workers == -1:
|
|
|
|
for i in tqdm(range(len(dataset)), desc='[Run]'):
|
|
|
|
data = dataset[i]
|
|
|
|
ret = model.at_step(data, i)
|
|
|
|
ret_all.append(ret)
|
|
|
|
else:
|
|
|
|
import torch
|
|
|
|
dataloader = torch.utils.data.DataLoader(dataset,
|
|
|
|
batch_size=1, num_workers=args.num_workers, shuffle=False, collate_fn=lambda x:x, drop_last=False)
|
|
|
|
index = 0
|
|
|
|
for data in tqdm(dataloader, desc='[Run]'):
|
|
|
|
data = data[0]
|
|
|
|
ret = model.at_step(data, index)
|
|
|
|
if not args.skip_final:
|
|
|
|
ret_all.append(ret)
|
|
|
|
index += 1
|
|
|
|
if not args.skip_final:
|
|
|
|
ret_all = model.at_final(ret_all)
|
2023-06-19 16:39:27 +08:00
|
|
|
|
|
|
|
def update_data_by_args(cfg_data, args):
|
|
|
|
if args.root is not None:
|
|
|
|
cfg_data.args.root = args.root
|
|
|
|
if args.subs is not None:
|
|
|
|
cfg_data.args.subs = args.subs
|
|
|
|
if args.subs_vis is not None:
|
|
|
|
cfg_data.args.subs_vis = args.subs_vis
|
|
|
|
if args.ranges is not None:
|
|
|
|
cfg_data.args.ranges = args.ranges
|
|
|
|
if args.cameras is not None:
|
|
|
|
cfg_data.args.reader.cameras.root = args.cameras
|
2023-10-16 19:45:09 +08:00
|
|
|
if args.skip_vis or args.skip_vis_step:
|
2023-06-19 16:39:27 +08:00
|
|
|
cfg_data.args.subs_vis = []
|
|
|
|
return cfg_data
|
|
|
|
|
|
|
|
def update_exp_by_args(cfg_exp, args):
|
|
|
|
opts_alias = []
|
|
|
|
if 'alias' in cfg_exp.keys():
|
|
|
|
for i in range(len(args.opt_exp)//2):
|
|
|
|
if args.opt_exp[i*2] in cfg_exp.alias.keys():
|
|
|
|
opts_alias.append(cfg_exp.alias[args.opt_exp[i*2]])
|
|
|
|
opts_alias.append(args.opt_exp[i*2+1])
|
|
|
|
cfg_exp.merge_from_list(opts_alias)
|
2023-07-10 22:11:38 +08:00
|
|
|
if args.skip_vis or args.skip_vis_step:
|
2023-06-19 16:39:27 +08:00
|
|
|
for key, val in cfg_exp.args.at_step.items():
|
|
|
|
if key.startswith('vis'):
|
|
|
|
val.skip = True
|
2023-07-10 22:11:38 +08:00
|
|
|
if args.skip_vis or args.skip_vis_final:
|
|
|
|
for key, val in cfg_exp.args.at_final.items():
|
2023-10-16 19:45:09 +08:00
|
|
|
if key.startswith('vis') or key == 'make_video':
|
2023-07-10 22:11:38 +08:00
|
|
|
val.skip = True
|
2023-06-19 16:39:27 +08:00
|
|
|
|
|
|
|
def load_cfg_from_file(cfg, args):
|
|
|
|
cfg = Config.load(cfg)
|
|
|
|
cfg_data = Config.load(cfg.data)
|
|
|
|
cfg_data.args.merge_from_other_cfg(cfg.data_opts)
|
|
|
|
cfg_data = update_data_by_args(cfg_data, args)
|
|
|
|
cfg_exp = Config.load(cfg.exp)
|
|
|
|
cfg_exp.args.merge_from_other_cfg(cfg.exp_opts)
|
|
|
|
update_exp_by_args(cfg_exp, args)
|
|
|
|
return cfg_data, cfg_exp
|
|
|
|
|
|
|
|
def load_cfg_from_cmd(args):
|
|
|
|
cfg_data = Config.load(args.data, args.opt_data)
|
|
|
|
cfg_data = update_data_by_args(cfg_data, args)
|
|
|
|
cfg_exp = Config.load(args.exp, args.opt_exp)
|
|
|
|
update_exp_by_args(cfg_exp, args)
|
|
|
|
return cfg_data, cfg_exp
|
|
|
|
|
|
|
|
def main_entrypoint():
|
|
|
|
import argparse
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--cfg', type=str, default=None)
|
|
|
|
for name in ['data', 'exp']:
|
|
|
|
parser.add_argument('--{}'.format(name), type=str, required=False)
|
|
|
|
parser.add_argument('--opt_{}'.format(name), type=str, nargs='+', default=[])
|
|
|
|
parser.add_argument('--root', type=str, default=None)
|
|
|
|
parser.add_argument('--subs', type=str, default=None, nargs='+')
|
|
|
|
parser.add_argument('--subs_vis', type=str, default=None, nargs='+')
|
|
|
|
parser.add_argument('--ranges', type=int, default=None, nargs=3)
|
|
|
|
parser.add_argument('--cameras', type=str, default=None, help='Camera file path')
|
|
|
|
parser.add_argument('--out', type=str, default=None)
|
2023-10-16 19:45:09 +08:00
|
|
|
parser.add_argument('--num_workers', type=int, default=-1)
|
2023-06-19 16:39:27 +08:00
|
|
|
parser.add_argument('--skip_vis', action='store_true')
|
2023-07-10 22:11:38 +08:00
|
|
|
parser.add_argument('--skip_vis_step', action='store_true')
|
|
|
|
parser.add_argument('--skip_vis_final', action='store_true')
|
2023-10-16 19:45:09 +08:00
|
|
|
parser.add_argument('--skip_final', action='store_true')
|
2023-06-19 16:39:27 +08:00
|
|
|
parser.add_argument('--debug', action='store_true')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.cfg is not None:
|
|
|
|
cfg_data, cfg_exp = load_cfg_from_file(args.cfg, args)
|
|
|
|
else:
|
|
|
|
cfg_data, cfg_exp = load_cfg_from_cmd(args)
|
|
|
|
|
|
|
|
if args.out is not None:
|
|
|
|
cfg_exp.args.output = args.out
|
|
|
|
out = cfg_exp.args.output
|
|
|
|
os.makedirs(out, exist_ok=True)
|
|
|
|
print(cfg_data, file=open(os.path.join(out, 'cfg_data.yml'), 'w'))
|
|
|
|
print(cfg_exp, file=open(os.path.join(out, 'cfg_exp.yml'), 'w'))
|
|
|
|
|
|
|
|
dataset = load_object(cfg_data.module, cfg_data.args)
|
|
|
|
print(dataset)
|
|
|
|
|
|
|
|
model = load_object(cfg_exp.module, cfg_exp.args)
|
2023-10-16 19:45:09 +08:00
|
|
|
process(dataset, model, args)
|
2023-06-19 16:39:27 +08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main_entrypoint()
|