{"cells":[{"metadata":{},"cell_type":"markdown","source":"# **1. Setting up environment**"},{"metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"## Importing packages\n\nlibrary(tidyverse) # metapackage with lots of helpful functions\nlibrary(caret)\nlibrary(plotly)\nlibrary(matrixStats)\nlibrary(grDevices)\nlibrary(gridExtra)\nlibrary(keras)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"list.files(path = \"../input\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Importing dataset**"},{"metadata":{"trusted":true},"cell_type":"code","source":"dtrain <- read.csv(\"../input/Kannada-MNIST/train.csv\")\ndtest <- read.csv(\"../input/Kannada-MNIST/test.csv\")\nsample_sub <- read.csv(\"../input/Kannada-MNIST/sample_submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Data overview**"},{"metadata":{"trusted":true},"cell_type":"code","source":"dim(dtrain)\ndim(dtest)\ndim(sample_sub)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x <- dtrain[,-1]\ny <- dtrain[,1]\ny <- factor(y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x2 <- matrix(as.numeric(unlist(x)), nrow = nrow(x))\nx_test <- matrix(as.numeric(unlist(dtest)), nrow = nrow(dtest))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"head(dtrain)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"head(dtest)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"head(sample_sub)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 2. **Data exploratory analysis**"},{"metadata":{},"cell_type":"markdown","source":"Create a function to visualize the matrix to visible number."},{"metadata":{"trusted":true},"cell_type":"code","source":"matrix.rotate <- function(img) { \n  img <- matrix(img, nrow=28L, byrow=T)\n  t(apply(img, 2, rev))\n}\nmnist.image <- function(img){\n  l = length(img)\n  rc = round(sqrt(l),0)\n  par(mfcol = c(rc, rc), mar=c(0.1,0.1,1,0.1))\n  for (i in img){\n    image(matrix.rotate(x2[i,]),\n          col = gray.colors(33), \n          axes = F, \n          main = paste(y[i]))\n  }\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"options(repr.plot.width=10, repr.plot.height=10)\nmnist.image(seq(1,25,1))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Check if any near zero value."},{"metadata":{"trusted":true},"cell_type":"code","source":"nzv <- nearZeroVar(x2)\npar(mfrow = c(1,1))\nimage(matrix(1:784 %in% nzv, 28, 28),\n      col = gray.colors(33))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# **3. Modeling**"},{"metadata":{"trusted":true},"cell_type":"code","source":"# create the data for CNN model\ndf_x <- dtrain[,-1]\ndf_y <- dtrain[,1]\ndf_y <- to_categorical(df_y)\n\n# create testing data\ndf_test_x <- dtest[,-1]\n\n# normalize to scale from 0 to 1\ndf_x <- df_x/255\ndf_test_x <- df_test_x/255\n\n# convert to matrix\ndf_x <- matrix(as.numeric(unlist(df_x)), nrow = nrow(df_x))\ndf_test_x <- matrix(as.numeric(unlist(df_test_x)), nrow = nrow(df_test_x))\n\n# reshape to convert each row to matrix 28 x 28\ndim(df_x) <- c(nrow(df_x), 28, 28, 1)\ndim(df_test_x) <- c(nrow(df_test_x), 28, 28, 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#build simple model\nmodel <- keras_model_sequential()\nmodel %>%\n\n# add first conv layer with filter F32-C3 x2  \n  layer_conv_2d(filters = 32, kernel_size = c(3,3),\n                activation = \"relu\", input_shape = c(28,28,1)) %>%\n  layer_batch_normalization() %>%\n  \n  layer_conv_2d(filters = 32, kernel_size = c(3,3),\n                activation = \"relu\") %>%\n  layer_batch_normalization() %>%\n  \n  layer_conv_2d(filters = 32, kernel_size = c(5,5), strides = c(2), \n                padding = \"Same\",\n                activation = \"relu\") %>%\n  layer_batch_normalization() %>%\n    layer_dropout(rate = 0.4) %>%\n\n# add second conv layer with filter F64-C3 x2 + F64-C5 \n  layer_conv_2d(filters = 64, kernel_size = c(3,3),\n                activation = \"relu\") %>%\n  layer_batch_normalization() %>%\n  \n  layer_conv_2d(filters = 64, kernel_size = c(3,3),\n                activation = \"relu\") %>%\n  layer_batch_normalization() %>%\n  \n  layer_conv_2d(filters = 64, kernel_size = c(5,5), strides = c(2), \n                padding = \"Same\",\n                activation = \"relu\") %>%\n  layer_batch_normalization() %>%\n    layer_dropout(rate = 0.4) %>%  \n  \n# adding dense layer  \n  \n  layer_flatten() %>%\n    layer_dense(units = 128, activation = 'relu', input_shape = c(784)) %>% \n  layer_batch_normalization() %>%\n  layer_dropout(rate = 0.4) %>% \n\n  layer_dense(units = 10, activation = 'softmax')\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Compile model."},{"metadata":{"trusted":true},"cell_type":"code","source":"#complie model\nmodel %>% compile(loss = \"categorical_crossentropy\",\n                   optimizer = \"adam\",\n                    metrics = c(\"accuracy\"))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Fitting model."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"# fit model\nmodel %>% fit(df_x, df_y,\n              epochs = 20, batch_size = 128,\n              validation_split = 0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"yhat_cnn <- model %>% predict_classes(df_test_x, batch_size=64)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# **4. Submission**"},{"metadata":{"trusted":true},"cell_type":"code","source":"# create a dataframe with CNN model\nmy_submission <- data.frame(id = sample_sub$id, label = yhat_cnn)\n\n# save our file\nwrite_csv(my_submission, 'submission_cnn.csv')","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"},"language_info":{"mimetype":"text/x-r-source","name":"R","pygments_lexer":"r","version":"3.4.2","file_extension":".r","codemirror_mode":"r"}},"nbformat":4,"nbformat_minor":1}