Skip to content

Instantly share code, notes, and snippets.

View pythonlessons's full-sized avatar

Rokas Liuberskis pythonlessons

View GitHub Profile
@pythonlessons
pythonlessons / Yolo_v3_Darknet-53.py
Created April 28, 2020 10:18
Yolo_v3_Darknet-53
def darknet53(input_data):
input_data = convolutional(input_data, (3, 3, 3, 32))
input_data = convolutional(input_data, (3, 3, 32, 64), downsample=True)
for i in range(1):
input_data = residual_block(input_data, 64, 32, 64)
input_data = convolutional(input_data, (3, 3, 64, 128), downsample=True)
for i in range(2):
@pythonlessons
pythonlessons / Yolo_v3_structure.py
Created April 28, 2020 10:25
Yolo_v3_structure
def YOLOv3(input_layer, NUM_CLASS):
# After the input layer enters the Darknet-53 network, we get three branches
route_1, route_2, conv = darknet53(input_layer)
# See the orange module (DBL) in the figure above, a total of 5 Subconvolution operation
conv = convolutional(conv, (1, 1, 1024, 512))
conv = convolutional(conv, (3, 3, 512, 1024))
conv = convolutional(conv, (1, 1, 1024, 512))
conv = convolutional(conv, (3, 3, 512, 1024))
conv = convolutional(conv, (1, 1, 1024, 512))
conv_lobj_branch = convolutional(conv, (3, 3, 512, 1024))
@pythonlessons
pythonlessons / Yolo_v3_residual_block.py
Created April 28, 2020 10:27
Yolo_v3_residual_block
def residual_block(input_layer, input_channel,
filter_num1, filter_num2):
short_cut = input_layer
conv = convolutional(input_layer, filters_shape=
(1, 1, input_channel, filter_num1))
conv = convolutional(conv , filters_shape=
(3, 3, filter_num1, filter_num2))
residual_output = short_cut + conv
return residual_output
@pythonlessons
pythonlessons / Yolo_v3_Bounding_Box.py
Created April 28, 2020 10:34
Yolo_v3_Bounding_Box
def decode(conv_output, NUM_CLASS, i=0):
# where i = 0, 1 or 2 to correspond to the three grid scales
conv_shape = tf.shape(conv_output)
batch_size = conv_shape[0]
output_size = conv_shape[1]
conv_output = tf.reshape(conv_output, (batch_size, output_size, output_size, 3, 5 + NUM_CLASS))
conv_raw_dxdy = conv_output[:, :, :, :, 0:2] # offset of center position
conv_raw_dwdh = conv_output[:, :, :, :, 2:4] # Prediction box length and width offset
@pythonlessons
pythonlessons / Yolo_v3_nms.py
Last active April 28, 2020 10:38
Yolo_v3_nms
def nms(bboxes, iou_threshold, sigma=0.3, method='nms'):
classes_in_img = list(set(bboxes[:, 5]))
best_bboxes = []
for cls in classes_in_img:
cls_mask = (bboxes[:, 5] == cls)
cls_bboxes = bboxes[cls_mask]
# Process 1: Determine whether the number of bounding boxes is greater than 0
while len(cls_bboxes) > 0:
# Process 2: Select the bounding box with the highest score according to score order A
@pythonlessons
pythonlessons / Yolo_v3_bbox_iou.py
Created April 29, 2020 19:55
Yolo_v3_bbox_iou
iou = bbox_iou(pred_xywh [:,:,:,:, np.newaxis,:], bboxes[:, np.newaxis, np.newaxis, np.newaxis,:,:])
# Find the value of IoU with the real largest prediction box
max_iou = tf.expand_dims(tf.reduce_max(iou, axis = -1 ), axis = -1 )
# If the largest IoU is less than the threshold, it is considered that the prediction box contains no objects, then the background box
respond_bgd = (1.0-respond_bbox) * tf.cast(max_iou<IOU_LOSS_THRESH, tf.float32)
conf_focal = tf.pow(respond_bbox-pred_conf, 2)
# Calculate the loss of confidence
@pythonlessons
pythonlessons / Yolo_v3_bbox_giou.py
Created April 30, 2020 05:11
Yolo_v3_bbox_giou
def bbox_giou (boxes1, boxes2):
...
# Calculate the iou value between the two bounding boxes
iou = inter_area / union_area
# Calculate the coordinates of the upper left corner and the lower right corner of the smallest closed convex surface
enclose_left_up = tf.minimum (boxes1 [..., :2], boxes2 [..., :2])
enclose_right_down = tf.maximum (boxes1 [..., 2:], boxes2 [..., 2:])
enclose = tf.maximum(enclose_right_down-enclose_left_up, 0.0 )
@pythonlessons
pythonlessons / Yolo_v3_weight_initialization.py
Created April 30, 2020 05:23
Yolo_v3_weight_initialization
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=(20,10))
x = np.random.randn(2000, 800) * 0.01 # Create input data
stds = [0.1, 0.05, 0.02, 0.01, 0.005, 0.001] # Try to use different standard deviations so that the initial weights are different
for i, std in enumerate(stds):
@pythonlessons
pythonlessons / Yolo_v3_tiny_structure.py
Created June 4, 2020 14:01
Yolo_v3_tiny_structure
def darknet19_tiny(input_data):
input_data = convolutional(input_data, (3, 3, 3, 16))
input_data = MaxPool2D(2, 2, 'same')(input_data)
input_data = convolutional(input_data, (3, 3, 16, 32))
input_data = MaxPool2D(2, 2, 'same')(input_data)
input_data = convolutional(input_data, (3, 3, 32, 64))
input_data = MaxPool2D(2, 2, 'same')(input_data)
input_data = convolutional(input_data, (3, 3, 64, 128))
input_data = MaxPool2D(2, 2, 'same')(input_data)
input_data = convolutional(input_data, (3, 3, 128, 256))
@pythonlessons
pythonlessons / Yolo_v3_object_tracker.py
Last active June 22, 2020 12:36
Yolo_v3_object_tracker
import os
import cv2
import numpy as np
import tensorflow as tf
from yolov3.yolov3 import Create_Yolov3
from yolov3.utils import load_yolo_weights, image_preprocess, postprocess_boxes, nms, draw_bbox, read_class_names
import time
from yolov3.configs import *
from deep_sort import nn_matching