{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install --upgrade git+https://github.com/aleju/imgaug.git","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import imgaug.augmenters as iaa\nimport numpy as np\nimport pandas as pd\nfrom keras.callbacks import ModelCheckpoint, ReduceLROnPlateau\nfrom keras.layers import Dense\nfrom keras.layers.convolutional import Conv2D\nfrom keras.layers.core import Flatten, Dropout\nfrom keras.layers.normalization import BatchNormalization\nfrom keras.layers.pooling import MaxPool2D\nfrom keras.models import Sequential\nfrom keras.optimizers import Adam\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# read data\ndf = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\npredictions = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\n\n# create mask for train-test split\nmsk = np.random.rand(len(df)) < 0.7\n\n# train-test split [ 30% ]\ntrain = df[msk]\ntest = test = df[~msk]\n\nprint(\"Train Samples: \", len(train))\nprint(\"Test Samples: \", len(test))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(32, (3, 3), activation=\"relu\", input_shape=(28, 28, 1)))\nmodel.add(Conv2D(32, (3, 3), activation=\"relu\"))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D((2, 2)))\nmodel.add(Dropout(0.15))\n\nmodel.add(Conv2D(64, (3, 3), activation=\"relu\"))\nmodel.add(Conv2D(64, (3, 3), activation=\"relu\"))\nmodel.add(MaxPool2D((2, 2)))\nmodel.add(Dropout(0.15))\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation=\"relu\"))\nmodel.add(Dense(16, activation=\"relu\"))\nmodel.add(Dense(10, activation=\"softmax\"))\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"seq = iaa.Sequential([\n    iaa.SomeOf((0, 1), [\n        iaa.PiecewiseAffine(scale=(0.01, 0.04)),\n        iaa.PerspectiveTransform(scale=(0.01, 0.04))\n    ], random_order=True),\n    iaa.SomeOf((0, 1), [\n        iaa.geometric.Affine(rotate=(-7, 7)),\n        iaa.geometric.Affine(shear=(-5, 5)),\n    ], random_order=True)\n])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def data_generator(mode, batch_size):\n    while True:\n        if mode == \"train\" :\n            batch = train.sample(n=batch_size)\n        else :\n            batch = test.sample(n=batch_size)\n        batch_x, batch_y = [], []\n        for (index, row) in batch.iterrows():\n            img = np.array(row[1:])\n            img = img.reshape((28, 28, 1)).astype(np.uint8)\n            batch_x.append(img)\n            batch_y.append(row[0])\n        batch_x = seq(images=batch_x)\n        batch_x = np.array(batch_x).astype(np.float32)\n        batch_y = np.array(batch_y)\n        # batch_y = to_categorical(batch_y, num_classes=10)\n        batch_x /= 255.0\n        yield batch_x, batch_y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(\n    optimizer=Adam(lr=0.0005),\n    loss=\"sparse_categorical_crossentropy\",\n    metrics=[\"accuracy\"]\n)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"batch_size = 64\n\nhistory = model.fit_generator(\n    data_generator(\"train\", batch_size),\n    epochs=25,\n    steps_per_epoch= len(train) // batch_size,\n    validation_data=data_generator(\"test\", batch_size),\n    validation_steps=int(0.5 * len(train)) // batch_size,\n    use_multiprocessing=True\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()\n# summarize history for loss\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n# predict for submission\nsubmission = []\nfor (index, row) in predictions.iterrows():\n    img = np.array(row)\n    x = img.reshape((1, 28, 28, 1)).astype(np.float32)\n    x /= 255.0\n    y = np.argmax(model.predict(x)[0])\n    submission.append([index+1, y])\n\ndf = pd.DataFrame(submission, columns=[\"ImageId\", \"label\"])\ndf.to_csv(\"/kaggle/working/submission.csv\", index=False)\ndf.head(20)","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}