{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%reload_ext autoreload\n", "%autoreload 2\n", "%matplotlib inline\n", "\n", "from fastai import *\n", "from fastai.vision import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tfms = get_transforms()\n", "bs = 64" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def show_image_batch_supercharged(dl:DataLoader, classes:Collection[str], rows:int=None, figsize:Tuple[int,int]=(12,15),\n", " denorm:Callable=None)->None:\n", " b_idx = next(iter(dl.batch_sampler))\n", " if rows is None: rows = int(math.sqrt(len(b_idx)))\n", " fig, axs = plt.subplots(rows,rows,figsize=figsize)\n", " for i, ax in zip(b_idx[:rows*rows], axs.flatten()):\n", " x,y = dl.dataset[i]\n", " x.show(ax=ax, y=y, classes=classes)\n", " plt.tight_layout()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def show_batch_supercharged(data:DataBunch, rows:int=3, figsize:Tuple[int,int]=(9,10), is_train:bool=True)->None:\n", " show_image_batch_supercharged(data.train_dl if is_train else data.valid_dl, data.classes,\n", " denorm=getattr(data,'denorm',None), figsize=figsize, rows=rows)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Pets" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path = untar_data(URLs.PETS)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = (ImageFileList.from_folder(path/'images')\n", " .label_from_re(r'^(.*)_\\d+.jpg$')\n", " .random_split_by_pct(0.2)\n", " .datasets(ImageClassificationDataset)\n", " .transform(tfms, size=224)\n", " .databunch(bs=bs, path=path))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = (ImageFileList.from_folder(path/'images')\n", " .label_from_re(r'^(.*)_\\d+.jpg$')\n", " .random_split_by_pct(0.2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data.show_batch(rows=3, figsize=(7,7))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "show_batch_supercharged(data, rows=3, figsize=(7,7))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Planet" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path = untar_data(URLs.PLANET_SAMPLE)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = (InputList.from_folder(path)\n", " .label_from_csv('labels.csv', sep=' ', suffix='.jpg', folder='train')\n", " .random_split_by_pct(0.2)\n", " .datasets(ImageMultiDataset)\n", " .transform(tfms, size=128)\n", " .databunch(bs=bs, path=path))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data.show_batch(rows=3, figsize=(10,10))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "show_batch_supercharged(data, rows=3, figsize=(10,10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Camvid" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path = untar_data(URLs.CAMVID)\n", "path_lbl = path/'labels'\n", "path_img = path/'images'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "get_y_fn = lambda x: path_lbl/f'{x.stem}_P{x.suffix}'\n", "codes = np.loadtxt(path/'codes.txt', dtype=str); codes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = (ImageFileList.from_folder(path_img)\n", " .label_from_func(get_y_fn)\n", " .random_split_by_pct()\n", " .datasets(SegmentationDataset, classes=codes)\n", " .transform(get_transforms(), size=128, tfm_y=True)\n", " .databunch(bs=64))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data.show_batch(rows=2, figsize=(7,7))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "show_batch_supercharged(data, rows=2, figsize=(7,7))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Coco" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path = untar_data(URLs.COCO_TINY)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "images, lbl_bbox = get_annotations(path/'train.json')\n", "img2bbox = {img:bb for img, bb in zip(images, lbl_bbox)}\n", "get_y_func = lambda o:img2bbox[o.name]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = (ImageFileList.from_folder(path)\n", " .label_from_func(get_y_func)\n", " .random_split_by_pct()\n", " .datasets(ObjectDetectDataset)\n", " .transform(tfms, size=128, tfm_y=True)\n", " .databunch(bs=16, collate_fn=bb_pad_collate))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data.show_batch(rows=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }