コード例 #1
0
    def _parse_eval_data(self, data):
        """Generates images and labels that are usable for model training.
    Args:
      data: a dict of Tensors produced by the decoder.
    Returns:
      images: the image tensor.
      labels: a dict of Tensors that contains labels.
    """

        shape = tf.shape(data['image'])
        image = data['image'] / 255
        boxes = data['groundtruth_boxes']
        width = shape[0]
        height = shape[1]

        image, boxes = preprocessing_ops.fit_preserve_aspect_ratio(
            image, boxes, width=width, height=height, target_dim=self._image_w)
        boxes = yolo_box_ops.yxyx_to_xcycwh(boxes)

        # find the best anchor for the ground truth labels to maximize the iou
        best_anchors = preprocessing_ops.get_best_anchor(boxes,
                                                         self._anchors,
                                                         width=self._image_w,
                                                         height=self._image_h)
        boxes = preprocessing_ops.pad_max_instances(boxes,
                                                    self._max_num_instances, 0)
        classes = preprocessing_ops.pad_max_instances(
            data['groundtruth_classes'], self._max_num_instances, 0)
        best_anchors = preprocessing_ops.pad_max_instances(
            best_anchors, self._max_num_instances, 0)
        area = preprocessing_ops.pad_max_instances(data['groundtruth_area'],
                                                   self._max_num_instances, 0)
        is_crowd = preprocessing_ops.pad_max_instances(
            tf.cast(data['groundtruth_is_crowd'], tf.int32),
            self._max_num_instances, 0)

        labels = {
            'source_id': data['source_id'],
            'bbox': tf.cast(boxes, self._dtype),
            'classes': tf.cast(classes, self._dtype),
            'area': tf.cast(area, self._dtype),
            'is_crowd': is_crowd,
            'best_anchors': tf.cast(best_anchors, self._dtype),
            'width': width,
            'height': height,
            'num_detections': tf.shape(data['groundtruth_classes'])[0],
        }

        grid = self._build_grid(labels,
                                self._image_w,
                                batch=False,
                                use_tie_breaker=self._use_tie_breaker)
        labels.update({'grid_form': grid})
        return image, labels
コード例 #2
0
ファイル: anchor.py プロジェクト: xiangww00/models
    def build_label_per_path(self,
                             key,
                             boxes,
                             classes,
                             width,
                             height,
                             iou_index=None):
        """Builds the labels for one path."""
        stride = self.level_strides[key]
        scale_xy = self.center_radius[
            key] if self.center_radius is not None else 1

        width = tf.cast(width // stride, boxes.dtype)
        height = tf.cast(height // stride, boxes.dtype)

        if self.anchor_free_level_limits is None:
            (boxes, classes, anchors,
             num_anchors) = self._get_anchor_id(key,
                                                boxes,
                                                classes,
                                                width,
                                                height,
                                                stride,
                                                iou_index=iou_index)
            boxes, classes, centers = self._get_centers(
                boxes, classes, anchors, width, height, scale_xy)
            ind_mask = tf.ones_like(classes)
            updates = tf.concat([boxes, ind_mask, classes], axis=-1)
        else:
            num_anchors = 1
            (centers, updates) = self._get_anchor_free(key, boxes, classes,
                                                       height, width, stride,
                                                       scale_xy)
            boxes, ind_mask, classes = tf.split(updates, [4, 1, 1], axis=-1)

        width = tf.cast(width, tf.int32)
        height = tf.cast(height, tf.int32)
        full = tf.zeros([height, width, num_anchors, 1], dtype=classes.dtype)
        full = tf.tensor_scatter_nd_add(full, centers, ind_mask)

        num_instances = int(self.num_instances[key])
        centers = preprocessing_ops.pad_max_instances(centers,
                                                      num_instances,
                                                      pad_value=0,
                                                      pad_axis=0)
        updates = preprocessing_ops.pad_max_instances(updates,
                                                      num_instances,
                                                      pad_value=0,
                                                      pad_axis=0)

        updates = tf.cast(updates, self.dtype)
        full = tf.cast(full, self.dtype)
        return centers, updates, full
コード例 #3
0
 def testPadMaxInstances(self, input_shape, instances, pad_axis):
     expected_output_shape = input_shape
     expected_output_shape[pad_axis] = instances
     output = preprocessing_ops.pad_max_instances(np.ones(input_shape),
                                                  instances,
                                                  pad_axis=pad_axis)
     self.assertAllEqual(expected_output_shape, tf.shape(output).numpy())
コード例 #4
0
  def set_shape(self, values, pad_axis=0, pad_value=0, inds=None):
    """Calls set shape for all input objects."""
    if inds is not None:
      values = tf.gather(values, inds)
    vshape = values.get_shape().as_list()

    values = preprocessing_ops.pad_max_instances(
        values, self._max_num_instances, pad_axis=pad_axis, pad_value=pad_value)

    vshape[pad_axis] = self._max_num_instances
    values.set_shape(vshape)
    return values