Skip to content

Commit

Permalink
config
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyang committed May 27, 2020
1 parent 2629bee commit 98cefe0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 79 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ Standard Method Package includes Flip left and right, Crop and Zoom(jitter=0.3)
|| | | | | | | L2 | 22.0 | 49.1 | 15.2 |
||| | | | | | L2 | 22.8 | 49.8 | 16.3 |
|||| | | | | L2 | 21.9 | 48.5 | 15.4 |
||| | | | | | CIoU | | | |
||| || | | | CIoU | | | |
||| | | | | | CIoU | 25.3 | 50.5 | 21.8 |
||| || | | | CIoU | 24.8 | 48.3 | 22.5 |
||| ||| | | CIoU | | | |
||| || || | CIoU | | | |
||| || | || CIoU | | | |
Expand Down
5 changes: 3 additions & 2 deletions cfgs/voc_yolov3-tiny.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ yolo:
mask: "3,4,5 0,1,2"

train:
label: "voc_yolov3_tiny_SM_DM_CIoU"
label: "voc_yolov3_tiny_SM_DM_CIoU_FL"
name_path: "./data/pascal_voc/voc.name"
anno_path: "./data/pascal_voc/train.txt"
image_size: "320,352,384,416,448,480,512,544,576,608"
Expand All @@ -17,12 +17,13 @@ train:
init_weight_path: "./ckpts/yolov3-tiny.h5"
save_weight_path: "./ckpts"

loss_type: "CIoU"
loss_type: "CIoU+FL"

mix_up: false
cut_mix: false
mosaic: false
label_smoothing: false
normal_method: true

ignore_threshold: 0.5

Expand Down
23 changes: 14 additions & 9 deletions core/dataset/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def __init__(self, cfg, verbose=0):
self.name_path = cfg['train']['name_path']
self.image_size = cfg["train"]["image_size"]
self.batch_size = cfg["train"]["batch_size"]

self.normal_method = cfg['train']["normal_method"]
self.mix_up = cfg['train']["mix_up"]
self.cut_mix = cfg['train']['cut_mix']
self.mosaic = cfg['train']['mosaic']
Expand Down Expand Up @@ -82,8 +84,6 @@ def __getitem__(self, idx):
image3, bboxes3, labels3,
image4, bboxes4, labels4)



bboxes = np.divide(bboxes, self._image_size)
bboxes[..., [0, 2]] = np.clip(bboxes[..., [0, 2]], 0., 1.)
bboxes[..., [1, 3]] = np.clip(bboxes[..., [1, 3]], 0., 1.)
Expand All @@ -100,13 +100,17 @@ def _getitem(self, sub_idx):
image = read_image(path)
bboxes, labels = np.array(bboxes), np.array(labels)

image = augment.random_distort(image)
image = augment.random_grayscale(image)
image, bboxes = augment.random_flip_lr(image, bboxes)
image, bboxes = augment.random_rotate(image, bboxes)
image, bboxes, labels = augment.random_crop_and_zoom(image, bboxes, labels, (self._image_size, self._image_size))

#image, bboxes = preprocess_image(image, (self._image_size, self._image_size), bboxes)
if self.normal_method:
image = augment.random_distort(image)
image = augment.random_grayscale(image)
image, bboxes = augment.random_flip_lr(image, bboxes)
image, bboxes = augment.random_rotate(image, bboxes)
image, bboxes, labels = augment.random_crop_and_zoom(image, bboxes, labels,
(self._image_size, self._image_size))
else:

image, bboxes = preprocess_image(image, (self._image_size, self._image_size), bboxes)

labels = augment.onehot(labels, self.num_classes, self.label_smoothing)

Expand All @@ -128,7 +132,8 @@ def _preprocess_true_boxes(self, bboxes, labels, weights):

bboxes_wh_exp = np.tile(np.expand_dims(bboxes_wh, 1), (1, self._anchors.shape[0], 1))
boxes_area = bboxes_wh_exp[..., 0] * bboxes_wh_exp[..., 1]
intersection = np.minimum(bboxes_wh_exp[..., 0], self._anchors[:, 0]) * np.minimum(bboxes_wh_exp[..., 1], self._anchors[:, 1])
intersection = np.minimum(bboxes_wh_exp[..., 0], self._anchors[:, 0]) * np.minimum(bboxes_wh_exp[..., 1],
self._anchors[:, 1])
iou = intersection / (boxes_area + anchor_area - intersection + 1e-8) # (N, A)
anchor_idxs = np.argmax(iou, axis=-1) # (N,)

Expand Down
67 changes: 1 addition & 66 deletions core/model/one_stage/yolov3/yolov3.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,71 +247,6 @@ def compute_output_shape(self, input_shape):
[input_shape[0][0]])


# def YoloLoss(anchors, stride, num_classes, ignore_thresh, bbox_loss):
# def wrapper(y_true, y_pred):
# # 0. default
# dtype = y_pred.dtype
# y_shape = tf.shape(y_pred)
# grid_w, grid_h = y_shape[2], y_shape[1]
# anchors_tensor = tf.cast(anchors, dtype) / tf.cast(tf.multiply([grid_w, grid_h], stride), dtype)
# y_true = tf.reshape(y_true, (y_shape[0], y_shape[1], y_shape[2], anchors_tensor.shape[0], num_classes + 6))
# y_pred = tf.reshape(y_pred, (y_shape[0], y_shape[1], y_shape[2], anchors_tensor.shape[0], num_classes + 5))
#
# # 1. transform all pred outputs
# # y_pred: (batch_size, grid, grid, anchors, (x, y, w, h, obj, ...cls))
# pred_box, pred_obj, pred_class, pred_xywh = _yolo_boxes(
# y_pred, num_classes, anchors_tensor)
# pred_xy = pred_xywh[..., 0:2]
# pred_wh = pred_xywh[..., 2:4]
#
# # 2. transform all true outputs
# # y_true: (batch_size, grid, grid, anchors, (x1, y1, x2, y2, obj, cls))
# true_box, true_obj, true_weight, true_class = tf.split(y_true, (4, 1, 1, num_classes), axis=-1)
# true_xy = (true_box[..., 0:2] + true_box[..., 2:4]) / 2
# true_wh = true_box[..., 2:4] - true_box[..., 0:2]
#
# # give higher weights to small boxes
# box_loss_scale = 2 - true_wh[..., 0] * true_wh[..., 1]
#
# # 3. inverting the pred box equations
# grid_size = tf.shape(y_true)[1]
# grid = tf.meshgrid(tf.range(grid_size), tf.range(grid_size))
# grid = tf.expand_dims(tf.stack(grid, axis=-1), axis=2)
# true_xy = true_xy * tf.cast(grid_size, dtype) - tf.cast(grid, dtype)
# true_wh = tf.math.log(true_wh / anchors_tensor)
# true_wh = tf.where(tf.math.is_inf(true_wh),
# tf.zeros_like(true_wh), true_wh)
#
# # 4. calculate all masks
# obj_mask = tf.squeeze(true_obj, -1)
# weight = tf.squeeze(true_weight, -1)
# # ignore false positive when iou is over threshold
# best_iou = tf.map_fn(
# lambda x: tf.reduce_max(_broadcast_iou(x[0], tf.boolean_mask(
# x[1], tf.cast(x[2], tf.bool))), axis=-1),
# (pred_box, true_box, obj_mask),
# dtype)
# ignore_mask = tf.cast(best_iou < ignore_thresh, dtype)
#
# # 5. calculate all losses
# xy_loss = obj_mask * box_loss_scale * 0.5 * tf.reduce_sum(tf.square(true_xy - pred_xy), axis=-1)
# wh_loss = obj_mask * box_loss_scale * 0.5 * tf.reduce_sum(tf.square(true_wh - pred_wh), axis=-1)
# obj_loss = tf.keras.losses.binary_crossentropy(true_obj, pred_obj)
# obj_loss = obj_mask * obj_loss + (1 - obj_mask) * ignore_mask * obj_loss
# # TODO: use binary_crossentropy instead
# class_loss = obj_mask * tf.keras.losses.categorical_crossentropy(true_class, pred_class)
#
# # 6. sum over (batch, gridx, gridy, anchors) => (batch, 1)
# xy_loss = tf.reduce_sum(weight * xy_loss, axis=(1, 2, 3))
# wh_loss = tf.reduce_sum(weight * wh_loss, axis=(1, 2, 3))
# obj_loss = tf.reduce_sum(weight * obj_loss, axis=(1, 2, 3))
# class_loss = tf.reduce_sum(weight * class_loss, axis=(1, 2, 3))
#
# return xy_loss + wh_loss + obj_loss + class_loss
#
# return wrapper


def _broadcast_iou(box_1, box_2):
# box_1: (..., (x1, y1, x2, y2))
# box_2: (N, (x1, y1, x2, y2))
Expand Down Expand Up @@ -429,7 +364,7 @@ def wrapper(y_true, y_pred):
cls_loss = obj_mask * tf.keras.losses.binary_crossentropy(true_cls, pred_cls)

def _focal_loss(y_true, y_pred, alpha=1, gamma=2):
focal_loss = alpha * tf.pow(tf.abs(y_true - y_pred), gamma)
focal_loss = tf.squeeze(alpha * tf.pow(tf.abs(y_true - y_pred), gamma), axis=-1)
return focal_loss

if 'FL' in type:
Expand Down

0 comments on commit 98cefe0

Please sign in to comment.