{"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":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport seaborn as sns","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage import io, data, filters\nfrom skimage.transform import resize\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.layers import Input, Conv2D, Dense, MaxPool2D, BatchNormalization, GlobalAvgPool2D, Flatten, Dropout, Conv2DTranspose, GlobalAveragePooling2D\nfrom tensorflow.keras import regularizers","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gpu_device = tf.config.experimental.list_physical_devices('GPU')[0]\ntf.config.experimental.set_virtual_device_configuration(gpu_device, [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=6144)])\ntf.config.experimental.set_visible_devices(gpu_device, 'GPU')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = train.iloc[:,1:].values.reshape(-1,28,28,1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def randomDigits():\n    fig, ax = plt.subplots(5, 5, figsize=(10, 10))\n    for i, axi in enumerate(ax.flat):\n        index = np.random.randint(0, train_images.shape[0])\n        axi.imshow(train_images[index].reshape(28, 28), cmap='gray', interpolation='nearest')\n        axi.set(xticks=[], yticks=[],\n                xlabel=str(train.iloc[index, 0]))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"randomDigits()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(train_images, train['label'], test_size=0.1, random_state=0)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"X_train shape: \", X_train.shape)\nprint(\"X_val shape: \", X_val.shape)\nprint(\"y_train shape: \", y_train.shape)\nprint(\"y_val shape: \", y_val.shape)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plotDistribution(data, title, labels):\n  plt.figure(figsize=(10, 5))\n  plt.bar(np.arange(10), data, color=plt.cm.tab20(np.arange(10)))\n  plt.xlabel('Digits')\n  plt.ylabel('Count')\n  plt.xticks(np.arange(10), labels)\n  plt.title(title)\n  plt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotDistribution(y_train.value_counts().sort_index(), 'Train Set', y_train.value_counts().sort_index().index)\nplotDistribution(y_val.value_counts().sort_index(), 'Validation Set', y_val.value_counts().sort_index().index)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.value_counts().sort_index()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val.value_counts().sort_index()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with tf.device('/GPU:0'):\n  model = tf.keras.models.Sequential([\n      Input(shape=(28, 28, 1)),\n      Conv2D(16, 3, activation='relu', kernel_regularizer=regularizers.l2(0.002)),\n      Conv2DTranspose(16, 3, activation='relu', kernel_regularizer=regularizers.l2(0.001)),\n\n      Conv2D(32, 3, activation='relu'),\n      BatchNormalization(),\n      Conv2DTranspose(32, 3, activation='relu'),\n\n      Conv2D(64, 3, activation='relu', kernel_regularizer=regularizers.l2(0.002)),\n      Conv2DTranspose(64, 3, activation='relu', kernel_regularizer=regularizers.l2(0.001)),\n\n      MaxPool2D(2),\n      Conv2D(128, 3, activation='relu', kernel_regularizer=regularizers.l2(0.0001)),\n      Conv2D(256, 3, activation='relu', kernel_regularizer=regularizers.l2(0.001)),\n      BatchNormalization(),\n\n\n      MaxPool2D(2),\n      GlobalAveragePooling2D(),\n      Dense(128, activation='relu', kernel_regularizer=regularizers.l2(0.001)),\n      Dropout(0.2),\n\n      Dense(64, activation='relu'),\n      Dropout(0.2),\n\n      Dense(32, activation='relu'),\n      Dropout(0.2),\n\n      Dense(10, activation='sigmoid')\n  ])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils.vis_utils import plot_model\nplot_model(model)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss = 'categorical_crossentropy', metrics=['accuracy'])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with tf.device ('/device:GPU:0'):\n  history = model.fit(X_train, tf.keras.utils.to_categorical(y_train), epochs=100, batch_size=16, validation_data=(X_val, tf.keras.utils.to_categorical(y_val)), callbacks=[\n    tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True, verbose=1),\n    tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', patience=5, min_delta=0.005, verbose=1, factor=0.5, min_lr=0.0001),\n    tf.keras.callbacks.TensorBoard(log_dir='/kaggle/working/TensorLogs/Logs4/', histogram_freq=1, write_graph=True, write_images=True, update_freq='epoch', profile_batch=2, embeddings_freq=1, embeddings_metadata=None),\n  ], shuffle=True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plotLossAndAccuracy(history):\n  fig, ax = plt.subplots(1, 2, figsize=(15, 5))\n  ax[0].plot(history.history['loss'], label='Train Loss')\n  ax[0].plot(history.history['val_loss'], label='Validation Loss')\n  ax[0].legend()\n  ax[0].set_title('Loss')\n  ax[0].set_xlabel('Epochs')\n  ax[0].set_ylabel('Loss')\n  ax[1].plot(history.history['accuracy'], label='Train Accuracy')\n  ax[1].plot(history.history['val_accuracy'], label='Validation Accuracy')\n  ax[1].legend()\n  ax[1].set_title('Accuracy')\n  ax[1].set_xlabel('Epochs')\n  ax[1].set_ylabel('Accuracy')\n  plt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotLossAndAccuracy(history)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = tf.math.confusion_matrix(y_val, np.argmax(model.predict(X_val), axis=1))\nplt.figure(figsize=(10, 10))\nsns.heatmap(cm, annot=True, fmt='d', cmap='magma')\nplt.xlabel('Predicted')\nplt.ylabel('Actual')\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score\n\nprint('Accuracy: ', accuracy_score(y_val, np.argmax(model.predict(X_val), axis=1)))\nprint('Precision: ', precision_score(y_val, np.argmax(model.predict(X_val), axis=1), average='macro'))\nprint('Recall: ', recall_score(y_val, np.argmax(model.predict(X_val), axis=1), average='macro'))\nprint('F1 Score: ', f1_score(y_val, np.argmax(model.predict(X_val), axis=1), average='macro'))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = test.values.reshape(-1, 28, 28, 1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax(model.predict(test_images), axis=1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def randomDigitsTest():\n  import random\n\n  random_indices = random.sample(range(len(test_images)), 25)\n\n  fig, ax = plt.subplots(5, 5, figsize=(25, 25))\n  for index, axi in enumerate(ax.flat):\n\n    image = test_images[random_indices[index]].reshape(28, 28)\n    prediction = predictions[random_indices[index]]\n\n    axi.imshow(image, cmap='gray', interpolation='nearest', aspect='auto')\n    axi.set(xticks=[], yticks=[], xlabel=f\"Pred = {prediction}\")\n    axi.set_title(\"Actual\", fontsize=14)\n  plt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"randomDigitsTest()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'ImageId': np.arange(1, 28001), 'Label': predictions})\nsubmission.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{},"execution_count":null,"outputs":[]}]}