{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#https://youtu.be/IitIl2C3Iy8\nimport cv2\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpplt\nimport tensorflow\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.layers import Dropout, MaxPooling2D, Activation\nfrom tensorflow.keras.layers import Conv2D, Flatten\nfrom tensorflow.keras.preprocessing import image\nimport tensorflow.keras.backend as i_d_f\nimport os\nimport pandas as pd\nfrom tensorflow.keras.preprocessing import image\nfrom keras import layers\nimport keras\nfrom sklearn.model_selection import train_test_split\nimport random","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-04T14:06:06.591067Z","iopub.execute_input":"2022-11-04T14:06:06.591450Z","iopub.status.idle":"2022-11-04T14:06:06.599947Z","shell.execute_reply.started":"2022-11-04T14:06:06.591420Z","shell.execute_reply":"2022-11-04T14:06:06.598515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\ntrain = pd.read_csv('../input/digit-recognizer/train.csv')\ntest = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:06.602796Z","iopub.execute_input":"2022-11-04T14:06:06.603326Z","iopub.status.idle":"2022-11-04T14:06:12.469172Z","shell.execute_reply.started":"2022-11-04T14:06:06.603259Z","shell.execute_reply":"2022-11-04T14:06:12.467981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_file = pd.read_csv('../input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:12.472268Z","iopub.execute_input":"2022-11-04T14:06:12.472805Z","iopub.status.idle":"2022-11-04T14:06:12.491912Z","shell.execute_reply.started":"2022-11-04T14:06:12.472758Z","shell.execute_reply":"2022-11-04T14:06:12.490969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:12.493416Z","iopub.execute_input":"2022-11-04T14:06:12.494527Z","iopub.status.idle":"2022-11-04T14:06:12.526389Z","shell.execute_reply.started":"2022-11-04T14:06:12.494475Z","shell.execute_reply":"2022-11-04T14:06:12.525121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.tail()","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:12.529465Z","iopub.execute_input":"2022-11-04T14:06:12.529865Z","iopub.status.idle":"2022-11-04T14:06:12.549951Z","shell.execute_reply.started":"2022-11-04T14:06:12.529829Z","shell.execute_reply":"2022-11-04T14:06:12.548974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:12.551160Z","iopub.execute_input":"2022-11-04T14:06:12.551674Z","iopub.status.idle":"2022-11-04T14:06:15.053221Z","shell.execute_reply.started":"2022-11-04T14:06:12.551631Z","shell.execute_reply":"2022-11-04T14:06:15.051962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:15.055011Z","iopub.execute_input":"2022-11-04T14:06:15.055499Z","iopub.status.idle":"2022-11-04T14:06:15.242672Z","shell.execute_reply.started":"2022-11-04T14:06:15.055457Z","shell.execute_reply":"2022-11-04T14:06:15.241713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = train.drop(['label'], axis=1)\ny_train = train.label","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:15.244404Z","iopub.execute_input":"2022-11-04T14:06:15.245176Z","iopub.status.idle":"2022-11-04T14:06:15.336386Z","shell.execute_reply.started":"2022-11-04T14:06:15.245130Z","shell.execute_reply":"2022-11-04T14:06:15.335480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:15.338079Z","iopub.execute_input":"2022-11-04T14:06:15.338840Z","iopub.status.idle":"2022-11-04T14:06:15.365755Z","shell.execute_reply.started":"2022-11-04T14:06:15.338796Z","shell.execute_reply":"2022-11-04T14:06:15.364442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:15.367896Z","iopub.execute_input":"2022-11-04T14:06:15.368721Z","iopub.status.idle":"2022-11-04T14:06:15.378297Z","shell.execute_reply.started":"2022-11-04T14:06:15.368657Z","shell.execute_reply":"2022-11-04T14:06:15.377077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = test.copy()","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:15.383369Z","iopub.execute_input":"2022-11-04T14:06:15.383793Z","iopub.status.idle":"2022-11-04T14:06:15.476199Z","shell.execute_reply.started":"2022-11-04T14:06:15.383758Z","shell.execute_reply":"2022-11-04T14:06:15.474513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train/255.0\nx_test = x_test/255.0","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:15.477821Z","iopub.execute_input":"2022-11-04T14:06:15.478164Z","iopub.status.idle":"2022-11-04T14:06:15.601864Z","shell.execute_reply.started":"2022-11-04T14:06:15.478133Z","shell.execute_reply":"2022-11-04T14:06:15.600837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.values.reshape(-1, 28, 28, 1)\nx_test = x_test.values.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:15.603312Z","iopub.execute_input":"2022-11-04T14:06:15.603701Z","iopub.status.idle":"2022-11-04T14:06:15.609953Z","shell.execute_reply.started":"2022-11-04T14:06:15.603664Z","shell.execute_reply":"2022-11-04T14:06:15.608734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"no_images=len(x_train)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:15.612029Z","iopub.execute_input":"2022-11-04T14:06:15.613002Z","iopub.status.idle":"2022-11-04T14:06:15.621425Z","shell.execute_reply.started":"2022-11-04T14:06:15.612954Z","shell.execute_reply":"2022-11-04T14:06:15.619410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(8, 6))\nplt.imshow(x_train[random.randint(0,no_images), :, :, 0]) \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:15.623281Z","iopub.execute_input":"2022-11-04T14:06:15.624598Z","iopub.status.idle":"2022-11-04T14:06:15.861615Z","shell.execute_reply.started":"2022-11-04T14:06:15.624512Z","shell.execute_reply":"2022-11-04T14:06:15.860141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(x_train[0, :, :, 0]) \nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:15.865537Z","iopub.execute_input":"2022-11-04T14:06:15.865893Z","iopub.status.idle":"2022-11-04T14:06:16.055629Z","shell.execute_reply.started":"2022-11-04T14:06:15.865863Z","shell.execute_reply":"2022-11-04T14:06:16.054232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def sides(image):\n    fig, im = plt.subplots(nrows=1, ncols=3, figsize=(30, 20))\n    im[0].imshow(image)\n    im[0].set_title('Original Image', fontsize=24)\n    im[1].imshow(cv2.flip(image, 0))\n    im[1].set_title('Vertical Flip', fontsize=24)\n    im[2].imshow(cv2.flip(image, 1))\n    im[2].set_title('Horizontal Flip', fontsize=24)\n    plt.show()\nsides(x_train[0, :, :, 0])","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:16.057173Z","iopub.execute_input":"2022-11-04T14:06:16.057573Z","iopub.status.idle":"2022-11-04T14:06:16.588540Z","shell.execute_reply.started":"2022-11-04T14:06:16.057535Z","shell.execute_reply":"2022-11-04T14:06:16.587567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:16.590114Z","iopub.execute_input":"2022-11-04T14:06:16.590740Z","iopub.status.idle":"2022-11-04T14:06:16.595723Z","shell.execute_reply.started":"2022-11-04T14:06:16.590703Z","shell.execute_reply":"2022-11-04T14:06:16.594512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(x_train[8,:].reshape(28,28),cmap=\"gray\")\nplt.show()\nplt.imshow(x_train[9,:].reshape(28,28),cmap=\"gray\")\nplt.show()\nplt.imshow(x_train[0,:].reshape(28,28),cmap=\"gray\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:16.597241Z","iopub.execute_input":"2022-11-04T14:06:16.597613Z","iopub.status.idle":"2022-11-04T14:06:17.163354Z","shell.execute_reply.started":"2022-11-04T14:06:16.597548Z","shell.execute_reply":"2022-11-04T14:06:17.161944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_size = 0.2\nx_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size = validation_size)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:17.165142Z","iopub.execute_input":"2022-11-04T14:06:17.165597Z","iopub.status.idle":"2022-11-04T14:06:17.569168Z","shell.execute_reply.started":"2022-11-04T14:06:17.165554Z","shell.execute_reply":"2022-11-04T14:06:17.567743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = keras.utils.to_categorical(y_train, num_classes = 10)\ny_val = keras.utils.to_categorical(y_val, num_classes = 10)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:17.570522Z","iopub.execute_input":"2022-11-04T14:06:17.570877Z","iopub.status.idle":"2022-11-04T14:06:17.577864Z","shell.execute_reply.started":"2022-11-04T14:06:17.570847Z","shell.execute_reply":"2022-11-04T14:06:17.576719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tensorflow.keras.models.Sequential()\nmodel.add(layers.Conv2D(32, (3, 3), (1, 1), padding='valid', input_shape=(28, 28, 1), name='conv2d_1_1'))\nmodel.add(layers.Conv2D(32, (3, 3), (1, 1), padding='same', name='conv2d_1_2'))\nmodel.add(layers.BatchNormalization(name='bn_1'))\nmodel.add(layers.Activation('relu', name='relu_1'))\nmodel.add(layers.MaxPooling2D((2, 2), (2, 2), padding='valid', name='mp2d_1'))\nmodel.add(layers.Dropout(0.2, name='drop_1'))\nmodel.add(layers.Conv2D(64, (3, 3), (1, 1), padding='valid', name='conv2d_2_1'))\nmodel.add(layers.Conv2D(64, (3, 3), (1, 1), padding='same', name='conv2d_2_2'))\nmodel.add(layers.BatchNormalization(name='bn_2'))\nmodel.add(layers.Activation('relu', name='relu_2'))\nmodel.add(layers.MaxPooling2D((2, 2), (2, 2), padding='valid', name='mp2d_2'))\nmodel.add(layers.Dropout(0.2, name='drop_2'))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(100, activation='relu'))\nmodel.add(layers.Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:17.579146Z","iopub.execute_input":"2022-11-04T14:06:17.579577Z","iopub.status.idle":"2022-11-04T14:06:17.800406Z","shell.execute_reply.started":"2022-11-04T14:06:17.579539Z","shell.execute_reply":"2022-11-04T14:06:17.798515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = \"Adam\", loss = \"CategoricalCrossentropy\", metrics = \"accuracy\")","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:17.801772Z","iopub.execute_input":"2022-11-04T14:06:17.802961Z","iopub.status.idle":"2022-11-04T14:06:17.819835Z","shell.execute_reply.started":"2022-11-04T14:06:17.802901Z","shell.execute_reply":"2022-11-04T14:06:17.818275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:17.821615Z","iopub.execute_input":"2022-11-04T14:06:17.822290Z","iopub.status.idle":"2022-11-04T14:06:17.834379Z","shell.execute_reply.started":"2022-11-04T14:06:17.822256Z","shell.execute_reply":"2022-11-04T14:06:17.833087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training = model.fit(x_train, y_train, validation_data = (x_val, y_val), steps_per_epoch=500, epochs=100, validation_steps=200)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T14:06:17.836041Z","iopub.execute_input":"2022-11-04T14:06:17.836455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nacc = training.history['accuracy']\nval_acc = training.history['accuracy']\nloss = training.history['loss']\nval_loss = training.history['val_loss']\nepochs = range(1, len(acc) + 1)\nplt.figure(figsize=(12,8))\nplt.plot(epochs, acc, 'bo', label='Training acc')\nplt.plot(epochs, val_acc, 'b', label='Validation acc')\nplt.title('Training and validation accuracy')\nplt.legend()\nplt.figure()\nplt.figure(figsize=(12,8))\nplt.plot(epochs, loss, 'bo', label='Training loss')\nplt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, classification_report","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(x_test).argmax(axis=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(18, 24))\n\nfor i in range(100):\n    plt.subplot(10, 10, i+1)\n    plt.imshow(x_test[i], cmap='gray')\n    plt.title(f\"Pred: {predictions[i]}\")\n    plt.axis('off')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_name = \"submission.csv\"\ny_pred = pd.Series(predictions, name='Label')\nsub = pd.concat([pd.Series(range(1, 28001), name=\"ImageId\"), y_pred], axis=1)\nsub.to_csv(file_name, index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.tail()","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}