{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"%matplotlib inline\n%config IPCompleter.greedy=True\n\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport sklearn\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\n\nimport tensorflow as tf\nimport tensorflow_hub as hub\nfrom tensorflow import keras\nfrom tensorflow.keras import datasets, layers, models\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D\nfrom tensorflow.keras.optimizers import Adam\n\nfrom keras.utils import Sequence\nfrom keras.utils.np_utils import to_categorical\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import LearningRateScheduler\n\nimport warnings\nwarnings.filterwarnings('ignore')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Each image is 28 pixels in height and 28 pixels in width\n# total of 784 pixels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# test = pd.read_csv(\"../../data/digitrecognizer/test.csv\")\n# train = pd.read_csv(\"../../data/digitrecognizer/train.csv\")\n\ntest = pd.read_csv(\"../input/digit-recognizer/test.csv\")\ntrain = pd.read_csv(\"../input/digit-recognizer/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.info(), test.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# split label and feature data\ntrain_labels = (train['label'])\ntrain_features = (train.iloc[:, 1:].values).astype('float32')\ntest_features = (test.values).astype('int32')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# reshape into pixel dimensions and 1 column for gray scale\ntrain_features = train_features.reshape(train_features.shape[0], 28, 28, 1)\ntest_features = test_features.reshape(test_features.shape[0], 28, 28, 1)\ntrain_features.shape, test_features.shape, train_labels.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# display sample digits \nfor i in range(16, 7):\n    plt.subplot(330 + (i+1))\n    plt.imshow(train_features[i], cmap=plt.get_cmap('gray'))\n    plt.title(train_labels[i]);\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"g = sns.countplot(train_labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# normalisation of the data so its between 0 and 1\ntrain_features = train_features.astype('float32')/255.\ntest_features = test_features.astype('float32')/255.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# convert the target labels to categorical \ntrain_labels = to_categorical(train_labels)\ntrain_features.shape, train_labels.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(train_features, train_labels, test_size=0.10, random_state=42)\nX_train.shape, X_val.shape, y_train.shape, y_val.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# setup the basic CNN\nmodel = Sequential()\nmodel.add(layers.Conv2D(32, kernel_size=(3, 3), \n                    activation='relu', \n                    kernel_initializer='he_normal',\n                    input_shape=(28, 28, 1)))\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Dropout(0.20))\n\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Dropout(0.25))\n\nmodel.add(layers.Conv2D(128, (3, 3), activation='relu'))\n#model.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Dropout(0.3))\n          \nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(128, activation='relu'))\nmodel.add(layers.Dropout(0.4))\nmodel.add(layers.Dense(10, activation='softmax'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Add optimser and compile the model.\noptimiser = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)\nmodel.compile(loss='categorical_crossentropy', optimizer = optimiser ,metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Model Parameters with learning rate scheduler\nbatchsize = 32\nnum_classes = 10\nepochs = 40\nstepsperepoch = X_train.shape[0]//64\nannealer = LearningRateScheduler(lambda x: 1e-4 * 0.9 ** x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Data augmentation for the images\n# datagen = ImageDataGenerator(\n#                     rotation_range=25,  \n#                     zoom_range = 0.10,  \n#                     width_shift_range=0.2, \n#                     height_shift_range=0.2)\nfrom keras.utils import Sequence\ndatagen = ImageDataGenerator(rotation_range=25,\n                                zoom_range = 0.10,  \n                                width_shift_range=0.2,\n                                height_shift_range=0.2)\n\ntrain_batches = datagen.flow(X_train, y_train, batch_size=32)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# fit the model \nhistory = model.fit_generator(generator=train_batches, \n                    steps_per_epoch = stepsperepoch,\n                    epochs=epochs,\n                    verbose=1,\n                    validation_data=(X_val, y_val),\n                    callbacks=[annealer])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"loss, accuracy = model.evaluate(X_train, y_train, verbose=False)\nprint(\"Training Accuracy: {:.4f}\".format(accuracy))\nloss, accuracy = model.evaluate(X_val, y_val, verbose=False)\nprint(\"Testing Accuracy:  {:.4f}\".format(accuracy))\nhistory_dict = history.history","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Plot Loss\nloss = history_dict['loss']\nval_loss = history_dict['val_loss']\n\nepochs = range(1, len(loss) + 1)\nplt.plot(epochs, loss, color='b', label='Training loss')\nplt.plot(epochs, val_loss, color='r', label='Validation loss')\nplt.title('Training and validation loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Plot Accuracy\nacc = history_dict['accuracy']\nval_acc = history_dict['val_accuracy']\n\nplt.clf() \nplt.plot(epochs, acc, color='b', label='Training acc')\nplt.plot(epochs, val_acc, color='r', label='Validation acc')\nplt.title('Training and validation accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Run predictions"},{"metadata":{"trusted":true},"cell_type":"code","source":"predict = model.predict_classes(test_features, verbose=0)\npredict.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result=pd.DataFrame({'ImageId': list(range(1,len(predict)+1)),\n                         'Label': predict})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result.to_csv('DSKsubmission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.4"}},"nbformat":4,"nbformat_minor":1}