(self, image, boxes=None, labels=None)
| 232 | ) |
| 233 | |
| 234 | def __call__(self, image, boxes=None, labels=None): |
| 235 | height, width, _ = image.shape |
| 236 | while True: |
| 237 | # randomly choose a mode |
| 238 | mode = random.choice(self.sample_options) |
| 239 | if mode is None: |
| 240 | return image, boxes, labels |
| 241 | |
| 242 | min_iou, max_iou = mode |
| 243 | if min_iou is None: |
| 244 | min_iou = float('-inf') |
| 245 | if max_iou is None: |
| 246 | max_iou = float('inf') |
| 247 | |
| 248 | # max trails (50) |
| 249 | for _ in range(50): |
| 250 | current_image = image |
| 251 | |
| 252 | w = random.uniform(0.3 * width, width) |
| 253 | h = random.uniform(0.3 * height, height) |
| 254 | |
| 255 | # aspect ratio constraint b/t .5 & 2 |
| 256 | if h / w < 0.5 or h / w > 2: |
| 257 | continue |
| 258 | |
| 259 | left = random.uniform(width - w) |
| 260 | top = random.uniform(height - h) |
| 261 | |
| 262 | # convert to integer rect x1,y1,x2,y2 |
| 263 | rect = np.array([int(left), int(top), int(left+w), int(top+h)]) |
| 264 | |
| 265 | # calculate IoU (jaccard overlap) b/t the cropped and gt boxes |
| 266 | overlap = jaccard_numpy(boxes, rect) |
| 267 | |
| 268 | # is min and max overlap constraint satisfied? if not try again |
| 269 | if overlap.min() < min_iou and max_iou < overlap.max(): |
| 270 | continue |
| 271 | |
| 272 | # cut the crop from the image |
| 273 | current_image = current_image[rect[1]:rect[3], rect[0]:rect[2], |
| 274 | :] |
| 275 | |
| 276 | # keep overlap with gt box IF center in sampled patch |
| 277 | centers = (boxes[:, :2] + boxes[:, 2:]) / 2.0 |
| 278 | |
| 279 | # mask in all gt boxes that above and to the left of centers |
| 280 | m1 = (rect[0] < centers[:, 0]) * (rect[1] < centers[:, 1]) |
| 281 | |
| 282 | # mask in all gt boxes that under and to the right of centers |
| 283 | m2 = (rect[2] > centers[:, 0]) * (rect[3] > centers[:, 1]) |
| 284 | |
| 285 | # mask in that both m1 and m2 are true |
| 286 | mask = m1 * m2 |
| 287 | |
| 288 | # have any valid boxes? try again if not |
| 289 | if not mask.any(): |
| 290 | continue |
| 291 |
nothing calls this directly
no test coverage detected