{"metadata":{"kernelspec":{"display_name":"base","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.11.5"},"kaggle":{"accelerator":"none","dataSources":[],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense,Flatten,Dropout\nfrom keras.utils import to_categorical\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D\nfrom tensorflow.keras.regularizers import l2","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img = pd.read_csv(\"train.csv\")\ntrain_img","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_img = train_img.copy()\nimg = plot_img.iloc[:,1:].to_numpy()\nimg = img.reshape(-1, 28, 28)\n\n#plot the images in the dataset \n\nfig, axes = plt.subplots(nrows=2, ncols=5, figsize=(10, 4))\nfor i in range(2):\n    for j in range(5):\n        axes[i][j].imshow(img[i*5+j])\n        axes[i][j].set_xticks([])\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_label = train_img['label'].values\nX_train = train_img.drop(columns=['label']).values","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_label = to_categorical(y_label)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train / 255 # normalize training data","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# reshape to be [samples][pixels][width][height]\nX_train = X_train.reshape(-1, 28, 28, 1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(X_train))\nprint(len(y_label))\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#split the data into train and test images\n\nfrom sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X_train, y_label, test_size=0.2, random_state=42)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Data augmentation\ndatagen = ImageDataGenerator(\n    rotation_range=10,\n    zoom_range=0.1,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    horizontal_flip=False,\n    vertical_flip=False\n)\n\n# Fit the data generator on training data\ndatagen.fit(X_train)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convolutional_model():\n\n    model = Sequential()\n\n    model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                    activation ='relu', input_shape = (28,28,1)))\n    model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                    activation ='relu'))\n    model.add(MaxPooling2D(pool_size=(2,2)))\n    model.add(Dropout(0.25))\n\n\n    model.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                    activation ='relu'))\n    model.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                    activation ='relu'))\n    model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n    model.add(Dropout(0.25))\n\n\n    model.add(Flatten())\n    model.add(Dense(256, activation = \"relu\"))\n    model.add(Dropout(0.5))\n    model.add(Dense(10, activation = \"softmax\"))\n    \n    \n    from keras.optimizers import RMSprop\n    optimizer = RMSprop(learning_rate=0.001, rho=0.9, epsilon=1e-08, decay=0.0)\n    model.compile(optimizer = optimizer , loss = \"categorical_crossentropy\", metrics=[\"accuracy\"])\n\n    return model","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint, EarlyStopping\n\nmodel = convolutional_model()\n\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau\n\n# Reduce learning rate when a metric has stopped improving\nlr_reduction = ReduceLROnPlateau(monitor='val_loss', \n                                 patience=3, \n                                 verbose=1, \n                                 factor=0.5, \n                                 min_lr=0.00001)\n\n# Include the callback in model training\nhistory = model.fit(datagen.flow(X_train, y_train, batch_size=86), \n                    epochs=20, \n                    validation_data=(X_test, y_test),\n                    callbacks=[lr_reduction])\n\n# Evaluate the model\nscores = model.evaluate(X_test, y_test, verbose=0)\nprint(f\"Accuracy: {scores[1]*100:.2f}% \\nError: {(1-scores[1])*100:.2f}%\")\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#draw the polt statistics for the model accuracy\n\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('Model Accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the test images\ntest_img = pd.read_csv(\"test.csv\")\ntest_img = test_img.values / 255.0\ntest_img = test_img.reshape(-1, 28, 28, 1)\n\n# Make predictions on the test images\npredictions = model.predict(test_img)\npredicted_classes = predictions.argmax(axis=1)\n\n# Plot some test images with their predicted labels\ndef plot_test_images(images, predictions, num_images=10):\n    plt.figure(figsize=(20, 4))\n    for i in range(num_images):\n        ax = plt.subplot(2, num_images // 2, i + 1)\n        plt.imshow(images[i].reshape(28, 28), cmap='gray')\n        plt.title(f\"Predicted: {predictions[i]}\")\n        plt.axis('off')\n\n# Plot the test images with their predictions\nplot_test_images(test_img, predicted_classes)\n\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# save the test images predections in an .csv file\n\noutput = pd.DataFrame({'ImageId': range(1, len(test_img) + 1), 'Label': predicted_classes})\noutput.to_csv('submission.csv', index=False)\n\nprint(\"The test images predictions have been saved to'submission.csv'\")","metadata":{},"execution_count":null,"outputs":[]}]}