in env_humanoid_imitation.py [0:0]
def create(self):
project_dir = self._config['project_dir']
ref_motion_db = self._config['character'].get('ref_motion_db')
ref_motion_scale = self._config['character'].get('ref_motion_scale')
ref_motion_file = []
for i, mdb in enumerate(ref_motion_db):
motions = []
if mdb.get('cluster_info'):
''' Read reference motions based on the cluster labels '''
assert mdb.get('data') is None, \
'This should not be specified when cluster_info is used'
dir = mdb['cluster_info'].get('dir')
label_file = mdb['cluster_info'].get('label_file')
sample_id = mdb['cluster_info'].get('sample_id')
labels = {}
assert label_file
if project_dir:
label_file = os.path.join(project_dir, label_file)
with open(label_file, 'r') as file:
for line in file:
l = re.split('[\t|\n|,|:| ]+', line)
id, rank, score, filename = int(l[0]), int(l[1]), float(l[2]), str(l[3])
if id not in labels.keys(): labels[id] = []
labels[id].append({'rank': rank, 'socre': score, 'filename': filename})
num_cluster = len(labels.keys())
for j in range(num_cluster):
if sample_id and j!=sample_id:
continue
for label in labels[j]:
if project_dir:
file = os.path.join(project_dir, dir, label['filename'])
motions.append(file)
else:
''' Read reference motions from the specified list of files and dirs '''
ref_motion_data = mdb.get('data')
motions = []
if ref_motion_data.get('file'):
motions += ref_motion_data.get('file')
if ref_motion_data.get('dir'):
for d in ref_motion_data.get('dir'):
if project_dir:
d = os.path.join(project_dir, d)
motions += utils.files_in_dir(d, ext=".bvh", sort=True)
if project_dir:
for j in range(len(motions)):
motions[j] = os.path.join(project_dir, motions[j])
'''
If num_sample is specified, we use only num_sample motions
from the entire reference motions.
'random' chooses randomly, 'top' chooses the first num_sample
'''
num_sample = mdb.get('num_sample')
if num_sample:
sample_method = mdb.get('sample_method')
if sample_method == 'random':
motions = random.choices(motions, k=num_sample)
elif sample_method == 'top':
motions = motions[:num_sample]
else:
raise NotImplementedError
ref_motion_file.append(motions)
''' Load Reference Motion '''
self._ref_motion_all = []
self._ref_motion_file_names = []
for i in range(self._num_agent):
ref_motion_all, ref_motion_file_names = \
load_motions(ref_motion_file[i],
self._base_motion[i].skel,
self._sim_agent[i]._char_info,
self._verbose)
self._ref_motion_all.append(ref_motion_all)
self._ref_motion_file_names.append(ref_motion_file_names)
''' Should call reset after all setups are done '''
self.reset({'add_noise': False})
self._initialized = True
if self._verbose:
print('----- Humanoid Imitation Environment Created -----')
for i in range(self._num_agent):
print('[Agent%d]: state(%d) and action(%d)' \
%(i, len(self.state(i)), self._action_space[i].dim))
print('-------------------------------')