Exemple #1
0
def yolo_eval(yolo_outputs,
              image_shape=(720., 1280.),
              max_boxes=10,
              score_threshold=.6,
              iou_threshold=.5):
    """
    Aca aplicamos todas las funciones antes definidas, para hacer el filtrado por threshold y para hacer 
    el non-max suppression

    """

    # Obtenemos las probabilidades y los tamaños de las cajas
    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs

    # Convertimos las cajas para que sean los bordes de las cajas

    boxes = yolo_boxes_to_corners(box_xy, box_wh)

    # Filtramos a traves del threshold antes definido
    scores, boxes, classes = yolo_filter_boxes(box_confidence, boxes,
                                               box_class_probs, iou_threshold)

    # escalamos a la imagen original
    boxes = scale_boxes(boxes, image_shape)

    # Aplicamos non-max suppression
    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes,
                                                      max_boxes, iou_threshold)

    return scores, boxes, classes
Exemple #2
0
def yolo_eval(yolo_outputs,
              image_shape=(720., 1280.),
              max_boxes=10,
              score_threshold=0.6,
              iou_threshold=0.5):
    """
    yolo_output:
        box_confidence: [b, 19, 19, 5, 1]
        box_xy: [b, 19, 19, 5, 2]
        box_wh: [b, 19, 19, 5, 2]
        box_class_probs: [b, 19, 19, 5, 80]
    input shape of image: [b, 608, 608]
    scrore_threshold: the argument 'threshold' in yolo_filter_boxes()
    """

    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs

    # covert to corner coordinates format
    boxes = yolo_boxes_to_corners(box_xy, box_wh)

    scores, boxes, classes = yolo_filter_boxes(box_confidence, boxes,
                                               box_class_probs,
                                               score_threshold)

    boxes = scale_boxes(boxes, image_shape)

    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes,
                                                      max_boxes, iou_threshold)

    return scores, boxes, classes
def yolo_eval(yolo_outputs,
              image_shape=(720., 1280.),
              max_boxes=100,
              score_threshold=0.3,
              iou_threshold=0.3):
    # 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
    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,
                                               threshold=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 a threshold of iou_threshold (≈1 line)
    scores, boxes, classes = yolo_non_max_suppression(
        scores,
        boxes,
        classes,
        max_boxes=max_boxes,
        iou_threshold=iou_threshold)
    ### END CODE HERE ###

    return scores, boxes, classes
def yolo_eval_extend(yolo_outputs,
                     image_shape,
                     max_boxes=10,
                     score_threshold=.6,
                     iou_threshold=.5):
    """Evaluate YOLO model on given input batch and return filtered boxes."""
    box_xy, box_wh, box_confidence, box_class_probs = yolo_outputs
    boxes = yolo_boxes_to_corners(box_xy, box_wh)
    boxes, scores, classes, all_scores = yolo_filter_boxes_extend(
        boxes, box_confidence, box_class_probs, threshold=score_threshold)

    # Scale boxes back to original image shape.
    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

    # TODO: Something must be done about this ugly hack!
    max_boxes_tensor = K.variable(max_boxes, dtype='int32')
    K.get_session().run(tf.variables_initializer([max_boxes_tensor]))
    nms_index = tf.image.non_max_suppression(boxes,
                                             scores,
                                             max_boxes_tensor,
                                             iou_threshold=iou_threshold)
    boxes = K.gather(boxes, nms_index)
    scores = K.gather(scores, nms_index)
    classes = K.gather(classes, nms_index)
    all_scores = K.gather(all_scores, nms_index)
    return boxes, scores, classes, all_scores
Exemple #5
0
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
    """

    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs[0], yolo_outputs[1], yolo_outputs[2], yolo_outputs[3]

    # Convert boxes to be ready for filtering functions
    boxes = yolo_boxes_to_corners(box_xy, box_wh)

    scores, boxes, classes = yolo_filter_boxes(box_confidence, boxes, box_class_probs)

    # Scale boxes back to original image shape.
    boxes = scale_boxes(boxes, image_shape)

    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes)

    return scores, boxes, classes
def yolo_eval(yolo_outputs,
              image_shape=(720., 1280.),
              max_boxes=10,
              score_threshold=.6,
              iou_threshold=.5):

    # Retrieving  outputs of the YOLO model
    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs

    # Converting boxes for filtering functions
    boxes = yolo_boxes_to_corners(box_xy, box_wh)

    #function(yolo_filter_boxes) for  Score-filtering with a threshold of score_threshold (≈1 line)
    scores, boxes, classes = yolo_filter_boxes(box_confidence,
                                               boxes,
                                               box_class_probs,
                                               threshold=score_threshold)

    # Scaling boxes back to original image shape.
    boxes = scale_boxes(boxes, image_shape)

    # Using one of the yolo_non_max_suppression function to perform Non-max suppression with a threshold of iou_threshold (≈1 line)
    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes)

    return scores, boxes, classes
def yolo_eval(yolo_outputs, image_shape = (720., 1280.), max_boxes=10, score_threshold=.6, iou_threshold=.5):
    '''
    This function converts the output of YOLO encoding (a lot of boxes) to our predicted boxes along with their scores, box coordinates and classes.
    
    One main argument to keep in mind is fron the yolo trained model -->
    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)
    
    the finction will return following values -->
    cores -- 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
    '''
    
    # first we will be retriving the outputs from the yolu outputs of the model
    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs
    
    # Converting boxes to be ready for filtering functions 
    boxes = yolo_boxes_to_corners(box_xy, box_wh)
    # this function is imported from yad2k.models.keras_yolo
    
    # filtering the boxes
    scores, boxes, classes = yolo_filter_boxes(box_confidence, boxes, box_class_probs, threshold = score_threshold)
    
    # scaling the boxes back to original image shape
    boxes = scale_boxes(boxes, image_shape)
    # this is imported from yolo_utils
    
    # using the non-max supression on the filtered bosex
    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes, max_boxes = max_boxes, iou_threshold = iou_threshold)
    
    return scores, boxes, classes
Exemple #8
0
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.

    # Retrieve outputs of the YOLO model
    box_xy, box_wh, box_confidence, box_class_probs = yolo_outputs

    # Convert boxes to be ready for filtering functions
    boxes = yolo_boxes_to_corners(box_xy, box_wh)

    # Use one of the functions 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,
                                               threshold=.6)

    # Scale boxes back to original image shape.
    boxes = scale_boxes(boxes, image_shape)

    # Use one of the functions implemented to perform Non-max suppression with a threshold of iou_threshold
    scores, boxes, classes = yolo_non_max_suppression(scores,
                                                      boxes,
                                                      classes,
                                                      max_boxes=10,
                                                      iou_threshold=0.5)

    return scores, boxes, classes
Exemple #9
0
def yolo_eval(yolo_outputs,
              image_shape=(720., 1280.),
              max_boxes=10,
              score_threshold=.6,
              iou_threshold=.5):

    # 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)

    return scores, boxes, classes
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
    """

    # Retrieve outputs of the YOLO model
    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs

    # Convert boxes to be ready for filtering functions
    boxes = yolo_boxes_to_corners(box_xy, box_wh)

    scores, boxes, classes = yolo_filter_boxes(box_confidence, boxes, box_class_probs, threshold = score_threshold)

    boxes = scale_boxes(boxes, image_shape)

    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes, max_boxes = max_boxes, iou_threshold = iou_threshold)

    return scores, boxes, classes
Exemple #11
0
def yolo_eval(yolo_outputs,
              image_shape=(720., 1280.),
              max_boxes=10,
              score_threshold=.6,
              iou_threshold=.5):

    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs

    boxes = yolo_boxes_to_corners(box_xy, box_wh)

    scores, boxes, classes = yolo_filter_boxes(box_confidence,
                                               boxes,
                                               box_class_probs,
                                               threshold=score_threshold)

    boxes = scale_boxes(boxes, image_shape)

    scores, boxes, classes = yolo_non_max_suppression(
        scores,
        boxes,
        classes,
        max_boxes=max_boxes,
        iou_threshold=iou_threshold)

    return scores, boxes, classes
Exemple #12
0
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.


    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
    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 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
def yolo_eval(yolo_outputs,
              image_shape=(720., 1280.),
              max_boxes=10,
              score_threshold=0.6,
              iou_threshold=0.5):
    """

    参数:
        yolo_outputs - 编码模型的输出(对于维度(608,608,3)的图片的维度,这里是(608,608)
        max_boxes - 整数,预测的框锚数量的最大值
        score_threshold - 实数, 可能行阈值
        iou_threshold - 实数, 交并比的阈值

    返回:
        scores - tensor类型,维度(,None),每个锚框的预测可能值
        boxes - tensor类型, 维度(4, None),预测的锚框的坐标
        classes - tensor类型,维度(, None),每个锚框预测分类
    """
    # 获取YOLO模型的输出
    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs
    # 中心点转换为边角
    boxes = yolo_boxes_to_corners(box_xy, box_wh)
    # 可信度分支过滤
    scores, boxes, classes = yolo_filter_boxes(box_confidence, boxes,
                                               box_class_probs,
                                               score_threshold)
    #   缩放框锚,以适应原图像
    boxes = yolo_utils.scale_boxes(boxes, image_shape)

    #使用非最大意志
    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes,
                                                      max_boxes, iou_threshold)

    return scores, boxes, classes
def yolo_eval(yolo_outputs,
              image_shape=(720., 1280.),
              max_boxes=50,
              score_threshold=.2,
              iou_threshold=.7):
    """
    Argument:
    yolo_outputs--output of your the encoding model contains 4 tensor:
    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)
    """
    # Retrieve outputs of YOLO model
    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs

    # Convert the way of representing boxes to which seem as (x1,x2,y1,y2)
    boxes = yolo_boxes_to_corners(box_xy, box_wh)

    # Perform Score-filtering with a threshold of score
    scores, boxes, classes = yolo_filter_boxes(box_confidence,
                                               boxes,
                                               box_class_probs,
                                               threshold=score_threshold)

    # Scale boxes back to original image shape
    boxes = scale_boxes(boxes, image_shape)

    # Perform Non_max_supression with a threshold of Iou
    scores, boxes, classes = yolo_non_max_supression(scores, boxes, classes,
                                                     max_boxes, iou_threshold)

    return scores, boxes, classes
Exemple #15
0
    def _construct_graph(self,
                         max_boxes=10,
                         score_threshold=0.6,
                         iou_threshold=0.5):
        """Creates operations and instantiates them on default graph.

        Args:
            max_boxes (int, optional):
                Max. number of bounding boxes for non-max suppression.
            score_threshold (float, optional):
                Threshold value for min. score for a bounding box for score-filtering.
            iou_threshold (float, optional):
                Intersection over union threshold for non-max suppression.
        """
        yolo_outputs = yolo_head(self._model.output, self._anchors,
                                 len(self._class_names))
        box_xy, box_wh, box_confidence, box_class_probs = yolo_outputs
        boxes = yolo_boxes_to_corners(
            box_xy,
            box_wh)  # Convert boxes to be ready for filtering functions
        scores, boxes, classes = self._filter_boxes(box_confidence, boxes,
                                                    box_class_probs,
                                                    score_threshold)
        boxes = self._scale_boxes(
            boxes)  # Scale boxes back to original image shape.
        scores, boxes, classes = self._non_max_suppression(
            scores, boxes, classes, max_boxes, iou_threshold)
        # Save tensors for later evaluation
        self._scores = scores
        self._boxes = boxes
        self._classes = classes
Exemple #16
0
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,
                                               threshold=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=max_boxes,
        iou_threshold=iou_threshold)

    ### END CODE HERE ###

    return scores, boxes, classes
Exemple #17
0
def yolo_eval(yolo_outputs,
              image_shape=(720., 1280.),
              max_boxes=10,
              score_threshold=0.6,
              iou_threshold=0.5):
    """
    将YOLO编码的输出(很多锚框)转换为预测框以及它们的分数, 框坐标和类。

    参数:
        yolo_outputs - 编码模型的输出(对于维度为(608,608,3)的图片), 包含4个tensors类型的变量:
                        box_confidence : tensor类型, 维度为(None, 19, 19, 5, 1)
                        box_xy         : tensor类型, 维度为(None, 19, 19, 5, 2)
                        box_wh         : tensor类型, 维度为(None, 19, 19, 5, 2)
                        box_class_probs: tensor类型, 维度为(None, 19, 19, 5, 80)
        image_shape - tensor类型, 维度为(2,), 包含了输入的图像的维度, 这里是(608.,608.)
        max_boxes - 整数, 预测的锚框数量的最大值
        score_threshold - 实数, 可能性阈值
        iou_threshold - 实数, 交并比阈值

    返回:
        scores - tensor类型, 维度为(,None), 每个锚框的预测的可能值
        boxes - tensor类型, 维度为(4,None), 预测的锚框的坐标
        classes - tensor类型, 维度为(,None), 每个锚框的预测的分类
    """

    # 获取YOLO模型的输出
    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs

    # 中心点转换为边角
    boxes = yolo_boxes_to_corners(box_xy, box_wh)

    # 可信度分值过滤
    scores, boxes, classes = yolo_filter_boxes(box_confidence, boxes,
                                               box_class_probs,
                                               score_threshold)

    # 缩放锚框, 以适应原始图像
    # 本代码中实现的是在(608,608,3)RGB图像上的YOLO, 而输入输出图像并不一定是(608,608,3), 所以需要缩放锚框以匹配输入输出图像
    boxes = yolo_utils.scale_boxes(boxes, image_shape)

    # 使用非最大值抑制
    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes,
                                                      max_boxes, iou_threshold)

    # print(type(boxes))
    # temp_box=np.array(boxes)
    # for i in range(temp_box.shape[1]):
    #     temp=temp_box[:,i]

    return scores, boxes, classes
Exemple #18
0
def yolo_eval(yolo_outputs, image_shape = (720., 1280.), max_boxes=10, score_threshold=.6, iou_threshold=.5):
    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs
	
    # 转换成用顶点表示的坐标
    boxes = yolo_boxes_to_corners(box_xy, box_wh)

    # 过滤所有 可信度 低于score_threshold的 anchor box
    scores, boxes, classes = yolo_filter_boxes(box_confidence, boxes, box_class_probs, score_threshold)
    
    # 由于YOLO是用608*608的图片训练出来的, 如果想测试其他分辨率的图片, 
    # 我们需要通过下面的函数来对anchor box 的位置和尺寸进行相应的缩放.
    boxes = scale_boxes(boxes, image_shape)

    # 进行 非最大值抑制 
    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes, max_boxes, iou_threshold)
    
    return scores, boxes, classes
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) for 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 code uses (608., 608.) (has to be float32 dtype)
    max_boxes -- integer, maximum number of predicted boxes wanted
    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
    """
    # Implement a function taking the output of the deep CNN and filter through all the boxes using the functions just implemented.
    # Retrieve outputs of the YOLO model
    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs[
        0], yolo_outputs[1], yolo_outputs[2], yolo_outputs[3]

    # 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 previously implemented to perform Score-filtering with a threshold of score_threshold
    scores, boxes, classes = yolo_filter_boxes(box_confidence,
                                               boxes,
                                               box_class_probs,
                                               threshold=0.5)

    # Scale boxes back to original image shape.
    boxes = scale_boxes(boxes, image_shape)

    # Use one of the functions implemented to perform Non-max suppression with
    # maximum number of boxes set to max_boxes and a threshold of iou_threshold
    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes)

    return scores, boxes, classes
def yolo_eval(yolo_outputs,
              image_shape=(720., 1280.),
              max_boxes=10,
              score_threshold=0.6,
              iou_threshold=0.5):
    """
    将YOLO编码的输出(很多锚框)转换为预测框以及它们的分数,框坐标和类。

    参数:
        yolo_outputs - 编码模型的输出(对于维度为(608,608,3)的图片),包含4个tensors类型的变量:
                        box_confidence : tensor类型,维度为(None, 19, 19, 5, 1)
                        box_xy         : tensor类型,维度为(None, 19, 19, 5, 2)
                        box_wh         : tensor类型,维度为(None, 19, 19, 5, 2)
                        box_class_probs: tensor类型,维度为(None, 19, 19, 5, 80)
        image_shape - tensor类型,维度为(2,),包含了输入的图像的维度,这里是(608.,608.)
        max_boxes - 整数,预测的锚框数量的最大值
        score_threshold - 实数,可能性阈值。
        iou_threshold - 实数,交并比阈值。

    返回:
        scores - tensor类型,维度为(,None),每个锚框的预测的可能值
        boxes - tensor类型,维度为(4,None),预测的锚框的坐标
        classes - tensor类型,维度为(,None),每个锚框的预测的分类
    """

    # 获取YOLO模型的输出
    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs

    # 中心点转换为边角
    boxes = yolo_boxes_to_corners(box_xy, box_wh)

    # 可信度分值过滤
    scores, boxes, classes = yolo_filter_boxes(box_confidence, boxes,
                                               box_class_probs,
                                               score_threshold)

    # 缩放锚框,以适应原始图像
    boxes = yolo_utils.scale_boxes(boxes, image_shape)

    # 使用非最大值抑制
    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes,
                                                      max_boxes, iou_threshold)

    return scores, boxes, classes
Exemple #21
0
def yolo_eval(yolo_outputs, image_shape = (720., 1280.), max_boxes = 10, \
 score_threshold = .6, iou_threshold = .5):

    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs
    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 a threshold of iou_threshold (≈1 line)
    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes,
                                                      max_boxes, iou_threshold)

    return scores, boxes, classes
    def _yolo_eval(self,
                   yolo_outputs,
                   image_shape,
                   max_boxes=10,
                   score_threshold=0.6,
                   iou_threshold=0.5):
        box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs

        boxes = yolo_boxes_to_corners(box_xy, box_wh)

        scores, boxes, classes = self._yolo_filter_boxes(
            box_confidence, boxes, box_class_probs, score_threshold)

        boxes = scale_boxes(boxes, image_shape)

        scores, boxes, classes = self._yolo_non_max_suppression(
            scores, boxes, classes, max_boxes, iou_threshold)

        return scores, boxes, classes
Exemple #23
0
def yolo_eval(yolo_outputs, image_shape = (720., 1280.), max_boxes=10, score_threshold=.6, iou_threshold=.5):
    
    
    # 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 
    boxes = yolo_boxes_to_corners(box_xy, box_wh)

    # Use yolo filter boxes function  to perform Score-filtering with a threshold of score_threshold
    scores, boxes, classes = yolo_filter_boxes(box_confidence, boxes, box_class_probs, threshold = .6)
    
    # Scale boxes back to original image shape.
    boxes = scale_boxes(boxes, image_shape)

    # Use function Non-max suppression with a threshold of iou_threshold 
    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes, max_boxes = 10, iou_threshold = 0.5)
    
    
    return scores, boxes, classes
Exemple #24
0
def yolo_eval(yolo_outputs, image_shape=(720.,1280.),
              max_boxes=10, score_threshold=0.6,iou_threshold=0.5):
    """
    将YOLO编码的输出(很多锚框)转换为预测框以及它们的分数,框坐标和类。

    参数:
        yolo_outputs - 编码模型的输出(对于维度为(608,608,3)的图片),包含4个tensors类型的变量:
                        box_confidence : tensor类型,维度为(None, 19, 19, 5, 1)
                        box_xy         : tensor类型,维度为(None, 19, 19, 5, 2)
                        box_wh         : tensor类型,维度为(None, 19, 19, 5, 2)
                        box_class_probs: tensor类型,维度为(None, 19, 19, 5, 80)
        image_shape - tensor类型,维度为(2,),包含了输入的图像的维度,这里是(608.,608.)
        max_boxes - 整数,预测的锚框数量的最大值
        score_threshold - 实数,可能性阈值。
        iou_threshold - 实数,交并比阈值。

    返回:
        scores - tensor类型,维度为(,None),每个锚框的预测的可能值
        boxes - tensor类型,维度为(4,None),预测的锚框的坐标
        classes - tensor类型,维度为(,None),每个锚框的预测的分类
    """

    #将yolo网络的输出结果获取
    box_confidence,box_xy,box_wh,box_class_probs = yolo_outputs

    #将中心格式转换为边界格式
    boxes = yolo_boxes_to_corners(box_xy, box_wh)

    #去除低概率值
    scores, boxes, classes = yolo_filter_boxes(box_confidence, boxes, box_class_probs, score_threshold)



    #非极大值抑制
    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes, max_boxes=10, iou_threshold=0.5)

    # 因为输入是(720.,1280.),而编码模型的输出为(608,608),所以需要对box进行按比例调整,
    # 我的理解是用(720.,1280.)resize到(608,608),但因为框无法进行resize,所以我们通过这个方式对框进行调整
    boxes = yolo_utils.scale_boxes(boxes, image_shape)

    return scores, boxes, classes
Exemple #25
0
def yolo_eval(yolo_outputs, image_shape = (720., 1280.), max_boxes=10, score_threshold=.6, iou_threshold=.5):
    
    
    # first we will be retriving the outputs from the yolu outputs of the model
    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs
    
    # Converting boxes to be ready for filtering functions 
    boxes = yolo_boxes_to_corners(box_xy, box_wh)
    # this function is imported from yad2k.models.keras_yolo
    
    # filtering the boxes
    scores, boxes, classes = yolo_filter_boxes(box_confidence, boxes, box_class_probs, threshold = score_threshold)
    
    # scaling the boxes back to original image shape
    boxes = scale_boxes(boxes, image_shape)
    # this is imported from yolo_utils
    
    # using the non-max supression on the filtered bosex
    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes, max_boxes = max_boxes, iou_threshold = iou_threshold)
    
    return scores, boxes, classes
Exemple #26
0
def yolo_eval(yolo_outputs,
              image_shape=(720., 1280.),
              max_boxes=10,
              score_threshold=.6,
              iou_threshold=.5):
    """
    Convert the output of YOLO encoding to predicted boxes, scores, box coordinates and classes.

    Arguments:
    yolo_outputs -- output of the encoding model contains:
                    box_confidence
                    box_xy
                    box_wh
                    box_class_probs
    image_shape -- input shape
    max_boxes -- maximum number of predicted boxes
    score_threshold -- if [ highest class probability score < threshold], then filter
    iou_threshold -- IoU threshold used for NMS filtering

    Returns:
    scores -- predicted score for each box
    boxes -- predicted box coordinates
    classes -- predicted class for each box
    """

    # YOLO outputs
    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs
    boxes = yolo_boxes_to_corners(box_xy, box_wh)

    # Filter a threshold of score_threshold
    scores, boxes, classes = yolo_filter_boxes(box_confidence, boxes,
                                               box_class_probs,
                                               score_threshold)

    # Scale boxes back to original image shape. Non-max suppression.
    boxes = scale_boxes(boxes, image_shape)
    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes,
                                                      max_boxes, iou_threshold)

    return scores, boxes, classes
Exemple #27
0
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.
    """

    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs

    boxes = yolo_boxes_to_corners(box_xy, box_wh)

    scores, boxes, classes = yolo_filter_boxes(box_confidence,
                                               boxes,
                                               box_class_probs,
                                               threshold=score_threshold)

    boxes = scale_boxes(boxes, image_shape)

    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes)

    return scores, boxes, classes
Exemple #28
0
def evaluate(yolo_outputs, image_shape=(720., 1280.), max_boxes=10, score_threshold=.5, iou_threshold=.5):
    """
    Converts the output of YOLO encoding (yolo_outputs) to 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,) original image shape
    max_boxes -- integer, maximum number of predicted boxes
    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
    """

    # Retrieve outputs of the YOLO model
    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs

    # Convert boxes to be ready for filtering functions
    boxes = yolo_boxes_to_corners(box_xy, box_wh)

    # Perform Score-filtering with a threshold of score_threshold
    scores, boxes, classes = filter_anchor_boxes(box_confidence, boxes, box_class_probs, threshold=score_threshold)

    # Scale boxes back to original image shape.
    boxes = scale_boxes(boxes, image_shape)

    # Non-max suppression with a threshold of iou_threshold
    scores, boxes, classes = non_max_suppression(scores, boxes, classes, max_boxes, iou_threshold)
    return scores, boxes, classes
def yolo_eval(yolo_outputs,
              image_shape=(720., 1280.),
              max_boxes=10,
              score_threshold=.6,
              iou_threshold=.5):

    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs

    # Convert boxes to be ready for filtering functions
    boxes = yolo_boxes_to_corners(box_xy, box_wh)

    # perform SNonecore-filtering with a threshold of score_threshold
    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)

    # Non-max suppression with a threshold of iou_threshold
    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes,
                                                      max_boxes, iou_threshold)

    return scores, boxes, classes
Exemple #30
0
    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 
    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, threshold=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 a threshold of iou_threshold (≈1 line)
    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes)
    
    ### END CODE HERE ###
    
    return scores, boxes, classes

Exemple #31
0
    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
    """
    
  
    
    # 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 
    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, threshold = 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 a threshold of iou_threshold (≈1 line)
    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes, max_boxes = max_boxes, iou_threshold = iou_threshold)
    
    
    
    return scores, boxes, classes
    
    sess = K.get_session()
    class_names = read_classes("model_data/coco_classes.txt")
    anchors = read_anchors("model_data/yolo_anchors.txt")
    image_shape = (720., 1280.)
    
    
    yolo_model = load_model("model_data/yolo.h5")
    
    
    yolo_model.summary()
    
    yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names))
    
    scores, boxes, classes = yolo_eval(yolo_outputs, image_shape)
    
    def predict(sess, image_file):
    """
    Runs the graph stored in "sess" to predict boxes for "image_file". Prints and plots the preditions.
    
    Arguments:
    sess -- your tensorflow/Keras session containing the YOLO graph
    image_file -- name of an image stored in the "images" folder.
    
    Returns:
    out_scores -- tensor of shape (None, ), scores of the predicted boxes
    out_boxes -- tensor of shape (None, 4), coordinates of the predicted boxes
    out_classes -- tensor of shape (None, ), class index of the predicted boxes
    
    
    """

    # Preprocess your image
    image, image_data = preprocess_image("images/" + image_file, model_image_size = (608, 608))

    # Run the session with the correct tensors and choose the correct placeholders in the feed_dict.=  
    out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={yolo_model.input: image_data, K.learning_phase(): 0})
   

    # Print predictions info
    print('Found {} boxes for {}'.format(len(out_boxes), image_file))
    # Generate colors for drawing bounding boxes.
    colors = generate_colors(class_names)
    # Draw bounding boxes on the image file
    draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors)
    # Save the predicted bounding box on the image
    image.save(os.path.join("out", image_file), quality=90)
    # Display the results
    output_image = scipy.misc.imread(os.path.join("out", image_file))
    imshow(output_image)
    
    return out_scores, out_boxes, out_classes
    
    out_scores, out_boxes, out_classes = predict(sess, "test.jpg")