text
stringlengths
0
1.47k
os.remove(path)
print(f'Removed stale {path} ({why} this run)')
def save_extra_captions(save_dir, all_captions, version, export_captions):
"""Write ``captions_<version>.json`` (keyed like ``captions.json``) from
the export's ``motion_captions_<version>.json`` map, or remove a stale
one when no saved clip has a caption of that version. Returns the dict
written (empty when none)."""
path = pjoin(save_dir, f'captions_{version}.json')
out = (by_feature_key(all_captions, export_captions)
if all_captions and export_captions else {})
if out:
save_json(path, out)
missing = sorted(set(all_captions) - set(out))
print(f'Saved {len(out)}/{len(all_captions)} {version} clip captions'
+ (f'; {len(missing)} saved clips have none, e.g. {missing[:3]}' if missing else ''))
else:
_remove_stale(save_dir, f'captions_{version}.json', f'no {version} captions')
return out
def save_outputs(save_dir, cond, all_filtered_clips, category_groups=None,
extra_captions=None, keep_category_groups=False):
"""Save ``cond.npy``, filtered clips, captions, and category groupings.
Caption files, keyed alike by saved clip (``<clip>-<NNN>``):
``captions.json`` from ``motion_captions.json`` (the normal caption; a
clip without one is not saved) and, for each version ``v`` of
``EXTRA_CAPTION_VERSIONS``, ``captions_<v>.json`` from
``extra_captions[v]`` (the export's ``motion_captions_<v>.json``). Extra
captions never enter ``cond`` or the ``cond_parts`` cache, so changing them
never re-processes an object; a version without captions has its stale
``captions_<v>.json`` removed.
Filtered clips, captions and category groups are only written when
present; when absent, the file an earlier run wrote is removed (the
training loader takes a missing ``captions.json`` as caption-free data
and selects category subsets from ``category_groups.json``), except a
``category_groups.json`` the run was told to leave alone
(*keep_category_groups*: ``--category_groups ""``, the file is copied in
by hand). Every file is
written atomically, so an interrupted run cannot leave a truncated output
behind for the training loader to trip over. Returns the merged captions
dict so callers can pass it to ``save_metadata_report``.
"""
atomic_np_save(pjoin(save_dir, 'cond.npy'), cond)
if all_filtered_clips:
save_json(pjoin(save_dir, 'filtered_clips.json'), all_filtered_clips)
total = sum(len(v) for v in all_filtered_clips.values())
print(f'Saved {total} filtered clips to filtered_clips.json')
else:
_remove_stale(save_dir, 'filtered_clips.json', 'no filtered clips')
all_captions = collect_captions(cond)
if all_captions:
save_json(pjoin(save_dir, 'captions.json'), all_captions)
print(f'Saved {len(all_captions)} clip captions')
else:
_remove_stale(save_dir, 'captions.json', 'no captions')
for version in EXTRA_CAPTION_VERSIONS:
save_extra_captions(save_dir, all_captions, version,
(extra_captions or {}).get(version))
if category_groups:
save_json(pjoin(save_dir, 'category_groups.json'), category_groups)
elif not keep_category_groups:
_remove_stale(save_dir, 'category_groups.json', 'no category groups')
return all_captions
def build_stats(clips_per_object, joints_per_object, total_frames, max_njoints, fps=30):
"""Aggregate per-object counts into the dataset summary dict.
``duration_min`` is a nominal figure: clips carry their own ``fps`` field
(30 for every source, 30 after the >=60 fps down-sampling in
:func:`~data_process.utils.motion_features._load_npz_anim`), but the
duration here simply divides the frame total by *fps*. The assumption is
reported alongside the value, so ``fps`` is echoed back in the stats.
"""
return {
'total_clips': sum(clips_per_object.values()),
'total_frames': total_frames,
'duration_min': total_frames / fps / 60,
'fps': fps,
'max_njoints': max_njoints,
'clips_per_object': clips_per_object,
'joints_per_object': joints_per_object,
}
def save_metadata_report(save_dir, stats, all_filtered_clips,
all_captions=None, category_groups=None):
"""Write a human-readable metadata summary to ``metadata.txt``.
``all_captions`` and ``category_groups`` are optional; their sections are
omitted when not supplied. The extra-caption counts are read back from
the ``captions_<v>.json`` files ``save_outputs`` wrote. The file is