Skip to content

Instantly share code, notes, and snippets.

@mogwai
Last active September 12, 2019 17:51
Show Gist options
  • Save mogwai/fc407b3f6e40494f0b4c1834793d690b to your computer and use it in GitHub Desktop.
Save mogwai/fc407b3f6e40494f0b4c1834793d690b to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Open Image v5 Dataset"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"%reload_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from fastai.vision import *\n",
"from fastprogress import progress_bar"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!wget https://open-images-dataset.s3.amazonaws.com/tar/validation.tar.gz -P {Config.data_path()}\n",
"!tar -xvf {Config.data_path()}/validation.tar.gz\n",
"!mv {Config.data_path()}/validation {Config.data_path()}/openimagesv5\n",
"!wget https://storage.googleapis.com/openimages/v5/validation-annotations-bbox.csv -P {Config.data_path()}/openimagesv5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"path = Config.data_path() / 'openimagesv5'\n",
"labels = pd.read_csv(path/'validation-annotations-bbox.csv')\n",
"labels['ImageID'] = labels['ImageID']+'.jpg' \n",
"classes = pd.read_csv(path/'classes.csv', names=['ID', 'Name'])\n",
"classes.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"labels.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get the real names of the classes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in classes['ID'].unique():\n",
" labels.loc[labels['LabelName']==i, 'LabelName'] = classes[classes['ID'] == i]['Name'].iloc[0]\n",
"labels.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"fastai requires the x, y points to be scaled to the image"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"files = [path/x for x in labels['ImageID'].unique().tolist()]\n",
"open_image(files[0]).px.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"w,h=1024,447"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"labels['XMin'] *= w\n",
"labels['XMax'] *= w\n",
"labels['YMin'] *= h\n",
"labels['YMax'] *= h"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"labels.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"ls = []\n",
"\n",
"for i in progress_bar(files):\n",
" id = Path(i).stem + '.jpg'\n",
" boxes = labels[labels['ImageID']==id]\n",
" # top, left, bottom, right\n",
" bounds = boxes[['YMin', 'XMin', 'YMax', 'XMax']].values.tolist()\n",
" l = boxes[['LabelName']].values.tolist()\n",
" l = [x[0] for x in l]\n",
" ls.append((bounds, l))\n",
"\n",
"ls = dict(zip(files, ls))\n",
"\n",
"def labeller(x): \n",
" return ls[x]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"data = ObjectItemList(files)\n",
"ldata = (data.split_by_rand_pct(0.2, seed=2).label_from_func(labeller))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = ldata.transform(size=224, tfm_y=True).databunch(bs=2, collate_fn=bb_pad_collate)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data.show_batch(rows=3, figsize=(9,9))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Basic Example Usage\n",
"\n",
"```py\n",
"coco = untar_data(URLs.COCO_TINY)\n",
"images, lbl_bbox = get_annotations(coco/'train.json')\n",
"img2bbox = dict(list(zip(images, lbl_bbox)))\n",
"get_y_func = lambda o:img2bbox[o.name]\n",
"\n",
"data = (ObjectItemList.from_folder(coco)\n",
" #Where are the images? -> in coco and its subfolders\n",
" .split_by_rand_pct() \n",
" #How to split in train/valid? -> randomly with the default 20% in valid\n",
" .label_from_func(get_y_func)\n",
" #How to find the labels? -> use get_y_func on the file name of the data\n",
" .transform(get_transforms(), tfm_y=True)\n",
" #Data augmentation? -> Standard transforms; also transform the label images\n",
" .databunch(bs=16, collate_fn=bb_pad_collate)) \n",
" #Finally we convert to a DataBunch, use a batch size of 16,\n",
" # and we use bb_pad_collate to collate the data into a mini-batch\n",
"```"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment