Spaces:
Sleeping
Sleeping
Commit ·
a45a95f
1
Parent(s): a19e8b0
GeoViG Debug: Add detailed logging for model inference
Browse files
app.py
CHANGED
|
@@ -104,8 +104,14 @@ def _draw_detections(image: Image.Image, result, score_thr=0.3):
|
|
| 104 |
draw = ImageDraw.Draw(img_draw)
|
| 105 |
seg_np_img = np.array(image.convert("RGB"))
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
# Robust unpacking for mmdet 2.x
|
| 108 |
-
if isinstance(result, (list, tuple)) and len(result) == 2:
|
| 109 |
bbox_result, segm_result = result
|
| 110 |
else:
|
| 111 |
bbox_result, segm_result = result, None
|
|
@@ -113,6 +119,13 @@ def _draw_detections(image: Image.Image, result, score_thr=0.3):
|
|
| 113 |
count = 0
|
| 114 |
if bbox_result is not None:
|
| 115 |
for cls_id, bboxes in enumerate(bbox_result):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
color = tuple(np.random.randint(100, 255, 3).tolist())
|
| 117 |
for i, bbox in enumerate(bboxes):
|
| 118 |
if bbox[4] < score_thr: continue
|
|
@@ -132,32 +145,32 @@ def _draw_detections(image: Image.Image, result, score_thr=0.3):
|
|
| 132 |
seg_sample = np.array(color, dtype=np.uint8)
|
| 133 |
seg_np_img[mask_bool] = (seg_np_img[mask_bool] * 0.5 + seg_sample * 0.5).astype(np.uint8)
|
| 134 |
except Exception as e:
|
| 135 |
-
print(f"Mask blend failed: {e}")
|
| 136 |
|
| 137 |
-
|
| 138 |
-
print(f"No objects detected above {score_thr} threshold.")
|
| 139 |
-
|
| 140 |
return img_draw.convert("RGB"), Image.fromarray(seg_np_img)
|
| 141 |
|
| 142 |
DET_MODEL_CACHE = {}
|
| 143 |
def detect(image: Image.Image, model_size: str, score_thr: float):
|
| 144 |
if image is None: return None, None
|
| 145 |
try:
|
| 146 |
-
# Move imports inside to avoid health-check timeout
|
| 147 |
import geovig_det_backbone
|
| 148 |
from mmdet.apis import init_detector, inference_detector
|
| 149 |
import cv2
|
| 150 |
-
except ImportError:
|
|
|
|
| 151 |
return image, image
|
| 152 |
|
| 153 |
key = f"det_{model_size.lower()}"
|
| 154 |
if key not in DET_MODEL_CACHE:
|
| 155 |
cfg = {"M": "configs/mask_rcnn_geovig_m_fpn_1x_coco.py", "B": "configs/mask_rcnn_geovig_b_fpn_1x_coco.py"}[model_size]
|
| 156 |
path = get_ckpt_path(key)
|
|
|
|
| 157 |
if not path: return image, image
|
| 158 |
DET_MODEL_CACHE[key] = init_detector(cfg, path, device="cpu")
|
| 159 |
|
| 160 |
cv_img = cv2.cvtColor(np.array(image.convert("RGB")), cv2.COLOR_RGB2BGR)
|
|
|
|
| 161 |
result = inference_detector(DET_MODEL_CACHE[key], cv_img)
|
| 162 |
return _draw_detections(image, result, score_thr=score_thr)
|
| 163 |
|
|
@@ -177,36 +190,39 @@ def segment_medical(image: Image.Image, dataset: str, score_thr: float):
|
|
| 177 |
cfg = {"Kvasir-SEG": "configs/kvasir/mask_rcnn_geovig_m_fpn_1x_kvasir.py", "DSB 2018": "configs/dsb/mask_rcnn_geovig_m_fpn_1x_dsb.py"}[dataset]
|
| 178 |
v_key = "kvasir_m" if dataset == "Kvasir-SEG" else "dsb_m"
|
| 179 |
path = get_ckpt_path(v_key)
|
|
|
|
| 180 |
if not path: return image
|
| 181 |
MED_MODEL_CACHE[key] = init_detector(cfg, path, device="cpu")
|
| 182 |
|
| 183 |
cv_img = cv2.cvtColor(np.array(image.convert("RGB")), cv2.COLOR_RGB2BGR)
|
|
|
|
| 184 |
result = inference_detector(MED_MODEL_CACHE[key], cv_img)
|
| 185 |
|
|
|
|
| 186 |
if isinstance(result, (list, tuple)) and len(result) == 2:
|
| 187 |
bbox_result, segm_result = result
|
| 188 |
else:
|
| 189 |
bbox_result, segm_result = result, None
|
| 190 |
|
| 191 |
combined_mask = np.zeros(cv_img.shape[:2], dtype=np.float32)
|
| 192 |
-
found_med =
|
| 193 |
-
if
|
| 194 |
-
for cls_id,
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
if
|
| 200 |
-
|
| 201 |
-
mask =
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
-
|
| 205 |
-
print(f"Medical: No segmentation found at threshold {score_thr}")
|
| 206 |
-
|
| 207 |
img_arr = np.array(image.convert("RGB")).astype(float)
|
| 208 |
overlay = img_arr.copy()
|
| 209 |
-
# Green-cyan tint for medical masks
|
| 210 |
overlay[combined_mask > 0.5] = np.array([0, 200, 150], dtype=float)
|
| 211 |
blended = (img_arr * 0.4 + overlay * 0.6).astype(np.uint8)
|
| 212 |
return Image.fromarray(blended)
|
|
|
|
| 104 |
draw = ImageDraw.Draw(img_draw)
|
| 105 |
seg_np_img = np.array(image.convert("RGB"))
|
| 106 |
|
| 107 |
+
print(f"DEBUG: Result type: {type(result)}")
|
| 108 |
+
if isinstance(result, (list, tuple)):
|
| 109 |
+
print(f"DEBUG: Result length: {len(result)}")
|
| 110 |
+
if len(result) > 0:
|
| 111 |
+
print(f"DEBUG: [0] type: {type(result[0])}")
|
| 112 |
+
|
| 113 |
# Robust unpacking for mmdet 2.x
|
| 114 |
+
if isinstance(result, (list, tuple)) and len(result) == 2 and isinstance(result[0], list):
|
| 115 |
bbox_result, segm_result = result
|
| 116 |
else:
|
| 117 |
bbox_result, segm_result = result, None
|
|
|
|
| 119 |
count = 0
|
| 120 |
if bbox_result is not None:
|
| 121 |
for cls_id, bboxes in enumerate(bbox_result):
|
| 122 |
+
if len(bboxes) == 0: continue
|
| 123 |
+
|
| 124 |
+
# Print highest score for this class to debug thresholding
|
| 125 |
+
max_s = np.max(bboxes[:, 4]) if bboxes.size > 0 else 0
|
| 126 |
+
if max_s > 0.1:
|
| 127 |
+
print(f"DEBUG: Cls {cls_id} ({COCO_NAMES[cls_id] if cls_id < len(COCO_NAMES) else cls_id}) max score: {max_s:.4f}")
|
| 128 |
+
|
| 129 |
color = tuple(np.random.randint(100, 255, 3).tolist())
|
| 130 |
for i, bbox in enumerate(bboxes):
|
| 131 |
if bbox[4] < score_thr: continue
|
|
|
|
| 145 |
seg_sample = np.array(color, dtype=np.uint8)
|
| 146 |
seg_np_img[mask_bool] = (seg_np_img[mask_bool] * 0.5 + seg_sample * 0.5).astype(np.uint8)
|
| 147 |
except Exception as e:
|
| 148 |
+
print(f"DEBUG: Mask blend failed: {e}")
|
| 149 |
|
| 150 |
+
print(f"DEBUG: Total detections drawn: {count}")
|
|
|
|
|
|
|
| 151 |
return img_draw.convert("RGB"), Image.fromarray(seg_np_img)
|
| 152 |
|
| 153 |
DET_MODEL_CACHE = {}
|
| 154 |
def detect(image: Image.Image, model_size: str, score_thr: float):
|
| 155 |
if image is None: return None, None
|
| 156 |
try:
|
|
|
|
| 157 |
import geovig_det_backbone
|
| 158 |
from mmdet.apis import init_detector, inference_detector
|
| 159 |
import cv2
|
| 160 |
+
except ImportError as e:
|
| 161 |
+
print(f"DEBUG: Import Error: {e}")
|
| 162 |
return image, image
|
| 163 |
|
| 164 |
key = f"det_{model_size.lower()}"
|
| 165 |
if key not in DET_MODEL_CACHE:
|
| 166 |
cfg = {"M": "configs/mask_rcnn_geovig_m_fpn_1x_coco.py", "B": "configs/mask_rcnn_geovig_b_fpn_1x_coco.py"}[model_size]
|
| 167 |
path = get_ckpt_path(key)
|
| 168 |
+
print(f"DEBUG: Loading detector {key} from {path}")
|
| 169 |
if not path: return image, image
|
| 170 |
DET_MODEL_CACHE[key] = init_detector(cfg, path, device="cpu")
|
| 171 |
|
| 172 |
cv_img = cv2.cvtColor(np.array(image.convert("RGB")), cv2.COLOR_RGB2BGR)
|
| 173 |
+
print(f"DEBUG: Running inference...")
|
| 174 |
result = inference_detector(DET_MODEL_CACHE[key], cv_img)
|
| 175 |
return _draw_detections(image, result, score_thr=score_thr)
|
| 176 |
|
|
|
|
| 190 |
cfg = {"Kvasir-SEG": "configs/kvasir/mask_rcnn_geovig_m_fpn_1x_kvasir.py", "DSB 2018": "configs/dsb/mask_rcnn_geovig_m_fpn_1x_dsb.py"}[dataset]
|
| 191 |
v_key = "kvasir_m" if dataset == "Kvasir-SEG" else "dsb_m"
|
| 192 |
path = get_ckpt_path(v_key)
|
| 193 |
+
print(f"DEBUG: Loading medical {key} from {path}")
|
| 194 |
if not path: return image
|
| 195 |
MED_MODEL_CACHE[key] = init_detector(cfg, path, device="cpu")
|
| 196 |
|
| 197 |
cv_img = cv2.cvtColor(np.array(image.convert("RGB")), cv2.COLOR_RGB2BGR)
|
| 198 |
+
print(f"DEBUG: Running medical inference...")
|
| 199 |
result = inference_detector(MED_MODEL_CACHE[key], cv_img)
|
| 200 |
|
| 201 |
+
print(f"DEBUG: Med Result type: {type(result)}")
|
| 202 |
if isinstance(result, (list, tuple)) and len(result) == 2:
|
| 203 |
bbox_result, segm_result = result
|
| 204 |
else:
|
| 205 |
bbox_result, segm_result = result, None
|
| 206 |
|
| 207 |
combined_mask = np.zeros(cv_img.shape[:2], dtype=np.float32)
|
| 208 |
+
found_med = 0
|
| 209 |
+
if bbox_result is not None:
|
| 210 |
+
for cls_id, bboxes in enumerate(bbox_result):
|
| 211 |
+
if bboxes.size > 0:
|
| 212 |
+
print(f"DEBUG: Med Cls {cls_id} max score: {np.max(bboxes[:, 4]):.4f}")
|
| 213 |
+
for i, bbox in enumerate(bboxes):
|
| 214 |
+
if i < len(segm_result[cls_id]) if segm_result else False:
|
| 215 |
+
if bbox[4] >= score_thr:
|
| 216 |
+
found_med += 1
|
| 217 |
+
mask = segm_result[cls_id][i]
|
| 218 |
+
if isinstance(mask, dict):
|
| 219 |
+
import pycocotools.mask as mask_util
|
| 220 |
+
mask = mask_util.decode(mask)
|
| 221 |
+
combined_mask = np.maximum(combined_mask, mask.astype(np.float32))
|
| 222 |
|
| 223 |
+
print(f"DEBUG: Med Total segments: {found_med}")
|
|
|
|
|
|
|
| 224 |
img_arr = np.array(image.convert("RGB")).astype(float)
|
| 225 |
overlay = img_arr.copy()
|
|
|
|
| 226 |
overlay[combined_mask > 0.5] = np.array([0, 200, 150], dtype=float)
|
| 227 |
blended = (img_arr * 0.4 + overlay * 0.6).astype(np.uint8)
|
| 228 |
return Image.fromarray(blended)
|