def yolo_eval(yolo_outputs, image_shape=(720.,1280.), max_boxes=10, score_threshold=0.6, iou_threshold=0.5): """ 将YOLO编码的输出(很多框)转换为预测框以及他们的分数、框坐标和类 :param yolo_outputs: 编码模型的输出(对于维度为608*608*3的图片),包含4个tensor类型的变量: box_confidence:tensor类型,shape of (None,19,19,5,1) box_xy:tensor类型,shape of (None,19,19,5,2) box_wh:tensor类型,shape of (None,19,19,5,2) box_class_probs:tensor类型, shape of (None,19,19,5,80) :param image_shape:tensor类型,shape of (2, ),包含了输入的图像的维度,这里是(608, 608) :param max_boxes:integer,预测的锚框数量的最大值 :param score_threshold:real value,可能的阈值 :param iou_threshold:real value,交并比阈值 :return: scores:tensor类型,shape of (None, ),每个锚框的预测的可能值 boxes:tensor类型,shape of (None,4),预测锚框的坐标 classes:tensor类型,shape of (None, ),每个锚框的预测的分类 """ # 获取YOLO模型的输出 box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs # 中心点转换为边角 boxes = yolo_boxes_to_corners(box_xy, box_wh) # score过滤,第一个过滤器 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=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_output, image_shape=(720., 1280.), max_boxes=10, score_threshold=0.6, iou_threshold=0.5): """ 将yolo的输出(多个锚框)转换为预测框以及他们的分数,框坐标和类 Args: yolo_output: 模型的输出。对(608,608,3)的图片包含四个Tensor向量 box_confidence:tensor,维度(None,19,19,5,1) box_xy:tensor,维度(None,19,19,5,2) box_hw:tensor,维度(None,19,19,5,2) box_class_probs:tensor,维度(None,19,19,5,80) image_shape: tensor,表示图像的维度,这里是(608,608) max_boxes: 预测的锚框数量的最大值 score_threshold: 可能性阈值 iou_threshold: 交并比阈值 Returns: scores: tensor,维度(,None)每个锚框预测的可能值 boxes: tensor,维度(4,None)每个预测锚框的坐标 classes: tensor,维度(,None)每个锚框预测的分类 """ box_confidence, box_xy, box_wh, box_class_prob = yolo_output # 中心点坐标转换为边角坐标 boxes = yolo_utils.yolo_boxes_to_corners(box_xy, box_wh) # 过滤掉低于score_threshold的框 scores, boxes, classes = yolo_filter_boxes(box_confidence, boxes, box_class_prob, score_threshold) # 缩放锚框,以适应原始图像 boxes = yolo_utils.scale_boxes(boxes, image_shape) # 使用非最大值抑制 scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes, max_boxes) 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): """ 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): 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
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
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. 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 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
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
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=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
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
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(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
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
def yolo_eval(box_xy, box_wh, box_confidence, box_class_probs, image_shape=(2500., 2500.), max_boxes=10, score_threshold=0.5, 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模型的输出 # image_input = Input(shape=(416,416, 3)) print("box_xy, box_wh :") print(box_xy, box_wh) #中心点转换为边角 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=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
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 predict(image_folder, model_path, batch_size=1, prediction_folder=""): dataset = ImagePredictLoader(image_folder) actual_classes = len(dataset.classes) # Reading in the anchors anchors = read_anchors("anchors.txt") num_anchors = len(anchors) num_classes = (actual_classes + 5) * num_anchors yolo_net = torch.load(model_path) predict_loader = DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True, num_workers=4) length_of_dataset = len(predict_loader) yolo_net.eval() for i, an_image in tqdm(enumerate(predict_loader), total=length_of_dataset): output = None with torch.no_grad(): output = yolo_net(an_image.to(device)) conv_output = output.permute(0, 2, 3, 1).reshape(-1, an_image.size()[2] // 32, an_image.size()[3] // 32, num_anchors, actual_classes + 5) box_confidence, box_xy, box_wh, box_class_prob = yolo_head( conv_output, anchors, actual_classes) boxes = boxes_to_corners(box_xy, box_wh) scores, classes, boxes = yolo_filter_boxes(box_confidence, box_class_prob, boxes, score_threshold=0.5) named_classes = [] for a_class in classes: named_classes.append(dataset.classes[int( a_class.cpu().data.numpy())]) image_shape = an_image.size()[2:4] boxes = scale_boxes(boxes, image_shape) draw_on_image(an_image, boxes, named_classes, i, prediction_folder)
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
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
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
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
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
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")
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 # In[25]: with tf.Session() as test_b: yolo_outputs = (tf.random_normal([19, 19, 5, 1], mean=1, stddev=4, seed = 1), tf.random_normal([19, 19, 5, 2], mean=1, stddev=4, seed = 1), tf.random_normal([19, 19, 5, 2], mean=1, stddev=4, seed = 1),