{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Source: https://course.fast.ai/Lessons/lesson2.html" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "# #hide\n", "! [ -e /content ] && mamba install -Uqq fastbook\n", "import fastbook\n", "fastbook.setup_book()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from fastbook import *\n", "from fastai.vision.widgets import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ims = search_images_ddg('gingko biloba tree leaf', max_images=10)\n", "len(ims)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dest = 'images/tree.jpg'\n", "download_url(ims[0], dest)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "im = Image.open(dest)\n", "im.to_thumb(128,128)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "categories = ('ash', 'chestnut', 'ginkgo biloba', 'silver maple', 'willow oak')\n", "path = Path('trees')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if not path.exists():\n", " path.mkdir()\n", " for o in categories:\n", " dest = (path/o)\n", " dest.mkdir(exist_ok=True)\n", " results = search_images_ddg(f'{o} tree leaf', max_images=100)\n", " download_images(dest, urls=results)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fns = get_image_files(path)\n", "fns" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "failed = verify_images(fns)\n", "failed" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "failed.map(Path.unlink);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## From Data to DataLoaders" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "trees = DataBlock(blocks=(ImageBlock, CategoryBlock),\n", " get_items=get_image_files,\n", " splitter=RandomSplitter(valid_pct=0.2, seed=42),\n", " get_y=parent_label,\n", " item_tfms=Resize(128))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dls = trees.dataloaders(path)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dls.valid.show_batch(max_n=12, nrows=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Data Augmentation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "trees = trees.new(item_tfms=Resize(128), batch_tfms=aug_transforms(mult=2))\n", "dls = trees.dataloaders(path)\n", "dls.train.show_batch(max_n=8, nrows=2, unique=True)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Training Your Model, and Using It to Clean Your Data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "trees = trees.new(item_tfms=RandomResizedCrop(224, min_scale=0.5),\n", " batch_tfms=aug_transforms())\n", "dls = trees.dataloaders(path)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn = vision_learner(dls, resnet18, metrics=error_rate)\n", "learn.fine_tune(5)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "interp = ClassificationInterpretation.from_learner(learn)\n", "interp.plot_confusion_matrix()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "interp.plot_top_losses(8, nrows=2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cleaner = ImageClassifierCleaner(learn)\n", "cleaner" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#hide\n", "for idx in cleaner.delete(): cleaner.fns[idx].unlink()\n", "for idx,cat in cleaner.change(): shutil.move(str(cleaner.fns[idx]), path/cat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Turning Your Model into an Online Application" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using the Model for Inference" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn.export('model.pkl')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path = Path()\n", "path.ls(file_exts='.pkl')\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn_inf = load_learner(path/'model.pkl')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn_inf.predict('images/ash.jpg')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn_inf.dls.vocab" ] } ], "metadata": { "jupytext": { "split_at_heading": true }, "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.9.15" }, "vscode": { "interpreter": { "hash": "6dbec04a431f157b103533e84207c2f49dc4e813632efce31ad46431e6a7d537" } } }, "nbformat": 4, "nbformat_minor": 4 }