Crop Arguments: img (Image): the image being input during training boxes (Tensor): the original bounding boxes in pt form labels (Tensor): the class labels for each bbox mode (float tuple): the min and max jaccard overlaps Return: (img, boxes, classes)
| 206 | |
| 207 | |
| 208 | class RandomSampleCrop(object): |
| 209 | """Crop |
| 210 | Arguments: |
| 211 | img (Image): the image being input during training |
| 212 | boxes (Tensor): the original bounding boxes in pt form |
| 213 | labels (Tensor): the class labels for each bbox |
| 214 | mode (float tuple): the min and max jaccard overlaps |
| 215 | Return: |
| 216 | (img, boxes, classes) |
| 217 | img (Image): the cropped image |
| 218 | boxes (Tensor): the adjusted bounding boxes in pt form |
| 219 | labels (Tensor): the class labels for each bbox |
| 220 | """ |
| 221 | def __init__(self): |
| 222 | self.sample_options = ( |
| 223 | # using entire original input image |
| 224 | None, |
| 225 | # sample a patch s.t. MIN jaccard w/ obj in .1,.3,.4,.7,.9 |
| 226 | (0.1, None), |
| 227 | (0.3, None), |
| 228 | (0.7, None), |
| 229 | (0.9, None), |
| 230 | # randomly sample a patch |
| 231 | (None, 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 |