{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\n\nimport pandas as pd\nimport numpy as np\nfrom matplotlib import pyplot as plt\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('../input/digit-recognizer/train.csv')\ntest_df = pd.read_csv('../input/digit-recognizer/test.csv')\n\nlen(train_df), len(test_df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom keras.utils import to_categorical\nfrom keras.layers import Conv2D, Dense, Flatten, MaxPool2D, Activation\nfrom keras.layers import Dropout, BatchNormalization\nfrom keras.optimizers import Adam, RMSprop\nfrom keras.models import Sequential\nfrom keras.callbacks import ReduceLROnPlateau\nfrom keras.preprocessing.image import ImageDataGenerator","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.iloc[:, 1:] /= 255\ntest_df /= 255\n\ntrain_inputs = train_df.iloc[:, 1:].values.reshape(len(train_df), 28, 28, 1)\ntest_inputs = test_df.iloc[:, :].values.reshape(len(test_df), 28, 28, 1)\n\ntrain_inputs.shape, test_inputs.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = to_categorical(train_df.label.values, num_classes=10)\nlabels[: 5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(train_inputs, labels, test_size=0.2, random_state=13)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(32, (3, 3), input_shape=(28, 28, 1), name=\"conv1\", padding=\"same\"))\nmodel.add(Activation(\"relu\", name=\"act1\"))\nmodel.add(Conv2D(32, (3, 3), input_shape=(28, 28, 1), name=\"conv2\", padding=\"same\"))\nmodel.add(Activation(\"relu\", name=\"act2\"))\nmodel.add(MaxPool2D((2, 2), name=\"mpool1\"))\nmodel.add(Dropout(0.25, name=\"drop1\"))\n\nmodel.add(Conv2D(64, (3, 3), name=\"conv3\", padding=\"same\"))\nmodel.add(Activation(\"relu\", name=\"act3\"))\nmodel.add(Conv2D(64, (3, 3), name=\"conv4\", padding=\"same\"))\nmodel.add(Activation(\"relu\", name=\"act4\"))\nmodel.add(MaxPool2D((2, 2), name=\"mpool2\"))\nmodel.add(Dropout(0.25, name=\"drop2\"))\n\nmodel.add(Flatten(name=\"flat1\"))\nmodel.add(BatchNormalization(name=\"bn1\"))\nmodel.add(Dense(512, activation=\"relu\", name=\"dense1\"))\nmodel.add(Dropout(0.5, name=\"drop3\"))\n\nmodel.add(Dense(10, activation=\"softmax\", name=\"softmax\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"datagen = ImageDataGenerator(zoom_range = 0.1,\n                             height_shift_range = 0.1,\n                             width_shift_range = 0.1,\n                             rotation_range = 10)\ndatagen.fit(X_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lr_reduce = ReduceLROnPlateau(monitor='val_acc', patience=3, factor=0.5, verbose=1, min_lr=0.00001)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer=Adam(), metrics=[\"accuracy\"], loss=\"categorical_crossentropy\")\nmodel.fit_generator(datagen.flow(X_train, y_train, batch_size=64),\n                    validation_data=(X_val, y_val), epochs=50, callbacks=[lr_reduce], verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_preds = model.predict(test_inputs)\ntest_labels = np.argmax(test_preds, axis=1)\ntest_labels[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub = pd.concat([pd.Series(range(1, 28001), name=\"ImageId\"), pd.Series(test_labels, name=\"Label\")], axis=1)\n\n\nsub.to_csv('digit_rec_cnn_submission.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}