{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport keras\nimport os\nfrom keras.layers import Input, Activation, add, Dense, Conv2D, MaxPooling2D, GlobalAveragePooling2D, Dropout, Flatten, BatchNormalization\nfrom keras.layers.advanced_activations import LeakyReLU\nfrom keras.regularizers import l2\nfrom keras.layers.core import Reshape\nfrom keras.models import Model, Sequential\nfrom keras.optimizers import RMSprop\nfrom keras.preprocessing.image import ImageDataGenerator\n\n# Using a GPU for this kernel\nconfig = tf.ConfigProto( device_count = {'GPU': 1 , 'CPU': 4} ) \nsess = tf.Session(config=config) \nkeras.backend.set_session(sess)\n\nfrom keras import backend as K\nK.set_image_dim_ordering('tf')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# Load up the data\ntrain_data = np.genfromtxt('../input/train.csv', delimiter=',')[1:]\ntrain_X = train_data[:, 1:]\ntrain_y_orig = train_data[:, :1]\n\n# converting to one-hot encoding \ntrain_y = np.zeros([train_y_orig.shape[0], 10])\nfor ind in range(train_y_orig.shape[0]):\n    train_y[ind][int(train_y_orig[ind][0])] = 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"45d043ad92c64855bc335530637698dac2d2c96e"},"cell_type":"code","source":"plt.hist(train_y_orig, color='firebrick', bins=10)\nplt.xticks(range(0,10))\nplt.xlabel(\"number\")\nplt.ylabel(\"count\")\nplt.rcParams[\"patch.force_edgecolor\"] = True\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ccaa3016d64f355dfdd3b09198c20daed541f6a7"},"cell_type":"code","source":"model = Sequential()\n\ninput_shape = (28, 28, 1)\n\nmodel.add(Conv2D(32, kernel_size=(7, 7), padding='same', activation='relu', input_shape=input_shape))\nmodel.add(Conv2D(32, kernel_size=(7, 7), padding='same', activation='relu'))\n# model.add(MaxPooling2D((2, 2), strides=(2,2)))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.4))\n\nmodel.add(Conv2D(64, kernel_size=(5, 5), strides=(2,2), padding='same', activation='relu'))\nmodel.add(Conv2D(64, kernel_size=(5, 5), strides=(2,2), padding='same', activation='relu'))\n# model.add(MaxPooling2D((2, 2), strides=(1,1)))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.4))\n\nmodel.add(Conv2D(128, kernel_size=(3, 3), strides=(3,3), padding='same', activation='relu'))\nmodel.add(Conv2D(128, kernel_size=(3, 3), strides=(3,3), padding='same', activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.2))\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dropout(0.6))\nmodel.add(Dense(10, activation='softmax'))\n\nopt = RMSprop()\nmodel.compile(loss='categorical_crossentropy',\n              optimizer=opt, \n              metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"95f477922dd13ffba1f46ea09b0a42a5dad42193"},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"182c340a1be170e580e77317d7bd4b2250f34848"},"cell_type":"code","source":"traingen = 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,"scrolled":false,"_uuid":"d03dfb284c4390c47c1b100fb932529d0e70b3c4"},"cell_type":"code","source":"batch_size=2**8\nepochs = 100\n\n# reshaping data and normalize\nn = train_X.shape[0]\ntrain_X = train_X.reshape((n, 28, 28, 1)).astype('float32') / 255\n\n# traingen.fit(train_X)\n# history = model.fit_generator(traingen.flow(train_X,train_y, batch_size=batch_size),\n#                               epochs=epochs,\n#                               steps_per_epoch=train_X.shape[0]//batch_size,\n#                              )\n\n\nhistory = model.fit(train_X, train_y, \n                    epochs=epochs,\n                    batch_size=batch_size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"da27986929c1880026e862afe0595d527170acb8"},"cell_type":"code","source":"plt.plot(history.history['acc'], color=\"dodgerblue\")\nplt.ylim(0.995, 1)\nplt.ylabel(\"accuracy\")\nplt.xlabel(\"epoch\")\nplt.show()\n\nplt.plot(history.history['loss'], color=\"firebrick\")\nplt.ylim(0, 0.015)\nplt.ylabel(\"loss\")\nplt.xlabel(\"epoch\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8505a1af64e277a7a47cbdb2b4b93dbf4d5183e9"},"cell_type":"code","source":"print(model.evaluate(train_X, train_y))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0eae7a11a4a36350bba86222ccffd44d9c8f672d"},"cell_type":"code","source":"# Load the test data\ntest_data = np.genfromtxt('../input/test.csv', delimiter=',')[1:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"db361de12f743743e28118fa8d8b1ca733bf7eec"},"cell_type":"code","source":"predictions = model.predict(test_data.reshape((test_data.shape[0], 28, 28, 1)).astype('float32') / 255)\npredictions = predictions.argmax(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"97a7c0c2f1bdf2161b865af941c7f2c1278b542c"},"cell_type":"code","source":"sub_data = np.zeros([predictions.shape[0], 2])\ncount = 0\nfor val in predictions:\n    sub_data[count] = [count + 1, val]\n    count += 1\nsub_data = sub_data.astype(int)\nnp.savetxt(fname=\"submission.csv\",\n           X=sub_data,\n           fmt='%i',\n           delimiter=',',\n           comments='',\n           header='ImageId,Label')","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}