{"cells":[{"metadata":{"_uuid":"65f8ecfbf00f42dbdd56c3568b8da2a48ed1504f"},"cell_type":"markdown","source":"# Digit Recognizer"},{"metadata":{"_uuid":"89c2549073d0fbca374bb5cd8ae38931fc2d58cd"},"cell_type":"markdown","source":"In this kernel a Convolutional neural network is trained to recognized Handwritten digits and predict the same from the test set given"},{"metadata":{"_uuid":"2a7c2c4e542fb8c3df0a7fac0bcffa4e20a83c6e"},"cell_type":"markdown","source":"## Importing the necessary Libraries"},{"metadata":{"trusted":true,"_uuid":"96b0765585703d986f479bbb03a015dc2bbbd541"},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom keras.utils.np_utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Conv2D, Flatten, MaxPool2D, Dropout\nfrom keras import optimizers\nfrom keras.preprocessing.image import ImageDataGenerator","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"dc66763e7d1f62aea6f921dfb62ed3b8a2aa8ef4"},"cell_type":"markdown","source":"## Reading the training and test data"},{"metadata":{"trusted":true,"_uuid":"0e9bda18aa0efb7d963056e8770310c22c1d318c"},"cell_type":"code","source":"train_data = pd.read_csv('../input/train.csv')\ntest_data = pd.read_csv('../input/test.csv')\ny_train = train_data.iloc[:,0].values\nx_train = train_data.iloc[:,1:].values\nx_test = test_data.iloc[:,:].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"975f7c13f1565308b30a0c943a78a71ca71ac1ad"},"cell_type":"code","source":"train_data.head(5)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"dbc5288c822d8a291f3fb8c6d9d19513d9a8ead6"},"cell_type":"markdown","source":"## Function to display the image"},{"metadata":{"trusted":true,"_uuid":"510c07463a9e23f01fdc9bf956ac3d6bb13f058f"},"cell_type":"code","source":"def row_calculator(number_of_images, number_of_columns):\n    if number_of_images % number_of_columns != 0:\n        return (number_of_images / number_of_columns)+1\n    else:\n        return (number_of_images / number_of_columns)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6bc2926c106e1e847b2fb7907346501857c1a593"},"cell_type":"code","source":"def display_image(x, img_size, number_of_images):\n    plt.figure(figsize = (8, 7))\n    if x.shape[0] > 0:\n        n_samples = x.shape[0]\n        x = x.reshape(n_samples, img_size, img_size)\n        number_of_rows = row_calculator(number_of_images, 4)\n        for i in range(number_of_images):\n            plt.subplot(number_of_rows, 4, i+1)\n            plt.imshow(x[i])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0378f1a514b590ea338089c1e546d80bca960e75"},"cell_type":"code","source":"display_image(x_train, 28, 16)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"580056db882cd117cf3dd7109294b695f0de78d7"},"cell_type":"code","source":"display_image(x_test, 28, 16)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4b1274b458c64695b11864a56072ae6aca0110ff"},"cell_type":"code","source":"g = sns.countplot(y_train)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c07cf3b33b2d12423ce5e72ccae0ecd4e6062b41"},"cell_type":"markdown","source":"Since most of the labels have almost equal number of training examples, the entire dataset can be used"},{"metadata":{"_uuid":"d73eae5aeb040a1cc759154bacfe19495ae952b4"},"cell_type":"markdown","source":"## Normalization and Encoding categorical variable"},{"metadata":{"trusted":true,"_uuid":"5684c07a5bfcc97620481bede97b67ef366f0b6d"},"cell_type":"code","source":"x_train = x_train/255\nx_test = x_test/255\ny_train = to_categorical(y_train, num_classes = 10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"770925e00d9bd27efad8d301001431ea1acfde02"},"cell_type":"code","source":"x_train = x_train.reshape(-1,28,28,1)\nx_test = x_test.reshape(-1,28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b0afe90d7e12f0ffb5cbe27a069ea144c1e5e674"},"cell_type":"code","source":"print(x_train.shape)\nprint(y_train.shape)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9e3b27b554895236775267f36c422039e5f6f31a"},"cell_type":"markdown","source":"## Creating the Neural Network"},{"metadata":{"trusted":true,"_uuid":"9b90c7bb02f32f1a5c5484484471d4a569d4928d"},"cell_type":"code","source":"recognizer = Sequential()\n\nrecognizer.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu', input_shape = (28,28,1)))\nrecognizer.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu'))\nrecognizer.add(MaxPool2D(pool_size=(2,2)))\nrecognizer.add(Dropout(0.25))\n\n\nrecognizer.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nrecognizer.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nrecognizer.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\nrecognizer.add(Dropout(0.25))\n\n\nrecognizer.add(Flatten())\nrecognizer.add(Dense(units = 256, input_dim = 1024, activation = 'relu'))\nrecognizer.add(Dense(units = 256, activation = \"relu\"))\nrecognizer.add(Dense(units = 256, activation = 'relu'))\nrecognizer.add(Dropout(0.5))\nrecognizer.add(Dense(10, activation = \"softmax\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"361517e18d7dfec3f5bbf0666bbf21d589c6e770"},"cell_type":"code","source":"recognizer.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7a3a551fb0118663d9d4246f8329231ce54d7f3e"},"cell_type":"code","source":"optimizer = optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d6b8317745a5edf0bc8fe984f5e62f8fb7502efc"},"cell_type":"code","source":"recognizer.compile(optimizer = optimizer , loss = \"categorical_crossentropy\", metrics=[\"accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"61c602e1b11ae370fe8c8f9692a70aa59fdf33b6"},"cell_type":"code","source":"datagen = ImageDataGenerator(\n        featurewise_center=False, \n        samplewise_center=False,  \n        featurewise_std_normalization=False,\n        samplewise_std_normalization=False,\n        zca_whitening=False,\n        rotation_range=10,\n        zoom_range = 0.1,  \n        width_shift_range=0.1, \n        height_shift_range=0.1,\n        horizontal_flip=False,\n        vertical_flip=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bedd5414061d81ddba62ac6c05b0fcb9a1315361"},"cell_type":"code","source":"datagen.fit(x_train)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"302a9c88c3144a2b28a53a4926deb1390f66c317"},"cell_type":"markdown","source":"## Training the Neural Network"},{"metadata":{"trusted":true,"_uuid":"b0b01409dc5e76e01bff61fb67543c275ea44f6b"},"cell_type":"code","source":"recognizer.fit_generator(datagen.flow(x_train,y_train, batch_size=100), epochs = 30, steps_per_epoch=x_train.shape[0] // 100)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"103b73e067738530cc106233ff4f8e8538693562"},"cell_type":"markdown","source":"## Making predictions on the test data"},{"metadata":{"trusted":false,"collapsed":true,"_uuid":"6c47aa2e3ded13069570e69a052dfbe97aeef577"},"cell_type":"code","source":"# predict results\nresults = recognizer.predict(x_test)\n\n# select the indix with the maximum probability\nresults = np.argmax(results,axis = 1)\n\nresults = pd.Series(results,name=\"Label\")\n\nsubmission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"cnn_with_visualization.csv\",index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}