{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, BatchNormalization\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras.utils import to_categorical\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau\nfrom keras.applications.resnet50 import ResNet50","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('../input/Kannada-MNIST/train.csv')\ntest_df = pd.read_csv('../input/Kannada-MNIST/test.csv')\nsample_sub = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')\ndig_mnist = pd.read_csv('../input/Kannada-MNIST/Dig-MNIST.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data = np.array(train_df.iloc[:, 1:])\nval_data = np.array(dig_mnist.iloc[:, 1:])\ntest_data = np.array(test_df.iloc[:, 1:])\n\ntrain_labels = to_categorical(train_df.iloc[:, 0])\nval_labels = to_categorical(dig_mnist.iloc[:, 0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"rows, cols = 28, 28\n\ntrain_data = train_data.reshape(train_data.shape[0], rows, cols, 1)\nval_data = val_data.reshape(val_data.shape[0], rows, cols, 1)\ntest_data = test_data.reshape(test_data.shape[0], rows, cols, 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data = train_data.astype('float32')\nval_data = val_data.astype('float32')\ntest_data = test_data.astype('float32')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data /= 255.0\nval_data /= 255.0\ntest_data /= 255.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"batch_size = 64\nepochs = 5\ninput_shape = (rows, cols, 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def baseline_model():\n    model = Sequential()\n    model.add(Conv2D(64, (3,3), strides=1, padding=\"same\", activation='relu', input_shape=input_shape))\n    model.add(BatchNormalization())\n    #model.add(Dropout(0.15))\n    \n    model.add(Conv2D(64, (3,3), strides=1, padding=\"same\", activation='relu'))\n    model.add(BatchNormalization())\n    \n    model.add(MaxPooling2D(pool_size=2, strides=2, padding=\"same\"))\n    model.add(Dropout(0.15))\n    \n    model.add(Conv2D(128, (3,3), strides=1, padding=\"same\", activation='relu'))\n    model.add(BatchNormalization())\n    #model.add(Dropout(0.15))\n    \n    model.add(Conv2D(128, (3,3), strides=1, padding=\"same\", activation='relu'))\n    model.add(BatchNormalization())\n    \n    model.add(MaxPooling2D(pool_size=2, strides=2, padding=\"same\"))\n    model.add(Dropout(0.15))\n    \n    model.add(Conv2D(256, (3,3), strides=1, padding=\"same\", activation='relu'))\n    model.add(BatchNormalization())\n    #model.add(Dropout(0.15))\n    \n    model.add(Conv2D(256, (3,3), strides=1, padding=\"same\", activation='relu'))\n    model.add(BatchNormalization())\n    \n    model.add(MaxPooling2D(pool_size=2, strides=2, padding=\"same\"))\n    model.add(Dropout(0.15))\n    \n    model.add(Flatten())\n#     model.add(Dense(128, activation='relu'))\n#     model.add(Dropout(0.5))\n    model.add(Dense(256, activation='relu'))\n    #model.add(Dropout(0.5))\n    model.add(Dense(10, activation='softmax'))\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = baseline_model()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"datagen = ImageDataGenerator(\n        featurewise_center=False,  # set input mean to 0 over the dataset\n        samplewise_center=False,  # set each sample mean to 0\n        featurewise_std_normalization=False,  # divide inputs by std of the dataset\n        samplewise_std_normalization=False,  # divide each input by its std\n        zca_whitening=False,  # apply ZCA whitening\n        rotation_range=8,  # randomly rotate images in the range (degrees, 0 to 180)\n        zoom_range = 0.15, # Randomly zoom image \n        width_shift_range=0.15,  # randomly shift images horizontally (fraction of total width)\n        height_shift_range=0.15,  # 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(train_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy', \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(train_data, train_labels, batch_size=batch_size),\n                              epochs = epochs, validation_data = (val_data, val_labels),\n                              steps_per_epoch=train_data.shape[0] // batch_size, \n                              callbacks=[learning_rate_reduction])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = model.predict_classes(test_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_sub.label = preds","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_sub.to_csv('SubmissionFile.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":1}