{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%reload_ext autoreload\n", "%autoreload 2\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "from nb_104 import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path = Path('data/camvid')\n", "path.ls()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path_lbl = path/'labels'\n", "path_img = path/'images'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fnames = get_image_files(path_img)\n", "fnames[:3]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lbl_names = get_image_files(path_lbl)\n", "lbl_names[:3]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "img_f = fnames[0]\n", "img = open_image(img_f)\n", "img.show(figsize=(5,5))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "get_y_fn = lambda x: path_lbl/f'{x.stem}_P{x.suffix}'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mask = open_mask(get_y_fn(img_f))\n", "mask.show(figsize=(5,5), alpha=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mask.shape,mask.data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "codes = np.loadtxt(path/'codes.txt', dtype=str); codes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Datasets" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "size=640\n", "bs=4" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = (ImageFileList.from_folder(path_img)\n", " .label_from_func(get_y_fn)\n", " .split_by_fname_file('../valid.txt')\n", " .datasets(SegmentationDataset, classes=codes)\n", " .transform(get_transforms(), size=size, tfm_y=True)\n", " .databunch(bs=bs)\n", " .normalize(imagenet_stats))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x,y = data.train_dl.one_batch()\n", "show_xy_images(data.denorm(x.cpu()),y,rows=2, figsize=(7,7))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x.shape,y.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Model" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "name2id = {v:k for k,v in enumerate(codes)}\n", "void_code = name2id['Void']\n", "\n", "def acc_camvid(input, target):\n", " target = target.squeeze()\n", " mask = target != void_code\n", " return (input.argmax(dim=1)[mask]==target[mask]).float().mean()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "metrics=acc_camvid\n", "# metrics=accuracy" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from fastai.vision.learner import cnn_config" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "@classmethod\n", "def Learner_create_unet(cls, data:DataBunch, arch:Callable, pretrained:bool=True,\n", " split_on:Optional[SplitFuncOrIdxList]=None, **kwargs:Any)->None:\n", " \"Build Unet learners.\"\n", " meta = cnn_config(arch)\n", " body = create_body(arch(pretrained), meta['cut'])\n", " model = models.unet.DynamicUnet(body, n_classes=data.c).cuda()\n", " learn = cls(data, model, **kwargs)\n", " learn.split(ifnone(split_on,meta['split']))\n", " if pretrained: learn.freeze()\n", " apply_init(model[2], nn.init.kaiming_normal_)\n", " return learn\n", "\n", "Learner.create_unet = Learner_create_unet" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn = Learner.create_unet(data, models.resnet34, metrics=metrics)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lr_find(learn)\n", "learn.recorder.plot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lr = 1e-2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn.fit_one_cycle(5, slice(lr))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn.save('stage-1')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn.load('stage-1')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn.unfreeze()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lr_find(learn)\n", "learn.recorder.plot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn.fit_one_cycle(12, slice(1e-5,lr/5))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn.save('stage-2')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn.load('stage-2')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn.fit_one_cycle(24, slice(1e-5,lr/15))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn.save('stage-3')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn.fit_one_cycle(12, slice(1e-6,lr/50), wd=1e-6)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }