(height, width, boxes=None)
| 116 | |
| 117 | |
| 118 | def random_sample_crop(height, width, boxes=None): |
| 119 | global sample_options |
| 120 | |
| 121 | while True: |
| 122 | # randomly choose a mode |
| 123 | mode = random.choice(sample_options) |
| 124 | if mode is None: |
| 125 | return height, width, boxes |
| 126 | |
| 127 | min_iou, max_iou = mode |
| 128 | if min_iou is None: |
| 129 | min_iou = float('-inf') |
| 130 | if max_iou is None: |
| 131 | max_iou = float('inf') |
| 132 | |
| 133 | for _ in range(50): |
| 134 | w = random.uniform(0.3 * width, width) |
| 135 | h = random.uniform(0.3 * height, height) |
| 136 | |
| 137 | if h / w < 0.5 or h / w > 2: |
| 138 | continue |
| 139 | |
| 140 | left = random.uniform(0, width - w) |
| 141 | top = random.uniform(0, height - h) |
| 142 | |
| 143 | rect = np.array([int(left), int(top), int(left+w), int(top+h)]) |
| 144 | overlap = jaccard_numpy(boxes, rect) |
| 145 | if overlap.min() < min_iou and max_iou < overlap.max(): |
| 146 | continue |
| 147 | |
| 148 | centers = (boxes[:, :2] + boxes[:, 2:]) / 2.0 |
| 149 | |
| 150 | m1 = (rect[0] < centers[:, 0]) * (rect[1] < centers[:, 1]) |
| 151 | m2 = (rect[2] > centers[:, 0]) * (rect[3] > centers[:, 1]) |
| 152 | mask = m1 * m2 |
| 153 | |
| 154 | if not mask.any(): |
| 155 | continue |
| 156 | |
| 157 | current_boxes = boxes[mask, :].copy() |
| 158 | current_boxes[:, :2] = np.maximum(current_boxes[:, :2], rect[:2]) |
| 159 | current_boxes[:, :2] -= rect[:2] |
| 160 | current_boxes[:, 2:] = np.minimum(current_boxes[:, 2:], rect[2:]) |
| 161 | current_boxes[:, 2:] -= rect[:2] |
| 162 | |
| 163 | return h, w, current_boxes |
| 164 | |
| 165 | |
| 166 | if __name__ == '__main__': |
no test coverage detected