如何从生成的目标可视化掩码和bboxes

2024-04-24 22:25:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在遵循这个官方指南https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html,并希望可视化bboxes和地面真相面具。我该怎么做?你知道吗

目标看起来是这样的:

{‘boxes’: tensor([[849., 525., 959., 635.],
[581., 659., 737., 867.],
[890., 734., 959., 813.],
[632., 839., 855., 945.]]),
‘labels’: tensor([1, 1, 1, 1]),
‘masks’: tensor([[[0, 0, 0, …, 0, 0, 0],
[0, 0, 0, …, 0, 0, 0],
[0, 0, 0, …, 0, 0, 0],
…,
[0, 0, 0, …, 0, 0, 0],
[0, 0, 0, …, 0, 0, 0],
[0, 0, 0, …, 0, 0, 0]],], dtype=torch.uint8),
‘image_id’: tensor([1]),
‘area’: tensor([12100., 32448., 5451., 23638.]),
‘iscrowd’: tensor([0, 0, 0, 0])}

由二值图像生成:

obj_ids = np.unique(mask)
# first id is the background, so remove it
obj_ids = obj_ids[1:]

# split the color-encoded mask into a set
# of binary masks
masks = mask == obj_ids[:, None, None]

num_objs = len(obj_ids)
boxes = []
for i in range(num_objs):
    pos = np.where(masks[i])
    xmin = np.min(pos[1])
    xmax = np.max(pos[1])
    ymin = np.min(pos[0])
    ymax = np.max(pos[0])
    boxes.append([xmin, ymin, xmax, ymax])

idx =1
# convert everything into a torch.Tensor
boxes = torch.as_tensor(boxes, dtype=torch.float32)
# there is only one class
labels = torch.ones((num_objs,), dtype=torch.int64)
masks = torch.as_tensor(np.uint8(masks), dtype=torch.uint8)

image_id = torch.tensor([idx])
area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])
# suppose all instances are not crowd
iscrowd = torch.zeros((num_objs,), dtype=torch.int64)

target = {}
target["boxes"] = boxes
target["labels"] = labels
target["masks"] = masks
target["image_id"] = image_id
target["area"] = area
target["iscrowd"] = iscrowd

Tags: posimageidobjidstargetlabelsnp