Spaces:
Sleeping
Sleeping
remove debug statements
Browse files
src/htr_pipeline/inferencer.py
CHANGED
@@ -26,51 +26,26 @@ class Inferencer:
|
|
26 |
|
27 |
@timer_func
|
28 |
def predict_regions(self, input_image, pred_score_threshold=0.5, containments_threshold=0.5, visualize=True):
|
29 |
-
import time
|
30 |
-
|
31 |
-
t1 = time.time()
|
32 |
-
|
33 |
input_image = self.preprocess_img.binarize_img(input_image)
|
34 |
|
35 |
image = mmcv.imread(input_image)
|
36 |
|
37 |
-
t2 = time.time()
|
38 |
-
|
39 |
-
print(f"Function executed bin and read in {(t2-t1):.4f}s")
|
40 |
-
|
41 |
-
t1 = time.time()
|
42 |
-
|
43 |
result = self.seg_model(image, return_datasample=True)
|
44 |
result_pred = result["predictions"][0]
|
45 |
-
t2 = time.time()
|
46 |
-
|
47 |
-
print(f"Function executed predict in {(t2-t1):.4f}s")
|
48 |
-
|
49 |
-
t1 = time.time()
|
50 |
|
51 |
filtered_result_pred = self.postprocess_seg_mask.filter_on_pred_threshold(
|
52 |
result_pred, pred_score_threshold=pred_score_threshold
|
53 |
)
|
54 |
|
55 |
-
t2 = time.time()
|
56 |
-
|
57 |
-
print(f"Function executed filter in {(t2-t1):.4f}s")
|
58 |
-
|
59 |
if len(filtered_result_pred.pred_instances.masks) == 0:
|
60 |
raise gr.Error("No Regions were predicted by the model")
|
61 |
|
62 |
else:
|
63 |
-
t1 = time.time()
|
64 |
-
|
65 |
result_align = self.process_seg_mask.align_masks_with_image(filtered_result_pred, image)
|
66 |
result_clean = self.postprocess_seg_mask.remove_overlapping_masks(
|
67 |
predicted_mask=result_align, containments_threshold=containments_threshold
|
68 |
)
|
69 |
|
70 |
-
t2 = time.time()
|
71 |
-
|
72 |
-
print(f"Function executed align and remove in {(t2-t1):.4f}s")
|
73 |
-
|
74 |
if visualize:
|
75 |
result_viz = self.seg_model.visualize(
|
76 |
inputs=[image], preds=[result_clean], return_vis=True, no_save_vis=True
|
@@ -78,8 +53,6 @@ class Inferencer:
|
|
78 |
else:
|
79 |
result_viz = None
|
80 |
|
81 |
-
t1 = time.time()
|
82 |
-
|
83 |
regions_cropped, polygons = self.process_seg_mask.crop_masks(result_clean, image)
|
84 |
order = self.ordering.order_regions_marginalia(result_clean)
|
85 |
|
@@ -87,10 +60,6 @@ class Inferencer:
|
|
87 |
polygons_ordered = [polygons[i] for i in order]
|
88 |
masks_ordered = [result_clean.pred_instances.masks[i] for i in order]
|
89 |
|
90 |
-
t2 = time.time()
|
91 |
-
|
92 |
-
print(f"Function executed crop and margin in {(t2-t1):.4f}s")
|
93 |
-
|
94 |
return result_viz, regions_cropped_ordered, polygons_ordered, masks_ordered
|
95 |
|
96 |
@timer_func
|
|
|
26 |
|
27 |
@timer_func
|
28 |
def predict_regions(self, input_image, pred_score_threshold=0.5, containments_threshold=0.5, visualize=True):
|
|
|
|
|
|
|
|
|
29 |
input_image = self.preprocess_img.binarize_img(input_image)
|
30 |
|
31 |
image = mmcv.imread(input_image)
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
result = self.seg_model(image, return_datasample=True)
|
34 |
result_pred = result["predictions"][0]
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
filtered_result_pred = self.postprocess_seg_mask.filter_on_pred_threshold(
|
37 |
result_pred, pred_score_threshold=pred_score_threshold
|
38 |
)
|
39 |
|
|
|
|
|
|
|
|
|
40 |
if len(filtered_result_pred.pred_instances.masks) == 0:
|
41 |
raise gr.Error("No Regions were predicted by the model")
|
42 |
|
43 |
else:
|
|
|
|
|
44 |
result_align = self.process_seg_mask.align_masks_with_image(filtered_result_pred, image)
|
45 |
result_clean = self.postprocess_seg_mask.remove_overlapping_masks(
|
46 |
predicted_mask=result_align, containments_threshold=containments_threshold
|
47 |
)
|
48 |
|
|
|
|
|
|
|
|
|
49 |
if visualize:
|
50 |
result_viz = self.seg_model.visualize(
|
51 |
inputs=[image], preds=[result_clean], return_vis=True, no_save_vis=True
|
|
|
53 |
else:
|
54 |
result_viz = None
|
55 |
|
|
|
|
|
56 |
regions_cropped, polygons = self.process_seg_mask.crop_masks(result_clean, image)
|
57 |
order = self.ordering.order_regions_marginalia(result_clean)
|
58 |
|
|
|
60 |
polygons_ordered = [polygons[i] for i in order]
|
61 |
masks_ordered = [result_clean.pred_instances.masks[i] for i in order]
|
62 |
|
|
|
|
|
|
|
|
|
63 |
return result_viz, regions_cropped_ordered, polygons_ordered, masks_ordered
|
64 |
|
65 |
@timer_func
|