This file contains hidden or 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 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): |
This file contains hidden or 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 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)) |
This file contains hidden or 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 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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
| 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 |
This file contains hidden or 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 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 ) | |
This file contains hidden or 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 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): |
This file contains hidden or 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 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)) |
This file contains hidden or 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 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 |