8.1.1.7.1.3.1.2. blueoil.networks.object_detection.yolo_v1

8.1.1.7.1.3.1.2.1. Module Contents

8.1.1.7.1.3.1.2.1.1. Classes

YoloV1

YOLO version1.

YoloV1Loss

YOLO v1 loss function.

8.1.1.7.1.3.1.2.1.2. Functions

summary_boxes(tag, images, boxes, image_size, max_outputs=3)

Draw bounding boxes images on Tensroboard.

format_XYWH_to_CXCYWH(boxes, axis=1)

Format form (x, y, w, h) to (center_x, center_y, w, h) along specific dimension.

format_CXCYWH_to_XYWH(boxes, axis=1)

Format form (center_x, center_y, w, h) to (x, y, w, h) along specific dimension.

format_CXCYWH_to_YX(inputs, axis=1)

Format from (x, y, w, h) to (y1, x1, y2, x2) boxes along specific dimension.

format_XYWH_to_YX(inputs, axis=1)

Format from (x, y, w, h) to (y1, x1, y2, x2) boxes along specific dimension.

class blueoil.networks.object_detection.yolo_v1.YoloV1(cell_size=7, boxes_per_cell=2, leaky_relu_scale=0.1, object_scale=1.0, no_object_scale=1.0, class_scale=1.0, coordinate_scale=5.0, num_max_boxes=1, *args, **kwargs)

Bases: blueoil.networks.base.BaseNetwork

YOLO version1.

YOLO v1. paper: https://arxiv.org/abs/1506.02640

placeholders(self)

placeholders

summary(self, output, labels=None)

Summary.

Parameters
  • output – tensor from inference.

  • labels – labels tensor.

metrics(self, output, labels, thresholds=[0.3, 0.5, 0.7])

Metrics.

Parameters
  • output – tensor from inference.

  • labels – labels tensor.

leaky_relu(self, inputs)
convert_gt_boxes_xywh_to_cxcywh(self, gt_boxes)

Convert gt_boxes format form (x, y, w, h) to (center_x, center_y, w, h).

Args: gt_boxes :3D tensor [batch_size, max_num_boxes, 5(x, y, w, h, class_id)]

offset_boxes(self)

Return yolo space offset of x and y.

Return: offset_x: shape is [batch_size, cell_size, cell_size, boxes_per_cell] offset_y: shape is [batch_size, cell_size, cell_size, boxes_per_cell]

convert_boxes_space_from_real_to_yolo(self, boxes)

Convert boxes space size from real to yolo.

Real space boxes coodinates are in the interval [0, image_size]. Yolo space boxes coodinates are in the interval [-1, 1].

Args: boxes: 5D Tensor, shape is [batch_size, cell_size, cell_size, boxes_per_cell, 4(center_x, center_y, w, h)].

convert_boxes_space_from_yolo_to_real(self, predict_boxes)

Convert predict boxes space size from yolo to real.

Real space boxes coodinates are in the interval [0, image_size]. Yolo space boxes coodinates are in the interval [-1, 1].

Args: boxes: 5D Tensor, shape is [batch_size, cell_size, cell_size, boxes_per_cell, 4(center_x, center_y, w, h)].

_predictions(self, output)

Separate combined inference outputs to predictions.

Args: output: combined fc outputs 2D Tensor.

shape is [batch_size, self.cell_size * self.cell_size * (self.num_classes + self.boxes_per_cell * 5)]

Returns: predict_classes: 4D Tensor [batch_size, cell_size, cell_size, num_classes] predict_confidence: 4D Tensor [batch_size, cell_size, cell_size, boxes_per_cell] predict_boxes: 5D Tensor [batch_size, cell_size, cell_size, boxes_per_cell, 4(center_x, center_y, w, h)]

predict_boxes(self, output, threshold=0.05)

Predict boxes with probabilty threshold.

Args: output: Tensor of inference() outputs. threshold: threshold of predict score.

Retrun:

python list of predict_boxes Tensor. predict_boxes shape is [num_predicted_boxes, 6(x, y, w, h, class_id, probability)]. python list lenght is batch size.

_post_process(self, predict_classes, predict_confidence, predict_boxes, threshold=0.05)

Predict boxes with probabilty threshold.

Args: predict_classes: [batch_size, cell_size, cell_size, num_classes] predict_confidence: [batch_size, cell_size, cell_size, boxes_per_cell] predict_boxes: [batch_size, cell_size, cell_size, boxes_per_cell, 4(center_x, center_y, w, h)] threshold: threshold of predict score.

Return: python list of predict_boxes Tensor. predict_boxes shape is [num_predicted_boxes, 6(x, y, w, h, class_id, probability)]. python list lenght is batch size.

_summary_predict_boxes(self, predict_classes, predict_confidence, predict_boxes, threshold=0.05)

Summary predict boxes on tensorboard.

Args: predict_classes: [batch_size, cell_size, cell_size, num_classes] predict_confidence: [batch_size, cell_size, cell_size, boxes_per_cell] predict_boxes: [batch_size, cell_size, cell_size, boxes_per_cell, 4(center_x, center_y, w, h)] threshold: threshold of predict score.

loss(self, output, gt_boxes)

Loss.

Parameters
  • output – 2D tensor. shape is [batch_size, self.cell_size * self.cell_size * (self.num_classes + self.boxes_per_cell * 5)]

  • gt_boxes – ground truth boxes 3D tensor. [batch_size, max_num_boxes, 4(x, y, w, h)].

inference(self, images, is_training)

Inference.

Parameters

images – images tensor. shape is (batch_num, height, width, channel)

base(self, images, is_training)

Base function contains inference.

Parameters
  • images – Input images.

  • is_training – A flag for if is training.

Returns

Inference result.

Return type

tf.Tensor

class blueoil.networks.object_detection.yolo_v1.YoloV1Loss(is_debug=False, cell_size=7, boxes_per_cell=2, object_scale=1.0, no_object_scale=1.0, class_scale=1.0, coordinate_scale=5.0, image_size=[448, 448], batch_size=64, classes=[], yolo=None)

YOLO v1 loss function.

_iou(self, boxes1, boxes2)

Calculate ious.

Args: boxes1: 5-D tensor [batch_size, cell_size, cell_size, boxes_per_cell, 4(x_center, y_center, w, h)] boxes2: 5-D tensor [batch_size, cell_size, cell_size, boxes_per_cell, 4(x_center, y_center, w, h)] Return:

iou: 4-D tensor [batch_size, cell_size, cell_size, boxes_per_cell]

_gt_boxes_to_cell_loop_cond(self, i, gt_boxes, cell_gt_box, object_mask)

Return True when gt_boxes is not dummy.

Args: i: scalr Tensor. while loop counter gt_boxes: 2D Tensor [max_num_boxes, 5(center_x, center_y, w, h, class_id)]

_gt_boxes_to_cell_loop_body(self, i, gt_boxes, cell_gt_box, object_mask)

Calculate the gt_boxes corresponding cell.

the cell`s object_mask is assigned 1.0 and the cell gt boxes assign gt_boxes coordinate.

Args: i: scalr Tensor. while loop counter gt_boxes: 2D Tensor [max_num_boxes, 5(center_x, center_y, w, h, class_id)] cell_gt_box: 3D Tensor [max_num_boxes, 5(center_x, center_y, w, h, class_id)]

_gt_boxes_to_cell(self, gt_boxes_list)

Check gt_boxes are not dummy, create cell_gt_boxes and object_mask from the gt_boxes.

Args: gt_boxes_list: Tensor [batch_size, max_num_boxes, 4(center_x, center_y, w, h)]

Return: cell_gt_boxes: Tensor [batch_size, cell_size, cell_size, 4(center_x, center_y, w, h)].

copy from non dummy gt boxes coodinate to corresponding cell.

object_masks: Tensor [batch_size, cell_size, cell_size, 1]. the cell that has gt boxes is 1, none is 0.

__call__(self, predict_classes, predict_confidence, predict_boxes, gt_boxes)

Loss function.

Args: predict_classes: [batch_size, cell_size, cell_size, num_classes] predict_confidence: [batch_size, cell_size, cell_size, boxes_per_cell] predict_boxes: [batch_size, cell_size, cell_size, boxes_per_cell, 4(center_x, center_y, w, h)] gt_boxes: ground truth boxes 3D tensor. [batch_size, max_num_boxes, 4(center_x, center_y, w, h)].

blueoil.networks.object_detection.yolo_v1.summary_boxes(tag, images, boxes, image_size, max_outputs=3)

Draw bounding boxes images on Tensroboard.

Args: tag: name of summary tag. images: Tesnsor of images [batch_size, height, widths, 3]. boxes: Tensor of boxes. assumed shape is [batch_size, num_boxes, 4(y1, x1, y2, x2)]. image_size: python list image size [height, width].

blueoil.networks.object_detection.yolo_v1.format_XYWH_to_CXCYWH(boxes, axis=1)

Format form (x, y, w, h) to (center_x, center_y, w, h) along specific dimension.

Args: boxes :a Tensor include boxes. [:, 4(x, y, w, h)] axis: which dimension of the inputs Tensor is boxes.

blueoil.networks.object_detection.yolo_v1.format_CXCYWH_to_XYWH(boxes, axis=1)

Format form (center_x, center_y, w, h) to (x, y, w, h) along specific dimension.

Args: boxes: A tensor include boxes. [:, 4(x, y, w, h)] axis: Which dimension of the inputs Tensor is boxes.

blueoil.networks.object_detection.yolo_v1.format_CXCYWH_to_YX(inputs, axis=1)

Format from (x, y, w, h) to (y1, x1, y2, x2) boxes along specific dimension.

Parameters
  • inputs – a Tensor include boxes.

  • axis – which dimension of the inputs Tensor is boxes.

blueoil.networks.object_detection.yolo_v1.format_XYWH_to_YX(inputs, axis=1)

Format from (x, y, w, h) to (y1, x1, y2, x2) boxes along specific dimension.

Parameters
  • inputs – a Tensor include boxes.

  • axis – which dimension of the inputs Tensor is boxes.