Seglink代码之二 数据增强 distorted_bounding_box_crop
2017-10-27 19:39
519 查看
def distorted_bounding_box_crop(image, labels, bboxes, xs, ys, min_object_covered, aspect_ratio_range, area_range, max_attempts = 200, scope=None): """Generates cropped_image using a one of the bboxes randomly distorted. See `tf.image.sample_distorted_bounding_box` for more documentation. Args: image: 3-D Tensor of image (it will be converted to floats in [0, 1]). bbox: 2-D float Tensor of bounding boxes arranged [num_boxes, coords] where each coordinate is [0, 1) and the coordinates are arranged as [ymin, xmin, ymax, xmax]. If num_boxes is 0 then it would use the whole image. min_object_covered: An optional `float`. Defaults to `0.1`. The cropped area of the image must contain at least this fraction of any bounding box supplied. aspect_ratio_range: An optional list of `floats`. The cropped area of the image must have an aspect ratio = width / height within this range. area_range: An optional list of `floats`. The cropped area of the image must contain a fraction of the supplied image within in this range. max_attempts: An optional `int`. Number of attempts at generating a cropped region of the image of the specified constraints. After `max_attempts` failures, return the entire image. scope: Optional scope for name_scope. Returns: A tuple, a 3-D Tensor cropped_image and the distorted bbox """ with tf.name_scope(scope, 'distorted_bounding_box_crop', [image, bboxes, xs, ys]): # Each bounding box has shape [1, num_boxes, box coords] and # the coordinates are ordered [ymin, xmin, ymax, xmax]. # distort_bbox reference bbox_begin, bbox_size, distort_bbox = tf.image.sample_distorted_bounding_box( tf.shape(image), bounding_boxes=tf.expand_dims(bboxes, 0), min_object_covered=min_object_covered, aspect_ratio_range=aspect_ratio_range, area_range=area_range, max_attempts=max_attempts, use_image_if_no_bounding_boxes=True) distort_bbox = distort_bbox[0, 0] # Crop the image to the specified bounding box. cropped_image = tf.slice(image, bbox_begin, bbox_size) # Restore the shape since the dynamic slice loses 3rd dimension. cropped_image.set_shape([None, None, 3]) # Update bounding boxes: resize and filter out. # distort_bbox 其实就是一个reference bboxes, xs, ys = tfe.bboxes_resize(distort_bbox, bboxes, xs, ys) labels, bboxes, xs, ys = tfe.bboxes_filter_overlap(labels, bboxes, xs, ys, threshold=BBOX_CROP_OVERLAP, assign_negative = False) return cropped_image, labels, bboxes, xs, ys, distort_bbox def bboxes_resize(bbox_ref, bboxes, xs, ys, name=None): """Resize bounding boxes based on a reference bounding box, assuming that the latter is [0, 0, 1, 1] after transform. Useful for updating a collection of boxes after cropping an image. """ # Tensors inputs. # boxes [y, x, h, w] # bbox_ref 这是crop_image相对于原图的比例 # bboxes 这是bbox相对于原图的比例 # xs, ys是由bbox生成的,一样~ with tf.name_scope(name, 'bboxes_resize'): h_ref = bbox_ref[2] - bbox_ref[0] w_ref = bbox_ref[3] - bbox_ref[1] # Translate. v = tf.stack([bbox_ref[0], bbox_ref[1], bbox_ref[0], bbox_ref[1]]) bboxes = bboxes - v xs = xs - bbox_ref[1] ys = ys - bbox_ref[0] # Scale. s = tf.stack([h_ref, w_ref, h_ref, w_ref]) #tf.global_variables_initializer() #s = tf.Session() #c = s.run(bboxes) bboxes = bboxes / s xs = xs / w_ref; ys = ys / h_ref; return bboxes, xs, ys
相关文章推荐
- InfoPath进阶之二:给InfoPath添加VSTA托管代码并从SharePoint列表查询数据
- Python图片数据增强crop、rotate、environment factor
- Ogitor探索之代码分析(3)---CBaseEditor::showBoundingBox(bool bShow)
- tf.image.sample_distorted_bounding_box(含其他对图形的操作)
- [Web-Harvest数据采集之二]Web-Harvest基础-抓取java代码分析
- 注意增强代码逻辑,过滤掉不需要的数据
- tf.image.crop_to_bounding_box的用法
- tensorflow学习 tf.image.sample_distorted_bounding_box
- 通过 ASP.NET 2.0 的数据增强功能来提高负载和减少代码 --作者:Dino Esposito
- 从XML到Java代码的数据绑定之二
- android 客户端数据传递之二:基于Http协议获取数据 代码
- 分享非常有用的Java程序(关键代码)(八)---Java InputStream读取网络响应Response数据的方法!(重要)
- Oracle数据恢复顾问(DRA)使用测试 (之二)
- vue组件表单数据回显验证及提交的实例代码
- 【数据结构与算法】【排序】简单选择排序的代码实现
- 增强Eclipse ,MyEclipse 的代码自动提示功能
- Excel数据导入Mysql数据库的实现代码
- 增强Eclipse、MyEclipse的代码自动提示功能
- asp.net实现数据分页的控件代码
- 用java代码实现从excel表格读取数据然后写入生成国际化配置文件properties