def rssd_predictions_to_ship_mask(self, boxes, labels): boxes, scores = self.box_coder.decode(boxes, labels) mask = np.zeros( self.image_size, dtype=np.uint16 ) # Just in case we have more than 255 ships in one image for i, bbox in enumerate(boxes): visualize_rbbox(mask, bbox, (i + 1, i + 1, i + 1), thickness=cv2.FILLED) return mask
def test_draw_sersdd_bboxes(): box_coder = RRNBoxCoder(768, 768) anchors = box_coder._get_anchor_wht() n = len(box_coder.anchor_boxes) print('Total number of anchor boxes in SERSSDBoxCoder', len(box_coder.anchor_boxes)) for i, (wht_fm, fm_size) in enumerate(zip(anchors, box_coder.feature_map_sizes)): image = np.zeros((box_coder.image_height, box_coder.image_width, 3), dtype=np.uint8) grid_size = box_coder.input_size / fm_size fm_w, fm_h = int(fm_size[0]), int(fm_size[1]) xy = meshgrid_numpy(fm_w, fm_h).astype(np.float32) + 0.5 xy = (xy * grid_size).reshape(-1, 2) for p in xy: p = p.astype(int) cv2.circle(image, tuple(p), 2, (200, 200, 200), cv2.FILLED) for wht in wht_fm: a = math.sqrt(wht[0] * wht[1]) w = int(a) h = int(a) cv2.rectangle(image, (p[0] - w // 2, p[1] - h // 2), (p[0] + w // 2, p[1] + h // 2), (100, 100, 100)) # rbox = [p[0], p[1], wht[0], wht[1], wht[2]] # visualize_rbbox(image, rbox, (i * 28, 255, 0), thickness=1) for wht in wht_fm: rbox = [ box_coder.image_height // 2, box_coder.image_width // 2, wht[0], wht[1], wht[2] ] visualize_rbbox(image, rbox, (i * 28, 255, 0), thickness=1) cv2.imshow("Image" + str(i), image) cv2.waitKey(-1)
def predict_as_csv(self, dataset, batch_size=1, workers=0): self.eval() dataloader = DataLoader(dataset, batch_size=batch_size, num_workers=workers) image_ids = [] rles = [] for image, y in tqdm(dataloader, total=len(dataloader)): image = image.cuda(non_blocking=True) pred_boxes, pred_labels = self(image) for image_id, boxes, scores in zip(y[ID_KEY], to_numpy(pred_boxes), to_numpy(pred_labels)): boxes, _, scores = dataset.box_coder.decode(boxes, scores) if len(boxes): mask = np.zeros((768, 768), dtype=np.uint8) # First, we resterize all masks for i, rbox in enumerate(boxes): visualize_rbbox(mask, rbox, color=(i + 1, i + 1, i + 1), thickness=cv2.FILLED) # Second, we do rle encoding. This prevents assigning same pixel to multiple instances for i, rbox in enumerate(boxes): rle = rle_encode(mask == (i + 1)) image_ids.append(image_id) rles.append(rle) else: image_ids.append(image_id) rles.append(None) return pd.DataFrame.from_dict({ 'ImageId': image_ids, 'EncodedPixels': rles })
def test_rssd_encoding(): id = 'dc6adaf6b.jpg' groundtruth = pd.read_csv( os.path.join(data_dir, 'train_ship_segmentations_v2.csv')) image = read_train_image(id, data_dir) rle = get_rle_from_groundtruth(groundtruth, id) label_image = rle2instancemask(rle) # Test what happens if we rotate # image = np.rot90(image).copy() # label_image = np.rot90(label_image).copy() rbboxes = instance_mask_to_rbboxes(label_image) print('Instances', rbboxes) labels = np.zeros(len(rbboxes), dtype=np.intp) box_coder = RSSDBoxCoder(768, 778) loc_targets, cls_targets = box_coder.encode(rbboxes, labels) print(loc_targets.shape, cls_targets.shape) cls_targets_one_hot = np.eye(2)[cls_targets] print(cls_targets_one_hot.shape) dec_boxes, dec_labels, dec_scores = box_coder.decode( loc_targets, cls_targets_one_hot) print(dec_boxes) for bbox in dec_boxes: visualize_rbbox(image, bbox, (255, 0, 0), thickness=3) for bbox in rbboxes: visualize_rbbox(image, bbox, (0, 255, 0), thickness=1) cv2.imshow('a', image) cv2.waitKey(-1)
def test_draw_rsdd_bboxes(): box_coder = RSSDBoxCoder(768, 768) anchors = box_coder._get_anchor_wht() n = len(FPNSSDBoxCoder().anchor_boxes) print('Total number of anchor boxes SSD', len(box_coder.anchor_boxes)) n = len(box_coder.anchor_boxes) print('Total number of anchor boxes in RSSD', len(box_coder.anchor_boxes)) for i, wht_fm in enumerate(anchors): image = np.zeros((box_coder.image_height, box_coder.image_width, 3), dtype=np.uint8) for wht in wht_fm: rbox = [ box_coder.image_height // 2, box_coder.image_width // 2, wht[0], wht[1], wht[2] ] visualize_rbbox(image, rbox, (i * 28, 255, 0), thickness=1) cv2.imshow("Image" + str(i), image) cv2.waitKey(-1)
def test_mask(): # id = 'aed55df18.jpg' id = 'dc6adaf6b.jpg' groundtruth = pd.read_csv( os.path.join(data_dir, 'train_ship_segmentations_v2.csv')) image = read_train_image(id, data_dir) rle = get_rle_from_groundtruth(groundtruth, id) label_image = rle2instancemask(rle) mask = instance_mask_to_fg_and_edge(label_image) dummy = np.zeros((label_image.shape[0], label_image.shape[1], 1), dtype=np.uint8) mask = np.concatenate([mask * 255, dummy], axis=2) cv2.imshow('mask (no edge)', (label_image > 0).astype(np.uint8) * 255) cv2.imshow('mask', mask) # cv2.waitKey(-1) rbboxes = instance_mask_to_rbboxes(label_image) for bbox in rbboxes: visualize_rbbox(image, bbox, (0, 255, 0)) cv2.imshow('a', image) cv2.waitKey(-1)
def test_rrssd_box_coder_synthetic(): label_image = np.zeros((768, 768), dtype=np.uint8) cv2.fillConvexPoly( label_image, np.expand_dims(cv2.boxPoints(((100, 100), (100, 20), 0)), 1).astype(int), (1, 1, 1)) cv2.fillConvexPoly( label_image, np.expand_dims(cv2.boxPoints(((200, 100), (100, 20), 45)), 1).astype(int), (2, 2, 2)) cv2.fillConvexPoly( label_image, np.expand_dims(cv2.boxPoints(((100, 200), (100, 20), 90)), 1).astype(int), (3, 3, 3)) cv2.fillConvexPoly( label_image, np.expand_dims(cv2.boxPoints(((200, 200), (100, 20), 135)), 1).astype(int), (4, 4, 4)) cv2.fillConvexPoly( label_image, np.expand_dims(cv2.boxPoints(((100 + 200, 100), (20, 100), 0)), 1).astype(int), (5, 5, 5)) cv2.fillConvexPoly( label_image, np.expand_dims(cv2.boxPoints(((200 + 200, 100), (20, 100), 45)), 1).astype(int), (6, 6, 6)) cv2.fillConvexPoly( label_image, np.expand_dims(cv2.boxPoints(((100 + 200, 200), (20, 100), 90)), 1).astype(int), (7, 7, 7)) cv2.fillConvexPoly( label_image, np.expand_dims(cv2.boxPoints(((200 + 200, 200), (20, 100), 135)), 1).astype(int), (8, 8, 8)) cv2.fillConvexPoly( label_image, np.expand_dims(cv2.boxPoints(((100, 105 + 200), (100, 20), 45. / 2)), 1).astype(int), (9, 9, 9)) cv2.fillConvexPoly( label_image, np.expand_dims(cv2.boxPoints(((200, 100 + 200), (16, 4), 49)), 1).astype(int), (10, 10, 10)) cv2.fillConvexPoly( label_image, np.expand_dims(cv2.boxPoints(((200, 100 + 210), (100, 20), 49)), 1).astype(int), (11, 11, 11)) cv2.fillConvexPoly( label_image, np.expand_dims(cv2.boxPoints(((200, 200 + 200), (100, 20), 165)), 1).astype(int), (12, 12, 12)) cv2.fillConvexPoly( label_image, np.expand_dims(cv2.boxPoints(((300, 300), (10, 6), 49)), 1).astype(int), (13, 13, 13)) cv2.fillConvexPoly( label_image, np.expand_dims(cv2.boxPoints(((300 + 50, 300), (200, 40), 90)), 1).astype(int), (14, 14, 14)) cv2.fillConvexPoly( label_image, np.expand_dims(cv2.boxPoints(((300 + 70, 300), (100, 20), 90)), 1).astype(int), (15, 15, 15)) cv2.fillConvexPoly( label_image, np.expand_dims(cv2.boxPoints(((500, 500), (2, 3), 9)), 1).astype(int), (16, 16, 16)) cv2.fillConvexPoly( label_image, np.expand_dims(cv2.boxPoints(((510, 500), (2, 3), 19)), 1).astype(int), (17, 17, 17)) cv2.fillConvexPoly( label_image, np.expand_dims(cv2.boxPoints(((540, 500), (4, 6), 29)), 1).astype(int), (18, 18, 18)) cv2.fillConvexPoly( label_image, np.expand_dims(cv2.boxPoints(((560, 500), (2, 3), 39)), 1).astype(int), (19, 19, 19)) image = (label2rgb(label_image, bg_label=0) * 255).astype(np.uint8) # Test what happens if we rotate # image = np.rot90(image).copy() # label_image = np.rot90(label_image).copy() rbboxes = instance_mask_to_rbboxes(label_image) print(rbboxes) labels = np.zeros(len(rbboxes), dtype=np.intp) box_coder = RRNBoxCoder(768, 768) loc_targets, cls_targets, anchors = box_coder.encode([], [], return_anchors=True) loc_targets, cls_targets, anchors = box_coder.encode(rbboxes, labels, return_anchors=True) print(loc_targets.shape, cls_targets.shape, anchors.shape) print('Object anchors', (cls_targets > 0).sum()) print('Background anchors', (cls_targets == 0).sum()) print('Ignore anchors', (cls_targets == -1).sum()) # cls_targets = np.expand_dims(cls_targets) # print(cls_targets_one_hot.shape) dec_boxes, dec_scores = box_coder.decode(loc_targets, cls_targets) print(dec_boxes) print('Total anchors', len(anchors)) for bbox in dec_boxes: visualize_rbbox(image, bbox, (255, 0, 255), thickness=3) for bbox in rbboxes: visualize_rbbox(image, bbox, (0, 255, 0), thickness=1) for bbox in anchors: visualize_rbbox(image, bbox, (255, 255, 255), thickness=1) cv2.imshow('overlays', image) cv2.waitKey(-1)