Process a batch of images and questions
(model, batch_images, batch_questions, id_list, all_outputs, has_bbox)
| 155 | return [int(x_min), int(y_min), int(x_max), int(y_max)] |
| 156 | |
| 157 | def process_batch(model, batch_images, batch_questions, id_list, all_outputs, has_bbox): |
| 158 | """Process a batch of images and questions""" |
| 159 | batch_results = model.segment_objects_batch(batch_images, batch_questions) |
| 160 | |
| 161 | for i, result in enumerate(batch_results): |
| 162 | try: |
| 163 | thinking = result["thinking"] |
| 164 | bboxes = result["bboxes"] |
| 165 | mask_all = result["masks"] |
| 166 | gt_mask = np.array(id_list[i]["mask"]) |
| 167 | |
| 168 | # visualize_result(batch_images[i], mask_all, gt_mask, thinking, batch_questions[i]) |
| 169 | |
| 170 | intersection, union = compute_iou(mask_all, gt_mask) |
| 171 | |
| 172 | bbox_iou = 0.0 |
| 173 | |
| 174 | try: |
| 175 | if has_bbox: |
| 176 | gt_bbox = id_list[i]["bbox"] |
| 177 | else: |
| 178 | gt_bbox = get_bbox(gt_mask) |
| 179 | |
| 180 | if gt_bbox == []: |
| 181 | if len(bboxes) == 0: |
| 182 | bbox_iou = 1.0 |
| 183 | else: |
| 184 | bbox_iou = 0.0 |
| 185 | else: |
| 186 | # adapt to multi-object detection |
| 187 | for pred_bbox in bboxes: |
| 188 | if compute_bbox_iou(pred_bbox, gt_bbox) > 0.5: |
| 189 | bbox_iou = 1.0 |
| 190 | break |
| 191 | merged_bbox = merge_bboxes(bboxes) |
| 192 | if merged_bbox: |
| 193 | if compute_bbox_iou(merged_bbox, gt_bbox) > 0.5: |
| 194 | bbox_iou = 1.0 |
| 195 | |
| 196 | except Exception as e: |
| 197 | print(f"Bbox error: {e}, Image ID: {id_list[i]['image_id']}, Ann ID: {id_list[i]['ann_id']}") |
| 198 | bbox_iou = 0.0 |
| 199 | |
| 200 | all_outputs.append({ |
| 201 | "image_id": id_list[i]["image_id"], |
| 202 | "ann_id": id_list[i]["ann_id"], |
| 203 | "think": thinking, |
| 204 | "intersection": int(intersection), |
| 205 | "union": int(union), |
| 206 | "bbox_iou": bbox_iou, |
| 207 | "non_object": int(gt_mask.sum()==0 and len(bboxes)==0), # for grefcoco where there is no object in the image |
| 208 | "non_object_GT": int(gt_mask.sum()==0) |
| 209 | }) |
| 210 | |
| 211 | except Exception as e: |
| 212 | print(f"Error processing result: {e}") |
| 213 | # Add penalty in this situation |
| 214 | all_outputs.append({ |
no test coverage detected