Process a batch of images and questions
(model, batch_images, batch_questions, id_list, all_outputs)
| 112 | json.dump(all_outputs, f, indent=2, ensure_ascii=False) |
| 113 | |
| 114 | def process_batch(model, batch_images, batch_questions, id_list, all_outputs): |
| 115 | """Process a batch of images and questions""" |
| 116 | batch_results = model.detect_objects_batch(batch_images, batch_questions) |
| 117 | for i, result in enumerate(batch_results): |
| 118 | try: |
| 119 | thinking = result["thinking"] |
| 120 | bboxes = result["bboxes"] |
| 121 | |
| 122 | gt_bboxes = id_list[i]["bbox"] |
| 123 | |
| 124 | if gt_bboxes and len(bboxes) > 0: |
| 125 | # # Use vectorized calculation of IOU matrix |
| 126 | # cost_matrix = -compute_bbox_iou(bboxes, gt_bboxes) # Use negative IOU as cost |
| 127 | |
| 128 | # # Use Hungarian algorithm for matching |
| 129 | # pred_indices, gt_indices = linear_sum_assignment(cost_matrix) |
| 130 | |
| 131 | # # Assign scores to each predicted box |
| 132 | # scores = np.zeros(len(bboxes)) |
| 133 | # for pred_idx, gt_idx in zip(pred_indices, gt_indices): |
| 134 | # scores[pred_idx] = -cost_matrix[pred_idx, gt_idx] # Convert back to positive IOU value |
| 135 | |
| 136 | # Add results |
| 137 | for pred_idx, pred_bbox in enumerate(bboxes): |
| 138 | all_outputs.append({ |
| 139 | "image_id": int(id_list[i]["image_id"]), |
| 140 | "ann_id": int(id_list[i]["ann_id"]), |
| 141 | "think": thinking, |
| 142 | "category_id": int(id_list[i]["cat_id"]), |
| 143 | "bbox": pred_bbox, |
| 144 | #"score": float(max(scores[pred_idx],0.0)) # Use the match score |
| 145 | "score": float((pred_bbox[2]-pred_bbox[0])*(pred_bbox[3]-pred_bbox[1])/(id_list[i]["img_width"]*id_list[i]["img_height"])) |
| 146 | }) |
| 147 | else: |
| 148 | # If there are no ground truth boxes or predicted boxes, score is 0 |
| 149 | for pred_bbox in bboxes: |
| 150 | all_outputs.append({ |
| 151 | "image_id": int(id_list[i]["image_id"]), |
| 152 | "ann_id": int(id_list[i]["ann_id"]), |
| 153 | "think": thinking, |
| 154 | "category_id": int(id_list[i]["cat_id"]), |
| 155 | "bbox": pred_bbox, |
| 156 | "score": 0.0 |
| 157 | }) |
| 158 | |
| 159 | except Exception as e: |
| 160 | # raise |
| 161 | print(f"Error processing result: {e}") |
| 162 | # Skip this because the implementation is different from the original |
| 163 | continue |
| 164 | |
| 165 | if __name__ == "__main__": |
| 166 | main() |
no test coverage detected