为何部分图像能显示Bounding Box,部分无法显示?(Keras模型)
问题:部分图像无法显示预测Bounding Box的调试方案
我参考Keras官方目标检测模型评估章节,用预训练模型绘制Bounding Box,但相同代码下,部分图像能正常显示框,部分图像有预测标签却看不到Bounding Box。已知图像显示黑色是数据类型问题,无需关注,求代码调试帮助。
我的代码:
model_load = load_model('stenosis_model_transfer.h5') # 计算IoU(交并比) def bounding_box_intersection_over_union(box_predicted, box_truth): # 获取边界框交集的坐标 top_x_intersect = max(box_predicted[0], box_truth[0]) top_y_intersect = max(box_predicted[1], box_truth[1]) bottom_x_intersect = min(box_predicted[2], box_truth[2]) bottom_y_intersect = min(box_predicted[3], box_truth[3]) # 计算交集面积 intersection_area = max(0, bottom_x_intersect - top_x_intersect + 1) * max( 0, bottom_y_intersect - top_y_intersect + 1 ) # 计算预测框和真实框的面积 box_predicted_area = (box_predicted[2] - box_predicted[0] + 1) * ( box_predicted[3] - box_predicted[1] + 1 ) box_truth_area = (box_truth[2] - box_truth[0] + 1) * ( box_truth[3] - box_truth[1] + 1 ) # 返回IoU值 return intersection_area / float( box_predicted_area + box_truth_area - intersection_area ) i, mean_iou = 0, 0 # 对比测试集中前5张图像的结果 for input_image in test_images[:5]: fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 15)) im = input_image plt.tight_layout() # 显示图像 ax1.imshow(im.astype("uint8")) ax2.imshow(im.astype("uint8")) input_image = cv2.resize( input_image, (224, 224), interpolation=cv2.INTER_AREA ) input_image = np.expand_dims(input_image, axis=0) preds = model_load.predict(input_image)[0] (h, w) = (im).shape[0:2] top_left_x, top_left_y = int(preds[0] * w), int(preds[1] * h) bottom_right_x, bottom_right_y = int(preds[2] * w), int(preds[3] * h) box_predicted = [top_left_x, top_left_y, bottom_right_x, bottom_right_y] # 创建预测边界框 rect = patches.Rectangle( (top_left_x, top_left_y), bottom_right_x - top_left_x, bottom_right_y - top_left_y, facecolor="none", edgecolor="red", linewidth=1, ) ax1.add_patch(rect) ax1.set_xlabel( "Predicted: " + str(top_left_x) + ", " + str(top_left_y) + ", " + str(bottom_right_x) + ", " + str(bottom_right_y) ) # 获取真实框坐标 top_left_x, top_left_y = int(test_targets[i][0] * w), int(test_targets[i][1] * h) bottom_right_x, bottom_right_y = int(test_targets[i][2] * w), int(test_targets[i][3] * h) box_truth = top_left_x, top_left_y, bottom_right_x, bottom_right_y mean_iou += bounding_box_intersection_over_union(box_predicted, box_truth) # 创建真实边界框 rect = patches.Rectangle( (top_left_x, top_left_y), bottom_right_x - top_left_x, bottom_right_y - top_left_y, facecolor="none", edgecolor="red", linewidth=1, ) ax2.add_patch(rect) ax2.set_xlabel( "Target: " + str(top_left_x) + ", " + str(top_left_y) + ", " + str(bottom_right_x) + ", " + str(bottom_right_y) + "\nIoU: " + str(bounding_box_intersection_over_union(box_predicted, box_truth)) ) i = i + 1 plt.show()
调试建议
- 修复边界框尺寸合法性问题:当模型预测的右下角坐标小于左上角坐标时,
bottom_right_x - top_left_x或bottom_right_y - top_left_y会为负数,导致patches.Rectangle生成不可见的框。添加坐标修正逻辑:# 修正预测框坐标 width = bottom_right_x - top_left_x height = bottom_right_y - top_left_y if width < 0: top_left_x, bottom_right_x = bottom_right_x, top_left_x width = -width if height < 0: top_left_y, bottom_right_y = bottom_right_y, top_left_y height = -height # 重新创建矩形 rect = patches.Rectangle((top_left_x, top_left_y), width, height, facecolor="none", edgecolor="red", linewidth=2) - 验证模型输出范围:模型预测的坐标值可能超出
[0,1]范围,缩放后会超出图像边界。添加打印检查preds值,若超出则做裁剪:print(f"Predicted raw values: {preds}") preds = np.clip(preds, 0.0, 1.0) # 将预测值限制在0-1之间 - 确认数据索引匹配:检查
test_images和test_targets的遍历索引是否严格对应,避免因索引错位导致目标框坐标异常。 - 提升框线可见性:将
linewidth从1改为2或3,避免因线宽过细在部分图像中被忽略。
内容的提问来源于stack exchange,提问作者NevMthw
相关产品推荐
相关产品推荐

