Skip to content

Instantly share code, notes, and snippets.

View Tony607's full-sized avatar

Chengwei Zhang Tony607

View GitHub Profile
@Tony607
Tony607 / union_area.py
Created March 25, 2018 01:20
Gentle guide on how YOLO Object Localization works with Keras (Part 2)
union_area = box1_area + box2_area - intersection_area
@Tony607
Tony607 / iou.py
Created March 25, 2018 01:20
Gentle guide on how YOLO Object Localization works with Keras (Part 2)
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])
@Tony607
Tony607 / backend.py
Created March 25, 2018 01:21
Gentle guide on how YOLO Object Localization works with Keras (Part 2)
from keras import backend as K
@Tony607
Tony607 / yolo_non_max_suppression.py
Created March 25, 2018 01:22
Gentle guide on how YOLO Object Localization works with Keras (Part 2)
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
@Tony607
Tony607 / computation_graph.py
Created March 25, 2018 01:23
Gentle guide on how YOLO Object Localization works with Keras (Part 2)
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)
@Tony607
Tony607 / predict.py
Created March 25, 2018 01:24
Gentle guide on how YOLO Object Localization works with Keras (Part 2)
# 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})
@Tony607
Tony607 / gpu_device.py
Created March 26, 2018 05:51
How to run Object Detection and Segmentation on a Video Fast for Free
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))
@Tony607
Tony607 / list_local_devices.py
Created March 26, 2018 05:52
How to run Object Detection and Segmentation on a Video Fast for Free | DLology
from tensorflow.python.client import device_lib
device_lib.list_local_devices()
@Tony607
Tony607 / install_coco.py
Created March 26, 2018 05:54
How to run Object Detection and Segmentation on a Video Fast for Free | DLology
!pip install Cython
!git clone https://github.com/waleedka/coco
!pip install -U setuptools
!pip install -U wheel
!make install -C coco/PythonAPI
@Tony607
Tony607 / Mask_RCNN.py
Created March 26, 2018 05:54
How to run Object Detection and Segmentation on a Video Fast for Free | DLology
!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