{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Digit Recognizer with Convolutional Neural Network","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"\n\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nimport os\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":false},"cell_type":"markdown","source":"# Data\n## Constants","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"IMAGE_SIZE = 28\nTRAIN_PATH = '../input/digit-recognizer/train.csv'\nTEST_PATH = '../input/digit-recognizer/test.csv'","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Helper functions","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def show_img(img, title=''):\n    # given a numpy array, plot it\n    vis = plt.imshow(img)\n    plt.title(title)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Load\nBecause the images are pixels flattened in a line, load the file and reshape it into a 28 x 28 image","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def parse_line(line):\n    # given an input line, transform the data into image and label\n    label = line['label'].iloc[0]\n    img_array = line.loc[:, line.columns != 'label']\n    img_array = np.array(img_array).astype('float32')\n    img_array.resize((28, 28))\n    img = Image.fromarray(test_image)\n    return label, img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_lines = pd.read_csv(TRAIN_PATH)\ntest_lines = pd.read_csv(TEST_PATH)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_label, test_img = parse_line(train_lines.loc[[0]])\nshow_img(test_img, title='label: ' + str(test_label))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Load the data into vars","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train = (train_lines.iloc[:,1:].values).astype('float32')\ny_train = (train_lines.iloc[:,0].values).astype('int32')\nx_test = (test_lines.iloc[:].values).astype('float32')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)\nx_test = x_test.reshape(x_test.shape[0], 28, 28, 1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"One hot encode the training labels","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train = tf.keras.utils.to_categorical(y_train)\ny_train.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Feed the data into an image generator","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    validation_split=0.2,\n    rotation_range=20,\n)\n\ntrain_generator = train_datagen.flow(x_train, y_train, seed=1, subset='training')\nvalidation_generator = train_datagen.flow(x_train, y_train, seed=1, subset='validation')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Model definition","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"model = tf.keras.models.Sequential([\n    # convolutions and down sampling\n    tf.keras.layers.Conv2D(16, (3, 3), activation='relu', input_shape=(IMAGE_SIZE, IMAGE_SIZE, 1), padding='same'),\n    tf.keras.layers.Conv2D(16, (3, 3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(64),\n    tf.keras.layers.Dense(32),\n    tf.keras.layers.Dense(10, activation='softmax')\n])\n\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"callbacks = []","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(x_test.shape)\nprint(x_train.shape)\nprint(y_train.shape)\nprint(model.output_shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_history = model.fit(\n    train_generator,\n    epochs=20,\n    validation_data=validation_generator,\n    validation_steps=int((y_train.shape[0] * 0.2) / 32),\n    callbacks=callbacks,\n    verbose=1\n)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Plot accuracy and loss over time","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(model_history.history['acc'])\nplt.plot(model_history.history['val_acc'])\nplt.title('Model Accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend(['Training set', 'Validation set'], loc='upper left')\nplt.show()\n\n# Loss \n\nplt.plot(model_history.history['val_loss'])\nplt.plot(model_history.history['loss'])\nplt.title('Model Loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend(['Training set', 'Test set'], loc='upper left')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Run on submission set","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = model.predict_classes(x_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submissions=pd.DataFrame({\n    'ImageId': list(range(1,len(predictions)+1)),\n    'Label': predictions\n})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submissions.to_csv('predictions.csv', index=False, header=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}