in detic/modeling/debug.py [0:0]
def debug_second_stage(images, instances, proposals=None, vis_thresh=0.3,
save_debug=False, debug_show_name=False, image_labels=[],
save_debug_path='output/save_debug/',
bgr=False):
images = _imagelist_to_tensor(images)
if 'COCO' in save_debug_path:
from detectron2.data.datasets.builtin_meta import COCO_CATEGORIES
cat2name = [x['name'] for x in COCO_CATEGORIES]
else:
from detectron2.data.datasets.lvis_v1_categories import LVIS_CATEGORIES
cat2name = ['({}){}'.format(x['frequency'], x['name']) \
for x in LVIS_CATEGORIES]
for i in range(len(images)):
image = images[i].detach().cpu().numpy().transpose(1, 2, 0).astype(np.uint8).copy()
if bgr:
image = image[:, :, ::-1].copy()
if instances[i].has('gt_boxes'):
bboxes = instances[i].gt_boxes.tensor.cpu().numpy()
scores = np.ones(bboxes.shape[0])
cats = instances[i].gt_classes.cpu().numpy()
else:
bboxes = instances[i].pred_boxes.tensor.cpu().numpy()
scores = instances[i].scores.cpu().numpy()
cats = instances[i].pred_classes.cpu().numpy()
for j in range(len(bboxes)):
if scores[j] > vis_thresh:
bbox = bboxes[j]
cl = COLORS[cats[j], 0, 0]
cl = (int(cl[0]), int(cl[1]), int(cl[2]))
cv2.rectangle(
image,
(int(bbox[0]), int(bbox[1])),
(int(bbox[2]), int(bbox[3])),
cl, 2, cv2.LINE_AA)
if debug_show_name:
cat = cats[j]
txt = '{}{:.1f}'.format(
cat2name[cat] if cat > 0 else '',
scores[j])
font = cv2.FONT_HERSHEY_SIMPLEX
cat_size = cv2.getTextSize(txt, font, 0.5, 2)[0]
cv2.rectangle(
image,
(int(bbox[0]), int(bbox[1] - cat_size[1] - 2)),
(int(bbox[0] + cat_size[0]), int(bbox[1] - 2)),
(int(cl[0]), int(cl[1]), int(cl[2])), -1)
cv2.putText(
image, txt, (int(bbox[0]), int(bbox[1] - 2)),
font, 0.5, (0, 0, 0), thickness=1, lineType=cv2.LINE_AA)
if proposals is not None:
proposal_image = images[i].detach().cpu().numpy().transpose(1, 2, 0).astype(np.uint8).copy()
if bgr:
proposal_image = proposal_image.copy()
else:
proposal_image = proposal_image[:, :, ::-1].copy()
bboxes = proposals[i].proposal_boxes.tensor.cpu().numpy()
if proposals[i].has('scores'):
scores = proposals[i].scores.detach().cpu().numpy()
else:
scores = proposals[i].objectness_logits.detach().cpu().numpy()
# selected = -1
# if proposals[i].has('image_loss'):
# selected = proposals[i].image_loss.argmin()
if proposals[i].has('selected'):
selected = proposals[i].selected
else:
selected = [-1 for _ in range(len(bboxes))]
for j in range(len(bboxes)):
if scores[j] > vis_thresh or selected[j] >= 0:
bbox = bboxes[j]
cl = (209, 159, 83)
th = 2
if selected[j] >= 0:
cl = (0, 0, 0xa4)
th = 4
cv2.rectangle(
proposal_image,
(int(bbox[0]), int(bbox[1])),
(int(bbox[2]), int(bbox[3])),
cl, th, cv2.LINE_AA)
if selected[j] >= 0 and debug_show_name:
cat = selected[j].item()
txt = '{}'.format(cat2name[cat])
font = cv2.FONT_HERSHEY_SIMPLEX
cat_size = cv2.getTextSize(txt, font, 0.5, 2)[0]
cv2.rectangle(
proposal_image,
(int(bbox[0]), int(bbox[1] - cat_size[1] - 2)),
(int(bbox[0] + cat_size[0]), int(bbox[1] - 2)),
(int(cl[0]), int(cl[1]), int(cl[2])), -1)
cv2.putText(
proposal_image, txt,
(int(bbox[0]), int(bbox[1] - 2)),
font, 0.5, (0, 0, 0), thickness=1,
lineType=cv2.LINE_AA)
if save_debug:
global cnt
cnt = (cnt + 1) % 5000
if not os.path.exists(save_debug_path):
os.mkdir(save_debug_path)
save_name = '{}/{:05d}.jpg'.format(save_debug_path, cnt)
if i < len(image_labels):
image_label = image_labels[i]
save_name = '{}/{:05d}'.format(save_debug_path, cnt)
for x in image_label:
class_name = cat2name[x]
save_name = save_name + '|{}'.format(class_name)
save_name = save_name + '.jpg'
cv2.imwrite(save_name, proposal_image)
else:
cv2.imshow('image', image)
if proposals is not None:
cv2.imshow('proposals', proposal_image)
cv2.waitKey()