Multi bounding box labels

I want to know if it is possible to label an image with multiple bounding box labels with or without class. I know with fastai v0.7 this was possible to create a data object with “ImageClassifierData” but I don’t find any similar to do that in fastai v1.

In examples and docs just take the largest bbox to label and to create the item classifier.

The example here (scroll a bit for object detection) uses multiple bounding boxes.


I have a doubt about the “get_y_func”. I don’t get my images with COCO JSON format, but I have created a dictionary with the same output format that “img2bbox” from a df.

{'1.tif': [[[67, 110, 99, 142]], ['pool']],
 '2.tif': [[[63, 150, 95, 182]], ['pool']],
 '3.tif': [[[179, 47, 211, 79], [72, 135, 104, 167]], ['pool', 'pool']],
 '4.tif': [[[126, 76, 158, 108], [58, 200, 90, 232]], ['pool', 'pool']],
 '5.tif': [[[79, 65, 111, 97], [27, 93, 59, 125], [-14, 148, 18, 180]],
  ['pool', 'pool', 'pool']],
 '6.tif': [[[47, 7, 79, 39], [6, 62, 38, 94]], ['pool', 'pool']], ...}

I don’t know how to do a function to label the ObjectItemList

It is solved. I didn’t understand exactly what was ‘o’.

Thanks for the example!

1 Like

How did you get this to work?

What was ‘o’?

Were you doing multi-label or were you doing object labelling?


‘o’ is just the name of the variable. This could be ‘o’ or whatever you want, the most important is know what exactly is, in our case, the element of the dictionary.

So, in fastai doc, show this example:

img2bbox = dict(zip(images, lbl_bbox))
get_y_func = lambda o:img2bbox[]

That is, from a dictionary with the correct format (Name: label_bbox), the function ‘get_y_func’ take the name of the element to search it into the dictionary, so you should find the way to take the name of the image.

Note you can get what ‘o’ exactly is:

get_y_func = lambda o:img2bbox[os.path.basename(o)]
data = (ObjectItemList.from_folder(coco).split_by_rand_pct().label_from_func(get_y_func)

This show an error with ‘o’ ( in my case is a PosixPath)

So, in my case, I did a pandas DF like this:

    fn	    bbox	            class	lbl_bbox
0	1.tif	[[54, 63, 86, 95]]	[pool]	[[[54, 63, 86, 95]], [pool]]
1	2.tif	[[85, 16, 117, 48]]	[pool]	[[[85, 16, 117, 48]], [pool]]

so, my function is:

img2bbox = dict(zip(bboxes_df.fn, bboxes_df['lbl_bbox'])) 
get_y_func = lambda o:img2bbox[os.path.basename(o)]

Thanks @almarca!

Is the notebook for your code available for me to look at by any chance?

The old link above is giving me a 404 now, but I was able to find the examples here: Data block tutorial | fastai