utils_tf.py 文件源码

python
阅读 32 收藏 0 点赞 0 评论 0

项目:blitznet 作者: dvornikita 项目源码 文件源码
def scale_distortions(image, gt_bboxes, gt_cats, params):
    """Samples a random box according to overlapping
    with gt objects criteria and crops it from an image"""
    image, gt_bboxes = tf.cond(tf.random_uniform([], 0, 1.0) < args.zoomout_prob,
                               lambda: zoomout(image, gt_bboxes, params),
                               lambda: (image, gt_bboxes))
    n_channels = image.shape[-1]

    def tf_random_choice(slices, bbox):
        sample = tf.multinomial(tf.log([[10.]*len(slices)]), 1)
        slices = tf.convert_to_tensor(slices)
        bbox = tf.convert_to_tensor(bbox)
        bbox_begin, bbox_size = tf.unstack(slices[tf.cast(sample[0][0],
                                                          tf.int32)])
        distort_bbox = bbox[tf.cast(sample[0][0], tf.int32)]
        return bbox_begin, bbox_size, distort_bbox

    bboxes = tf.expand_dims(xywh_to_yxyx(gt_bboxes), 0)
    samplers = []
    boxes = []
    for iou in params['sample_jaccards']:
        sample_distorted_bounding_box = tf.image.sample_distorted_bounding_box(
            tf.shape(image),
            bounding_boxes=bboxes,
            min_object_covered=iou,
            aspect_ratio_range=[0.5, 2.0],
            area_range=[0.3, 1.0],
            max_attempts=params['crop_max_tries'],
            use_image_if_no_bounding_boxes=True)
        samplers.append(sample_distorted_bounding_box[:2])
        boxes.append(sample_distorted_bounding_box[2][0][0])
    bbox_begin, bbox_size, distort_bbox = tf_random_choice(samplers, boxes)
    cropped_image = tf.slice(image, bbox_begin, bbox_size)
    # Nope TF, you are wrong, cropping does not change channels.
    cropped_image.set_shape([None, None, n_channels])
    y1, x1, y2, x2 = tf.unstack(distort_bbox)

    def check(center, mini, maxi):
        return tf.logical_and((center >= mini), (center <= maxi))
    gt_centers = gt_bboxes[:, :2] + gt_bboxes[:, 2:] / 2
    mask = tf.logical_and(check(gt_centers[:, 0], x1, x2),
                          check(gt_centers[:, 1], y1, y2))
    gt_bboxes = tf.boolean_mask(gt_bboxes, mask)
    gt_cats = tf.boolean_mask(gt_cats, mask)
    w = tf.to_float(x2-x1)
    h = tf.to_float(y2-y1)

    gt_x, gt_y, gt_w, gt_h = tf.unstack(gt_bboxes, axis=1)
    gt_x2 = gt_x + gt_w
    gt_y2 = gt_y + gt_h
    gt_x1_clip = tf.clip_by_value(gt_x - x1, 0, w)/w
    gt_x2_clip = tf.clip_by_value(gt_x2 - x1, 0, w)/w
    gt_y1_clip = tf.clip_by_value(gt_y - y1, 0, h)/h
    gt_y2_clip = tf.clip_by_value(gt_y2 - y1, 0, h)/h
    gt_w_clip = gt_x2_clip - gt_x1_clip
    gt_h_clip = gt_y2_clip - gt_y1_clip
    gt_bboxes = tf.stack([gt_x1_clip, gt_y1_clip, gt_w_clip, gt_h_clip],
                         axis=1)

    return cropped_image, gt_bboxes, gt_cats
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号