draw_bounding_box function of pytorch not completing bounding boxes

Viewed 14

I am working with torchvision.utils function draw_bounding_boxes to draw boxes on the image as a visualization. I confirmed that boxes fed to the function are of form (xmin, ymin, xmax, ymax), but the boxes produced lack lines on the boundary. See fig below:

Pascal VOC image

Can anyone please tell me what would be possible problem here. The code is as given below:

DATA_DIR = "../Downloads/" if not torch.cuda.is_available() else "../ssd/PascalVOC"
batch_size = 1
image_dim = (512, 512)  # height, width

if device == "cuda":
    datagen = VOCLoader(rootDir=DATA_DIR, target_transform=VOCAnnotationTransform,
                        imSize=image_dim, split='train', scale=True, falseSamplePercentage=100, 
                        random_flip=True, boxErrorPercentage=30, random_sampler=0)
    val_datagen = VOCLoader(rootDir=DATA_DIR, target_transform=VOCAnnotationTransform,
                        imSize=image_dim, split='val', scale=True, falseSamplePercentage=100, 
                        boxErrorPercentage=30, random_sampler=0)
    trainData = torch.utils.data.DataLoader(datagen, batch_size=batch_size, shuffle=True,
                                            collate_fn=collate_fn)
    valData = torch.utils.data.DataLoader(val_datagen, batch_size=batch_size, shuffle=True,
                                            collate_fn=collate_fn)
else: 
    datagen = VOCLoader(rootDir=DATA_DIR, target_transform=VOCAnnotationTransform,
                        imSize=image_dim, split='train', scale=True, falseSamplePercentage=50,
                        random_flip=True, random_sampler=0)
    val_datagen = VOCLoader(rootDir=DATA_DIR, target_transform=VOCAnnotationTransform,
                        imSize=image_dim, split='val', scale=True, falseSamplePercentage=50,
                        random_sampler=0)
    trainData = torch.utils.data.DataLoader(datagen, batch_size=batch_size, shuffle=False,
                                            collate_fn=collate_fn)
    valData = torch.utils.data.DataLoader(val_datagen, batch_size=batch_size, shuffle=False,
                                            collate_fn=collate_fn)

trainBar = tqdm.tqdm(trainData)
valBar = tqdm.tqdm(valData)
for batch, data in enumerate(valBar):
        
        image = data[0].to(device)
        
        # grid = make_grid(image.cpu())
        # show(grid)
        
        
        targetDict = data[1]
        target = targetDict['boxes']
        falseBoxes = targetDict['falseBoxes'].to(device)   
        falseBoxes_list = tensors_to_list(falseBoxes)
        pred_box, pred_var = model(image, falseBoxes_list)
        target = torch.concat([*target], dim=0).to(device)
        pred_coords = decode_pred_bbox_xyxy_xyxy(falseBoxes_list, pred_box, image_dim)
        variance = torch.exp(pred_var)
        image = de_normalize_img(image.squeeze().cpu())
        image = draw_bounding_boxes((image * 255).type(torch.uint8).squeeze(), target, 
                                    colors=(0, 255, 0))
        image = draw_bounding_boxes(image, pred_coords.type(torch.int), colors=(255, 0, 0))
        image = draw_bounding_boxes(image, falseBoxes[:, 1:], colors=(0, 255, 255))
        grid = make_grid(image)
        show(grid, f"Results/image{batch}.png")
        print("Done")

The function show()just converts the tensor to numpy and uses matplotlib to save it in a file.

1 Answers

Sorry, the problem was with the show() function which used plt.imshow() with argument interpolation="nearest". If I do not pass any extra arguments other than the image. Then it is displaying correctly.

Pascal Voc Image

Related