博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DeepLearning.ai作业:(4-3)-- 目标检测(Object detection)
阅读量:4099 次
发布时间:2019-05-25

本文共 9674 字,大约阅读时间需要 32 分钟。


title: ‘DeepLearning.ai作业:(4-3)-- 目标检测(Object detection)’

id: dl-ai-4-3h
tags:

  • homework
    categories:
  • AI
  • Deep Learning
    date: 2018-10-11 20:15:58

首发于个人博客:,欢迎来访

本周的作业实现了YOLO算法,并用于自动驾驶的目标检测中。

Model details

输入: (m, 608, 608, 3)

输出: (m, 19, 19, 5, 85)

IMAGE (m, 608, 608, 3) -> DEEP CNN -> ENCODING (m, 19, 19, 5, 85)

也就是有5个Anchor boxes,一共有80个分类。

所以,每个box的scores也就是等于每个类预测的可能性:

Filtering with a threshold on class scores

这个时候开始创建一个函数,得到每一个box中scores最大的那个类,分数,以及位置,去掉其他没用的。

# GRADED FUNCTION: yolo_filter_boxesdef yolo_filter_boxes(box_confidence, boxes, box_class_probs, threshold = .6):    """Filters YOLO boxes by thresholding on object and class confidence.        Arguments:    box_confidence -- tensor of shape (19, 19, 5, 1)    boxes -- tensor of shape (19, 19, 5, 4)    box_class_probs -- tensor of shape (19, 19, 5, 80)    threshold -- real value, if [ highest class probability score < threshold], then get rid of the corresponding box        Returns:    scores -- tensor of shape (None,), containing the class probability score for selected boxes    boxes -- tensor of shape (None, 4), containing (b_x, b_y, b_h, b_w) coordinates of selected boxes    classes -- tensor of shape (None,), containing the index of the class detected by the selected boxes        Note: "None" is here because you don't know the exact number of selected boxes, as it depends on the threshold.     For example, the actual output size of scores would be (10,) if there are 10 boxes.    """        # Step 1: Compute box scores    ### START CODE HERE ### (≈ 1 line)    box_scores = box_confidence * box_class_probs    ### END CODE HERE ###        # Step 2: Find the box_classes thanks to the max box_scores, keep track of the corresponding score    ### START CODE HERE ### (≈ 2 lines)    box_classes = K.argmax(box_scores, axis=-1)    #得到box的类别 (19,19,5)    box_class_scores = K.max(box_scores, axis=-1, keepdims=False)  #得到box这个类别的分数(19,19,5)    ### END CODE HERE ###        # Step 3: Create a filtering mask based on "box_class_scores" by using "threshold". The mask should have the    # same dimension as box_class_scores, and be True for the boxes you want to keep (with probability >= threshold)    ### START CODE HERE ### (≈ 1 line)    filtering_mask = box_class_scores >= threshold    ### END CODE HERE ###        # Step 4: Apply the mask to scores, boxes and classes    ### START CODE HERE ### (≈ 3 lines)    scores = tf.boolean_mask(box_class_scores, filtering_mask)    boxes = tf.boolean_mask(boxes, filtering_mask)    classes = tf.boolean_mask(box_classes, filtering_mask)    ### END CODE HERE ###        return scores, boxes, classes

Non-max suppression

找到了这些boxes后,还需要进行筛选过滤掉。先完成一个IOU算法:

# GRADED FUNCTION: ioudef iou(box1, box2):    """Implement the intersection over union (IoU) between box1 and box2        Arguments:    box1 -- first box, list object with coordinates (x1, y1, x2, y2)    box2 -- second box, list object with coordinates (x1, y1, x2, y2)    """    # Calculate the (y1, x1, y2, x2) coordinates of the intersection of box1 and box2. Calculate its Area.    ### START CODE HERE ### (≈ 5 lines)    xi1 = np.maximum(box1[0], box2[0])    yi1 = np.maximum(box1[1], box2[1])    xi2 = np.minimum(box1[2], box2[2])    yi2 = np.minimum(box1[3], box2[3])    inter_area = max(xi2 - xi1,0) * max(yi2 - yi1,0)    ### END CODE HERE ###        # Calculate the Union area by using Formula: Union(A,B) = A + B - Inter(A,B)    ### START CODE HERE ### (≈ 3 lines)    box1_area = (box1[2] - box1[0]) * (box1[3] - box1[1])    box2_area = (box2[2] - box2[0]) * (box2[3] - box2[1])    union_area = box1_area + box2_area - inter_area    ### END CODE HERE ###        # compute the IoU    ### START CODE HERE ### (≈ 1 line)    iou = inter_area / union_area    ### END CODE HERE ###        return iou

tensorflow已经帮你实现了iou算法了,不用用自己刚才写的了:

思想就是拿掉IOU比较大的那些box

# GRADED FUNCTION: yolo_non_max_suppressiondef yolo_non_max_suppression(scores, boxes, classes, max_boxes = 10, iou_threshold = 0.5):    """    Applies Non-max suppression (NMS) to set of boxes        Arguments:    scores -- tensor of shape (None,), output of yolo_filter_boxes()    boxes -- tensor of shape (None, 4), output of yolo_filter_boxes() that have been scaled to the image size (see later)    classes -- tensor of shape (None,), output of yolo_filter_boxes()    max_boxes -- integer, maximum number of predicted boxes you'd like    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 (4, None), predicted box coordinates    classes -- tensor of shape (, None), predicted class for each box        Note: The "None" dimension of the output tensors has obviously to be less than max_boxes. Note also that this    function will transpose the shapes of scores, boxes, classes. This is made for convenience.    """        max_boxes_tensor = K.variable(max_boxes, dtype='int32')     # tensor to be used in tf.image.non_max_suppression()    K.get_session().run(tf.variables_initializer([max_boxes_tensor])) # initialize variable max_boxes_tensor        # Use tf.image.non_max_suppression() to get the list of indices corresponding to boxes you keep    ### START CODE HERE ### (≈ 1 line)    nms_indices = tf.image.non_max_suppression(boxes,scores,max_boxes,iou_threshold)    ### END CODE HERE ###        # Use K.gather() to select only nms_indices from scores, boxes and classes    ### START CODE HERE ### (≈ 3 lines)    scores = K.gather(scores,nms_indices)    boxes = K.gather(boxes,nms_indices)    classes = K.gather(classes,nms_indices)    ### END CODE HERE ###        return scores, boxes, classes

而后结合刚才的函数,先去掉scores低的,然后运算NMS算法

# GRADED FUNCTION: yolo_evaldef 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     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 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        Note: "None" actually represents the number of predicted boxes, it varies between 0 and max_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.    # You'll need to use feed_dict={yolo_model.input: ... , K.learning_phase(): 0})    ### START CODE HERE ### (≈ 1 line)    out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict = {
yolo_model.input:image_data, K.learning_phase(): 0}) ### END CODE HERE ### # 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 in the notebook output_image = scipy.misc.imread(os.path.join("out", image_file)) imshow(output_image) return out_scores, out_boxes, out_classes

转载地址:http://frrii.baihongyu.com/

你可能感兴趣的文章
python数字逆序输出及多个print输出在同一行
查看>>
python九九乘法表(详解)
查看>>
ESP8266 WIFI数传 Pixhaw折腾笔记
查看>>
苏宁产品经理面经
查看>>
百度产品经理群面
查看>>
去哪儿一面+平安科技二面+hr面+贝贝一面+二面产品面经
查看>>
MongDB学习之路(二)基本指令
查看>>
MongoDB学习之路(三)复制集
查看>>
VMware安装centos6.8常见问题
查看>>
安装git在Linux环境配置
查看>>
【java面试】面向对象三大特性:封装、继承、多态
查看>>
【java面试】equals()与“==”的使用
查看>>
【java面试】静态变量、static关键字及其用法
查看>>
【java并发】AQS学习
查看>>
SpringBoot(一)创建HelloWorld和报错解决
查看>>
wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz 下载失败【已解决】
查看>>
Kettle开源项目一款ETL工具
查看>>
kettle学习免费视频教程【正在学习-亲测!】
查看>>
部署Kettle7.1到linux后执行./kitchen.sh报错No libwebkitgtk1.0 detected
查看>>
CentOS7.2的yum、python卸载和重新安装【亲测有效!20190626】
查看>>