-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathdecimer_segmentation.py
More file actions
637 lines (484 loc) · 19.4 KB
/
Copy pathdecimer_segmentation.py
File metadata and controls
637 lines (484 loc) · 19.4 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
"""
DECIMER Segmentation - Optimized Production Implementation
This Software is under the MIT License
Refer to LICENSE or https://opensource.org/licenses/MIT for more information
Written by ©Kohulan Rajan 2020
Optimized for production performance 2024
Performance Optimizations Applied:
- Model warmup to eliminate cold-start latency
- Reduced proposal counts (POST_NMS_ROIS: 1000→500, DETECTION_MAX: 100→50)
- cuDNN autotuning for GPU operations
- TensorFlow graph optimizations
"""
from __future__ import annotations
import os
# =============================================================================
# PERFORMANCE: Set environment variables BEFORE importing TensorFlow
# =============================================================================
os.environ["TF_CUDNN_USE_AUTOTUNE"] = "1"
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "1"
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
import logging
from typing import List, Tuple, Union, Optional
from concurrent.futures import ThreadPoolExecutor
import threading
import argparse
import cv2
import numpy as np
import pymupdf
import pystow
import tensorflow as tf
from .optimized_complete_structure import complete_structure_mask
from .mrcnn import model as modellib
from .mrcnn import visualize
from .mrcnn import moldetect
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Constants
MODEL_DOWNLOAD_URL = (
"https://zenodo.org/record/10663579/files/mask_rcnn_molecule.h5?download=1"
)
MODEL_FILENAME = "mask_rcnn_molecule.h5"
# Global model instance with thread-safe lazy loading
_model: Optional[modellib.MaskRCNN] = None
_model_lock = threading.Lock()
_model_warmed_up: bool = False
class InferenceConfig(moldetect.MolDetectConfig):
"""
Optimized inference configuration for MRCNN model.
Reduced proposal counts provide ~1.5-2x speedup with minimal accuracy impact.
"""
GPU_COUNT = 1
IMAGES_PER_GPU = 1
DETECTION_MIN_CONFIDENCE = 0.75
# PERFORMANCE: Reduced from defaults for faster inference
POST_NMS_ROIS_INFERENCE = 500 # Default: 1000
DETECTION_MAX_INSTANCES = 100 # Default: 100
PRE_NMS_LIMIT = 6000 # Default: 6000
def __init__(self):
super().__init__()
_inference_config = InferenceConfig()
def get_model() -> modellib.MaskRCNN:
"""
Thread-safe lazy loading of the MRCNN model with warmup.
Returns:
modellib.MaskRCNN: Loaded and warmed-up model with trained weights
"""
global _model, _model_warmed_up
if _model is not None and _model_warmed_up:
return _model
with _model_lock:
if _model is not None and _model_warmed_up:
return _model
_model = _load_model_internal()
if not _model_warmed_up:
_warmup_model(_model)
_model_warmed_up = True
return _model
def _load_model_internal() -> modellib.MaskRCNN:
"""Load model with TensorFlow optimizations."""
# PERFORMANCE: Enable graph optimizations
try:
tf.config.optimizer.set_experimental_options(
{
"layout_optimizer": True,
"constant_folding": True,
"shape_optimization": True,
"remapping": True,
"arithmetic_optimization": True,
"dependency_optimization": True,
"loop_optimization": True,
"function_optimization": True,
"debug_stripper": True,
}
)
except Exception as e:
logger.debug(f"Some optimizer options not available: {e}")
model_path = pystow.join("DECIMER-Segmentation_model")
print(model_path)
if not os.path.exists(str(model_path) + "/" + MODEL_FILENAME):
logger.info("Downloading model weights...")
download_trained_weights(MODEL_DOWNLOAD_URL, str(model_path))
logger.info("Successfully downloaded the segmentation model weights!")
model = modellib.MaskRCNN(mode="inference", model_dir=".", config=_inference_config)
model.load_weights(os.path.join(model_path, MODEL_FILENAME), by_name=True)
return model
def _warmup_model(model: modellib.MaskRCNN) -> None:
"""
Warm up model with dummy inference to eliminate cold-start latency.
First inference triggers lazy initialization and can be 10-100x slower.
"""
logger.info("Warming up model...")
dummy_image = np.ones((1024, 1024, 3), dtype=np.uint8) * 255
cv2.rectangle(dummy_image, (100, 100), (200, 200), (0, 0, 0), 2)
try:
_ = model.detect([dummy_image], verbose=0)
logger.info("Model warmup complete")
except Exception as e:
logger.warning(f"Model warmup encountered issue: {e}")
def download_trained_weights(model_url: str, model_path: str, verbose=1):
"""This function downloads the trained modelto a given location.
If the model exists on the given location this function
will be skipped.
Args:
model_url (str): trained model url for downloading.
model_path (str): model default path to download.
Returns:
path (str): downloaded model.
"""
# Download trained models
if verbose > 0:
print("Downloading trained model to " + str(model_path))
model_path = pystow.ensure("DECIMER-Segmentation_model", url=model_url)
if verbose > 0:
print("... done downloading trained model!")
segmentation_model = get_model()
def segment_chemical_structures_from_file(
file_path: str,
expand: bool = True,
return_bboxes: bool = False,
**kwargs,
) -> Union[List[np.ndarray], Tuple[List[np.ndarray], List[Tuple[int, int, int, int]]]]:
"""
Segment chemical structures from a PDF or image file.
Args:
file_path: Path to input file (PDF or image)
expand: Whether to expand masks to capture complete structures
return_bboxes: Whether to return bounding boxes along with segments
Returns:
List of segmented chemical structure images as numpy arrays.
If return_bboxes is True, returns a tuple of (segments, bboxes).
"""
if not os.path.exists(file_path):
raise FileNotFoundError(f"Input file not found: {file_path}")
if "poppler_path" in kwargs:
logger.warning("poppler_path parameter is deprecated and ignored")
images = _load_images_from_file(file_path)
if not images:
logger.warning(f"No images could be extracted from {file_path}")
return ([], []) if return_bboxes else []
# Process all images sequentially (model can't parallelize)
all_segments = []
all_bboxes = []
for image in images:
segments, bboxes = segment_chemical_structures(
image, expand, return_bboxes=True
)
all_segments.extend(segments)
all_bboxes.extend(bboxes)
return (all_segments, all_bboxes) if return_bboxes else all_segments
def _load_images_from_file(file_path: str) -> List[np.ndarray]:
"""Load images from PDF or image file."""
if file_path.lower().endswith(".pdf"):
return _load_pdf_pages(file_path)
else:
return _load_single_image(file_path)
def _load_pdf_pages(pdf_path: str) -> List[np.ndarray]:
"""Load all pages from a PDF as images using PyMuPDF."""
pdf_document = pymupdf.open(pdf_path)
page_count = pdf_document.page_count
if page_count == 1:
# Single page - no threading overhead
page = pdf_document[0]
matrix = pymupdf.Matrix(300 / 72, 300 / 72)
pix = page.get_pixmap(matrix=matrix, alpha=False)
img_array = np.frombuffer(pix.samples, dtype=np.uint8).reshape(
pix.h, pix.w, pix.n
)
if pix.n == 3:
img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
pdf_document.close()
return [img_array.copy()]
# Multiple pages - use threading for I/O
images = [None] * page_count
def render_page(page_num: int) -> Tuple[int, np.ndarray]:
page = pdf_document[page_num]
matrix = pymupdf.Matrix(300 / 72, 300 / 72)
pix = page.get_pixmap(matrix=matrix, alpha=False)
img_array = np.frombuffer(pix.samples, dtype=np.uint8).reshape(
pix.h, pix.w, pix.n
)
if pix.n == 3:
img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
return page_num, img_array.copy()
with ThreadPoolExecutor(max_workers=min(4, page_count)) as executor:
futures = [executor.submit(render_page, i) for i in range(page_count)]
for future in futures:
page_num, img_array = future.result()
images[page_num] = img_array
pdf_document.close()
return [img for img in images if img is not None]
def _load_single_image(image_path: str) -> List[np.ndarray]:
"""Load a single image file."""
image = cv2.imread(image_path, cv2.IMREAD_COLOR)
if image is None:
raise ValueError(f"Could not load image: {image_path}")
return [image]
def segment_chemical_structures(
image: np.ndarray,
expand: bool = True,
visualization: bool = False,
return_bboxes: bool = False,
) -> Union[List[np.ndarray], Tuple[List[np.ndarray], List[Tuple[int, int, int, int]]]]:
"""
Segment chemical structures from an image.
Args:
image: Input image as numpy array (BGR format)
expand: Whether to expand masks to capture complete structures
visualization: Whether to display visualization (Jupyter only)
return_bboxes: Whether to return bounding boxes along with segments
Returns:
List of segmented structure images, optionally with bounding boxes.
If return_bboxes is True, returns a tuple of (segments, bboxes).
"""
if image is None or image.size == 0:
return ([], []) if return_bboxes else []
# Ensure BGR format
if len(image.shape) == 2:
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
elif image.shape[2] == 4:
image = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR)
# Get masks
if expand:
masks = get_expanded_masks(image)
else:
masks, _, _ = get_mrcnn_results(image)
# Apply masks to extract segments
segments, bboxes = apply_masks(image, masks)
if visualization and len(bboxes) > 0:
_visualize_results(image, masks, bboxes)
# Sort in reading order and filter empty
if segments:
segments, bboxes = _sort_segments_bboxes(segments, bboxes)
filtered = [
(s, b)
for s, b in zip(segments, bboxes)
if s is not None and s.size > 0 and s.shape[0] > 0 and s.shape[1] > 0
]
if filtered:
segments, bboxes = zip(*filtered)
segments, bboxes = list(segments), list(bboxes)
else:
segments, bboxes = [], []
return (segments, bboxes) if return_bboxes else segments
def _visualize_results(
image: np.ndarray, masks: np.ndarray, bboxes: List[Tuple[int, int, int, int]]
) -> None:
"""Display visualization of detection results."""
visualize.display_instances(
image=image,
masks=masks,
class_ids=np.array([0] * len(bboxes)),
boxes=np.array(bboxes),
class_names=np.array(["structure"] * len(bboxes)),
)
def get_mrcnn_results(
image: np.ndarray,
) -> Tuple[np.ndarray, List[Tuple[int, int, int, int]], List[float]]:
"""
Run MRCNN detection on an image.
Returns:
Tuple of (masks, bounding_boxes, confidence_scores)
"""
model = segmentation_model
results = model.detect([image], verbose=0)
return (
results[0]["masks"],
results[0]["rois"].tolist(),
results[0]["scores"].tolist(),
)
def get_expanded_masks(image: np.ndarray) -> np.ndarray:
"""
Get expanded masks that capture complete chemical structures.
Returns:
Expanded masks array of shape (height, width, num_masks)
"""
masks, bboxes, _ = get_mrcnn_results(image)
if len(bboxes) == 0:
return masks
max_size = _determine_depiction_size_with_buffer(bboxes)
return complete_structure_mask(
image_array=image, mask_array=masks, max_depiction_size=max_size, debug=False
)
def determine_depiction_size_with_buffer(
bboxes: List[Tuple[int, int, int, int]],
) -> Tuple[int, int]:
"""Calculate maximum depiction size with 10% buffer."""
if not bboxes:
return (100, 100)
bboxes_array = np.array(bboxes)
heights = bboxes_array[:, 2] - bboxes_array[:, 0]
widths = bboxes_array[:, 3] - bboxes_array[:, 1]
return (int(1.1 * np.max(heights)), int(1.1 * np.max(widths)))
# Alias for internal use
_determine_depiction_size_with_buffer = determine_depiction_size_with_buffer
def _sort_segments_bboxes(
segments: List[np.ndarray],
bboxes: List[Tuple[int, int, int, int]],
row_threshold: int = 50,
) -> Tuple[List[np.ndarray], List[Tuple[int, int, int, int]]]:
"""Sort segments in reading order (top-to-bottom, left-to-right)."""
if not segments:
return segments, bboxes
indices = sorted(range(len(bboxes)), key=lambda i: bboxes[i][0])
rows = []
current_row = [indices[0]]
for i in indices[1:]:
if abs(bboxes[i][0] - bboxes[current_row[-1]][0]) < row_threshold:
current_row.append(i)
else:
current_row.sort(key=lambda idx: bboxes[idx][1])
rows.append(current_row)
current_row = [i]
current_row.sort(key=lambda idx: bboxes[idx][1])
rows.append(current_row)
sorted_indices = [idx for row in rows for idx in row]
return ([segments[i] for i in sorted_indices], [bboxes[i] for i in sorted_indices])
def apply_masks(
image: np.ndarray, masks: np.ndarray
) -> Tuple[List[np.ndarray], List[Tuple[int, int, int, int]]]:
"""
Apply masks to image and extract segmented regions.
Returns:
Tuple of (segment_images, bounding_boxes)
"""
if masks.size == 0 or masks.shape[2] == 0:
return [], []
num_masks = masks.shape[2]
segments = []
bboxes = []
for i in range(num_masks):
segment, bbox = _apply_single_mask(image, masks[:, :, i])
segments.append(segment)
bboxes.append(bbox)
return segments, bboxes
def _apply_single_mask(
image: np.ndarray, mask: np.ndarray
) -> Tuple[np.ndarray, Tuple[int, int, int, int]]:
"""Apply a single mask to extract a segment from the image."""
rows = np.any(mask, axis=1)
cols = np.any(mask, axis=0)
if not rows.any() or not cols.any():
return np.zeros((1, 1, 4), dtype=np.uint8), (0, 0, 0, 0)
y_indices = np.where(rows)[0]
x_indices = np.where(cols)[0]
y0, y1 = y_indices[0], y_indices[-1] + 1
x0, x1 = x_indices[0], x_indices[-1] + 1
roi = image[y0:y1, x0:x1].copy()
mask_roi = mask[y0:y1, x0:x1]
if len(roi.shape) == 2:
roi = cv2.cvtColor(roi, cv2.COLOR_GRAY2BGR)
alpha = (mask_roi * 255).astype(np.uint8)
b, g, r = cv2.split(roi)
rgba = cv2.merge([b, g, r, alpha])
rgba[alpha == 0] = [255, 255, 255, 255]
return rgba, (y0, x0, y1, x1)
def save_images(images: List[np.ndarray], output_dir: str, base_name: str) -> None:
"""Save images to disk with generated filenames."""
os.makedirs(output_dir, exist_ok=True)
for index, image in enumerate(images):
if image is None or image.size == 0:
continue
filepath = os.path.join(output_dir, f"{base_name}_{index}.png")
cv2.imwrite(filepath, image)
def get_bnw_image(image: np.ndarray) -> np.ndarray:
"""Convert image to black and white using Otsu thresholding."""
if image is None or image.size == 0:
return image
if len(image.shape) == 3:
gray = cv2.cvtColor(
image, cv2.COLOR_BGRA2GRAY if image.shape[2] == 4 else cv2.COLOR_BGR2GRAY
)
else:
gray = image
_, binarized = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
return binarized
def get_square_image(image: np.ndarray, target_size: int = 299) -> np.ndarray:
"""Resize image to square without distortion (with padding)."""
if image is None or image.size == 0:
return np.full((target_size, target_size), 255, dtype=np.uint8)
if len(image.shape) == 3:
gray = cv2.cvtColor(
image, cv2.COLOR_BGRA2GRAY if image.shape[2] == 4 else cv2.COLOR_BGR2GRAY
)
else:
gray = image
h, w = gray.shape[:2]
scale = target_size / max(h, w)
new_h, new_w = int(h * scale), int(w * scale)
resized = cv2.resize(gray, (new_w, new_h), interpolation=cv2.INTER_LANCZOS4)
output = np.full((target_size, target_size), 255, dtype=np.uint8)
y_offset = (target_size - new_h) // 2
x_offset = (target_size - new_w) // 2
output[y_offset : y_offset + new_h, x_offset : x_offset + new_w] = resized
return output
# =============================================================================
# Configuration API
# =============================================================================
def set_inference_config(
post_nms_rois: int = 500,
detection_max_instances: int = 50,
detection_min_confidence: float = 0.7,
) -> None:
"""
Configure inference parameters for speed/accuracy tradeoff.
Must be called BEFORE first model load.
Args:
post_nms_rois: ROIs after NMS (default: 500, original: 1000)
detection_max_instances: Max detections (default: 50, original: 100)
detection_min_confidence: Min confidence (default: 0.7)
"""
if _model is not None:
logger.warning(
"Config changes won't take effect - model already loaded. Call reset_model() first."
)
return
_inference_config.POST_NMS_ROIS_INFERENCE = post_nms_rois
_inference_config.DETECTION_MAX_INSTANCES = detection_max_instances
_inference_config.DETECTION_MIN_CONFIDENCE = detection_min_confidence
_inference_config.PRE_NMS_LIMIT = post_nms_rois * 6
def reset_model() -> None:
"""Reset model to allow reconfiguration."""
global _model, _model_warmed_up
with _model_lock:
_model = None
_model_warmed_up = False
logger.info("Model reset. New config will apply on next load.")
# =============================================================================
# CLI
# =============================================================================
def main():
"""Command-line interface for DECIMER Segmentation."""
parser = argparse.ArgumentParser(
description="Segment chemical structures from scientific literature"
)
parser.add_argument(
"--input", "-i", required=True, help="Input file path (PDF or image)"
)
parser.add_argument("--output", "-o", help="Output directory")
parser.add_argument(
"--no-expand", action="store_true", help="Disable mask expansion"
)
parser.add_argument(
"--fast", action="store_true", help="Use faster settings (fewer proposals)"
)
args = parser.parse_args()
if args.fast:
set_inference_config(post_nms_rois=300, detection_max_instances=30)
output_dir = args.output or f"{args.input}_output"
segment_dir = os.path.join(output_dir, "segments")
logger.info("Loading model...")
get_model()
logger.info(f"Processing: {args.input}")
segments = segment_chemical_structures_from_file(
args.input, expand=not args.no_expand
)
if not segments:
logger.warning("No chemical structures found.")
return
base_name = os.path.splitext(os.path.basename(args.input))[0]
save_images(segments, segment_dir, base_name)
logger.info(f"Saved {len(segments)} segments to {segment_dir}")
if __name__ == "__main__":
main()