Open Image v5 Dataset

In [ ]:
%matplotlib inline
%reload_ext autoreload
%autoreload 2
In [ ]:
from fastai.vision import *
from fastprogress import progress_bar
In [ ]:
!wget https://open-images-dataset.s3.amazonaws.com/tar/validation.tar.gz -P {Config.data_path()}
!tar -xvf {Config.data_path()}/validation.tar.gz
!mv {Config.data_path()}/validation  {Config.data_path()}/openimagesv5
!wget https://storage.googleapis.com/openimages/v5/validation-annotations-bbox.csv -P {Config.data_path()}/openimagesv5
In [ ]:
path = Config.data_path() / 'openimagesv5'
labels = pd.read_csv(path/'validation-annotations-bbox.csv')
labels['ImageID'] = labels['ImageID']+'.jpg'  
classes = pd.read_csv(path/'classes.csv', names=['ID', 'Name'])
classes.head()
In [ ]:
labels.head()

Get the real names of the classes

In [ ]:
for i in classes['ID'].unique():
    labels.loc[labels['LabelName']==i, 'LabelName'] = classes[classes['ID'] == i]['Name'].iloc[0]
labels.head()

fastai requires the x, y points to be scaled to the image

In [ ]:
files = [path/x for x in labels['ImageID'].unique().tolist()]
open_image(files[0]).px.shape
In [ ]:
w,h=1024,447
In [ ]:
labels['XMin'] *= w
labels['XMax'] *= w
labels['YMin'] *= h
labels['YMax'] *= h
In [ ]:
labels.head()
In [ ]:
ls = []

for i in progress_bar(files):
    id = Path(i).stem + '.jpg'
    boxes = labels[labels['ImageID']==id]
    # top, left, bottom, right
    bounds = boxes[['YMin', 'XMin', 'YMax', 'XMax']].values.tolist()
    l =  boxes[['LabelName']].values.tolist()
    l = [x[0] for x in l]
    ls.append((bounds, l))

ls = dict(zip(files, ls))

def labeller(x):    
    return ls[x]
In [ ]:
data = ObjectItemList(files)
ldata = (data.split_by_rand_pct(0.2, seed=2).label_from_func(labeller))
In [ ]:
data = ldata.transform(size=224, tfm_y=True).databunch(bs=2, collate_fn=bb_pad_collate)
In [ ]:
data.show_batch(rows=3, figsize=(9,9))

Basic Example Usage

coco = untar_data(URLs.COCO_TINY)
images, lbl_bbox = get_annotations(coco/'train.json')
img2bbox = dict(list(zip(images, lbl_bbox)))
get_y_func = lambda o:img2bbox[o.name]

data = (ObjectItemList.from_folder(coco)
        #Where are the images? -> in coco and its subfolders
        .split_by_rand_pct()                          
        #How to split in train/valid? -> randomly with the default 20% in valid
        .label_from_func(get_y_func)
        #How to find the labels? -> use get_y_func on the file name of the data
        .transform(get_transforms(), tfm_y=True)
        #Data augmentation? -> Standard transforms; also transform the label images
        .databunch(bs=16, collate_fn=bb_pad_collate))   
        #Finally we convert to a DataBunch, use a batch size of 16,
        # and we use bb_pad_collate to collate the data into a mini-batch