{"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)\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 the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras import layers\nfrom keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D\nfrom keras.layers import AveragePooling2D, MaxPooling2D, Dropout\nfrom keras.models import Model\nfrom keras.preprocessing.image import ImageDataGenerator","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train_data = pd.read_csv(\"../input/train.csv\")\ntest_data = pd.read_csv(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train = train_data[\"label\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train = train_data.drop(labels=\"label\", axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"number of training examples\", x_train.shape[0])\nprint(\"number of test examples\", test_data.shape[0] )\nprint(\"number of pixels\", x_train.shape[1])\nprint(\"number of rows and columns\", int(np.sqrt(x_train.shape[1])))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#print(np.amax(x_train[1:], axis=1))      #To check if the pixels are normalized","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Normalise the train and test data pixels\n\nx_train = x_train/255\ntest_data = test_data/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train = x_train.values.reshape(x_train.shape[0],28,28,1)\nX_test = test_data.values.reshape(test_data.shape[0],28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(X_train.shape)\nprint(X_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.preprocessing import OneHotEncoder\n\nencoder = OneHotEncoder()\nY_train = encoder.fit_transform(y_train.values.reshape(-1,1))\nY_train = Y_train.toarray()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train_split, X_test_split, y_train_split, y_test_split = train_test_split(X_train, Y_train, stratify=Y_train, test_size = 0.10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def keras_model(input_shape):\n\n    X_input = Input(input_shape)\n\n    X = ZeroPadding2D((3,3))(X_input)\n    \n    X = Conv2D(48, (5,5), strides=(1,1), name='conv0')(X)\n    X = BatchNormalization(axis = 3, name='bn0')(X)\n    X = Activation('relu')(X)\n    \n    X = Conv2D(32, (3,3), strides=(1,1), name='conv1')(X)\n    X = BatchNormalization(axis = 3, name='bn1')(X)\n    X = Activation('relu')(X)\n    \n    X = Conv2D(16, (3,3), strides=(1,1), name='conv2')(X)\n    X = BatchNormalization(axis = 3, name='bn2')(X)\n    X = Activation('relu')(X)\n    \n    X = MaxPooling2D((2,2), name='max_pool')(X)\n    \n    X = Flatten()(X)\n    X = Dense(10, activation='sigmoid', name='fc1')(X)\n    \n    model = Model(inputs = X_input, output= X, name='digit_model')\n    \n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = keras_model(X_train_split[0].shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#optimizer = Adam(lr=0.01, momentum=0.9, nesterov=True, clipnorm=1.)\nmodel.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])\n","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=10,  # randomly rotate images in the range (degrees, 0 to 180)\n        zoom_range = 0.1, # Randomly zoom image \n        width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)\n        height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)\n        horizontal_flip=False,  # randomly flip images\n        vertical_flip=False)  # randomly flip images\n\n\ndatagen.fit(X_train)\n\nbatch_size = 16\nepochs = 15\n\nhistory = model.fit_generator(datagen.flow(X_train_split,y_train_split, batch_size=batch_size),\n                              epochs = epochs, validation_data = (X_test_split,y_test_split),\n                              verbose = 2, steps_per_epoch=X_train_split.shape[0] // batch_size\n                              )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#model.fit(x = X_train_split, y=y_train_split, epochs=6, batch_size=16)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = model.evaluate(x=X_test_split, y=y_test_split)\n\nprint(\"Loss=\", (preds[0]))\nprint(\"Accuracy=\", (preds[1]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"res = model.predict(X_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_test = np.argmax(res, axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_test = pd.Series(y_test, name='label')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"final = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),y_test],axis = 1)\n\nfinal.to_csv(\"mnist_submission.csv\",index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}