MCPcopy Create free account
hub / github.com/QWTforGithub/T2LDM / distorted_bounding_box_crop

Function distorted_bounding_box_crop

timm/data/tf_preprocessing.py:32–81  ·  view source on GitHub ↗

Generates cropped_image using one of the bboxes randomly distorted. See `tf.image.sample_distorted_bounding_box` for more documentation. Args: image_bytes: `Tensor` of binary image data. bbox: `Tensor` of bounding boxes arranged `[1, num_boxes, coords]` where each coo

(image_bytes,
                                bbox,
                                min_object_covered=0.1,
                                aspect_ratio_range=(0.75, 1.33),
                                area_range=(0.05, 1.0),
                                max_attempts=100,
                                scope=None)

Source from the content-addressed store, hash-verified

30
31
32def distorted_bounding_box_crop(image_bytes,
33 bbox,
34 min_object_covered=0.1,
35 aspect_ratio_range=(0.75, 1.33),
36 area_range=(0.05, 1.0),
37 max_attempts=100,
38 scope=None):
39 """Generates cropped_image using one of the bboxes randomly distorted.
40
41 See `tf.image.sample_distorted_bounding_box` for more documentation.
42
43 Args:
44 image_bytes: `Tensor` of binary image data.
45 bbox: `Tensor` of bounding boxes arranged `[1, num_boxes, coords]`
46 where each coordinate is [0, 1) and the coordinates are arranged
47 as `[ymin, xmin, ymax, xmax]`. If num_boxes is 0 then use the whole
48 image.
49 min_object_covered: An optional `float`. Defaults to `0.1`. The cropped
50 area of the image must contain at least this fraction of any bounding
51 box supplied.
52 aspect_ratio_range: An optional list of `float`s. The cropped area of the
53 image must have an aspect ratio = width / height within this range.
54 area_range: An optional list of `float`s. The cropped area of the image
55 must contain a fraction of the supplied image within in this range.
56 max_attempts: An optional `int`. Number of attempts at generating a cropped
57 region of the image of the specified constraints. After `max_attempts`
58 failures, return the entire image.
59 scope: Optional `str` for name scope.
60 Returns:
61 cropped image `Tensor`
62 """
63 with tf.name_scope(scope, 'distorted_bounding_box_crop', [image_bytes, bbox]):
64 shape = tf.image.extract_jpeg_shape(image_bytes)
65 sample_distorted_bounding_box = tf.image.sample_distorted_bounding_box(
66 shape,
67 bounding_boxes=bbox,
68 min_object_covered=min_object_covered,
69 aspect_ratio_range=aspect_ratio_range,
70 area_range=area_range,
71 max_attempts=max_attempts,
72 use_image_if_no_bounding_boxes=True)
73 bbox_begin, bbox_size, _ = sample_distorted_bounding_box
74
75 # Crop the image to the specified bounding box.
76 offset_y, offset_x, _ = tf.unstack(bbox_begin)
77 target_height, target_width, _ = tf.unstack(bbox_size)
78 crop_window = tf.stack([offset_y, offset_x, target_height, target_width])
79 image = tf.image.decode_and_crop_jpeg(image_bytes, crop_window, channels=3)
80
81 return image
82
83
84def _at_least_x_are_equal(a, b, x):

Callers 1

_decode_and_random_cropFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected