我正在学习Andrew的卷积神经网络,并在课程的第三周讨论利用YOLO算法进行目标检测。我不明白编程作业中有一个部分使用一个叫做'scale_boxes‘的函数。这是在课程材料中所描述的功能。
“*有几种表示框的方法,例如通过它们的角或它们的中点和高度/宽度。YOLO在不同的时间使用以下函数(我们提供的)在几种这样的格式之间进行转换:
box = yolo_boxes_to_corners(box_xy,box_wh),它将yolo盒坐标(x、y、w、h)转换为盒角坐标(x1、y1、x2、y2),以适应yolo_filter_boxes的输入。
boxes = scale_boxes(boxes,image_shape) YOLO的网络被训练为在608x608图像上运行。如果您要在不同大小的图像上测试这些数据--例如,汽车检测数据集有720x1280张图像--这一步骤将重新调整框的大小,以便将它们绘制在原始的720x1280图像之上。“
函数scale_boxes本身定义为:
def scale_boxes(boxes, image_shape):
""" Scales the predicted boxes in order to be drawable on the image"""
height = image_shape[0]
width = image_shape[1]
image_dims = K.stack([height, width, height, width])
image_dims = K.reshape(image_dims, [1, 4])
boxes = boxes * image_dims
return boxes它在以下函数'yolo_eval‘中使用:
def yolo_eval(yolo_outputs, image_shape = (720., 1280.), max_boxes=10, score_threshold=.6, iou_threshold=.5):
"""
Converts the output of YOLO encoding (a lot of boxes) to your predicted boxes along with their scores, box coordinates and classes.
Arguments:
yolo_outputs -- output of the encoding model (for image_shape of (608, 608, 3)), contains 4 tensors:
box_confidence: tensor of shape (None, 19, 19, 5, 1)
box_xy: tensor of shape (None, 19, 19, 5, 2)
box_wh: tensor of shape (None, 19, 19, 5, 2)
box_class_probs: tensor of shape (None, 19, 19, 5, 80)
image_shape -- tensor of shape (2,) containing the input shape, in this notebook we use (608., 608.) (has to be float32 dtype)
max_boxes -- integer, maximum number of predicted boxes you'd like
score_threshold -- real value, if [ highest class probability score < threshold], then get rid of the corresponding box
iou_threshold -- real value, "intersection over union" threshold used for NMS filtering
Returns:
scores -- tensor of shape (None, ), predicted score for each box
boxes -- tensor of shape (None, 4), predicted box coordinates
classes -- tensor of shape (None,), predicted class for each box
"""
### START CODE HERE ###
# Retrieve outputs of the YOLO model (≈1 line)
box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs
# Convert boxes to be ready for filtering functions (convert boxes box_xy and box_wh to corner coordinates)
boxes = yolo_boxes_to_corners(box_xy, box_wh)
# Use one of the functions you've implemented to perform Score-filtering with a threshold of score_threshold (≈1 line)
scores, boxes, classes = yolo_filter_boxes(box_confidence,boxes,box_class_probs,score_threshold)
# Scale boxes back to original image shape.
boxes = scale_boxes(boxes, image_shape)
# Use one of the functions you've implemented to perform Non-max suppression with
# maximum number of boxes set to max_boxes and a threshold of iou_threshold (≈1 line)
scores, boxes, classes = yolo_non_max_suppression(scores,boxes,classes,max_boxes,iou_threshold)
### END CODE HERE ###
return scores, boxes, classes我不明白函数'scale_boxes‘的必要性。在讨论论坛上似乎也没有任何答案或注意,这就是为什么我在这里张贴这个问题。
有谁能详细解释一下这个功能到底是做什么的,为什么需要它?
发布于 2020-07-16 11:19:39
YOLO的网络被训练成在608x608图像上运行。如果您要在不同大小的图像上测试这些数据--例如,汽车检测数据集有720x1280图像--这一步骤重新调整了这些框的大小,以便能够在原始的720x1280图像上绘制它们。
因为你用的是一个预先训练过的模型。它会将你的图像调整到它训练的大小。不管是你做的还是模型做的,都是在后台做的。
边界框值是图像上的简单坐标。它将随着图像大小的变化而变化。想象一张大图像上的一张脸,一张小图像上的一张脸。

因此,YOLO将返回较小图像的坐标,如果您在原始图像上绘制它,它将不会覆盖整个对象。所以你用两个图像大小的比例来重放它。
您可以通过调整原始图像到YOLO训练图像的大小来实现同样的目标,然后您就不需要缩放您的边界框了。您可以简单地在这个调整大小的图像上绘制相同的框。
https://datascience.stackexchange.com/questions/77719
复制相似问题