-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathbenchmark.py
84 lines (67 loc) · 2.15 KB
/
benchmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
import json
from pathlib import Path
from dataclasses import dataclass
from typing import Tuple, List
import numpy as np
def read_array(path: Path) -> np.ndarray:
return np.fromfile(str(path), dtype=np.uint16)
@dataclass
class RawMeta:
name: str
scene_id: str
light: str
ISO: int
exp_time: float
bayer_pattern: str
shape: Tuple[int, int]
wb_gain: Tuple[float, float, float]
CCM: Tuple[
Tuple[float, float, float],
Tuple[float, float, float],
Tuple[float, float, float],
]
ROIs: List[Tuple[int, int, int, int]]
class BenchmarkLoader:
def __init__(self, dataset_info_json: Path, base_path=None):
with dataset_info_json.open() as f:
self._dataset = [
{
'input': x['input'],
'gt': x['gt'],
'meta': RawMeta(**x['meta'])
}
for x in json.load(f)
]
if base_path is None:
self.base_path = dataset_info_json.parent
else:
self.base_path = Path(base_path)
def __len__(self):
return len(self._dataset)
def __iter__(self):
self._idx = 0
return self
def __next__(self) -> Tuple[np.ndarray, np.ndarray, RawMeta]:
if self._idx >= len(self):
raise StopIteration
input_bayer, gt_bayer, meta = self._load_idx(self._idx)
self._idx += 1
return input_bayer, gt_bayer, meta
def _load_idx(self, idx: int):
item = self._dataset[idx]
img_files = item['input'], item['gt']
meta = item['meta']
bayers = []
for img_file in img_files:
if not Path(img_file).is_absolute():
img_file = self.base_path / img_file
bayer = read_array(img_file)
bayer = bayer.reshape(*meta.shape)
# Reno 10x outputs BGGR order
assert meta.bayer_pattern == 'BGGR'
bayer = bayer.astype(np.float32) / 65535
bayers.append(bayer)
input_bayer, gt_bayer = bayers
return input_bayer, gt_bayer, meta
# vim: ts=4 sw=4 sts=4 expandtab