{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Semantic Segmentation with Fastai\n", "Semantic Segmentation means the pixel wise segmentation of an image in different classes.\n", "Here we use the fastai lib, with which it is very easy to learn such models, among others.\n", "\n", "We use the [Camvid](http://mi.eng.cam.ac.uk/research/projects/VideoRec/CamVid/) dataset." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# import fastai lib\n", "from fastai.vision.all import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# get dataset and untar\n", "path = untar_data(URLs.CAMVID_TINY)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create fastai dataloader\n", "dls = SegmentationDataLoaders.from_label_func(\n", " path, bs=8, fnames = get_image_files(path/\"images\"),\n", " label_func = lambda o: path/'labels'/f'{o.stem}_P{o.suffix}',\n", " codes = np.loadtxt(path/'codes.txt', dtype=str)\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create learner object\n", "learn = unet_learner(dls, resnet34)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# fit model\n", "learn.fine_tune(8)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# show learning results\n", "learn.show_results(max_n=4, figsize=(20,30))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# fit a bit more\n", "learn.fine_tune(10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# show results again\n", "learn.show_results(max_n=4, figsize=(20,30))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.10" } }, "nbformat": 4, "nbformat_minor": 4 }