-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
359 lines (286 loc) · 13.2 KB
/
run.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import argparse
import logging
import os
from multiprocessing.pool import Pool
from pathlib import Path
from typing import Callable, Dict, Optional, Sequence, Tuple, Type, Union
import cv2
import numpy as np
import torch
from detectron2.checkpoint import DetectionCheckpointer
from detectron2.data import MetadataCatalog
from detectron2.engine import DefaultPredictor
from detectron2.modeling import build_model
from torch.profiler import ProfilerActivity, profile, record_function
from torch.utils.data import DataLoader, Dataset
from torch.utils.data._utils.collate import collate, default_collate_fn_map
from tqdm import tqdm
from core.setup import setup_cfg, setup_logging
from datasets.augmentations import ResizeLongestEdge, ResizeScaling, ResizeShortestEdge
from page_xml.output_pageXML import OutputPageXML
from utils.image_utils import load_image_array_from_path, load_image_tensor_from_path
from utils.input_utils import clean_input_paths, get_file_paths
from utils.logging_utils import get_logger_name
def get_arguments() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Run file to inference using the model found in the config file")
detectron2_args = parser.add_argument_group("detectron2")
detectron2_args.add_argument("-c", "--config", help="config file", required=True)
detectron2_args.add_argument("--opts", nargs=argparse.REMAINDER, help="optional args to change", default=[])
io_args = parser.add_argument_group("IO")
io_args.add_argument("-i", "--input", nargs="+", help="Input folder", type=str, action="extend", required=True)
io_args.add_argument("-o", "--output", help="Output folder", type=str, required=True)
args = parser.parse_args()
return args
class Predictor(DefaultPredictor):
"""
Predictor runs the model specified in the config, on call the image is processed and the results dict is output
"""
def __init__(self, cfg):
"""
Predictor runs the model specified in the config, on call the image is processed and the results dict is output
Args:
cfg (CfgNode): config
"""
self.cfg = cfg.clone() # cfg can be modified by model
self.model = build_model(self.cfg)
self.model.eval()
if len(cfg.DATASETS.TEST):
self.metadata = MetadataCatalog.get(cfg.DATASETS.TEST[0])
self.input_format = cfg.INPUT.FORMAT
assert self.input_format in ["RGB", "BGR"], self.input_format
checkpointer = DetectionCheckpointer(self.model)
checkpointer.load(cfg.TEST.WEIGHTS)
if cfg.INPUT.RESIZE_MODE == "none":
self.aug = ResizeScaling(scale=1) # HACK percentage of 1 is no scaling
elif cfg.INPUT.RESIZE_MODE in ["shortest_edge", "longest_edge"]:
if cfg.INPUT.RESIZE_MODE == "shortest_edge":
self.aug = ResizeShortestEdge(cfg.INPUT.MIN_SIZE_TEST, cfg.INPUT.MAX_SIZE_TEST, "choice")
elif cfg.INPUT.RESIZE_MODE == "longest_edge":
self.aug = ResizeLongestEdge(cfg.INPUT.MIN_SIZE_TEST, cfg.INPUT.MAX_SIZE_TEST, "choice")
elif cfg.INPUT.RESIZE_MODE == "scaling":
self.aug = ResizeScaling(cfg.INPUT.SCALING, cfg.INPUT.MAX_SIZE_TEST)
else:
raise NotImplementedError(f"{cfg.INPUT.RESIZE_MODE} is not a known resize mode")
def gpu_call(self, original_image: torch.Tensor):
with torch.no_grad(): # https://github.com/sphinx-doc/sphinx/issues/4258
# Apply pre-processing to image.
channels, height, width = original_image.shape
assert channels == 3, f"Must be a BGR image, found {channels} channels"
image = torch.as_tensor(original_image, dtype=torch.float32, device=self.cfg.MODEL.DEVICE)
if self.input_format == "BGR":
# whether the model expects BGR inputs or RGB
image = image[[2, 1, 0], :, :]
if self.cfg.INPUT.RESIZE_MODE == "none":
new_height, new_width = height, width
elif self.cfg.INPUT.RESIZE_MODE in ["shortest_edge", "longest_edge"]:
new_height, new_width = self.aug.get_output_shape(
height, width, self.cfg.INPUT.MIN_SIZE_TEST, self.cfg.INPUT.MAX_SIZE_TEST
)
elif self.cfg.INPUT.RESIZE_MODE == "scaling":
new_height, new_width = self.aug.get_output_shape(
height, width, self.cfg.INPUT.SCALING, self.cfg.INPUT.MAX_SIZE_TEST
)
else:
raise NotImplementedError(f"{self.cfg.INPUT.RESIZE_MODE} is not a known resize mode")
if self.cfg.INPUT.RESIZE_MODE != "none":
image = torch.nn.functional.interpolate(image[None], mode="bilinear", size=(new_height, new_width))[0]
inputs = {"image": image, "height": new_height, "width": new_width}
predictions = self.model([inputs])[0]
return predictions, height, width
def cpu_call(self, original_image: np.ndarray):
with torch.no_grad(): # https://github.com/sphinx-doc/sphinx/issues/4258
# Apply pre-processing to image.
height, width, channels = original_image.shape
assert channels == 3, f"Must be a BGR image, found {channels} channels"
image = self.aug.get_transform(original_image).apply_image(original_image)
image = torch.as_tensor(image, dtype=torch.float32, device=self.cfg.MODEL.DEVICE).permute(2, 0, 1)
if self.input_format == "RGB":
# whether the model expects BGR inputs or RGB
image = image[[2, 1, 0], :, :]
inputs = {"image": image, "height": height, "width": width}
predictions = self.model([inputs])[0]
return predictions, height, width
def __call__(self, original_image):
"""
Not really useful, but shows what call needs to be made
"""
return self.gpu_call(original_image)
class LoadingDataset(Dataset):
def __init__(self, data):
self.data = data
def __len__(self):
return len(self.data)
def __getitem__(self, index):
path = self.data[index]
# TODO Move resize and load to this part of the dataloader
return load_image_tensor_from_path(path), path
def collate_numpy(batch):
collate_map = default_collate_fn_map
def new_map(batch, *, collate_fn_map: Optional[Dict[Union[Type, Tuple[Type, ...]], Callable]] = None):
return batch
collate_map.update({np.ndarray: new_map, type(None): new_map})
return collate(batch, collate_fn_map=collate_map)
class SavePredictor(Predictor):
"""
Extension on the predictor that actually saves the part on the prediction we current care about: the semantic segmentation as pageXML
"""
def __init__(
self,
cfg,
input_paths: str | Path,
output_dir: str | Path,
output_page: OutputPageXML,
):
"""
Extension on the predictor that actually saves the part on the prediction we current care about: the semantic segmentation as pageXML
Args:
cfg (CfgNode): config
input_dir (str | Path): path to input dir
output_dir (str | Path): path to output dir
gen_page (GenPageXML): class to convert from predictions to pageXML
"""
super().__init__(cfg)
self.logger = logging.getLogger(get_logger_name())
self.input_paths: Optional[Sequence[Path]] = None
if input_paths is not None:
self.set_input_paths(input_paths)
self.output_dir: Optional[Path] = None
if output_dir is not None:
self.set_output_dir(output_dir)
if not isinstance(output_page, OutputPageXML):
raise TypeError(
f"Must provide conversion from mask to pageXML. Current type is {type(output_page)}, not OutputPageXML"
)
self.output_page = output_page
# Formats found here: https://docs.opencv.org/4.x/d4/da8/group__imgcodecs.html#imread
self.image_formats = [
".bmp",
".dib",
".jpeg",
".jpg",
".jpe",
".jp2",
".png",
".webp",
".pbm",
".pgm",
".ppm",
".pxm",
".pnm",
".pfm",
".sr",
".ras",
".tiff",
".tif",
".exr",
".hdr",
".pic",
]
# Formats https://pytorch.org/vision/main/generated/torchvision.io.read_image.html
# self.image_formats = [".jpeg", ".jpg", ".jpe",
# ".png"]
def set_input_paths(
self,
input_paths: str | Path | Sequence[str | Path],
) -> None:
"""
Setter for image paths, also cleans them to be a list of Paths
Args:
input_paths (str | Path | Sequence[str | Path]): path(s) from which to extract the images
Raises:
FileNotFoundError: input path not found on the filesystem
PermissionError: input path not accessible
"""
input_paths = clean_input_paths(input_paths)
all_input_paths = []
for input_path in input_paths:
if not input_path.exists():
raise FileNotFoundError(f"Input ({input_path}) is not found")
if not os.access(path=input_path, mode=os.R_OK):
raise PermissionError(f"No access to {input_path} for read operations")
input_path = input_path.resolve()
all_input_paths.append(input_path)
self.input_paths = all_input_paths
def set_output_dir(self, output_dir: str | Path) -> None:
"""
Setter for the output dir
Args:
output_dir (str | Path): path to output dir
"""
if isinstance(output_dir, str):
output_dir = Path(output_dir)
if not output_dir.is_dir():
self.logger.info(f"Could not find output dir ({output_dir}), creating one at specified location")
output_dir.mkdir(parents=True)
self.output_dir = output_dir.resolve()
# def save_prediction(self, input_path: Path | str):
def save_prediction(self, image, input_path):
"""
Run the model and get the prediction, and save pageXML or a mask image depending on the mode
Args:
input_path (Path | str): path to single image
Raises:
TypeError: no output dir is specified
"""
if self.output_dir is None:
raise TypeError("Cannot run when the output dir is None")
if image is None:
self.logger.warning(f"Image at {input_path} has not loaded correctly, ignoring for now")
return
if isinstance(image, np.ndarray):
outputs = self.cpu_call(image)
elif isinstance(image, torch.Tensor):
outputs = self.gpu_call(image)
else:
raise TypeError(f"Unknown image type: {type(image)}")
output_image = outputs[0]["sem_seg"]
# output_image = torch.argmax(output_image, dim=-3).cpu().numpy()
self.output_page.link_image(input_path)
self.output_page.generate_single_page(output_image, input_path, old_height=outputs[1], old_width=outputs[2])
def process(self):
"""
Run the model on all images within the input dir
Raises:
TypeError: no input dir is specified
TypeError: no output dir is specified
"""
if self.input_paths is None:
raise TypeError("Cannot run when the input_paths is None")
if self.output_dir is None:
raise TypeError("Cannot run when the output_dir is None")
input_paths = get_file_paths(self.input_paths, self.image_formats)
# Single thread
# for inputs in tqdm(input_paths):
# self.save_prediction(inputs)
dataset = LoadingDataset(input_paths)
dataloader = DataLoader(dataset, shuffle=False, batch_size=None, num_workers=16, pin_memory=False, collate_fn=None)
for inputs in tqdm(dataloader, desc="Predicting PageXML"):
# self.logger.warning(inputs)
self.save_prediction(inputs[0], inputs[1])
# Multithread <- does not work with cuda
# with Pool(os.cpu_count()) as pool:
# results = list(tqdm(pool.imap_unordered(
# self.save_prediction, image_paths), total=len(image_paths)))
def main(args: argparse.Namespace) -> None:
cfg = setup_cfg(args)
setup_logging(cfg, save_log=False)
output_page = OutputPageXML(
mode=cfg.MODEL.MODE,
output_dir=args.output,
line_width=cfg.PREPROCESS.BASELINE.LINE_WIDTH,
regions=cfg.PREPROCESS.REGION.REGIONS,
merge_regions=cfg.PREPROCESS.REGION.MERGE_REGIONS,
region_type=cfg.PREPROCESS.REGION.REGION_TYPE,
)
predictor = SavePredictor(
cfg=cfg,
input_paths=args.input,
output_dir=args.output,
output_page=output_page,
)
# with profile(activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA], with_stack=True, record_shapes=True) as prof:
predictor.process()
# print(prof.key_averages(group_by_stack_n=5).table(sort_by="cpu_time_total", row_limit=10))
if __name__ == "__main__":
args = get_arguments()
main(args)