-
Notifications
You must be signed in to change notification settings - Fork 67
/
pipeline.py
219 lines (176 loc) · 8.46 KB
/
pipeline.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File: pipeline.py
# Description: Main pipeline of fast-openISP
# Created: 2021/10/22 20:34
# Author: Qiu Jueqin ([email protected])
import os.path as op
import sys
import time
import copy
import math
import importlib
from collections import OrderedDict
from multiprocessing import Process
import numpy as np
from utils.yacs import Config
from modules.basic_module import MODULE_DEPENDENCIES
class Pipeline:
""" Core fast-openISP pipeline """
def __init__(self, cfg):
"""
:param cfg: yacs.Config object, configurations about camera specs and module parameters
"""
self.cfg = cfg
saturation_values = self.get_saturation_values()
with self.cfg.unfreeze():
self.cfg.saturation_values = saturation_values
self.modules = self.get_modules()
def get_saturation_values(self):
"""
Get saturation pixel values in different stages in the pipeline.
Raw stage: dataflow before the BLC modules (not included)
HDR stage: dataflow after the BLC modules (included) and before the bit-depth compression
module, i.e., Gamma in openISP (not included)
SDR stage: dataflow after the Gamma module (included)
"""
raw_max_value = 2 ** self.cfg.hardware.raw_bit_depth - 1
sdr_max_value = 255
# Saturation values should be carefully calculated if BLC module is activated
if 'blc' in self.cfg.module_enable_status:
blc = self.cfg.blc
hdr_max_r = raw_max_value - blc.bl_r
hdr_max_b = raw_max_value - blc.bl_b
hdr_max_gr = int(raw_max_value - blc.bl_gr + hdr_max_r * blc.alpha / 1024)
hdr_max_gb = int(raw_max_value - blc.bl_gb + hdr_max_b * blc.beta / 1024)
hdr_max_value = max(hdr_max_r, hdr_max_b, hdr_max_gr, hdr_max_gb)
else:
hdr_max_value = raw_max_value
return Config({'raw': raw_max_value,
'hdr': hdr_max_value,
'sdr': sdr_max_value})
def get_modules(self):
""" Get activated ISP modules according to the configuration """
if op.dirname(__file__) not in sys.path:
sys.path.insert(0, op.dirname(__file__))
enabled_modules = tuple(m for m, en in self.cfg.module_enable_status.items() if en)
modules = OrderedDict()
for module_name in enabled_modules:
package = importlib.import_module('modules.{}'.format(module_name))
module_cls = getattr(package, module_name.upper())
module = module_cls(self.cfg)
for m in MODULE_DEPENDENCIES.get(module_cls.__name__, []):
if m not in enabled_modules:
raise RuntimeError(
'{} is unavailable when {} is deactivated'.format(module_name, m)
)
modules[module_name] = module
return modules
def execute(self, bayer, save_intermediates=False, verbose=True):
"""
ISP pipeline execution
:param bayer: input Bayer array, np.ndarray(H, W)
:param save_intermediates: whether to save intermediate results from all ISP modules
:param verbose: whether to print timing messages
:return:
data: a dict containing results from different domains (Bayer, RGB, and YCbCr)
and the final RGB output (data['output'])
intermediates: a dict containing intermediate results if save_intermediates=True,
otherwise a empty dict
"""
def print_(*args, **kwargs):
return print(*args, **kwargs) if verbose else None
pipeline_start = time.time()
data = OrderedDict(bayer=bayer)
intermediates = OrderedDict()
for module_name, module in self.modules.items():
start = time.time()
print_('Executing {}... '.format(module_name), end='', flush=True)
module.execute(data)
if save_intermediates:
intermediates[module_name] = copy.copy(data)
print_('Done. Elapsed {:.3f}s'.format(time.time() - start))
data['output'] = self.get_output(data)
print_('Pipeline elapsed {:.3f}s'.format(time.time() - pipeline_start))
return data, intermediates
def get_output(self, data):
"""
Post-process the pipeline result to get the final output
:param data: argument returned by self.execute()
:return: displayable result: np.ndarray(H, W, 3) in np.uint8 dtype
"""
if 'y_image' in data and 'cbcr_image' in data:
ycbcr_image = np.dstack([data['y_image'][..., None], data['cbcr_image']])
output = ycbcr_to_rgb(ycbcr_image)
elif 'rgb_image' in data:
output = data['rgb_image']
if output.dtype != np.uint8:
output = output.astype(np.float32)
output = (255 * output / self.cfg.saturation_values.hdr).astype(np.uint8)
elif 'bayer' in data:
output = data['bayer'] # actually not an RGB image, looks very dark for most cameras
output = output.astype(np.float32)
output = (255 * output / self.cfg.saturation_values.raw).astype(np.uint8)
else:
raise NotImplementedError
return output
def run(self, raw_path, save_dir, load_raw_fn, suffix=''):
"""
A higher level API that writes ISP result into disk
:param raw_path: path to the raw file to be processed
:param save_dir: directory to save the output (shares the same filename as the input)
:param load_raw_fn: function to load the Bayer array from the raw_path
:param suffix: suffix to added to the output filename
"""
import cv2
bayer = load_raw_fn(raw_path)
data, _ = self.execute(bayer, save_intermediates=False, verbose=False)
output = cv2.cvtColor(data['output'], cv2.COLOR_RGB2BGR)
filename = op.splitext(op.basename(raw_path))[0]
save_path = op.join(save_dir, '{}.png'.format(filename + suffix))
cv2.imwrite(save_path, output)
def batch_run(self, raw_paths, save_dirs, load_raw_fn, suffixes='', num_processes=1):
"""
Batch version of self.run via multiprocessing
:param raw_paths: list of paths to the raw files to be executed
:param save_dirs: list of directories to save the outputs. If given a string, it will be
copied to a N-element list, where N is the number of paths in raw_paths
:param load_raw_fn: function to load the Bayer array from the raw_path
:param suffixes: a list of suffixes to added to the output filenames
:param num_processes: number of processes in multiprocessing
"""
num_files = len(raw_paths)
num_batches = math.ceil(num_files / num_processes)
if not isinstance(save_dirs, (list, tuple)):
save_dirs = [save_dirs for _ in range(num_files)]
if not isinstance(suffixes, (list, tuple)):
suffixes = [suffixes for _ in range(num_files)]
for batch_id in range(num_batches):
indices = [batch_id * num_processes + rank for rank in range(num_processes)]
indices = [i for i in indices if i < num_files]
batch_size = len(indices)
raw_paths_batch = [raw_paths[i] for i in indices]
save_dirs_batch = [save_dirs[i] for i in indices]
suffixes_batch = [suffixes[i] for i in indices]
pool = []
for rank in range(batch_size):
pool.append(
Process(target=self.run,
kwargs={'raw_path': raw_paths_batch[rank],
'save_dir': save_dirs_batch[rank],
'load_raw_fn': load_raw_fn,
'suffix': suffixes_batch[rank]})
)
for p in pool:
p.start()
for p in pool:
p.join()
def ycbcr_to_rgb(ycbcr_array):
""" Convert YCbCr 3-channel array into sRGB array """
assert ycbcr_array.dtype == np.uint8
matrix = np.array([[298, 0, 409],
[298, -100, -208],
[298, 516, 0]], dtype=np.int32).T # x256
bias = np.array([-56992, 34784, -70688], dtype=np.int32).reshape(1, 1, 3) # x256
ycbcr_array = ycbcr_array.astype(np.int32)
rgb_array = np.right_shift(ycbcr_array @ matrix + bias, 8)
rgb_array = np.clip(rgb_array, 0, 255)
return rgb_array.astype(np.uint8)