{"cells":[{"metadata":{"_uuid":"532dd88c442160b8b0c312650b546000b07bf505","_cell_guid":"2b9ff1eb-d5a1-4c1b-a0f6-1a47a017e67e","trusted":true,"collapsed":true},"cell_type":"code","source":"# This is code is modification of \n# https://github.com/keras-team/keras/blob/master/examples/mnist_cnn.py\n# Refer https://github.com/keras-team/keras/blob/master/examples/mnist_cnn.py\n# Refer https://machinelearningmastery.com/grid-search-hyperparameters-deep-learning-models-python-keras/\n# https://www.learnopencv.com/image-classification-using-feedforward-neural-network-in-keras/\n# Refer to its license for details\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom subprocess import check_output\nfrom __future__ import print_function\nimport keras\nfrom keras.datasets import mnist\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten\nfrom keras.layers import Conv2D, MaxPooling2D\n#from keras import backend as K\n\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n# we see that test.csv and train.csv are present","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3c79690fbe2b4760c257a281d152902ef122b4bb","_cell_guid":"a15669e3-7eee-4a99-bfd8-ad37cee0e18d","scrolled":false,"trusted":true,"collapsed":true},"cell_type":"code","source":"batch_size = 64\nnum_classes = 10 # 0 to 9\nepochs = 20\n\ndf = pd.read_csv('../input/train.csv', sep=',') \nprint(df.shape) # 42000, 785\n#print(type(df)) # pandas.core.frame.DataFrame.\ndf.head(1)\n# label, pixel0, pixel1, pixel2,...\n# split into input x_train and output (y_train) variables\nx = df.loc[:, df.columns != 'label'] # shape 42000, 785\ny = df[\"label\"] \nnum_rows = x.shape[0]\ninput_dim = x.shape[1] # number of columns 784\nprint(input_dim) # 784\nprint(num_rows) # 42000\nx = np.array(x)\n# reshape into 28,28,1\nx = x.reshape(num_rows,28,28,1)\nx = np.array(x, dtype=\"float\") / 255.0\n# convert the labels from integers to vectors\ny = keras.utils.to_categorical(y, num_classes)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0dabdcb09ca8ec754a51c4a040590d41f519de4e","_cell_guid":"7c5a78aa-82e0-4f09-abcd-20db1ae1afdc","trusted":true,"collapsed":true},"cell_type":"code","source":"from keras.layers.core import Activation, Flatten, Dense\n\nmodel = Sequential()\nmodel.add(Conv2D(20, (3, 3), padding=\"same\", input_shape=(28, 28, 1))) \nmodel.add(Activation(\"relu\"))\nmodel.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))\n\nmodel.add(Conv2D(50, (3, 3), padding=\"same\", input_shape=(28, 28, 1))) \nmodel.add(Activation(\"relu\"))\nmodel.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(500))\nmodel.add(Activation(\"relu\"))\n# softmax is for single class predictions, sigmoid for multi class predictions\nmodel.add(Dense(units=num_classes))\nmodel.add(Activation(\"softmax\"))\n# use binary_crossentropy if there are two classes\nmodel.compile(loss=keras.losses.binary_crossentropy,\n              optimizer=keras.optimizers.Adadelta(),\n              metrics=['accuracy'])\n# TBD learning rate, decay ??","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7d61356717865a5b1f88b700882798306a6b7c93","_cell_guid":"f9e0c56d-d46d-42d7-aacb-7576a6115802","trusted":true,"collapsed":true},"cell_type":"code","source":"#construct the image generator for data augmentation\nimport sys\nprint(\"Generating images...\")\nsys.stdout.flush()\nfrom keras.preprocessing.image import ImageDataGenerator\naug = ImageDataGenerator(rotation_range=30, width_shift_range=0.1, \\\n    height_shift_range=0.1, shear_range=0.2, zoom_range=0.2,\\\n    horizontal_flip=False, fill_mode=\"nearest\")\n\n# splitting data\nprint(\"splitting data into 75% and 25%...\")\nsys.stdout.flush()\nfrom sklearn.model_selection import train_test_split\n(x_train, valX, y_train, valY) = train_test_split(x,y,test_size=0.25, random_state=10)\n\n# train the network\nprint(\"training network...\")\nsys.stdout.flush()\n\nH = model.fit_generator(aug.flow(x_train, y_train, batch_size=batch_size), \\\n    validation_data=(valX, valY), \\\n    validation_steps=len(valX) // batch_size, \\\n    steps_per_epoch=len(x_train) // batch_size, \\\n    epochs=epochs, verbose=1)\n# model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0242dab297c16b6a72ba15601c6913cac0b883de","_cell_guid":"2f1ebb5f-d2bf-4558-9548-56702f9e2cac","trusted":true,"collapsed":true},"cell_type":"code","source":"print(\"Saving model to disk\")\nimport sys\nsys.stdout.flush()\nmodel.save(\"/tmp/mymodel\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4161fe01f990a63e47c2b67505efa1b8bae99608","_cell_guid":"eee7f9bb-1b61-48d6-acba-c21cf333efc1","trusted":true,"collapsed":true},"cell_type":"code","source":"df_test = pd.read_csv('../input/test.csv', sep=',') \ndf_test.head(1) # pixel0..pixel783\ntest_num_records = df_test.shape[0]\nprint(test_num_records) # 28000\nx_test = np.array(df_test)\nx_test = x_test.reshape(test_num_records,28,28,1)\nx_test = x_test.astype('float32')\nx_test /= 255","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"82e420274de6efcfe4ba35a1dd494539a082d201","_cell_guid":"8a54bb4b-cd5e-46de-8cd9-d64b34392733","trusted":true,"collapsed":true},"cell_type":"code","source":"from keras.models import load_model\nmymodel = load_model('/tmp/mymodel')\nyFit = mymodel.predict(x_test, batch_size=10, verbose=1)\n#print(type(yFit)) # numpy.ndarray\nimport csv  \nwith open('output.csv', 'w', newline='') as csvfile:\n    fieldnames = ['ImageId','Label']\n    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)\n    writer.writeheader()\n    for index in range(test_num_records): # 0 to test_num_records-1\n        classesProbs = yFit[index]\n        ansLabel = 0\n        maxProb = 0;\n        for idx in range(10): # 0 to 9\n            if(classesProbs[idx] > maxProb):\n                ansLabel = idx\n                maxProb = classesProbs[idx]\n        writer.writerow({'ImageId': index+1, 'Label': ansLabel})\nprint(\"Writing complete\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"970117aaba348fc3fc1268744fa90a483c2da7fa","collapsed":true},"cell_type":"code","source":"%matplotlib inline\nfrom keras.preprocessing import image\nnum_2=40\nimport matplotlib.pyplot as plt\nimport numpy as np\nwhile(num_2<=100):\n df1 = pd.read_csv('../input/test.csv', sep=',') \n df2 = pd.read_csv('output.csv', sep=',')\n x = df1.loc[:, df1.columns != 'label'] # shape 42000, 785\n image0 = x.iloc[num_2]\n x2 = np.array(image0)\n # reshape into 28,28,1\n x3 = x2.reshape(28,28)\n plt.imshow(x3)\n plt.show()\n out_row_20 = df2.iloc[num_2]\n print(out_row_20)\n num_2=num_2+1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"a8a0f78918b1dd93d471224f3298138b345c6e33"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.5","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}