{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Prediction without internet"},{"metadata":{},"cell_type":"markdown","source":"And here there last notebook of the serie, the one where the predictions are made and all the dependencies imported weirdly by force using the dataset, without internet."},{"metadata":{},"cell_type":"markdown","source":"Thanks to this [notebook](https://www.kaggle.com/c/cassava-leaf-disease-classification/discussion/198738) to reminds us the steps to do so. Here the magic happen in R, which a difficulty coming to the package h5py that need to be uninstall and reinstall in an older version to load keras models."},{"metadata":{},"cell_type":"markdown","source":"The model used here came from [this notebook](https://www.kaggle.com/cdk292/efficientnetb0-with-r-and-tf2-cyclic-lr), even if there is a step in a other notebook to load it and save it as a complete tf model."},{"metadata":{"trusted":true},"cell_type":"code","source":"#reticulate::py_install(packages = \"tensorflow\", version = \"2.3.0\", pip=TRUE)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","trusted":true},"cell_type":"code","source":"library(tidyverse)\nlibrary(tensorflow)\ntf$executing_eagerly()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tensorflow::tf_version()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Here I flex with my own version of keras. Basically, it is a fork with application wrapper for the efficient net."},{"metadata":{},"cell_type":"markdown","source":"**Disclaimer : I did not writte the code for the really handy applications wrappers.** It came [from this commit](https://github.com/rstudio/keras/commit/c406ec55f7bb2864ac58a17f963448810a531c18) for which the PR is hold until the fully release of tf 2.3, as stated [in this PR](https://github.com/rstudio/keras/pull/1097). I am not sure why the PR is closed."},{"metadata":{"trusted":true},"cell_type":"code","source":"#devtools::install_github(\"Cdk29/keras\", dependencies = FALSE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"install.packages(\"../input/keras-cdk292/keras-master\", repos = NULL, type = \"source\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"library(keras)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"library(reticulate)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"reticulate::virtualenv_remove(packages=\"h5py\", envname = \"r-reticulate\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"config <- reticulate::py_config()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Origin of the installation method."},{"metadata":{"trusted":true},"cell_type":"code","source":"system2(config$python, c(\"-m\", \"pip\", \"install\", \"--quiet\", shQuote(\"../input/h5py-legacy/h5py-2.10.0\")))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#image_path<-'/kaggle/input/cassava-leaf-disease-classification//train_images'","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Constructing the model to load the weight "},{"metadata":{"trusted":true},"cell_type":"code","source":"model <- load_model_tf(\"../input/fork-of-efficientnetb0-model-construction/model/\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"summary(model)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Predict"},{"metadata":{},"cell_type":"markdown","source":"Well, this part is for demo only, with internet \"On\" I cannot submit this predictions."},{"metadata":{"trusted":true},"cell_type":"code","source":"list.files(\"/kaggle/input/cassava-leaf-disease-classification/test_images/\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test<-as.data.frame(list.files(\"/kaggle/input/cassava-leaf-disease-classification/test_images/\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"colnames(test)<-\"image_id\"\ntest$image_id<-as.character(test$image_id)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"head(test)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**With shuffle = FALSE to not mix images and got the right order in the predictions.**"},{"metadata":{"trusted":true},"cell_type":"code","source":"image_path<-\"/kaggle/input/cassava-leaf-disease-classification/test_images\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_generator <- flow_images_from_dataframe(dataframe = test, \n                                              directory = image_path,\n                                              class_mode = NULL,\n                                              x_col = \"image_id\",\n                                              y_col = NULL,\n                                              target_size = c(448, 448),\n                                              shuffle = FALSE,\n                                              batch_size=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_generator","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"num_test_images<-as.numeric(dim(test)[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"num_test_images","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_submission<-read.csv(\"/kaggle/input/cassava-leaf-disease-classification/sample_submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred <- model %>% predict_generator(test_generator, steps=num_test_images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"head(pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred<-as.data.frame(pred)\ncolnames(pred)<-c(\"CBB\",\"CBSD\", \"CGM\", \"CMD\", \"Healthy\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"head(pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label<-c()\nfor (row in 1:dim(pred)[1]){\n    label<-c(label, which(pred[row,]==max(pred[row,])))\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label<-(label-1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"prediction<-as.data.frame(cbind(\"id\", label))\ncolnames(prediction)<-c(\"image_id\", \"label\")\nprediction$label<-as.numeric(label)\nhead(prediction)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"prediction$image_id<-list.files(\"/kaggle/input/cassava-leaf-disease-classification/test_images/\")\n#prediction$image_id\nhead(prediction)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"head(pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"write.csv(prediction, file='submission.csv', row.names=FALSE, quote=FALSE)","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}