{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2023-11-20T06:57:44.027900Z","iopub.execute_input":"2023-11-20T06:57:44.028407Z","iopub.status.idle":"2023-11-20T06:57:44.038638Z","shell.execute_reply.started":"2023-11-20T06:57:44.028370Z","shell.execute_reply":"2023-11-20T06:57:44.036843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Conv2D,MaxPooling2D\nfrom keras.models import Model\nfrom keras.layers import Input,InputLayer\nfrom keras import backend as K\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom tensorflow.keras.callbacks import ModelCheckpoint\nfrom tensorflow.keras.models import load_model\nimport h5py","metadata":{"execution":{"iopub.status.busy":"2023-11-20T06:59:16.134912Z","iopub.execute_input":"2023-11-20T06:59:16.135404Z","iopub.status.idle":"2023-11-20T06:59:16.665785Z","shell.execute_reply.started":"2023-11-20T06:59:16.135367Z","shell.execute_reply":"2023-11-20T06:59:16.664123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to prepare data\ndef prep_data(raw):\n    img_rows, img_cols = 28, 28\n    num_classes = 10\n    y = raw[:, 0]\n    out_y = keras.utils.to_categorical(y, num_classes)\n    x = raw[:,1:]\n    num_images = raw.shape[0]\n    out_x = x.reshape(num_images, img_rows, img_cols, 1)\n    out_x = out_x / 255\n    return out_x, out_y , img_rows, img_cols, num_classes, num_images\n\n# Load and prepare MNIST training data\nmnist_file = \"/kaggle/input/digit-recognizer/train.csv\"\nmnist_data = np.loadtxt(mnist_file, skiprows=1, delimiter=',')\nx, y, img_rows, img_cols, num_classes, num_images = prep_data(mnist_data)\nprint('input shape : {}'.format(x.shape))\nprint('output shape : {}'.format(y.shape))","metadata":{"execution":{"iopub.status.busy":"2023-11-20T06:59:35.079967Z","iopub.execute_input":"2023-11-20T06:59:35.080415Z","iopub.status.idle":"2023-11-20T06:59:39.381756Z","shell.execute_reply.started":"2023-11-20T06:59:35.080382Z","shell.execute_reply":"2023-11-20T06:59:39.380333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Initialize a model\ncnn_pilot = Sequential()\n\n# Add the first convolutional layer\ncnn_pilot.add(Conv2D(filters=16,\n                     kernel_size=5,\n                     activation='relu',\n                     input_shape=(img_rows, img_cols, 1)))\n\n# Add the first pooling layer\ncnn_pilot.add(MaxPooling2D(pool_size=(2, 2)))\n\n# Add the second convolutional layer\ncnn_pilot.add(Conv2D(filters=32,\n                     kernel_size=5,\n                     activation='relu'))\n\n# Add the 2nd pooling layer\ncnn_pilot.add(MaxPooling2D(pool_size=(2, 2)))","metadata":{"execution":{"iopub.status.busy":"2023-11-20T06:59:50.302497Z","iopub.execute_input":"2023-11-20T06:59:50.302927Z","iopub.status.idle":"2023-11-20T06:59:50.487058Z","shell.execute_reply.started":"2023-11-20T06:59:50.302892Z","shell.execute_reply":"2023-11-20T06:59:50.485827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Add flattening and dense layers\ncnn_pilot.add(Flatten())\ncnn_pilot.add(Dense(units=64,\n                    activation='relu'))\ncnn_pilot.add(Dense(units=num_classes,\n                    activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-11-20T07:00:46.854471Z","iopub.execute_input":"2023-11-20T07:00:46.854978Z","iopub.status.idle":"2023-11-20T07:00:46.873557Z","shell.execute_reply.started":"2023-11-20T07:00:46.854938Z","shell.execute_reply":"2023-11-20T07:00:46.871752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\ncnn_pilot.compile(loss=\"categorical_crossentropy\",\n                   optimizer='adam',\n                   metrics=['accuracy'])\ncnn_pilot.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-20T07:01:04.023814Z","iopub.execute_input":"2023-11-20T07:01:04.024327Z","iopub.status.idle":"2023-11-20T07:01:04.049041Z","shell.execute_reply.started":"2023-11-20T07:01:04.024287Z","shell.execute_reply":"2023-11-20T07:01:04.047997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save initial weights\nWsave = cnn_pilot.get_weights()","metadata":{"execution":{"iopub.status.busy":"2023-11-20T07:01:11.086913Z","iopub.execute_input":"2023-11-20T07:01:11.087383Z","iopub.status.idle":"2023-11-20T07:01:11.142875Z","shell.execute_reply.started":"2023-11-20T07:01:11.087346Z","shell.execute_reply":"2023-11-20T07:01:11.141931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nhistory = cnn_pilot.fit(x,\n                        y,\n                        batch_size=100,\n                        epochs=100,\n                        validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-11-20T07:01:39.622445Z","iopub.execute_input":"2023-11-20T07:01:39.622869Z","iopub.status.idle":"2023-11-20T07:21:03.200481Z","shell.execute_reply.started":"2023-11-20T07:01:39.622835Z","shell.execute_reply":"2023-11-20T07:21:03.199121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot training history\nplt.figure(figsize=(10, 5))\nplt.plot(history.history['loss'], label='train')\nplt.plot(history.history['val_loss'], label='test')\nplt.title('loss')\nplt.legend()\nplt.show()\n\nplt.figure(figsize=(10, 5))\nplt.plot(history.history['accuracy'], label='train')\nplt.plot(history.history['val_accuracy'], label='test')\nplt.title('accuracy')\nplt.legend()\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Restore initial weights\ncnn_pilot.set_weights(Wsave)\n\n# Implement Early Stopping and Model Checkpoint callbacks\nes = EarlyStopping(monitor='val_loss', mode='min', verbose=1 , patience=5)\nmc = ModelCheckpoint(\"best_cnn.h5\", monitor='val_loss', mode='min', verbose=1, save_best_only=True)\n\n# Train the model with callbacks\nhistory = cnn_pilot.fit(x,\n                        y,\n                        batch_size=100,\n                        epochs=100,\n                        validation_split=0.2,\n                        verbose=1,\n                        callbacks=[es,mc])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the best model\ncnn_best = load_model(\"best_cnn.h5\")\n\n# Evaluate and print training accuracy\n_, train_acc = cnn_best.evaluate(x, y, verbose=0)\nprint(\"Train: {}\".format(train_acc))\n\n# Plot training history for the best model\nplt.figure(figsize=(10, 5))\nplt.plot(history.history['loss'], label='train')\nplt.plot(history.history['val_loss'], label='test')\nplt.title('loss')\nplt.legend()\nplt.show()\n\nplt.figure(figsize=(10, 5))\nplt.plot(history.history['accuracy'], label='train')\nplt.plot(history.history['val_accuracy'], label='test')\nplt.title('accuracy')\nplt.legend()\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to visualize layer activations\ndef image_journey(img_num):\n    layer_outputs = [layer.output for layer in cnn_pilot.layers[: 4]]\n    activation_model = keras.Model(inputs=cnn_pilot.inputs, outputs=layer_outputs)\n    activations = activation_model.predict(x[img_num].reshape(1, 28, 28, 1))\n    layer_list = ['1st convolution layer','1st pooling layer','2nd convolution layer','2nd pooling layer']\n    \n    fig, ax = plt.subplots(1, 1 ,figsize=(4, 4))\n    plt.subplots_adjust(left=0, bottom=-0.2, right=1, top=0.9, wspace=None, hspace=0.1)\n    fig.suptitle('original image', fontsize=30)\n    ax.imshow(x[img_num][:, :, 0], cmap='gray')\n    \n    for i in range(4):\n        activation = activations[i]\n        activation_index = 0\n        fig, ax = plt.subplots(1, 6, figsize=(30, 3))\n        fig.suptitle(layer_list[i], fontsize=50)\n        plt.subplots_adjust(left=0, bottom=-0.6, right=1, top=0.6,wspace=None, hspace=0.1)\n        \n        for row in range(0, 6):\n            ax[row].imshow(activation[0, :, :, activation_index], cmap='gray')\n            activation_index += 1\n            \n# Visualize layer activations for a specific image\nimage_journey(200)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load and prepare MNIST test data\nmnist_test = \"/kaggle/input/digit-recognizer/test.csv\"\nmnist_test = np.loadtxt(mnist_test, skiprows=1, delimiter=',')\nnum_images = mnist_test.shape[0]\nout_x = mnist_test.reshape(num_images, img_rows, img_cols, 1) / 255\n\n# Make predictions on the test data\nresults = cnn_pilot.predict(out_x)\nresults = np.argmax(results, axis=1)\n\n# Create submission DataFrame and wave it to a CSV file\nsubmissions = pd.DataFrame({\"ImageId\": list(range(1, len(results) + 1)),\"Label\": results})\nsubmissions.to_csv(\"submission.csv\", index=False, header=True)","metadata":{},"execution_count":null,"outputs":[]}]}