API details.
import random
import numpy as np
from detection_nbdev.utils import random_bbox, visualize
bboxA = random_bbox()
bboxB = random_bbox()
iou = bbox_iou(bboxA, bboxB)
print('label:', bboxA)
print('bbox:', bboxB)
print('IoU:', iou)
visualize(
np.zeros((224,224,3)),
[bboxA, bboxB],
[0, 1],
{0:'bboxA', 1:'bboxB'},
)
import numpy as np
import albumentations as A
from albumentations.pytorch.transforms import ToTensorV2
from torchvision.transforms import ToTensor, Compose, ToPILImage, Normalize
from detection_nbdev.dataset import XMLDetectionDataModule, XMLDetectionDataset
# Setup a model
modelname = 'fasterrcnn_resnet50_fpn'
model = TorchVisionDetector(modelname, pretrained=True)
# Prepare data
data_dir = '../detection/data/'
image_transform = Compose([
ToTensor(),
lambda x: x.unsqueeze(0)])
dataset = XMLDetectionDataset(root='../detection/data/train', image_transform=image_transform, target_transform=None, transform=None)
img, label = dataset[0]
model.eval()
pred = model(img)
bbox = pred[0][0].detach().numpy()
iou = bbox_iou(label, bbox)
print('label:', label)
print('bbox:', bbox)
print('IoU:', iou)
visualize(
img.detach().numpy()[0].transpose(1,2,0),
[bbox.astype(int), label],
[0, 1],
{0:'bboxA', 1:'bboxB'},
)
# visualize(
# img.detach().numpy().transpose(1,2,0),
# [bbox,label],
# [0, 1],
# {0:'prediction', 1:'label'},
# )
dm = XMLDetectionDataModule(
data_dir,
image_transform=ToTensor())
dm.setup(mode='use_dir')
trainer = pl.Trainer(gpus=1, checkpoint_callback=False)
trainer.test(model, dm.test_dataloader())
# test_eq(trainer.fit(model, dm), 1)