Download code/agt/data/reader.py from MRBSTUDIO/AGT: direct link, hf CLI and curl.
- Browser
- Download file 4.26 kB
-
https://huggingface.co/MRBSTUDIO/AGT/resolve/main/code/agt/data/reader.py
- Command line
-
hf download hf://MRBSTUDIO/AGT/code/agt/data/reader.py
-
curl -L -o reader.py https://huggingface.co/MRBSTUDIO/AGT/resolve/main/code/agt/data/reader.py
4.26 kB
| from __future__ import annotations | |
| from collections import OrderedDict | |
| from dataclasses import asdict | |
| import numpy as np | |
| from .clips import build_clips | |
| from .grid import ScoringGrid | |
| from .protocol import ClipProtocol | |
| from .scene import SceneIndex, SceneSource | |
| class ClipReader: | |
| RGB_KINDS = ('clean', 'dirty') | |
| def __init__(self, index: SceneIndex, protocol: ClipProtocol, *, thread_count=2, thread_type=None, cache_scenes=2): | |
| self.index, self.protocol = index, protocol | |
| self.grid = ScoringGrid(protocol.grid) | |
| self.thread_count, self.thread_type = thread_count, thread_type | |
| self._sources = OrderedDict() | |
| self._cache_scenes = max(1, int(cache_scenes)) | |
| self._clips = None | |
| self._skipped = {} | |
| def source(self, sample_id: str) -> SceneSource: | |
| if sample_id in self._sources: | |
| self._sources.move_to_end(sample_id) | |
| return self._sources[sample_id] | |
| src = SceneSource(self.index.by_id[sample_id], self.grid, thread_count=self.thread_count, | |
| thread_type=self.thread_type) | |
| self._sources[sample_id] = src | |
| while len(self._sources) > self._cache_scenes: | |
| _, old = self._sources.popitem(last=False) | |
| old.close() | |
| return src | |
| def clips_for(self, sample_id: str) -> list: | |
| record = self.index.by_id[sample_id] | |
| skipped = [] | |
| clips = build_clips(self.protocol, record, self.source(sample_id).frames_meta, sample_id=sample_id, | |
| skipped=skipped, tolerance_us=self.index.selection_tolerance_us(record)) | |
| self._skipped[sample_id] = skipped | |
| return clips | |
| def skipped_windows(self) -> list: | |
| self.clips() | |
| return [w for sample_id in sorted(self._skipped) for w in self._skipped[sample_id]] | |
| def clips(self) -> list: | |
| if self._clips is None: | |
| self._clips = [c for record in self.index for c in self.clips_for(self.index.sample_id(record))] | |
| return list(self._clips) | |
| def load(self, clip: dict, *, rgb=RGB_KINDS) -> dict: | |
| src = self.source(clip['sample_id']) | |
| idx = [int(i) for i in clip['frame_indices']] | |
| out = {'frame_indices': np.asarray(idx, dtype=np.int64), | |
| 'timestamps_us': src.timestamps_mid_us[idx].copy(), | |
| 'timestamps_start_us': src.timestamps_start_us[idx].copy(), | |
| 'is_keyframe': np.asarray(clip['is_keyframe'], dtype=np.uint8), | |
| 'roles': np.asarray(clip['roles']), | |
| 't_norm': np.asarray(clip['t_norm'], dtype=np.float64)} | |
| np.testing.assert_array_equal(out['timestamps_us'], np.asarray(clip['timestamps_us'])) | |
| out['c2w'] = src.poses(idx, reference=clip['context_frames'][0]) | |
| for kind in rgb: | |
| if kind not in self.RGB_KINDS: | |
| raise ValueError(kind) | |
| out['rgb_' + kind] = src.rgb(kind, idx) | |
| out['camera_params'] = asdict(src.grid_camera) | |
| out['native_camera'] = asdict(src.camera) | |
| record = src.record | |
| out['metadata'] = {'clip_id': clip['clip_id'], 'sample_id': clip['sample_id'], 'sample_key': clip['sample_key'], | |
| 'protocol_id': self.protocol.protocol_id, 'window_mode': self.protocol.window_mode, | |
| 'grid': self.grid.label, 'eval_set_id': self.index.eval_set_id, | |
| 'calibration_profile': src.calibration_profile, 'source_id': record.source_id, | |
| 'template_id': record.template_id, 'map_version': record.map_version, | |
| 'lighting_profile': record.lighting_profile, 'noise_seed': record.noise_seed, | |
| 'time_of_day': record.time_of_day, 'group': record.group} | |
| return out | |
| def close(self): | |
| for src in self._sources.values(): | |
| src.close() | |
| self._sources.clear() | |
| def open_reader(data_root, split: str, protocol: ClipProtocol, *, map_version: str = 'v2', sources=None, | |
| time_of_day: str = 'both', **kwargs) -> ClipReader: | |
| index = SceneIndex.from_data_root(data_root, split, map_version=map_version, sources=sources, time_of_day=time_of_day) | |
| return ClipReader(index, protocol, **kwargs) | |