{"id":21502449,"url":"https://github.com/eagerai/fastai","last_synced_at":"2025-04-12T20:45:37.416Z","repository":{"id":38301091,"uuid":"284675404","full_name":"EagerAI/fastai","owner":"EagerAI","description":"R interface to fast.ai","archived":false,"fork":false,"pushed_at":"2025-02-15T10:52:27.000Z","size":72398,"stargazers_count":118,"open_issues_count":4,"forks_count":13,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-03T22:11:15.766Z","etag":null,"topics":["audio","collaborative-filtering","darknet","darknet-image-classification","fastai","medical","object-detection","r","tabular","text","vision"],"latest_commit_sha":null,"homepage":"https://eagerai.github.io/fastai/","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EagerAI.png","metadata":{"files":{"readme":"README.md","changelog":"NEWS.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-03T10:53:27.000Z","updated_at":"2024-09-13T02:55:15.000Z","dependencies_parsed_at":"2024-01-07T00:09:00.242Z","dependency_job_id":"d254ddee-1e2e-41ce-af27-4f6610bbf0d5","html_url":"https://github.com/EagerAI/fastai","commit_stats":{"total_commits":910,"total_committers":8,"mean_commits":113.75,"dds":0.02197802197802201,"last_synced_commit":"ae6389f3d6878f04c1ee93aa8c0be56537ace252"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EagerAI%2Ffastai","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EagerAI%2Ffastai/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EagerAI%2Ffastai/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EagerAI%2Ffastai/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EagerAI","download_url":"https://codeload.github.com/EagerAI/fastai/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248631687,"owners_count":21136556,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["audio","collaborative-filtering","darknet","darknet-image-classification","fastai","medical","object-detection","r","tabular","text","vision"],"created_at":"2024-11-23T18:15:01.208Z","updated_at":"2025-04-12T20:45:37.383Z","avatar_url":"https://github.com/EagerAI.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"## R interface to fastai\n\nThe fastai package provides R wrappers to\n[fastai](https://github.com/fastai/fastai).\n\nThe fastai library simplifies training fast and accurate neural nets using\nmodern best practices. See the\n[fastai website](https://eagerai.github.io/fastai/) to get started. The library\nis based on research into deep learning best practices undertaken at `fast.ai`,\nand includes \"out of the box\" support for `vision`, `text`, `tabular`, and `collab`\n(collaborative filtering) models.\n\n\u003cimg src=\"files/fastai.png\" width=200 align=right style=\"margin-left: 15px;\" alt=\"fastai\"/\u003e\n\n[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html)\n[![CRAN status](https://www.r-pkg.org/badges/version/fastai)](https://CRAN.R-project.org/package=fastai)\n[![Last month downloads](http://cranlogs.r-pkg.org/badges/last-month/fastai?color=green)](https://cran.r-project.org/package=fastai)\n\n## Continuous Build Status\n\n| Build             | Status                                                                         |\n| ----------------- | ------------------------------------------------------------------------------ |\n| **Bionic**        | ![ubuntu_18](https://github.com/EagerAI/fastai/workflows/ubuntu_18/badge.svg) |\n| **Focal**         | ![ubuntu_20](https://github.com/EagerAI/fastai/workflows/ubuntu_20/badge.svg) |\n| **Mac OS**        | ![mac_os](https://github.com/EagerAI/fastai/workflows/mac_os/badge.svg)       |\n| **Windows**       | ![windows](https://github.com/EagerAI/fastai/workflows/windows/badge.svg)     |\n\n## Installation\n\n**1. Install miniconda and activate environment:**\n\n```\nreticulate::install_miniconda()\nreticulate::conda_create('r-reticulate')\n```\n\n**2. The dev version:**\n\n```\ndevtools::install_github('eagerai/fastai')\n```\n\n\n**3. Later, you need to install the python module `fastai`:**\n\n```\nreticulate::use_condaenv('r-reticulate',required = TRUE)\nfastai::install_fastai(gpu = FALSE, cuda_version = '11.6', overwrite = FALSE)\n```\n\n\n**4. Restart RStudio!**\n\n## fast.ai extensions:\n\n1. [NLP, Transformers](https://github.com/ohmeow/blurr)\n2. [Object Detection](https://github.com/airctic/icevision)\n3. [Time-series](https://github.com/tcapelle/timeseries_fastai)\n4. [CycleGAN](https://github.com/tmabraham/UPIT)\n5. [Audio](https://github.com/fastaudio/fastaudio)\n\n## Kaggle\n\nWe currently prepare the examples of usage of the fastai from R in Kaggle competitions:\n\n- [Introduction](https://www.kaggle.com/henry090/r-interface-to-fastai)\n- [MNIST with Pytorch and fastai](https://www.kaggle.com/henry090/r-and-fastai)\n- [NLP Binary Classification](https://www.kaggle.com/henry090/r-fastai-and-transformers)\n- [Audio classification](https://www.kaggle.com/henry090/fast-ai-from-r)\n- [CycleGAN](https://www.kaggle.com/henry090/r-fast-ai-and-cyclegan)\n- [Fastai on Colab TPUs](https://colab.research.google.com/drive/1PiBECDM552No-5apVIB8LqUSdSqqJSi-?usp=sharing)\n\n\u003e Contributions are very welcome! \n\n## Tabular data\n\n```\nlibrary(magrittr)\nlibrary(fastai)\n\n# download\nURLs_ADULT_SAMPLE()\n\n# read data\ndf = data.table::fread('adult_sample/adult.csv')\n```\n\nVariables:\n\n```\ndep_var = 'salary'\ncat_names = c('workclass', 'education', 'marital-status', 'occupation', 'relationship', 'race')\ncont_names = c('age', 'fnlwgt', 'education-num')\n```\n\nPreprocess strategy:\n\n```\nprocs = list(FillMissing(),Categorify(),Normalize())\n```\n\nPrepare:\n\n```\ndls = TabularDataTable(df, procs, cat_names, cont_names,\n      y_names = dep_var, splits = list(c(1:32000),c(32001:32561))) %\u003e%\n      dataloaders(bs = 64)\n```\n\nSummary:\n\n```\nmodel = dls %\u003e% tabular_learner(layers=c(200,100), metrics=accuracy)\nmodel %\u003e% summary()\n```\n\n```\nTabularModel (Input shape: ['64 x 7', '64 x 3'])\n================================================================\nLayer (type)         Output Shape         Param #    Trainable\n================================================================\nEmbedding            64 x 6               60         True\n________________________________________________________________\nEmbedding            64 x 8               136        True\n________________________________________________________________\nEmbedding            64 x 5               40         True\n________________________________________________________________\nEmbedding            64 x 8               136        True\n________________________________________________________________\nEmbedding            64 x 5               35         True\n________________________________________________________________\nEmbedding            64 x 4               24         True\n________________________________________________________________\nEmbedding            64 x 3               9          True\n________________________________________________________________\nDropout              64 x 39              0          False\n________________________________________________________________\nBatchNorm1d          64 x 3               6          True\n________________________________________________________________\nBatchNorm1d          64 x 42              84         True\n________________________________________________________________\nLinear               64 x 200             8,400      True\n________________________________________________________________\nReLU                 64 x 200             0          False\n________________________________________________________________\nBatchNorm1d          64 x 200             400        True\n________________________________________________________________\nLinear               64 x 100             20,000     True\n________________________________________________________________\nReLU                 64 x 100             0          False\n________________________________________________________________\nLinear               64 x 2               202        True\n________________________________________________________________\n\nTotal params: 29,532\nTotal trainable params: 29,532\nTotal non-trainable params: 0\n\nOptimizer used: \u003cfunction Adam at 0x7fa246283598\u003e\nLoss function: FlattenedLoss of CrossEntropyLoss()\n\nCallbacks:\n  - TrainEvalCallback\n  - Recorder\n  - ProgressCallback\n```\n\nBefore fitting try to find optimal learning rate:\n\n```\nmodel %\u003e% lr_find()\n\nmodel %\u003e% plot_lr_find(dpi = 200)\n```\n\n\u003cimg src=\"files/plot_lr.png\" height=500 align=center alt=\"lr\"/\u003e\n\nRun:\n\n```\nmodel %\u003e% fit(5, lr = 10^-1)\n```\n\n```\nepoch     train_loss  valid_loss  accuracy  time\n0         0.360149    0.329587    0.846702  00:04\n1         0.352106    0.345761    0.828877  00:04\n2         0.368743    0.340913    0.844920  00:05\n3         0.347277    0.333084    0.852050  00:04\n4         0.348969    0.350707    0.830660  00:04\n```\n\nPlot loss history:\n\n```\nmodel %\u003e% plot_loss(dpi = 200)\n```\n\n\u003cimg src=\"files/plot_loss.png\" height=500 align=center alt=\"lr\"/\u003e\n\n\nSee training process:\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"files/fastai.gif\" height=350 align=center alt=\"train\"/\u003e\n\u003c/p\u003e\n\n\n\nGet confusion matrix:\n\n```\nmodel %\u003e% get_confusion_matrix()\n```\n\n```\n       \u003c50k  \u003e=50k\n\u003c50k   407    22\n\u003e=50k   68    64\n```\n\nPlot it:\n\n```\ninterp = ClassificationInterpretation_from_learner(model)\n\ninterp %\u003e% plot_confusion_matrix(dpi = 90,figsize = c(6,6))\n```\n\n\u003cimg src=\"files/conf_.png\" height=500 align=center alt=\"Pets\"/\u003e\n\nGet predictions on new data:\n\n```\n\u003e model %\u003e% predict(df[10:15,])\n\n       \u003c50k     \u003e=50k classes\n1 0.5108562 0.4891439       0\n2 0.4827824 0.5172176       1\n3 0.4873166 0.5126833       1\n4 0.5013804 0.4986197       0\n5 0.4964157 0.5035844       1\n6 0.5111378 0.4888622       0\n```\n\n## Image data\n\nGet Pets dataset:\n\n```\nURLs_PETS()\n```\n\nDefine path to folders:\n\n```\npath = 'oxford-iiit-pet'\npath_anno = 'oxford-iiit-pet/annotations'\npath_img = 'oxford-iiit-pet/images'\nfnames = get_image_files(path_img)\n```\n\nSee one of examples:\n\n```\nfnames[1]\n\noxford-iiit-pet/images/american_pit_bull_terrier_129.jpg\n```\n\nDataloader:\n\n```\ndls = ImageDataLoaders_from_name_re(\n  path, fnames, pat='(.+)_\\\\d+.jpg$',\n  item_tfms=Resize(size = 460), bs = 10,\n  batch_tfms=list(Normalize_from_stats( imagenet_stats() )\n                  )\n)\n```\n\nShow batch for visualization:\n\n```\ndls %\u003e% show_batch()\n```\n\n\u003cimg src=\"files/pets.png\" height=500 align=center alt=\"Pets\"/\u003e\n\nModel architecture:\n\n```\nlearn = cnn_learner(dls, resnet34(), metrics = error_rate)\n```\n\nAnd fit:\n\n```\nlearn %\u003e% fit_one_cycle(n_epoch = 2)\n\nepoch     train_loss  valid_loss  error_rate  time\n0         0.904872    0.317927    0.105548    00:35\n1         0.694395    0.239520    0.083897    00:36\n```\n\nGet confusion matrix and plot:\n\n```\nconf = learn %\u003e% get_confusion_matrix()\n\nlibrary(highcharter)\nhchart(conf, label = TRUE) %\u003e%\n    hc_yAxis(title = list(text = 'Actual')) %\u003e%\n    hc_xAxis(title = list(text = 'Predicted'),\n             labels = list(rotation = -90))\n```\n\n\u003cimg src=\"files/conf.png\" height=500 align=center alt=\"Pets\"/\u003e\n\n\u003e Note that the plot is built with highcharter.\n\nPlot top losses:\n\n```\ninterp = ClassificationInterpretation_from_learner(learn)\n\ninterp %\u003e% plot_top_losses(k = 9, figsize = c(15,11))\n```\n\n\u003cimg src=\"files/top_loss.png\" height=500 align=center alt=\"Pets\"/\u003e\n\nAlternatively, load images from folders:\n\n```\n# get sample data\nURLs_MNIST_SAMPLE()\n\n# transformations\npath = 'mnist_sample'\nbs = 20\n\n#load into memory\ndata = ImageDataLoaders_from_folder(path, size = 26, bs = bs)\n\n# Visualize and train\ndata %\u003e% show_batch(dpi = 150)\n\nlearn = cnn_learner(data, resnet18(), metrics = accuracy)\nlearn %\u003e% fit(2)\n```\n\n\u003cimg src=\"files/mnist.png\" height=500 align=center alt=\"Mnist\"/\u003e\n\n**What about the implementation of the latest\n[Computer Vision models](https://github.com/huggingface/pytorch-image-models)?**\n\nThere is a function in fastai `timm_learner` which originally written by\n[Zachary Mueller](https://github.com/walkwithfastai/walkwithfastai.github.io/).\nIt helps to quickly load the pretrained models from\n[timm library](https://github.com/huggingface/pytorch-image-models).\n\nFirst, lets's see the list of available models (TOP 10):\n\n```\n\u003e str(as.list(timm_list_models()[1:10]))\nList of 10\n $ : chr \"adv_inception_v3\"\n $ : chr \"cspdarknet53\"\n $ : chr \"cspdarknet53_iabn\"\n $ : chr \"cspresnet50\"\n $ : chr \"cspresnet50d\"\n $ : chr \"cspresnet50w\"\n $ : chr \"cspresnext50\"\n $ : chr \"cspresnext50_iabn\"\n $ : chr \"darknet53\"\n $ : chr \"densenet121\"\n```\n\nExciting!\n\nNow, load and train pets dataset:\n\n```\nlibrary(magrittr)\nlibrary(fastai)\n\npath = 'oxford-iiit-pet'\n\npath_img = 'oxford-iiit-pet/images'\n\nfnames = get_image_files(path_img)\n\ndls = ImageDataLoaders_from_name_re(\n  path, fnames, pat='(.+)_\\\\d+.jpg$',\n  item_tfms=Resize(size = 460), bs = 10,\n  batch_tfms=list(Normalize_from_stats( imagenet_stats() )\n  )\n)\n\nlearn = timm_learner(dls, 'cspdarknet53', metrics = list(accuracy, error_rate))\n\nlearn %\u003e% summary()\n```\n\n\u003cdetails\u003e\u003csummary\u003eModel summary\u003c/summary\u003e\n\u003cp\u003e\n\n```\nSequential (Input shape: ['10 x 3 x 224 x 224'])\n================================================================\nLayer (type)         Output Shape         Param #    Trainable\n================================================================\nConv2d               10 x 32 x 224 x 224  864        False\n________________________________________________________________\nLeakyReLU            10 x 32 x 224 x 224  0          False\n________________________________________________________________\nConv2d               10 x 64 x 112 x 112  18,432     False\n________________________________________________________________\nLeakyReLU            10 x 64 x 112 x 112  0          False\n________________________________________________________________\nConv2d               10 x 128 x 112 x 11  8,192      False\n________________________________________________________________\nLeakyReLU            10 x 128 x 112 x 11  0          False\n________________________________________________________________\nConv2d               10 x 32 x 112 x 112  2,048      False\n________________________________________________________________\nLeakyReLU            10 x 32 x 112 x 112  0          False\n________________________________________________________________\nConv2d               10 x 64 x 112 x 112  18,432     False\n________________________________________________________________\nLeakyReLU            10 x 64 x 112 x 112  0          False\n________________________________________________________________\nConv2d               10 x 64 x 112 x 112  4,096      False\n________________________________________________________________\nLeakyReLU            10 x 64 x 112 x 112  0          False\n________________________________________________________________\nConv2d               10 x 64 x 112 x 112  8,192      False\n________________________________________________________________\nLeakyReLU            10 x 64 x 112 x 112  0          False\n________________________________________________________________\nConv2d               10 x 128 x 56 x 56   73,728     False\n________________________________________________________________\nLeakyReLU            10 x 128 x 56 x 56   0          False\n________________________________________________________________\nConv2d               10 x 128 x 56 x 56   16,384     False\n________________________________________________________________\nLeakyReLU            10 x 128 x 56 x 56   0          False\n________________________________________________________________\nConv2d               10 x 64 x 56 x 56    4,096      False\n________________________________________________________________\nLeakyReLU            10 x 64 x 56 x 56    0          False\n________________________________________________________________\nConv2d               10 x 64 x 56 x 56    36,864     False\n________________________________________________________________\nLeakyReLU            10 x 64 x 56 x 56    0          False\n________________________________________________________________\nConv2d               10 x 64 x 56 x 56    4,096      False\n________________________________________________________________\nLeakyReLU            10 x 64 x 56 x 56    0          False\n________________________________________________________________\nConv2d               10 x 64 x 56 x 56    36,864     False\n________________________________________________________________\nLeakyReLU            10 x 64 x 56 x 56    0          False\n________________________________________________________________\nConv2d               10 x 64 x 56 x 56    4,096      False\n________________________________________________________________\nLeakyReLU            10 x 64 x 56 x 56    0          False\n________________________________________________________________\nConv2d               10 x 128 x 56 x 56   16,384     False\n________________________________________________________________\nLeakyReLU            10 x 128 x 56 x 56   0          False\n________________________________________________________________\nConv2d               10 x 256 x 28 x 28   294,912    False\n________________________________________________________________\nLeakyReLU            10 x 256 x 28 x 28   0          False\n________________________________________________________________\nConv2d               10 x 256 x 28 x 28   65,536     False\n________________________________________________________________\nLeakyReLU            10 x 256 x 28 x 28   0          False\n________________________________________________________________\nConv2d               10 x 128 x 28 x 28   16,384     False\n________________________________________________________________\nLeakyReLU            10 x 128 x 28 x 28   0          False\n________________________________________________________________\nConv2d               10 x 128 x 28 x 28   147,456    False\n________________________________________________________________\nLeakyReLU            10 x 128 x 28 x 28   0          False\n________________________________________________________________\nConv2d               10 x 128 x 28 x 28   16,384     False\n________________________________________________________________\nLeakyReLU            10 x 128 x 28 x 28   0          False\n________________________________________________________________\nConv2d               10 x 128 x 28 x 28   147,456    False\n________________________________________________________________\nLeakyReLU            10 x 128 x 28 x 28   0          False\n________________________________________________________________\nConv2d               10 x 128 x 28 x 28   16,384     False\n________________________________________________________________\nLeakyReLU            10 x 128 x 28 x 28   0          False\n________________________________________________________________\nConv2d               10 x 128 x 28 x 28   147,456    False\n________________________________________________________________\nLeakyReLU            10 x 128 x 28 x 28   0          False\n________________________________________________________________\nConv2d               10 x 128 x 28 x 28   16,384     False\n________________________________________________________________\nLeakyReLU            10 x 128 x 28 x 28   0          False\n________________________________________________________________\nConv2d               10 x 128 x 28 x 28   147,456    False\n________________________________________________________________\nLeakyReLU            10 x 128 x 28 x 28   0          False\n________________________________________________________________\nConv2d               10 x 128 x 28 x 28   16,384     False\n________________________________________________________________\nLeakyReLU            10 x 128 x 28 x 28   0          False\n________________________________________________________________\nConv2d               10 x 128 x 28 x 28   147,456    False\n________________________________________________________________\nLeakyReLU            10 x 128 x 28 x 28   0          False\n________________________________________________________________\nConv2d               10 x 128 x 28 x 28   16,384     False\n________________________________________________________________\nLeakyReLU            10 x 128 x 28 x 28   0          False\n________________________________________________________________\nConv2d               10 x 128 x 28 x 28   147,456    False\n________________________________________________________________\nLeakyReLU            10 x 128 x 28 x 28   0          False\n________________________________________________________________\nConv2d               10 x 128 x 28 x 28   16,384     False\n________________________________________________________________\nLeakyReLU            10 x 128 x 28 x 28   0          False\n________________________________________________________________\nConv2d               10 x 128 x 28 x 28   147,456    False\n________________________________________________________________\nLeakyReLU            10 x 128 x 28 x 28   0          False\n________________________________________________________________\nConv2d               10 x 128 x 28 x 28   16,384     False\n________________________________________________________________\nLeakyReLU            10 x 128 x 28 x 28   0          False\n________________________________________________________________\nConv2d               10 x 128 x 28 x 28   147,456    False\n________________________________________________________________\nLeakyReLU            10 x 128 x 28 x 28   0          False\n________________________________________________________________\nConv2d               10 x 128 x 28 x 28   16,384     False\n________________________________________________________________\nLeakyReLU            10 x 128 x 28 x 28   0          False\n________________________________________________________________\nConv2d               10 x 256 x 28 x 28   65,536     False\n________________________________________________________________\nLeakyReLU            10 x 256 x 28 x 28   0          False\n________________________________________________________________\nConv2d               10 x 512 x 14 x 14   1,179,648  False\n________________________________________________________________\nLeakyReLU            10 x 512 x 14 x 14   0          False\n________________________________________________________________\nConv2d               10 x 512 x 14 x 14   262,144    False\n________________________________________________________________\nLeakyReLU            10 x 512 x 14 x 14   0          False\n________________________________________________________________\nConv2d               10 x 256 x 14 x 14   65,536     False\n________________________________________________________________\nLeakyReLU            10 x 256 x 14 x 14   0          False\n________________________________________________________________\nConv2d               10 x 256 x 14 x 14   589,824    False\n________________________________________________________________\nLeakyReLU            10 x 256 x 14 x 14   0          False\n________________________________________________________________\nConv2d               10 x 256 x 14 x 14   65,536     False\n________________________________________________________________\nLeakyReLU            10 x 256 x 14 x 14   0          False\n________________________________________________________________\nConv2d               10 x 256 x 14 x 14   589,824    False\n________________________________________________________________\nLeakyReLU            10 x 256 x 14 x 14   0          False\n________________________________________________________________\nConv2d               10 x 256 x 14 x 14   65,536     False\n________________________________________________________________\nLeakyReLU            10 x 256 x 14 x 14   0          False\n________________________________________________________________\nConv2d               10 x 256 x 14 x 14   589,824    False\n________________________________________________________________\nLeakyReLU            10 x 256 x 14 x 14   0          False\n________________________________________________________________\nConv2d               10 x 256 x 14 x 14   65,536     False\n________________________________________________________________\nLeakyReLU            10 x 256 x 14 x 14   0          False\n________________________________________________________________\nConv2d               10 x 256 x 14 x 14   589,824    False\n________________________________________________________________\nLeakyReLU            10 x 256 x 14 x 14   0          False\n________________________________________________________________\nConv2d               10 x 256 x 14 x 14   65,536     False\n________________________________________________________________\nLeakyReLU            10 x 256 x 14 x 14   0          False\n________________________________________________________________\nConv2d               10 x 256 x 14 x 14   589,824    False\n________________________________________________________________\nLeakyReLU            10 x 256 x 14 x 14   0          False\n________________________________________________________________\nConv2d               10 x 256 x 14 x 14   65,536     False\n________________________________________________________________\nLeakyReLU            10 x 256 x 14 x 14   0          False\n________________________________________________________________\nConv2d               10 x 256 x 14 x 14   589,824    False\n________________________________________________________________\nLeakyReLU            10 x 256 x 14 x 14   0          False\n________________________________________________________________\nConv2d               10 x 256 x 14 x 14   65,536     False\n________________________________________________________________\nLeakyReLU            10 x 256 x 14 x 14   0          False\n________________________________________________________________\nConv2d               10 x 256 x 14 x 14   589,824    False\n________________________________________________________________\nLeakyReLU            10 x 256 x 14 x 14   0          False\n________________________________________________________________\nConv2d               10 x 256 x 14 x 14   65,536     False\n________________________________________________________________\nLeakyReLU            10 x 256 x 14 x 14   0          False\n________________________________________________________________\nConv2d               10 x 256 x 14 x 14   589,824    False\n________________________________________________________________\nLeakyReLU            10 x 256 x 14 x 14   0          False\n________________________________________________________________\nConv2d               10 x 256 x 14 x 14   65,536     False\n________________________________________________________________\nLeakyReLU            10 x 256 x 14 x 14   0          False\n________________________________________________________________\nConv2d               10 x 512 x 14 x 14   262,144    False\n________________________________________________________________\nLeakyReLU            10 x 512 x 14 x 14   0          False\n________________________________________________________________\nConv2d               10 x 1024 x 7 x 7    4,718,592  False\n________________________________________________________________\nLeakyReLU            10 x 1024 x 7 x 7    0          False\n________________________________________________________________\nConv2d               10 x 1024 x 7 x 7    1,048,576  False\n________________________________________________________________\nLeakyReLU            10 x 1024 x 7 x 7    0          False\n________________________________________________________________\nConv2d               10 x 512 x 7 x 7     262,144    False\n________________________________________________________________\nLeakyReLU            10 x 512 x 7 x 7     0          False\n________________________________________________________________\nConv2d               10 x 512 x 7 x 7     2,359,296  False\n________________________________________________________________\nLeakyReLU            10 x 512 x 7 x 7     0          False\n________________________________________________________________\nConv2d               10 x 512 x 7 x 7     262,144    False\n________________________________________________________________\nLeakyReLU            10 x 512 x 7 x 7     0          False\n________________________________________________________________\nConv2d               10 x 512 x 7 x 7     2,359,296  False\n________________________________________________________________\nLeakyReLU            10 x 512 x 7 x 7     0          False\n________________________________________________________________\nConv2d               10 x 512 x 7 x 7     262,144    False\n________________________________________________________________\nLeakyReLU            10 x 512 x 7 x 7     0          False\n________________________________________________________________\nConv2d               10 x 512 x 7 x 7     2,359,296  False\n________________________________________________________________\nLeakyReLU            10 x 512 x 7 x 7     0          False\n________________________________________________________________\nConv2d               10 x 512 x 7 x 7     262,144    False\n________________________________________________________________\nLeakyReLU            10 x 512 x 7 x 7     0          False\n________________________________________________________________\nConv2d               10 x 512 x 7 x 7     2,359,296  False\n________________________________________________________________\nLeakyReLU            10 x 512 x 7 x 7     0          False\n________________________________________________________________\nConv2d               10 x 512 x 7 x 7     262,144    False\n________________________________________________________________\nLeakyReLU            10 x 512 x 7 x 7     0          False\n________________________________________________________________\nConv2d               10 x 1024 x 7 x 7    1,048,576  False\n________________________________________________________________\nLeakyReLU            10 x 1024 x 7 x 7    0          False\n________________________________________________________________\nAdaptiveAvgPool2d    10 x 1024 x 1 x 1    0          False\n________________________________________________________________\nAdaptiveMaxPool2d    10 x 1024 x 1 x 1    0          False\n________________________________________________________________\nFlatten              10 x 2048            0          False\n________________________________________________________________\nBatchNorm1d          10 x 2048            4,096      True\n________________________________________________________________\nDropout              10 x 2048            0          False\n________________________________________________________________\nLinear               10 x 512             1,048,576  True\n________________________________________________________________\nReLU                 10 x 512             0          False\n________________________________________________________________\nBatchNorm1d          10 x 512             1,024      True\n________________________________________________________________\nDropout              10 x 512             0          False\n________________________________________________________________\nLinear               10 x 37              18,944     True\n________________________________________________________________\n\nTotal params: 27,654,496\nTotal trainable params: 1,072,640\nTotal non-trainable params: 26,581,856\n\nOptimizer used: \u003cfunction Adam at 0x7fc1cfc16f28\u003e\nLoss function: FlattenedLoss of CrossEntropyLoss()\n\nModel frozen up to parameter group #1\n\nCallbacks:\n  - TrainEvalCallback\n  - Recorder\n  - ProgressCallback\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\nAnd finally, fit:\n\n```\nlearn %\u003e% fit_one_cycle(3)\n```\n\n```\nepoch   train_loss   valid_loss   accuracy   error_rate   time\n------  -----------  -----------  ---------  -----------  ------\n0       1.206384     0.518956     0.847091   0.152909     01:00\n1       0.841627     0.411970     0.890392   0.109608     00:58\n2       0.657220     0.328548     0.899188   0.100812     00:59\n\n```\n\nSee results:\n\n```\nlearn %\u003e% show_results()\n```\n\nImpressive!\n\n\u003cimg src=\"files/darknet.png\" height=500 align=center alt=\"Mnist\"/\u003e\n\n### GAN example\n\nGet data (4,4 GB):\n\n```\nURLs_LSUN_BEDROOMS()\n\npath = 'bedroom'\n```\n\nDataloader function:\n\n```\nget_dls \u003c- function(bs, size) {\n  dblock = DataBlock(blocks = list(TransformBlock(), ImageBlock()),\n                     get_x = generate_noise(),\n                     get_items = get_image_files(),\n                     splitter = IndexSplitter(c()),\n                     item_tfms = Resize(size, method = \"crop\"),\n                     batch_tfms = Normalize_from_stats(c(0.5,0.5,0.5), c(0.5,0.5,0.5))\n  )\n  dblock %\u003e% dataloaders(source = path, path = path,bs = bs)\n}\n\ndls = get_dls(128, 64)\n```\n\nGenerator and discriminator:\n\n```\ngenerator = basic_generator(out_size = 64, n_channels = 3, n_extra_layers = 1)\ncritic    = basic_critic(in_size = 64, n_channels = 3, n_extra_layers = 1,\n                                    act_cls = partial(nn$LeakyReLU, negative_slope = 0.2))\n\n```\n\nModel:\n\n```\nlearn = GANLearner_wgan(dls, generator, critic, opt_func = partial(Adam(), mom=0.))\n```\n\nAnd fit:\n\n```\nlearn$recorder$train_metrics = TRUE\nlearn$recorder$valid_metrics = FALSE\n\nlearn %\u003e% fit(1, 2e-4, wd = 0)\n```\n\n```\nepoch     train_loss  gen_loss  crit_loss  time\n0         -0.555554   0.516327  -0.967604  05:06\n```\n\nThis is the result for 1 epoch.\n\n```\nlearn %\u003e% show_results(max_n = 16, figsize = c(8,8), ds_idx=0)\n```\n\n\u003ccenter\u003e\n\n\u003cimg src=\"files/gan.png\" height=600 align=center alt=\"Mnist\"/\u003e\n\n\u003c/center\u003e\n\n\n\n## Unet example\n\nCall libraries:\n\n```\nlibrary(fastai)\nlibrary(magrittr)\n```\n\nGet data\n\n```\nURLs_CAMVID()\n```\n\nSpecify folders:\n\n```\npath = 'camvid'\nfnames = get_image_files(paste(path,'images',sep = '/'))\nlbl_names = get_image_files(paste(path,'labels',sep = '/'))\ncodes = data.table::fread(paste(path,'codes.txt',sep = '/'), header = FALSE)[['V1']]\nvalid_fnames = data.table::fread(paste(path,'valid.txt',sep = '/'),header = FALSE)[['V1']]\n# batch size\nbs = 8\n```\n\nDefine a loader object:\n\n```\ncamvid = DataBlock(blocks = c(ImageBlock(), MaskBlock(codes)),\n                   get_items = get_image_files,\n                   splitter = FileSplitter('camvid/valid.txt'),\n                   get_y = function(x) {paste('camvid/labels/',x$stem,'_P',x$suffix,sep = '')},\n                   batch_tfms = list(Normalize_from_stats( imagenet_stats() )\n                   )\n)\n\n# prefix and suffix of the name of the file\nx$stem; x$suffix\n```\n\nDataloader object and list of labels:\n\n```\ndls = camvid %\u003e% dataloaders(source = \"camvid/images\", bs = bs, path = path)\n\ndls %\u003e% show_batch()\n\nvoid_code = which(codes == \"Void\")\n\ndls$vocab = codes\n\nname2id = as.list(1:(length(codes)))\nnames(name2id) = codes\n```\n\n\u003ccenter\u003e\n\n\u003cimg src=\"files/unet.png\" height=600 align=center alt=\"Mnist\"/\u003e\n\n\u003c/center\u003e\n\n```\nstr(name2id)\nList of 32\n $ Animal           : int 1\n $ Archway          : int 2\n $ Bicyclist        : int 3\n $ Bridge           : int 4\n $ Building         : int 5\n $ Car              : int 6\n $ CartLuggagePram  : int 7\n $ Child            : int 8\n $ Column_Pole      : int 9\n $ Fence            : int 10\n $ LaneMkgsDriv     : int 11\n $ LaneMkgsNonDriv  : int 12\n $ Misc_Text        : int 13\n $ MotorcycleScooter: int 14\n $ OtherMoving      : int 15\n $ ParkingBlock     : int 16\n $ Pedestrian       : int 17\n $ Road             : int 18\n $ RoadShoulder     : int 19\n $ Sidewalk         : int 20\n $ SignSymbol       : int 21\n $ Sky              : int 22\n $ SUVPickupTruck   : int 23\n $ TrafficCone      : int 24\n $ TrafficLight     : int 25\n $ Train            : int 26\n $ Tree             : int 27\n $ Truck_Bus        : int 28\n $ Tunnel           : int 29\n $ VegetationMisc   : int 30\n $ Void             : int 31\n $ Wall             : int 32\n```\n\nCustom accuracy function:\n\n```\nacc_camvid \u003c- function(input, target) {\n  target = target$squeeze(1L)\n  # exclude/filter void label\n  mask = target != void_code\n  return(\n    (input$argmax(dim=1L)[mask]$eq(target[mask])) %\u003e%\n      float() %\u003e% mean()\n  )\n}\n\nattr(acc_camvid, \"py_function_name\") \u003c- 'acc_camvid'\n```\n\n\u003cdetails\u003e\u003csummary\u003eDebug acc_camvid manually\u003c/summary\u003e\n\u003cp\u003e\n\n```\nbatch = dls %\u003e% one_batch(convert = FALSE)\n```\n\n```\n[[1]]\nTensorImage([[[[-1.4419e+00, -1.3117e+00, -1.1976e+00,  ...,  2.2489e+00,\n            2.2238e+00,  2.0948e+00],\n          [-1.5401e+00, -1.5213e+00, -1.4010e+00,  ...,  1.9834e+00,\n            2.2378e+00,  2.2173e+00],\n          [-1.6401e+00, -1.5477e+00, -1.5588e+00,  ...,  9.1953e-01,\n            1.9501e+00,  1.1138e+00],\n          ...,\n          [-1.6852e+00, -1.5440e+00, -1.5132e+00,  ..., -1.0596e+00,\n           -1.0711e+00, -1.0674e+00],\n          [-1.5265e+00, -1.6030e+00, -1.5804e+00,  ..., -1.0268e+00,\n           -1.0946e+00, -1.1181e+00],\n          [-1.5423e+00, -1.5516e+00, -1.6014e+00,  ..., -1.1734e+00,\n           -1.1293e+00, -1.0777e+00]],\n\n         [[-1.3446e+00, -1.2023e+00, -1.0470e+00,  ...,  2.4286e+00,\n            2.4090e+00,  2.2977e+00],\n          [-1.4481e+00, -1.4276e+00, -1.2930e+00,  ...,  2.1422e+00,\n            2.4158e+00,  2.3778e+00],\n          [-1.5607e+00, -1.4584e+00, -1.4641e+00,  ...,  1.0026e+00,\n            2.0258e+00,  1.1376e+00],\n          ...,\n          [-1.5809e+00, -1.4399e+00, -1.4133e+00,  ..., -7.8931e-01,\n           -7.9807e-01, -7.9637e-01],\n          [-1.4161e+00, -1.4909e+00, -1.4646e+00,  ..., -8.0615e-01,\n           -8.5201e-01, -8.5311e-01],\n          [-1.4472e+00, -1.4567e+00, -1.5077e+00,  ..., -9.4607e-01,\n           -8.9744e-01, -8.2074e-01]],\n\n         [[-1.1164e+00, -1.0162e+00, -9.1189e-01,  ...,  2.6257e+00,\n            2.5726e+00,  2.4016e+00],\n          [-1.2195e+00, -1.1752e+00, -1.0595e+00,  ...,  2.3488e+00,\n            2.6271e+00,  2.5764e+00],\n          [-1.3316e+00, -1.2451e+00, -1.2400e+00,  ...,  1.0476e+00,\n            2.1812e+00,  1.3635e+00],\n          ...,\n          [-1.2881e+00, -1.1393e+00, -1.1035e+00,  ..., -3.8940e-01,\n           -4.0598e-01, -3.9861e-01],\n          [-1.1427e+00, -1.2167e+00, -1.1906e+00,  ..., -3.6462e-01,\n           -4.3055e-01, -4.5333e-01],\n          [-1.1525e+00, -1.1651e+00, -1.2190e+00,  ..., -4.8259e-01,\n           -4.3712e-01, -4.1413e-01]]],\n\n\n        [[[-2.0552e-01,  3.9563e-01,  4.0691e-01,  ..., -9.7342e-01,\n           -7.8957e-01, -7.6035e-01],\n          [-3.8852e-01,  4.2912e-01,  4.4469e-01,  ..., -1.0449e+00,\n           -8.5347e-01, -7.5299e-01],\n          [ 3.5939e-01,  3.6353e-01,  4.7028e-01,  ..., -9.3101e-01,\n           -8.7398e-01, -7.9327e-01],\n          ...,\n          [-1.0510e+00, -1.0661e+00, -9.6690e-01,  ..., -1.3688e+00,\n           -1.4543e+00, -1.4645e+00],\n          [-1.0578e+00, -1.0939e+00, -9.3117e-01,  ..., -1.3939e+00,\n           -1.4033e+00, -1.4209e+00],\n          [-9.9012e-01, -1.0312e+00, -1.0074e+00,  ..., -1.4274e+00,\n           -1.3829e+00, -1.3758e+00]],\n\n         [[ 6.0090e-02,  7.8124e-01,  7.5145e-01,  ..., -8.2881e-01,\n           -6.7773e-01, -6.3718e-01],\n          [-1.7114e-01,  7.8613e-01,  7.8531e-01,  ..., -9.0003e-01,\n           -7.3661e-01, -5.8707e-01],\n          [ 7.3440e-01,  7.5691e-01,  8.2297e-01,  ..., -8.0694e-01,\n           -7.5451e-01, -6.2783e-01],\n          ...,\n          [-7.8971e-01, -7.8585e-01, -7.4870e-01,  ..., -1.2630e+00,\n           -1.3108e+00, -1.3046e+00],\n          [-7.8414e-01, -7.9617e-01, -7.2847e-01,  ..., -1.2297e+00,\n           -1.2414e+00, -1.2594e+00],\n          [-7.3135e-01, -7.7442e-01, -7.4849e-01,  ..., -1.2259e+00,\n           -1.1889e+00, -1.2022e+00]],\n\n         [[ 4.4920e-01,  1.2392e+00,  1.3399e+00,  ..., -6.0991e-01,\n           -4.5250e-01, -4.4251e-01],\n          [ 2.7577e-01,  1.2913e+00,  1.3755e+00,  ..., -6.8060e-01,\n           -5.1114e-01, -3.7442e-01],\n          [ 1.0632e+00,  1.3052e+00,  1.3774e+00,  ..., -5.8343e-01,\n           -5.2787e-01, -3.9803e-01],\n          ...,\n          [-4.4165e-01, -4.4558e-01, -3.8942e-01,  ..., -8.7048e-01,\n           -9.2835e-01, -9.2750e-01],\n          [-4.4233e-01, -4.6348e-01, -3.7176e-01,  ..., -8.6960e-01,\n           -8.8080e-01, -8.9788e-01],\n          [-3.8967e-01, -4.3118e-01, -3.8587e-01,  ..., -8.7933e-01,\n           -8.4775e-01, -8.5052e-01]]],\n\n\n        [[[ 1.2805e+00,  2.2139e+00,  9.9765e-01,  ...,  6.6338e-01,\n           -4.0192e-01,  2.8007e-01],\n          [ 1.0171e+00,  1.8849e+00,  1.1654e+00,  ..., -1.0001e+00,\n            1.1788e+00,  2.0717e+00],\n          [ 2.8709e-01,  1.9494e+00,  2.1978e+00,  ..., -6.7389e-01,\n            3.2762e-01,  4.5549e-01],\n          ...,\n          [-4.3609e-01, -4.2635e-01, -4.6298e-01,  ...,  7.7548e-02,\n            3.6271e-02, -3.1759e-02],\n          [-3.7265e-01, -4.3453e-01, -4.4666e-01,  ..., -7.5601e-02,\n            5.3570e-03, -2.9393e-02],\n          [-3.7581e-01, -4.0105e-01, -4.2908e-01,  ...,  8.5172e-03,\n           -3.3988e-03, -1.8303e-02]],\n\n         [[ 1.3276e+00,  2.3720e+00,  1.0603e+00,  ...,  8.6043e-01,\n           -1.1662e-01,  5.2147e-01],\n          [ 1.0938e+00,  2.0233e+00,  1.2629e+00,  ..., -9.1610e-01,\n            1.3807e+00,  2.2914e+00],\n          [ 3.8840e-01,  2.1078e+00,  2.3635e+00,  ..., -5.8584e-01,\n            5.2653e-01,  7.8300e-01],\n          ...,\n          [-3.1636e-01, -3.0640e-01, -3.4385e-01,  ...,  1.3784e-01,\n            9.5460e-02,  2.5607e-02],\n          [-2.5150e-01, -3.1476e-01, -3.2716e-01,  ..., -1.9409e-02,\n            6.3717e-02,  2.8037e-02],\n          [-2.5473e-01, -2.8054e-01, -3.0920e-01,  ...,  6.6963e-02,\n            5.4727e-02,  3.9424e-02]],\n\n         [[ 1.8118e+00,  2.6126e+00,  1.5284e+00,  ...,  1.3408e+00,\n            3.8263e-01,  9.4347e-01],\n          [ 1.4345e+00,  2.2263e+00,  1.5055e+00,  ..., -4.0407e-01,\n            1.9165e+00,  2.5325e+00],\n          [ 6.9120e-01,  2.3214e+00,  2.5724e+00,  ..., -5.9273e-02,\n            7.6707e-01,  9.8036e-01],\n          ...,\n          [-3.2707e-02, -2.5592e-02, -6.5520e-02,  ...,  3.1733e-01,\n            2.8317e-01,  2.2166e-01],\n          [ 1.6474e-02, -4.1773e-02, -5.1314e-02,  ...,  1.6267e-01,\n            2.4836e-01,  2.1449e-01],\n          [ 2.4832e-02,  1.0270e-02, -1.5259e-02,  ...,  2.3768e-01,\n            2.2930e-01,  2.2220e-01]]],\n\n\n        ...,\n\n\n        [[[-1.5176e-02, -1.9729e-02, -5.4177e-02,  ...,  2.0812e+00,\n            2.2489e+00,  2.2242e+00],\n          [-1.0897e-02,  3.5695e-02,  2.3053e-03,  ...,  2.1605e+00,\n            2.0372e+00,  2.1403e+00],\n          [-2.8262e-02, -3.0313e-02, -3.4347e-02,  ...,  2.2136e+00,\n            2.2489e+00,  1.2613e+00],\n          ...,\n          [-1.2644e+00, -1.2548e+00, -1.2313e+00,  ..., -1.3335e+00,\n           -1.3230e+00, -1.2787e+00],\n          [-1.1986e+00, -1.2068e+00, -1.1631e+00,  ..., -1.2694e+00,\n           -1.2973e+00, -1.2696e+00],\n          [-1.2508e+00, -1.2447e+00, -1.2294e+00,  ..., -1.0572e+00,\n           -1.0660e+00, -1.0694e+00]],\n\n         [[ 2.2227e-01,  2.1430e-01,  2.1605e-01,  ...,  2.3389e+00,\n            2.4286e+00,  2.4286e+00],\n          [ 2.0176e-01,  2.4693e-01,  2.4092e-01,  ...,  2.3745e+00,\n            2.2931e+00,  2.3820e+00],\n          [ 1.8103e-01,  1.7892e-01,  1.7477e-01,  ...,  2.4036e+00,\n            2.4286e+00,  1.4878e+00],\n          ...,\n          [-1.0710e+00, -1.0613e+00, -1.0374e+00,  ..., -1.2492e+00,\n           -1.2385e+00, -1.2225e+00],\n          [-1.0040e+00, -1.0124e+00, -9.6780e-01,  ..., -1.1836e+00,\n           -1.2122e+00, -1.2193e+00],\n          [-1.0572e+00, -1.0510e+00, -1.0354e+00,  ..., -9.5631e-01,\n           -9.6512e-01, -9.6444e-01]],\n\n         [[ 5.4786e-01,  5.5583e-01,  5.3839e-01,  ...,  2.5781e+00,\n            2.6400e+00,  2.6400e+00],\n          [ 5.3558e-01,  5.8483e-01,  5.6649e-01,  ...,  2.5895e+00,\n            2.5283e+00,  2.6400e+00],\n          [ 5.2345e-01,  5.2294e-01,  5.1033e-01,  ...,  2.6400e+00,\n            2.6400e+00,  1.7087e+00],\n          ...,\n          [-8.1354e-01, -8.0387e-01, -7.9721e-01,  ..., -1.0014e+00,\n           -9.9075e-01, -9.5806e-01],\n          [-7.4687e-01, -7.5518e-01, -7.2870e-01,  ..., -9.4173e-01,\n           -9.6991e-01, -9.5030e-01],\n          [-7.9981e-01, -7.9358e-01, -7.9630e-01,  ..., -7.3474e-01,\n           -7.4333e-01, -7.3628e-01]]],\n\n\n        [[[ 6.8056e-01,  6.8056e-01,  6.9105e-01,  ..., -3.6921e-01,\n           -3.1641e-01, -3.3400e-01],\n          [ 6.9991e-01,  7.1771e-01,  6.8056e-01,  ..., -3.3319e-01,\n           -3.4023e-01, -3.8674e-01],\n          [ 6.9781e-01,  7.1034e-01,  6.9885e-01,  ..., -2.9567e-01,\n           -3.0638e-01, -2.8775e-01],\n          ...,\n          [-1.4393e+00, -1.4183e+00, -1.4183e+00,  ..., -1.3420e+00,\n           -1.4022e+00, -1.3872e+00],\n          [-1.4436e+00, -1.4326e+00, -1.4335e+00,  ..., -1.3950e+00,\n           -1.3800e+00, -1.3734e+00],\n          [-1.4509e+00, -1.4539e+00, -1.4533e+00,  ..., -1.3681e+00,\n           -1.4340e+00, -1.3650e+00]],\n\n         [[ 2.0471e+00,  2.0471e+00,  2.0603e+00,  ..., -6.5347e-02,\n            2.6326e-02,  3.4833e-02],\n          [ 2.0525e+00,  2.0750e+00,  2.0818e+00,  ..., -4.7675e-02,\n           -5.2935e-03, -2.6855e-02],\n          [ 2.0976e+00,  2.1136e+00,  2.1051e+00,  ...,  1.8606e-02,\n            4.1052e-02,  8.5274e-02],\n          ...,\n          [-1.2304e+00, -1.2244e+00, -1.2219e+00,  ..., -1.2425e+00,\n           -1.3041e+00, -1.2836e+00],\n          [-1.2239e+00, -1.2107e+00, -1.2107e+00,  ..., -1.2967e+00,\n           -1.2813e+00, -1.2746e+00],\n          [-1.2210e+00, -1.2154e+00, -1.2157e+00,  ..., -1.2695e+00,\n           -1.3401e+00, -1.2696e+00]],\n\n         [[ 2.6400e+00,  2.6400e+00,  2.6400e+00,  ...,  3.4950e-01,\n            4.4111e-01,  4.1667e-01],\n          [ 2.6400e+00,  2.6400e+00,  2.6400e+00,  ...,  3.3850e-01,\n            3.8055e-01,  3.7792e-01],\n          [ 2.6400e+00,  2.6400e+00,  2.6400e+00,  ...,  4.4053e-01,\n            4.5217e-01,  4.8598e-01],\n          ...,\n          [-8.2900e-01, -8.1651e-01, -8.1498e-01,  ..., -9.5577e-01,\n           -1.0173e+00, -9.9684e-01],\n          [-8.3432e-01, -8.2192e-01, -8.2227e-01,  ..., -1.0234e+00,\n           -1.0080e+00, -1.0014e+00],\n          [-8.3237e-01, -8.2912e-01, -8.2936e-01,  ..., -1.0039e+00,\n           -1.0649e+00, -9.9452e-01]]],\n\n\n        [[[ 2.0699e+00,  1.9477e+00,  2.0700e+00,  ..., -1.5310e+00,\n           -1.6490e+00, -1.6860e+00],\n          [ 1.8292e+00,  2.1599e+00,  1.8882e+00,  ..., -1.6536e+00,\n           -1.6374e+00, -1.6022e+00],\n          [ 2.0288e+00,  1.7863e+00,  2.0564e+00,  ..., -1.6149e+00,\n           -1.6315e+00, -1.5586e+00],\n          ...,\n          [-1.4481e+00, -1.3921e+00, -1.4195e+00,  ..., -1.5045e+00,\n           -1.5133e+00, -1.5381e+00],\n          [-1.4223e+00, -1.3757e+00, -1.3943e+00,  ..., -1.5238e+00,\n           -1.5371e+00, -1.5453e+00],\n          [-1.4134e+00, -1.4104e+00, -1.4300e+00,  ..., -1.5163e+00,\n           -1.5862e+00, -1.5565e+00]],\n\n         [[ 1.5571e+00,  1.4284e+00,  1.8346e+00,  ..., -1.4521e+00,\n           -1.6496e+00, -1.6908e+00],\n          [ 1.2790e+00,  1.6710e+00,  1.3942e+00,  ..., -1.5838e+00,\n           -1.6467e+00, -1.6069e+00],\n          [ 1.4661e+00,  1.2568e+00,  1.7123e+00,  ..., -1.5898e+00,\n           -1.6761e+00, -1.6212e+00],\n          ...,\n          [-1.2567e+00, -1.2393e+00, -1.2457e+00,  ..., -1.4077e+00,\n           -1.4073e+00, -1.4286e+00],\n          [-1.2191e+00, -1.2129e+00, -1.2214e+00,  ..., -1.4193e+00,\n           -1.4265e+00, -1.4403e+00],\n          [-1.2213e+00, -1.2350e+00, -1.2495e+00,  ..., -1.4075e+00,\n           -1.4811e+00, -1.4504e+00]],\n\n         [[ 1.1398e+00,  1.0327e+00,  1.4135e+00,  ..., -1.2147e+00,\n           -1.4180e+00, -1.4598e+00],\n          [ 8.6931e-01,  1.2768e+00,  1.0129e+00,  ..., -1.3449e+00,\n           -1.3906e+00, -1.3518e+00],\n          [ 1.1199e+00,  9.0534e-01,  1.2758e+00,  ..., -1.3922e+00,\n           -1.4662e+00, -1.4051e+00],\n          ...,\n          [-8.5999e-01, -8.2594e-01, -8.6729e-01,  ..., -1.0699e+00,\n           -1.0976e+00, -1.1388e+00],\n          [-8.4630e-01, -8.2145e-01, -8.4266e-01,  ..., -1.1058e+00,\n           -1.1325e+00, -1.1478e+00],\n          [-8.5198e-01, -8.5977e-01, -8.7435e-01,  ..., -1.1186e+00,\n           -1.1739e+00, -1.1579e+00]]]], device='cuda:0')\n\n[[2]]\nTensorMask([[[ 4,  4,  4,  ...,  4,  4,  4],\n         [ 4,  4,  4,  ...,  4,  4,  4],\n         [ 4,  4,  4,  ...,  4,  4,  4],\n         ...,\n         [19, 19, 19,  ..., 17, 17, 17],\n         [19, 19, 19,  ..., 17, 17, 17],\n         [19, 19, 19,  ..., 17, 17, 17]],\n\n        [[ 4,  4,  4,  ...,  4,  4,  4],\n         [ 4,  4,  4,  ...,  4,  4,  4],\n         [ 4,  4,  4,  ...,  4,  4,  4],\n         ...,\n         [17, 17, 17,  ..., 17, 17, 17],\n         [17, 17, 17,  ..., 17, 17, 17],\n         [17, 17, 17,  ..., 17, 17, 17]],\n\n        [[26, 21, 26,  ..., 26, 26, 26],\n         [26, 21, 26,  ..., 26, 26, 26],\n         [26, 21, 21,  ..., 26, 26, 26],\n         ...,\n         [17, 17, 17,  ..., 17, 17, 17],\n         [17, 17, 17,  ..., 17, 17, 17],\n         [17, 17, 17,  ..., 17, 17, 17]],\n\n        ...,\n\n        [[ 4,  4,  4,  ..., 26, 26, 26],\n         [ 4,  4,  4,  ..., 26, 26, 26],\n         [ 4,  4,  4,  ..., 26, 26, 26],\n         ...,\n         [17, 17, 17,  ..., 19, 19, 19],\n         [17, 17, 17,  ..., 19, 19, 19],\n         [17, 17, 17,  ..., 19, 19, 19]],\n\n        [[21, 21, 21,  ...,  4,  4,  4],\n         [21, 21, 21,  ...,  4,  4,  4],\n         [21, 21, 21,  ...,  4,  4,  4],\n         ...,\n         [17, 17, 17,  ..., 19, 19, 19],\n         [17, 17, 17,  ..., 19, 19, 19],\n         [17, 17, 17,  ..., 19, 19, 19]],\n\n        [[ 4,  4,  4,  ..., 30, 30, 30],\n         [ 4,  4,  4,  ..., 30, 30, 30],\n         [ 4,  4,  4,  ..., 30, 30, 30],\n         ...,\n         [17, 17, 17,  ..., 17, 17, 17],\n         [17, 17, 17,  ..., 17, 17, 17],\n         [17, 17, 17,  ..., 17, 17, 17]]], device='cuda:0')\n```\n\nThe shape of the tensors:\n\n```\nbatch[[1]]$shape;batch[[2]]$shape\n```\n\n```\ntorch.Size([8, 3, 200, 266])\ntorch.Size([8, 200, 266])\n```\n\nDefine input and target:\n\n```\ninput = batch[[1]]\ntarget = batch[[2]]\n```\n\nFilter Void class:\n\n```\nmask = target != void_code\n```\n\n`31` will be filtered as `False`:\n\n```\nTensorMask([[[True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True],\n         ...,\n         [True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True]],\n\n        [[True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True],\n         ...,\n         [True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True]],\n\n        [[True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True],\n         ...,\n         [True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True]],\n\n        ...,\n\n        [[True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True],\n         ...,\n         [True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True]],\n\n        [[True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True],\n         ...,\n         [True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True]],\n\n        [[True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True],\n         ...,\n         [True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True],\n         [True, True, True,  ..., True, True, True]]], device='cuda:0')\n```\n\n```\n\u003e (input$argmax(dim=1L)[mask] == target[mask])\ntensor([False, False, False,  ..., False, False, False], device='cuda:0')\n```\n\n```\n\u003e (input$argmax(dim=1L)[mask] == target[mask]) %\u003e%\n              float()\ntensor([0., 0., 0.,  ..., 0., 0., 0.], device='cuda:0')\n```\n\n```\n\u003e (input$argmax(dim=1L)[mask]==target[mask]) %\u003e%\n              float() %\u003e% mean()\ntensor(0.0011, device='cuda:0')\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\nResnet34 model architecture for unet:\n\n```\nlearn = unet_learner(dls, resnet34(), metrics = acc_camvid)\n```\n\nAnd finally, fit:\n\n```\nlr = 3e-3\nwd = 1e-2\n\nlearn %\u003e% fit_one_cycle(2, slice(lr), pct_start = 0.9, wd = wd)\n```\n\n```\nepoch     train_loss  valid_loss  acc_camvid  time\n0         1.367869    1.239496    0.666145    00:25\n1         0.929434    0.661407    0.839969    00:23\n```\n\n```\nlearn %\u003e% show_results(max_n = 1, figsize = c(20,10), vmin = 1, vmax = 30)\n```\n\n\u003cimg src=\"files/unetres.png\" height=500 align=center alt=\"lr\"/\u003e\n\n## Collab (Collaborative filtering)\n\nCall libraries:\n\n```\nlibrary(zeallot)\nlibrary(magrittr)\n```\n\nGet data:\n\n```\nURLs_MOVIE_LENS_ML_100k()\n```\n\nSpecify column names:\n\n```\nc(user,item,title)  %\u003c-% list('userId','movieId','title')\n```\n\nRead datasets:\n\n```\nratings = fread('ml-100k/u.data', col.names = c(user,item,'rating','timestamp'))\nmovies = fread('ml-100k/u.item', col.names = c(item, 'title', 'date', 'N', 'url',\n                                                           paste('g',1:19,sep = '')))\n```\n\nLeft join on item:\n\n```\nrating_movie = ratings[movies[, .SD, .SDcols=c(item,title)], on = item]\n```\n\nLoad data from dataframe (R):\n\n```\ndls = CollabDataLoaders_from_df(rating_movie, seed=42, valid_pct=0.1, bs=64, item_name=title, path='ml-100k')\n```\n\nBuild model:\n\n```\nlearn = collab_learner(dls, n_factors = 40, y_range=c(0, 5.5))\n```\n\nStart learning:\n\n```\nlearn %\u003e% fit_one_cycle(1, 5e-3,  wd = 1e-1)\n```\n\nGet top 1,000 movies:\n\n```\ntop_movies = head(unique(rating_movie[ , count := .N, by = .(title)]\n                    [order(count,decreasing = T)]\n                    [, c('title','count')]),\n                   1e3)[['title']]\n```\n\nFind mean ratings for the films:\n\n```\nmean_ratings = unique(rating_movie[ , .(mean = mean(rating)), by = title])\n```\n\n```\n                                          title     mean\n   1:                          Toy Story (1995) 3.878319\n   2:                          GoldenEye (1995) 3.206107\n   3:                         Four Rooms (1995) 3.033333\n   4:                         Get Shorty (1995) 3.550239\n   5:                            Copycat (1995) 3.302326\n  ---\n1660:                      Sweet Nothing (1995) 3.000000\n1661:                         Mat' i syn (1997) 1.000000\n1662:                          B. Monkey (1998) 3.000000\n1663:                       You So Crazy (1994) 3.000000\n1664: Scream of Stone (Schrei aus Stein) (1991) 3.000000\n```\n\nExtract bias:\n\n```\nmovie_bias = learn %\u003e% get_bias(top_movies, is_item = TRUE)\n\nresult = data.table(bias = movie_bias,\n           title = top_movies)\n\nres = merge(result, mean_ratings, all.y = FALSE)\n\nres[order(bias, decreasing = TRUE)]\n```\n\n```\n                                           title        bias     mean\n   1:                           Star Wars (1977)  0.29479960 4.358491\n   2:                               Fargo (1996)  0.25264889 4.155512\n   3:                      Godfather, The (1972)  0.23247446 4.283293\n   4:           Silence of the Lambs, The (1991)  0.22765337 4.289744\n   5:                             Titanic (1997)  0.22353025 4.245714\n  ---\n 996: Children of the Corn: The Gathering (1996) -0.05671900 1.315789\n 997:                       Jungle2Jungle (1997) -0.05957306 2.439394\n 998:                  Leave It to Beaver (1997) -0.06268980 1.840909\n 999:             Speed 2: Cruise Control (1997) -0.06567496 2.131579\n1000:           Island of Dr. Moreau, The (1996) -0.07530680 2.157895\n```\n\nGet weights:\n\n```\nmovie_w = learn %\u003e% get_weights(top_movies, is_item = TRUE, convert = TRUE)\n```\n\nVisualize with highcharter:\n\n```\nrownames(movie_w) = res$title\n\nhighcharter::hchart(princomp(movie_w, cor = TRUE)) %\u003e% highcharter::hc_legend(enabled = FALSE)\n```\n\n\u003cimg src=\"files/pca.png\" height=500 align=center alt=\"PCA\"/\u003e\n\n## Text data\n\nGrab data:\n\n```\nURLs_IMDB()\n```\n\nSpecify path and small batch_size because it consumes a lot of GPU:\n\n```\npath = 'imdb'\nbs = 20\n```\n\nCreate datablock and iterator:\n\n```\nimdb_lm = DataBlock(blocks=list(TextBlock_from_folder(path, is_lm = TRUE)),\n                    get_items = partial(get_text_files(),\n                    folders = c('train', 'test', 'unsup')),\n                    splitter = RandomSplitter(0.1))\n\ndbunch_lm = imdb_lm %\u003e% dataloaders(source = path, path = path, bs = bs, seq_len = 80)\n```\n\nLoad a pretrained model and fit:\n\n```\nlearn = language_model_learner(dbunch_lm, AWD_LSTM(), drop_mult = 0.3,\n                               metrics = list(accuracy, Perplexity()))\n\nlearn %\u003e% fit_one_cycle(1, 2e-2, moms = c(0.8, 0.7, 0.8))\n```\n\n\u003e Note:\n\u003e [AWD_LSTM() can throw an error](https://github.com/fastai/fastai/issues/1439).\n\u003e In this case find and clean \".fastai\" folder.\n\n## Medical data\n\n[Import dicom data](https://www.kaggle.com/c/rsna-intracranial-hemorrhage-detection/overview):\n\n```\nimg = dcmread('hemorrhage.dcm')\n```\n\nVisualize data with different\n[windowing effects](https://radiopaedia.org/articles/windowing-ct):\n\n```\ndicom_windows = dicom_windows()\nscale = list(FALSE, TRUE, dicom_windows$brain, dicom_windows$subdural)\ntitles = c('raw','normalized','brain windowed','subdural windowed')\n\nlibrary(zeallot)\nc(fig, axs[[2]]) %\u003c-% subplots()\n\nfor (i in 1:4) {\n  img %\u003e% show(scale = scale[[i]],\n               ax = axs[[i]],\n               title=titles[i])\n}\n\nimg %\u003e% plot(dpi = 250)\n```\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"files/brain.png\" height=500 align=center alt=\"dicom\"/\u003e\n\u003c/p\u003e\n\nApply different cmaps:\n\n```\nimg %\u003e% show(cmap = cm()$gist_ncar, figsize = c(6,6))\nimg %\u003e% plot()\n```\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"files/cmap.png\" height=500 align=center alt=\"dicom\"/\u003e\n\u003c/p\u003e\n\nOr get dcm matrix and plot with ggplot:\n\n```\ntypes = c('raw', 'normalized', 'brain', 'subdural')\np_ = list()\nfor ( i in 1:length(types)) {\n  p = nandb::matrix_raster_plot(img %\u003e% get_dcm_matrix(type = types[i]))\n  p_[[i]] = p\n}\n\nggpubr::ggarrange(p_[[1]], p_[[2]], p_[[3]], p_[[4]], labels = types)\n```\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"files/dcm.png\" height=500 align=center alt=\"dicom\"/\u003e\n\u003c/p\u003e\n\nLet's try a relatively complex example:\n\n```\nlibrary(ggplot2)\n\n# crop parameters\nimg = dcmread('hemorrhage.dcm')\nres = img %\u003e% mask_from_blur(win_brain()) %\u003e%\n  mask2bbox()\n\ntypes = c('raw', 'normalized', 'brain', 'subdural')\n\n# colors for matrix filling\ncolors = list(viridis::inferno(30), viridis::magma(30),\n              viridis::plasma(30), viridis::cividis(30))\nscan_ = c('uniform_blur2d', 'gauss_blur2d')\np_ = list()\n\nfor ( i in 1:length(types)) {\n  if(i == 3) {\n    scan = scan_[1]\n  } else if (i==4) {\n    scan = scan_[2]\n  } else {\n    scan = ''\n  }\n\n  # crop with x/y_lim functions from ggplot\n  if(i==2) {\n    p = nandb::matrix_raster_plot(img %\u003e% get_dcm_matrix(type = types[i],\n                                                         scan = scan),\n                                                         colours = colors[[i]])\n    p = p + ylim(c(res[[1]][[1]],res[[2]][[1]])) + xlim(c(res[[1]][[2]],res[[2]][[2]]))\n\n  # zoom image (25 %)\n  } else if (i==4) {\n\n    img2 = img\n    img2 %\u003e% zoom(0.25)\n    p = nandb::matrix_raster_plot(img2 %\u003e% get_dcm_matrix(type = types[i],\n                                                          scan = scan),\n                                                          colours = colors[[i]])\n  } else {\n    p = nandb::matrix_raster_plot(img %\u003e% get_dcm_matrix(type = types[i],\n                                                         scan = scan),\n                                                         colours = colors[[i]])\n  }\n\n  p_[[i]] = p\n}\n\nggpubr::ggarrange(p_[[1]],\n                  p_[[2]],\n                  p_[[3]],\n                  p_[[4]],\n                  labels = paste(types[1:4],\n                                 paste(c('','',scan_))[1:4])\n                  )\n```\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"files/dcm2.png\" height=500 align=center alt=\"dicom2\"/\u003e\n\u003c/p\u003e\n\n## Additional features\n\n### Find optimal learning rate\n\nGet optimal learning rate and then fit:\n\n```\ndata = model %\u003e% lr_find()\ndata\n\n# SuggestedLRs(lr_min=0.017378008365631102, lr_steep=0.0020892962347716093)\n```\n\n```\n         lr_rates   losses\n1 0.0000001000000 5.349157\n2 0.0000001202264 5.231493\n3 0.0000001445440 5.087494\n4 0.0000001737801 5.068282\n5 0.0000002089296 5.043181\n6 0.0000002511886 5.023340\n```\n\nVisualize:\n\n```\nhighcharter::hchart(data, \"line\", highcharter::hcaes(y = losses, x = lr_rates ))\n```\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"files/lr.png\" height=500 align=center alt=\"Learning_rates\"/\u003e\n\u003c/p\u003e\n\n### Visualize batch\n\nVisualize tensor(s):\n\n```\n# get batch\nbatch = dls %\u003e% one_batch(convert = TRUE)\n\n# visualize img 9 with transformations\nmagick::image_read(batch[[1]][[9]])\n```\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"files/cat.png\" height=500 align=center alt=\"Batch\"/\u003e\n\u003c/p\u003e\n\n### Mask\n\nVisualize mask:\n\n```\nlibrary(magrittr)\nlibrary(fastai)\n\n# original image\nfns = get_image_files('camvid/images')\ncam_fn = capture.output(fns[0])\n\n# mask\nmask_fn = 'camvid/labels/0016E5_01110_P.png'\ncam_img = Image_create(cam_fn)\n\n# create mask\ntmask = Transform(Mask_create())\nmask = tmask(mask_fn)\n\n# visualize\nmask %\u003e% to_matrix() %\u003e%\n  nandb::matrix_raster_plot(colours = viridis::plasma(3)) + theme(legend.position = \"none\")\n```\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"files/mask.png\" height=500 align=center alt=\"Mask\"/\u003e\n\u003c/p\u003e\n\n### TensorPoints\n\nLoad Tiny Mnist:\n\n```\n# download\nURLs_MNIST_TINY()\n\n# black and white img\ntimg = Transform(ImageBW_create)\nmnist_fn = \"mnist_tiny/valid/3/9007.png\"\nmnist_img = timg(mnist_fn)\n\n# resize img\npnt_img = TensorImage(mnist_img %\u003e% Image_resize(size = list(28,35)))\n\n# visualize\nlibrary(ggplot2)\npnt_img %\u003e% to_matrix() %\u003e% nandb::matrix_raster_plot(colours = c('white','black')) +\n  geom_point(aes(x=0, y=0),size=2, colour=\"red\")+\n  geom_point(aes(x=0, y=35),size=2, colour=\"red\")+\n  geom_point(aes(x=28, y=0),size=2, colour=\"red\")+\n  geom_point(aes(x=28, y=35),size=2, colour=\"red\")+\n  geom_point(aes(x=9, y=17),size=2, colour=\"red\")+\n  theme(legend.position = \"none\")\n```\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"files/ggplot.png\" height=500 align=center alt=\"Mnist_3\"/\u003e\n\u003c/p\u003e\n\n### Annotations on Tiny COCO\n\n```\nlibrary(magrittr)\nlibrary(zeallot)\nlibrary(fastai)\n\nURLs_COCO_TINY()\n\nc(images, lbl_bbox) %\u003c-% get_annotations('coco_tiny/train.json')\ntimg = Transform(ImageBW_create)\nidx = 49\nc(coco_fn,bbox) %\u003c-% list(paste('coco_tiny/train',images[[idx]],sep = '/'),\n                       lbl_bbox[[idx]])\ncoco_img = timg(coco_fn)\n\ntbbox = LabeledBBox(TensorBBox(bbox[[1]]), bbox[[2]])\n\n```\n\n```\n(#2) [TensorBBox([[ 91.3000,  77.9400, 102.4300,  82.4700],\n        [ 27.5800,  77.6500,  40.7600,  82.3400]]),['tv', 'tv']]\n```\n\nVisualize:\n\n```\nlibrary(imager)\ncoco = imager::load.image(coco_fn)\nplot(coco,axes=F)\n\nfor ( i in 1:length(bbox[[1]])) {\n  rect(bbox[[1]][[i]][[1]],bbox[[1]][[i]][[2]],\n       bbox[[1]][[i]][[3]],bbox[[1]][[i]][[4]],\n       border = \"white\", lwd = 2)\n\n  text(bbox[[1]][[i]][[3]]-2.5,bbox[[1]][[i]][[4]]+2.5, labels = bbox[[2]][i],\n       offset = 2,\n       pos = 2,\n       cex = 1,\n       col = \"white\"\n  )\n}\n\n```\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"files/annotate.png\" height=500 align=center alt=\"Annotation\"/\u003e\n\u003c/p\u003e\n\nAlternatively, we could see batch via dataloader:\n\n```\nidx = 3\nc(coco_fn,bbox) %\u003c-% list(paste('coco_tiny/train',images[[idx]],sep = '/'),\n                          lbl_bbox[[idx]])\n\ncoco_bb = function(x) {\n TensorBBox_create(bbox[[1]])\n}\n\ncoco_lbl = function(x) {\n  bbox[[2]]\n}\n\ncoco_dsrc = Datasets(c(rep(coco_fn,10)),\n                     list(Image_create(), list(coco_bb),\n                     list( coco_lbl, MultiCategorize(add_na = TRUE) )\n                          ), n_inp = 1)\n\ncoco_tdl = TfmdDL(coco_dsrc, bs = 9,\n                  after_item = list(BBoxLabeler(), PointScaler(),\n                                 ToTensor()),\n                  after_batch = list(IntToFloatTensor())\n                  )\n\ncoco_tdl %\u003e% show_batch(dpi = 200)\n```\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"files/annotate_.png\" height=500 align=center alt=\"Annotation_\"/\u003e\n\u003c/p\u003e\n\n### NN module\n\nTo build a custom sequential model and pass it to learner:\n\n```\nnn$Sequential() +\n  nn$Conv2d(1L,20L,5L) +\n  nn$Conv2d(1L,20L,5L) +\n  nn$Conv2d(1L,20L,5L)\n```\n\n```\nSequential(\n  (0): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))\n  (1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))\n  (2): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))\n)\n```\n\nTo specify the name of the layers, one has to pass layer within lists, because\ntorch layers have no `name` argument:\n\n```\nnn$Sequential() +\n  nn$Conv2d(1L,20L,5L) +\n  list('my_conv2',nn$Conv2d(1L,20L,5L)) +\n  nn$Conv2d(1L,20L,5L) +\n  list('my_conv4',nn$Conv2d(1L,20L,5L))\n```\n\n```\nSequential(\n  (0): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))\n  (my_conv2): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))\n  (1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))\n  (my_conv4): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))\n)\n```\n\n## Code of Conduct\n\nPlease note that the fastai project is released with a\n[Contributor Code of Conduct](https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html).\nBy contributing to this project, you agree to abide by its terms.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feagerai%2Ffastai","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feagerai%2Ffastai","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feagerai%2Ffastai/lists"}