# -*- coding: utf-8 -*
import os
import scipy.io
import scipy.misc
import tensorflow as tf
from keras import backend as K
from keras.models import load_model, Model
from yolo_utils import read_classes, read_anchors, generate_colors, preprocess_image, draw_boxes, scale_boxes
from yad2k.models.keras_yolo import yolo_head, yolo_boxes_to_corners, preprocess_true_boxes, yolo_loss, yolo_body
# GRADED FUNCTION: yolo_filter_boxes
def 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_class_scores = K.max(box_scores, axis=-1, keepdims=False)
### 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
# GRADED FUNCTION: yolo_non_max_suppression
def 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
# GRADED FUNCTION: 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
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)
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
吴恩达编程作业答案.rar (63个子文件)
编程作业答案
2 改善深层神经网络:超参数调试、正则化以及优化
Week1 深层学习的实用
assignment1
3.Gradient+Checking.ipynb 26KB
translate_Gradient+Checking_answer.ipynb 26KB
translate_Regularization_answer.ipynb 428KB
2.Regularization_answer.ipynb 431KB
2.Regularization.ipynb 38KB
1.Initialization_answer.ipynb 472KB
1.Initialization.ipynb 25KB
3.Gradient+Checking_answer.ipynb 26KB
translate_Initialization_answer.ipynb 470KB
Week2 优化算法
assignment2
Optimization+methods.ipynb 454KB
translate_Optimization+methods.ipynb 454KB
Week3 超参数调试&正则化&框架
assignment3
Tensorflow+Tutorial.ipynb 55KB
translate_Tensorflow+Tutorial.ipynb 53KB
1 神经网络与深度学习
Week2 神经网络基础
assignment2
assignment2_2.ipynb 132KB
translate_assignment2_2.ipynb 127KB
translate_assignment2_1.ipynb 40KB
assignment2_1.ipynb 42KB
Week3 浅层神经网络
assignment3
assignment3_answer.ipynb 793KB
assignment3.ipynb 136KB
translate_assignment3_answer.ipynb 791KB
Week4 深层神经网络
assignment4
translate_assignment4_1.ipynb 54KB
assignment4_2_answer.ipynb 1.98MB
assignment4_2.ipynb 1.94MB
assignment4_1_answer.ipynb 60KB
assignment4_1.ipynb 50KB
5 序列模型
Week2 自然语言处理与词嵌入
emojify
Emojify+-+v2-starter.ipynb 44KB
Emojify+-+v2-finished.ipynb 68KB
word-vector-representation
Operations+on+word+vectors+-+v2-finished.ipynb 33KB
Operations+on+word+vectors+-+v2-starter.ipynb 29KB
Week1 循环序列模型
jazz-improvisation-with-lstm
Improvise+a+Jazz+Solo+with+an+LSTM+Network+-+v1-finished.ipynb 1.72MB
Improvise+a+Jazz+Solo+with+an+LSTM+Network+-+v1-starter.ipynb 29KB
dinosaur-island-character-level-language-model
Dinosaurus+Island+--+Character+level+language+model+final+-+v3-finished.ipynb 45KB
Dinosaurus+Island+--+Character+level+language+model+final+-+v3-starter.ipynb 37KB
building-rnn-step-by-step
Building+a+Recurrent+Neural+Network+-+Step+by+Step+-+v3-finish.ipynb 84KB
Building+a+Recurrent+Neural+Network+-+Step+by+Step+-+v3-starter.ipynb 77KB
Week3 序列模型和注意力机制
machine-translation
Neural+machine+translation+with+attention+-+v3-finished.ipynb 85KB
Neural+machine+translation+with+attention+-+v3-starter.ipynb 32KB
trigger-word-detection
Trigger+word+detection+-+v1-finished.ipynb 23.15MB
Trigger+word+detection+-+v1-starter.ipynb 53KB
4 卷积神经网络
Week2 深层卷积神经网络实例探究
dp_hw2.png 497KB
4.2 深度卷积网络模型
ResNets
Residual Networks-v2.ipynb 196KB
Residual Networks-v2-answer.ipynb 196KB
KerasTutorial
Keras-Tutorial-Happy House v2.ipynb 62KB
Keras-Tutorial-Happy House v2-answer.ipynb 61KB
Week3 目标检测
4.3 目标检测
Car detection for Autonomous Driving
Autonomous driving application-Car detection-v1.py 9KB
Autonomous driving application-Car detection-v1.ipynb 242KB
Autonomous driving application-Car detection-v1-answer.ipynb 241KB
dp_hw3.htm 9.01MB
dp_hw3.png 1.61MB
Week1 卷积神经网络
4.1 卷积模型
Convolution model-Step by Step-v1.ipynb 48KB
Convolution model-Application-v1.ipynb 63KB
Convolution model-Step by Step-v2.ipynb 57KB
Convolution model-Step by Step-v2-answer.ipynb 57KB
Convolution model-Application-v1-answer.ipynb 129KB
Convolution model-Step by Step-v1-answer.ipynb 47KB
dp_hw1.png 388KB
Week4 特殊的应用
Face Recognition
Face Recognition for the Happy House-v3-answer.ipynb 32KB
Face Recognition for the Happy House-v3.ipynb 32KB
Neural Style Transfer
Art Generation with Neural Style Transfer-v2-answer.ipynb 438KB
Art Generation with Neural Style Transfer-v2.ipynb 750KB
Week 4 课后验证.htm 7.65MB
3 结构化机器学习项目
dp_hw2.png 1.83MB
dp_hw1.png 2.18MB
共 63 条
- 1
资源评论
夜半钟声人未眠
- 粉丝: 314
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功