{"cells":[{"metadata":{},"cell_type":"markdown","source":"# xResnet with R"},{"metadata":{},"cell_type":"markdown","source":"**Updates :** While I wanted to run a resnext or a sre_resnext (it happen in an other kernel with the quick explanation on what is what), here I just run a xresnet, that I mistaken for a resnext (too many x)."},{"metadata":{},"cell_type":"markdown","source":"Resnexts and its derivative is one of the state of the art models for computer vision and ensemble well with efficientnet, so it is normal for me to try to train one. This kernel does not use keras. Two days ago I broke my tensorflow set up on my personnal computer. Apparently trying to uninstall and reinstall different version of keras to try to run a resnext can create unexpected mess that required the use of my emergency Ubuntu 20.10 pen drive to clean out."},{"metadata":{},"cell_type":"markdown","source":"Since I did not find convenient way to implement a resnext using keras, I will switch to fastai, which have implemented them since one year, even if I did not noticed at the time, despite [commenting the notebook](https://www.kaggle.com/jhoward/from-prototyping-to-submission-fastai). In this kernel I use a wrapper of fastai that enthousiast me quite a lot. Source material is https://henry090.github.io/fastai and https://www.kaggle.com/henry090/fast-ai-from-r-timm-learner, as well as the fastai book."},{"metadata":{},"cell_type":"markdown","source":"If you have seen my other kernels it should not come as a surprise than I know fastai, since I reimplemented some kind of learning rate finder and cyclical learning rate finder in keras. What about the submissions ? Indeed, here we install a lot of things from internet. But a model trained here can be load in a python kernel, so there is no big problem about it, exept for using a Python kernel for the submission.\n"},{"metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","trusted":true},"cell_type":"code","source":"# This R environment comes with many helpful analytics packages installed\n# It is defined by the kaggle/rstats Docker image: https://github.com/kaggle/docker-rstats\n# For example, here's a helpful package to load\n\nlibrary(tidyverse) # metapackage of all tidyverse packages\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nlist.files(path = \"../input\")\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"devtools::install_github(\"rstudio/reticulate\", dependencies = FALSE) #required to not have to specify num_worker=0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"devtools::install_github(\"henry090/fastai\",dependencies=FALSE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fastai::install_fastai(gpu = TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"library(fastai)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Data loader"},{"metadata":{},"cell_type":"markdown","source":"I am using this two ressources : [the documentation of fastai](https://docs.fast.ai/vision.data.html#ImageDataLoaders.from_df) and the [tutorial of the wrapper](https://henry090.github.io/fastai/articles/basic_img_class.html)."},{"metadata":{"trusted":true},"cell_type":"code","source":"path_img = '../input/cassava-leaf-disease-classification/train_images/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels<-read_csv('/kaggle/input/cassava-leaf-disease-classification//train.csv')\n#labels = data.table::fread('/kaggle/input/cassava-leaf-disease-classification//train.csv')\nhead(labels)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Version 31 : trying a BS of 16"},{"metadata":{"trusted":true},"cell_type":"code","source":"dataloader <- fastai::ImageDataLoaders_from_df(df=labels, path=path_img, bs=16, seed=6, \n                                               item_tfms = Resize(448),\n                                               batch_tfms = aug_transforms(size=224, min_scale=0.75))\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataloader %>% show_batch(dpi = 200 ,figsize = c(6,6))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataloader %>% show_batch(dpi = 200 ,figsize = c(6,6))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataloader %>% show_batch(dpi = 200 ,figsize = c(6,6))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We can wrotte call in R style :=) :"},{"metadata":{"trusted":true},"cell_type":"code","source":"learnR <- dataloader %>% cnn_learner(xresnet50(), metrics = accuracy,  model_dir=\"/kaggle/working/\") #prettier","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"At the moment xresnet50() create out of memory errors."},{"metadata":{"trusted":true},"cell_type":"code","source":"learnR %>% lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learnR %>% plot_lr_find(dpi = 200)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Version 32 : I add a few more epochs, I did not noticed that there were still room for improvement. \nFastai V2 introduced something to quickly test the fine-tuning a model without thinking it too much :"},{"metadata":{"trusted":true},"cell_type":"code","source":"#result <- learnR %>% fit_one_cycle(n_epoch = 8)\nlearnR %>% fine_tune(epochs = 16, freeze_epochs = 8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learnR %>% plot_loss(dpi = 200)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interp <- ClassificationInterpretation_from_learner(learnR)\n\ninterp %>% plot_confusion_matrix(dpi = 200,figsize = c(6,6))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learnR$save(file=\"learnR_xresnet.pth\")","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"3.6.3"}},"nbformat":4,"nbformat_minor":4}