{"cells":[{"metadata":{},"cell_type":"markdown","source":"# DL First Project - MNIST Digit Recognizer"},{"metadata":{},"cell_type":"markdown","source":"I'm learning deep learning and this is my first project. I decided to train my skills through this data set because I want to develop my skills for computer vision."},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"#importing libreries\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nimport seaborn as sns\n\n\n#importing dataset\ntrain = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"../input/digit-recognizer/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#separating train dataset\nX = train.drop (['label'], axis=1)\nY = train[\"label\"]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now I want to check the missing data from train set and test set"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Check the data\nX.isnull().any().describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.isnull().any().describe()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Normalization"},{"metadata":{},"cell_type":"markdown","source":"Now let's go to normalize the grayscale data."},{"metadata":{"trusted":true},"cell_type":"code","source":"X = X / 255.0\ntest = test / 255.0","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Reshaping"},{"metadata":{},"cell_type":"markdown","source":"We need to reshape data because we want a 3D images."},{"metadata":{"trusted":true},"cell_type":"code","source":"#reshape to 3 dimensional\nX = X.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### OneHot Encoding "},{"metadata":{},"cell_type":"markdown","source":"In Y label we have 10 numbers, so we need to encode."},{"metadata":{"trusted":true},"cell_type":"code","source":"Y = tf.keras.utils.to_categorical(Y)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Split data "},{"metadata":{},"cell_type":"markdown","source":"I'm going to split data in train and validation set (20%)."},{"metadata":{"trusted":true},"cell_type":"code","source":"#splitting data\nX_train, X_val, y_train, y_val = train_test_split(X, Y, test_size=0.1, random_state = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"a = plt.imshow(X_train[3][:,:,0]) #let's see an example","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## CNN"},{"metadata":{},"cell_type":"markdown","source":"Now we are going to build our Convolutional Neural Network."},{"metadata":{},"cell_type":"markdown","source":"First we import what we need from tensorflow and keras."},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom keras.optimizers import RMSprop\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau, ModelCheckpoint","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(filters = 36, kernel_size = (5,5),activation ='relu', input_shape = (28,28,1)))\nmodel.add(Conv2D(filters = 36, kernel_size = (5,5),activation ='relu'))\n\nmodel.add(MaxPool2D(pool_size=(3,3)))\nmodel.add(Dropout(0.30))\n\nmodel.add(Conv2D(filters = 72, kernel_size = (3,3),activation ='relu'))\nmodel.add(Conv2D(filters = 72, kernel_size = (3,3),activation ='relu'))\n\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.30))\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation = \"softmax\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now let's set up an optimization algorithm, the cost and loss function."},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0),loss = 'categorical_crossentropy', metrics = ['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We are going to improve the images thanks to data augmentation"},{"metadata":{"trusted":true},"cell_type":"code","source":"datagen = ImageDataGenerator(\n        rotation_range=10,  # randomly rotate images in the range (degrees, 0 to 180)\n        zoom_range = 0.1, # Randomly zoom image \n        width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)\n        height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)\n        horizontal_flip=False,  # randomly flip images\n        vertical_flip=False)  # randomly flip images\n\ndatagen.fit(X_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"epochs = 10 #fix number of epochs\nbatch_size = 86 #fix batch\n\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=3, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit_generator(datagen.flow(X_train,y_train, batch_size=batch_size),\n                              epochs = epochs, validation_data = (X_val,y_val),\n                              verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size\n                              , callbacks=[learning_rate_reduction])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Evaluation"},{"metadata":{"trusted":true},"cell_type":"code","source":"accuracy = history.history['accuracy']\nval_accuracy = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = len(accuracy)\n\nplt.plot(range(epochs), accuracy, 'b', label='Training accuracy')\nplt.plot(range(epochs), val_accuracy, 'r', label='Validation accuracy')\nplt.title('Training and validation accuracy')\n\nplt.figure()\n\nplt.plot(range(epochs), loss, 'b', label='Training Loss')\nplt.plot(range(epochs), val_loss, 'r', label='Training Loss')\nplt.title('Training and validation loss')\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# predict results\nresults = model.predict(test)\n\n# select the indix with the maximum probability\nresults = np.argmax(results,axis = 1)\n\nresults = pd.Series(results,name=\"Label\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"cnn_mnist_datagen.csv\",index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}