{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%reload_ext autoreload\n", "%autoreload 2\n", "\n", "\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Install FastAI\n", "\n", "https://github.com/fastai/fastai\n", "\n", "In short, `conda activate` the python environement, and run: \n", "```bash\n", "conda install -c fastai -c pytorch -c anaconda fastai gh anaconda\n", "```\n", "\n", "Note: For more details about the modelling approach, please refer to: \n", "https://github.com/fastai/fastbook/blob/master/01_intro.ipynb" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from fastai.vision.all import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path = untar_data(URLs.CAMVID_TINY)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def acc_camvid(inp, targ, void_code=0):\n", " targ = targ.squeeze(1)\n", " mask = targ != void_code\n", " return (inp.argmax(dim=1)[mask] == targ[mask]).float().mean()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_y(o, path=path):\n", " return path / \"labels\" / f\"{o.stem}_P{o.suffix}\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dls = SegmentationDataLoaders.from_label_func(\n", " path,\n", " bs=8,\n", " fnames=get_image_files(path / \"images\"),\n", " label_func=get_y,\n", " codes=np.loadtxt(path / \"codes.txt\", dtype=str),\n", ")\n", "dls.one_batch()[0].shape[-2:], get_c(dls)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn = unet_learner(dls, resnet50, metrics=acc_camvid)\n", "learn.fine_tune(20)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn.show_results(max_n=6, figsize=(7,8))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn.export(\"./fastai_unet.pkl\")" ] } ], "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.8.5" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "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": 4 }