in data/transforms/image.py [0:0]
def __call__(self, data: Dict) -> Dict:
boxes = data.get("box_coordinates", None)
if boxes is not None:
# guard against no boxes
if boxes.shape[0] == 0:
return data
image = data["image"]
labels = data["box_labels"]
height, width = image.shape[:2]
while True:
# randomly choose a mode
min_jaccard_overalp = random.choice(self.sample_options)
if min_jaccard_overalp == 0.0:
return data
for _ in range(self.trials):
current_image = image
w = random.uniform(0.3 * width, width)
h = random.uniform(0.3 * height, height)
aspect_ratio = h / w
if not (self.min_aspect_ratio <= aspect_ratio <= self.max_aspect_ratio):
continue
left = random.uniform(0, width - w)
top = random.uniform(0, height - h)
# convert to integer rect x1,y1,x2,y2
rect = np.array([int(left), int(top), int(left + w), int(top + h)])
# calculate IoU (jaccard overlap) b/t the cropped and gt boxes
ious = jaccard_numpy(boxes, rect)
# is min and max overlap constraint satisfied? if not try again
if ious.max() < min_jaccard_overalp:
continue
# cut the crop from the image
current_image = current_image[rect[1]:rect[3], rect[0]:rect[2], :]
# keep overlap with gt box IF center in sampled patch
centers = (boxes[:, :2] + boxes[:, 2:]) * 0.5
# mask in all gt boxes that above and to the left of centers
m1 = (rect[0] < centers[:, 0]) * (rect[1] < centers[:, 1])
# mask in all gt boxes that under and to the right of centers
m2 = (rect[2] > centers[:, 0]) * (rect[3] > centers[:, 1])
# mask in that both m1 and m2 are true
mask = m1 * m2
# have any valid boxes? try again if not
if not mask.any():
continue
# take only matching gt boxes
current_boxes = boxes[mask, :].copy()
# take only matching gt labels
current_labels = labels[mask]
# should we use the box left and top corner or the crop's
current_boxes[:, :2] = np.maximum(current_boxes[:, :2], rect[:2])
# adjust to crop (by substracting crop's left,top)
current_boxes[:, :2] -= rect[:2]
current_boxes[:, 2:] = np.minimum(current_boxes[:, 2:], rect[2:])
# adjust to crop (by substracting crop's left,top)
current_boxes[:, 2:] -= rect[:2]
data["image"] = current_image
data["box_labels"] = current_labels
data["box_coordinates"] = current_boxes
return data
return data