Converting batches to the required input of object detection models.
from fastai_object_detection.dataloaders import ObjectDetectionDataLoaders
from fastai_object_detection.datasets import CocoData
from fastai.vision.all import *
path,df = CocoData.get_path_df("ds-cats-dogs")
dls = ObjectDetectionDataLoaders.from_df(df, bs=9, item_tfms=[Resize(600)])
b = dls.one_batch()
dls.show_batch(b, figsize=(10,10))
[(t.shape, type(t)) for t in b]
adapter = ObjDetAdapter()
xb,yb = adapter.transform_batch(b[0], *b[1:])
num_objs = [t.item() for t in b[1].sum((-2,-1)).gt(0).sum(1)]
num_objs
for i,d in enumerate(yb[0]):
n1 = len(d["labels"])
n2,_ = d["boxes"].shape
n3,_,_ = d["masks"].shape
test_eq(n1,n2)
test_eq(n2,n3)
test_eq(n3,num_objs[i])
bbs = d["boxes"]
area = (bbs[:,2]-bbs[:,0])*(bbs[:,3]-bbs[:,1])
test_eq((area<=0).any(), False)
for i,b in enumerate(dls.train):
xb,yb = adapter.transform_batch(b[0], *b[1:])
num_objs = [t.item() for t in b[1].sum((-2,-1)).gt(0).sum(1)]
for i,d in enumerate(yb[0]):
n1 = len(d["labels"])
n2,_ = d["boxes"].shape
n3,_,_ = d["masks"].shape
test_eq(n1,n2)
test_eq(n2,n3)
test_eq(n1,num_objs[i])
bbs = d["boxes"]
area = (bbs[:,2]-bbs[:,0])*(bbs[:,3]-bbs[:,1])
test_eq((area<=0).any(), False)