Skip to content

Commit

Permalink
change relu from leaky relu into mesh in yolov5
Browse files Browse the repository at this point in the history
  • Loading branch information
avBuffer committed Jul 16, 2020
1 parent 9f8e0b1 commit 682db0d
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 112 deletions.
26 changes: 13 additions & 13 deletions core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,49 +14,49 @@

# Set the class name
__C.YOLO.NET_TYPE = 'darknet53' # 'darknet53' 'mobilenetv2'
__C.YOLO.CLASSES = "./data/classes/coco.names"
__C.YOLO.ANCHORS = "./data/anchors/coco_anchors.txt" # yolov3/5 : yolo_anchors.txt; yolov4 : yolov4_anchors.txt
__C.YOLO.CLASSES = './data/classes/coco.names'
__C.YOLO.ANCHORS = './data/anchors/coco_anchors.txt' # yolov3/5 : yolo_anchors.txt; yolov4 : yolov4_anchors.txt
__C.YOLO.MOVING_AVE_DECAY = 0.9995
__C.YOLO.STRIDES = [8, 16, 32]
__C.YOLO.STRIDES_TINY = [16, 32]
__C.YOLO.ANCHOR_PER_SCALE = 3
__C.YOLO.IOU_LOSS_THRESH = 0.5
__C.YOLO.UPSAMPLE_METHOD = "resize"
__C.YOLO.UPSAMPLE_METHOD = 'resize'

__C.YOLO.WIDTH_SCALE_V5 = 0.50 # yolov5 small:0.50 / middle:0.75 / large:1.00 / extend:1.25
__C.YOLO.DEPTH_SCALE_V5 = 0.33 # yolov5 small:0.33(1/3) / middle:0.67(2/3) / large:1.00 / extend:1.33(4/3)

__C.YOLO.ORIGINAL_WEIGHT = "./checkpoint/yolov3_coco.ckpt"
__C.YOLO.DEMO_WEIGHT = "./checkpoint/yolov3_coco_demo.ckpt"
__C.YOLO.ORIGINAL_WEIGHT = './checkpoint/yolov3_coco.ckpt'
__C.YOLO.DEMO_WEIGHT = './checkpoint/yolov3_coco_demo.ckpt'


# Train options
__C.TRAIN = edict()

__C.TRAIN.ANNOT_PATH = "./data/COCO/2017/train.txt"
__C.TRAIN.BATCH_SIZE = 6
__C.TRAIN.ANNOT_PATH = './data/COCO/2017/train.txt'
__C.TRAIN.BATCH_SIZE = 16 if __C.YOLO.NET_TYPE == 'mobilenetv2' else 4
__C.TRAIN.INPUT_SIZE = [320, 352, 384, 416, 448, 480, 512, 544, 576, 608]
__C.TRAIN.DATA_AUG = True
__C.TRAIN.LEARN_RATE_INIT = 1e-4
__C.TRAIN.LEARN_RATE_END = 1e-6
__C.TRAIN.WARMUP_EPOCHS = 2
__C.TRAIN.FISRT_STAGE_EPOCHS = 20
__C.TRAIN.SECOND_STAGE_EPOCHS = 30
__C.TRAIN.INITIAL_WEIGHT = "./weights/yolov4_coco.ckpt"
__C.TRAIN.CKPT_PATH = "./checkpoint"
__C.TRAIN.INITIAL_WEIGHT = './weights/yolov4_coco.ckpt'
__C.TRAIN.CKPT_PATH = './checkpoint'


# TEST options
__C.TEST = edict()

__C.TEST.ANNOT_PATH = "./data/dataset/voc_test.txt"
__C.TEST.ANNOT_PATH = './data/dataset/voc_test.txt'
__C.TEST.BATCH_SIZE = 2
__C.TEST.INPUT_SIZE = 544
__C.TEST.INPUT_SIZE = 416
__C.TEST.DATA_AUG = False
__C.TEST.WRITE_IMAGE = True
__C.TEST.WRITE_IMAGE_PATH = "./data/detection/"
__C.TEST.WRITE_IMAGE_PATH = './data/detection/'
__C.TEST.WRITE_IMAGE_SHOW_LABEL = True
__C.TEST.WEIGHT_FILE = "./checkpoint/yolov3_test_loss=9.2099.ckpt-5"
__C.TEST.WEIGHT_FILE = './checkpoint/yolov3_test_loss=9.2099.ckpt-5'
__C.TEST.SHOW_LABEL = True
__C.TEST.SCORE_THRESHOLD = 0.3
__C.TEST.IOU_THRESHOLD = 0.45
52 changes: 26 additions & 26 deletions core/yolov4.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def mish(inputs):


def conv(input_data, filters_shape, trainable, name, downsample=False, activate=True, bn=True, act_fun='leaky_relu'):
"""Define Conv layer"""
'''Define Conv layer'''
with tf.variable_scope(name):
if downsample:
pad_h, pad_w = (filters_shape[0] - 2) // 2 + 1, (filters_shape[1] - 2) // 2 + 1
Expand Down Expand Up @@ -57,7 +57,7 @@ def res_block(input_data, input_channel, filter_num1, filter_num2, trainable, na
input_data = conv(input_data, filters_shape=(1, 1, input_channel, filter_num1),
trainable=trainable, name='conv1', act_fun='mish')
input_data = conv(input_data, filters_shape=(3, 3, filter_num1, filter_num2),
trainable=trainable, name='conv2', act_fun='mish')
trainable=trainable, name='conv2', act_fun='mish')
residual_ouput = input_data + short_cut
return residual_ouput

Expand All @@ -78,11 +78,11 @@ def upsample(input_data, name, method='deconv'):


def cspfirst_stage(input_data, trainable, filters):
"""First csp stage.
'''First csp stage.
param input_data: The input tensor
param trainable: A bool parameter, True ==> training, False ==> not train.
param filters: Filter nums
return: Output tensors and the last Conv layer counter of this stage"""
return: Output tensors and the last Conv layer counter of this stage'''
c = filters
route = input_data
route = conv(route, (1, 1, c, c), trainable=trainable, name='conv2', act_fun='mish')
Expand All @@ -98,15 +98,15 @@ def cspfirst_stage(input_data, trainable, filters):


def cspstage(input_data, trainable, filters, loop, layer_nums, route_nums, res_nums):
"""CSPNets stage
'''CSPNets stage
param input_data: The input tensor
param trainable: A bool parameter, True ==> training, False ==> not train.
param filters: Filter nums
param loop: ResBlock loop nums
param layer_nums: Counter of Conv layers
param route_nums: Counter of route nums
param res_nums: Counter of ResBlock nums
return: Output tensors and the last Conv layer counter of this stage"""
return: Output tensors and the last Conv layer counter of this stage'''
c = filters
out_layer = layer_nums + 1 + loop + 1
route = input_data
Expand All @@ -122,10 +122,10 @@ def cspstage(input_data, trainable, filters, loop, layer_nums, route_nums, res_n


def cspdarknet53(input_data, trainable):
"""CSPDarknet53 body; source: https://arxiv.org/pdf/1911.11929.pdf
'''CSPDarknet53 body; source: https://arxiv.org/pdf/1911.11929.pdf
param input_data: Input tensor
param trainable: A bool parameter, True ==> training, False ==> not train.
return: Three stage tensors"""
return: Three stage tensors'''
input_data = conv(input_data, (3, 3, 3, 32), trainable=trainable, name='conv0', act_fun='mish')
input_data = conv(input_data, (3, 3, 32, 64), trainable=trainable, name='conv1', downsample=True, act_fun='mish')

Expand Down Expand Up @@ -181,8 +181,8 @@ def __init__(self, input_data, trainable):


def __build_network(self, input_data):
"""Build yolov4 body, including SPP, PAN, Yolov3 Head/Neck.
param input_data: Input tensor, return: Three stage outputs"""
'''Build yolov4 body, including SPP, PAN, Yolov3 Head/Neck.
param input_data: Input tensor, return: Three stage outputs'''
route_1, route_2, input_data = cspdarknet53(input_data, self.trainable)

# 19 x 19 head
Expand Down Expand Up @@ -261,11 +261,11 @@ def __build_network(self, input_data):


def decode(self, conv_ouput, anchors, strides):
"""Decode yolov4, use sigmoid decode conv_output.
'''Decode yolov4, use sigmoid decode conv_output.
param conv_ouput: The output of yolov4 body.
param anchors: The anchors
param strides: Three dimensions, default [8, 16, 32]
return: The predict of conv_output"""
return: The predict of conv_output'''
conv_shape = tf.shape(conv_ouput)
batch_size = conv_shape[0]
output_size = conv_shape[1]
Expand Down Expand Up @@ -294,10 +294,10 @@ def decode(self, conv_ouput, anchors, strides):


def bbox_iou(self, boxes1, boxes2):
"""Calculate bbox iou; source:
'''Calculate bbox iou; source:
param boxes1: Tensor, shape=(i1,...,iN, 4), xywh
param boxes2: Tensor, shape=(j, 4), xywh
return: Tensor, shape=(i1,...,iN, j)"""
return: Tensor, shape=(i1,...,iN, j)'''
boxes1_area = boxes1[..., 2] * boxes1[..., 3]
boxes2_area = boxes2[..., 2] * boxes2[..., 3]

Expand All @@ -316,10 +316,10 @@ def bbox_iou(self, boxes1, boxes2):


def bbox_giou(self, boxes1, boxes2):
"""Calculate giou loss; source: https://arxiv.org/abs/1902.09630
'''Calculate giou loss; source: https://arxiv.org/abs/1902.09630
param boxes1: Tensor, shape=(batch, feat_w, feat_h, anchor_num, 4), xywh
param boxes2: Tensor, shape=(batch, feat_w, feat_h, anchor_num, 4), xywh
return: Tensor, shape=(batch, feat_w, feat_h, anchor_num, 1)"""
return: Tensor, shape=(batch, feat_w, feat_h, anchor_num, 1)'''
boxes1 = tf.concat([boxes1[..., :2] - boxes1[..., 2:] * 0.5, boxes1[..., :2] + boxes1[..., 2:] * 0.5], axis=-1)
boxes2 = tf.concat([boxes2[..., :2] - boxes2[..., 2:] * 0.5, boxes2[..., :2] + boxes2[..., 2:] * 0.5], axis=-1)

Expand Down Expand Up @@ -347,10 +347,10 @@ def bbox_giou(self, boxes1, boxes2):


def bbox_diou(self, boxes1, boxes2):
"""Calculate diou; source: https://arxiv.org/pdf/1911.08287v1.pdf
'''Calculate diou; source: https://arxiv.org/pdf/1911.08287v1.pdf
param boxes1: Tensor, shape=(batch, feat_w, feat_h, anchor_num, 4), xywh
param boxes2: Tensor, shape=(batch, feat_w, feat_h, anchor_num, 4), xywh
return: Tensor, shape=(batch, feat_w, feat_h, anchor_num, 1)"""
return: Tensor, shape=(batch, feat_w, feat_h, anchor_num, 1)'''
boxes1_center, boxes2_center = boxes1[..., :2], boxes2[..., :2]
boxes1 = tf.concat([boxes1[..., :2] - boxes1[..., 2:] * 0.5, boxes1[..., :2] + boxes1[..., 2:] * 0.5], axis=-1)
boxes2 = tf.concat([boxes2[..., :2] - boxes2[..., 2:] * 0.5, boxes2[..., :2] + boxes2[..., 2:] * 0.5], axis=-1)
Expand Down Expand Up @@ -380,10 +380,10 @@ def bbox_diou(self, boxes1, boxes2):


def bbox_ciou(self, boxes1, boxes2):
"""Calculate ciou; source: https://arxiv.org/pdf/1911.08287v1.pdf
'''Calculate ciou; source: https://arxiv.org/pdf/1911.08287v1.pdf
param boxes1: Tensor, shape=(batch, feat_w, feat_h, anchor_num, 4), xywh
param boxes2: Tensor, shape=(batch, feat_w, feat_h, anchor_num, 4), xywh
return: Tensor, shape=(batch, feat_w, feat_h, anchor_num, 1)"""
return: Tensor, shape=(batch, feat_w, feat_h, anchor_num, 1)'''
boxes1_1, boxes2_1 = boxes1, boxes2
boxes1_center, boxes2_center = boxes1[..., :2], boxes2[..., :2]

Expand Down Expand Up @@ -419,24 +419,24 @@ def bbox_ciou(self, boxes1, boxes2):


def focal_loss(self, y_true, y_pred, gamma=2.0, alpha=1):
"""Compute focal loss; source:https://arxiv.org/abs/1708.02002
'''Compute focal loss; source:https://arxiv.org/abs/1708.02002
param y_true: Ground truth targets, tensor of shape (?, num_boxes, num_classes).
param y_pred: Predicted logits, tensor of shape (?, num_boxes, num_classes).
param gamma: Exponent of the modulating factor (1 - p_t) ^ gamma.
param alpha: Optional alpha weighting factor to balance positives vs negatives.
return: Focal factor"""
return: Focal factor'''
focal_loss = alpha * tf.pow(tf.abs(y_true - y_pred), gamma)
return focal_loss


def _label_smoothing(self, y_true, label_smoothing):
"""Label smoothing. source: https://arxiv.org/pdf/1906.02629.pdf"""
'''Label smoothing. source: https://arxiv.org/pdf/1906.02629.pdf'''
label_smoothing = tf.constant(label_smoothing, dtype=tf.float32)
return y_true * (1.0 - label_smoothing) + 0.5 * label_smoothing


def yolov4_loss(self, conv, pred, label, bboxes, stride, iou_use=1, focal_use=False, label_smoothing=0):
"""Reture yolov4_loss tensor.
'''Reture yolov4_loss tensor.
param conv: The outputs of yolov4 body, conv_sbbox, conv_mbbox, conv_lbbox
param pred: The outputs of decode, pred_sbbox, pred_mbbox, pred_lbbox
param label: The input label boxes
Expand All @@ -445,7 +445,7 @@ def yolov4_loss(self, conv, pred, label, bboxes, stride, iou_use=1, focal_use=Fa
param iou_use: The iou loss (0, 1, 2) ==> (giou, diou, ciou)
param focal_use: The focal loss (0, 1, 2) ==> (normal, sigmoid_focal, focal)
param label_smoothing: The label smoothing
return: Tensor, shape=(1,)"""
return: Tensor, shape=(1,)'''
conv_shape = tf.shape(conv)
batch_size = conv_shape[0]
output_size = conv_shape[1]
Expand Down Expand Up @@ -498,7 +498,7 @@ def yolov4_loss(self, conv, pred, label, bboxes, stride, iou_use=1, focal_use=Fa


def compute_loss(self, label_sbbox, label_mbbox, label_lbbox, true_sbbox, true_mbbox, true_lbbox, iou_use, focal_use, label_smoothing):
"""Compute loss; location loss, confidence loss, class prob loss """
'''Compute loss; location loss, confidence loss, class prob loss '''
with tf.name_scope('smaller_box_loss'):
loss_sbbox = self.yolov4_loss(self.conv_sbbox, self.pred_sbbox, label_sbbox, true_sbbox, stride=self.strides[0],
iou_use=iou_use, focal_use=focal_use, label_smoothing=label_smoothing)
Expand Down
Loading

0 comments on commit 682db0d

Please sign in to comment.