:mod:`blueoil.networks.object_detection.yolo_v1` ================================================ .. py:module:: blueoil.networks.object_detection.yolo_v1 Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: blueoil.networks.object_detection.yolo_v1.YoloV1 blueoil.networks.object_detection.yolo_v1.YoloV1Loss Functions ~~~~~~~~~ .. autoapisummary:: blueoil.networks.object_detection.yolo_v1.summary_boxes blueoil.networks.object_detection.yolo_v1.format_XYWH_to_CXCYWH blueoil.networks.object_detection.yolo_v1.format_CXCYWH_to_XYWH blueoil.networks.object_detection.yolo_v1.format_CXCYWH_to_YX blueoil.networks.object_detection.yolo_v1.format_XYWH_to_YX .. py:class:: 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: :class:`blueoil.networks.base.BaseNetwork` YOLO version1. YOLO v1. paper: https://arxiv.org/abs/1506.02640 .. method:: placeholders(self) placeholders .. method:: summary(self, output, labels=None) Summary. :param output: tensor from inference. :param labels: labels tensor. .. method:: metrics(self, output, labels, thresholds=[0.3, 0.5, 0.7]) Metrics. :param output: tensor from inference. :param labels: labels tensor. .. method:: leaky_relu(self, inputs) .. method:: 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)] .. method:: 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] .. method:: 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)]. .. method:: 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)]. .. method:: _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)] .. method:: 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. .. method:: _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. .. method:: _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. .. method:: loss(self, output, gt_boxes) Loss. :param output: 2D tensor. shape is [batch_size, self.cell_size * self.cell_size * (self.num_classes + self.boxes_per_cell * 5)] :param gt_boxes: ground truth boxes 3D tensor. [batch_size, max_num_boxes, 4(x, y, w, h)]. .. method:: inference(self, images, is_training) Inference. :param images: images tensor. shape is (batch_num, height, width, channel) .. method:: base(self, images, is_training) Base function contains inference. :param images: Input images. :param is_training: A flag for if is training. :returns: Inference result. :rtype: tf.Tensor .. py:class:: 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. .. method:: _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] .. method:: _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)] .. method:: _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)] .. method:: _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. .. method:: __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)]. .. function:: 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]. .. function:: 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. .. function:: 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. .. function:: format_CXCYWH_to_YX(inputs, axis=1) Format from (x, y, w, h) to (y1, x1, y2, x2) boxes along specific dimension. :param inputs: a Tensor include boxes. :param axis: which dimension of the inputs Tensor is boxes. .. function:: format_XYWH_to_YX(inputs, axis=1) Format from (x, y, w, h) to (y1, x1, y2, x2) boxes along specific dimension. :param inputs: a Tensor include boxes. :param axis: which dimension of the inputs Tensor is boxes.