This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
union_area = box1_area + box2_area - intersection_area |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def 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. | |
xi1 = max(box1[0], box2[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras import backend as K |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
yolo_model = load_model("model_data/yolo.h5") | |
# Convert final layer features to bounding box parameters | |
yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) | |
image_shape = K.placeholder(shape=(2, )) | |
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Preprocess your image | |
image, image_data = preprocess_image("images/" + image_file, model_image_size = (608, 608)) | |
sess = K.get_session() | |
# 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, | |
input_image_shape: [image.size[1], image.size[0]], | |
K.learning_phase(): 0}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
device_name = tf.test.gpu_device_name() | |
if device_name != '/device:GPU:0': | |
raise SystemError('GPU device not found') | |
print('Found GPU at: {}'.format(device_name)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tensorflow.python.client import device_lib | |
device_lib.list_local_devices() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!pip install Cython | |
!git clone https://github.com/waleedka/coco | |
!pip install -U setuptools | |
!pip install -U wheel | |
!make install -C coco/PythonAPI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!git clone https://github.com/matterport/Mask_RCNN | |
# cd to the code directory and optionally download the weights file | |
import os | |
os.chdir('./Mask_RCNN') | |
!wget https://github.com/matterport/Mask_RCNN/releases/download/v2.0/mask_rcnn_coco.h5 |