{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# loading the important libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport plotly.express as px\nimport seaborn as sns\n%matplotlib inline\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport itertools\n\n\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D ,BatchNormalization\nfrom keras.optimizers import RMSprop ,Adam\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau , LearningRateScheduler\n\n\n\nsns.set(style='white', context='notebook', palette='deep') # seaborn style ","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:33:20.192951Z","iopub.execute_input":"2024-02-16T06:33:20.193397Z","iopub.status.idle":"2024-02-16T06:33:34.362310Z","shell.execute_reply.started":"2024-02-16T06:33:20.193357Z","shell.execute_reply":"2024-02-16T06:33:34.361284Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loging The Data","metadata":{}},{"cell_type":"code","source":"# loding the data pathes \ntrain =pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest  =pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\nprint (\"the shape of train data is \" , train.shape )\nprint (\"the shape of test data is \" , test.shape )","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:33:34.363895Z","iopub.execute_input":"2024-02-16T06:33:34.364542Z","iopub.status.idle":"2024-02-16T06:33:39.864299Z","shell.execute_reply.started":"2024-02-16T06:33:34.364514Z","shell.execute_reply":"2024-02-16T06:33:39.863377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:33:39.865480Z","iopub.execute_input":"2024-02-16T06:33:39.865773Z","iopub.status.idle":"2024-02-16T06:33:39.901637Z","shell.execute_reply.started":"2024-02-16T06:33:39.865749Z","shell.execute_reply":"2024-02-16T06:33:39.900792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:33:39.903450Z","iopub.execute_input":"2024-02-16T06:33:39.903731Z","iopub.status.idle":"2024-02-16T06:33:39.955164Z","shell.execute_reply.started":"2024-02-16T06:33:39.903707Z","shell.execute_reply":"2024-02-16T06:33:39.954304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=train.drop(labels = [\"label\"],axis = 1)\ny_train=train['label']\n\n# we will delete train df after this becous it tacke 251.5 MB from memory\ndel train ","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:33:39.956318Z","iopub.execute_input":"2024-02-16T06:33:39.956562Z","iopub.status.idle":"2024-02-16T06:33:40.037602Z","shell.execute_reply.started":"2024-02-16T06:33:39.956539Z","shell.execute_reply":"2024-02-16T06:33:40.036830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# know the number of image in each calss \ndf=y_train.value_counts().reset_index()\ndf","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:33:40.038808Z","iopub.execute_input":"2024-02-16T06:33:40.039076Z","iopub.status.idle":"2024-02-16T06:33:40.056068Z","shell.execute_reply.started":"2024-02-16T06:33:40.039053Z","shell.execute_reply":"2024-02-16T06:33:40.055175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show the number  of  the  diferant labels\n\nfig=px.bar(df,x='label',y='count',title='The number of images in each label')\nfig.update_xaxes(range=[0, 9])\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:33:40.057209Z","iopub.execute_input":"2024-02-16T06:33:40.057516Z","iopub.status.idle":"2024-02-16T06:33:41.719632Z","shell.execute_reply.started":"2024-02-16T06:33:40.057484Z","shell.execute_reply":"2024-02-16T06:33:41.718782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# data preprocessing","metadata":{}},{"cell_type":"markdown","source":"## normalization ","metadata":{}},{"cell_type":"code","source":"X_train = X_train / 255.0\ntest = test / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:33:41.720846Z","iopub.execute_input":"2024-02-16T06:33:41.721124Z","iopub.status.idle":"2024-02-16T06:33:41.814434Z","shell.execute_reply.started":"2024-02-16T06:33:41.721098Z","shell.execute_reply":"2024-02-16T06:33:41.813402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## reshaping","metadata":{}},{"cell_type":"code","source":"# Reshape image in 3 dimensions (height = 28px, width = 28px , canal = 1)\nX_train = X_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:33:41.815511Z","iopub.execute_input":"2024-02-16T06:33:41.815792Z","iopub.status.idle":"2024-02-16T06:33:41.820572Z","shell.execute_reply.started":"2024-02-16T06:33:41.815769Z","shell.execute_reply":"2024-02-16T06:33:41.819639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## encoding the labels","metadata":{}},{"cell_type":"code","source":"# encoding the labels\nfrom tensorflow.keras.utils import to_categorical\n\ny_train = to_categorical(y_train, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:33:41.823766Z","iopub.execute_input":"2024-02-16T06:33:41.824020Z","iopub.status.idle":"2024-02-16T06:33:41.936093Z","shell.execute_reply.started":"2024-02-16T06:33:41.823992Z","shell.execute_reply":"2024-02-16T06:33:41.935289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## show exampels ","metadata":{}},{"cell_type":"code","source":"# show examples\nex = plt.imshow(X_train[10][:,:,0])","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:33:41.937105Z","iopub.execute_input":"2024-02-16T06:33:41.937389Z","iopub.status.idle":"2024-02-16T06:33:42.230656Z","shell.execute_reply.started":"2024-02-16T06:33:41.937365Z","shell.execute_reply":"2024-02-16T06:33:42.229761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show examples\nex = plt.imshow(X_train[100][:,:,0])","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:33:42.231673Z","iopub.execute_input":"2024-02-16T06:33:42.231935Z","iopub.status.idle":"2024-02-16T06:33:42.510571Z","shell.execute_reply.started":"2024-02-16T06:33:42.231910Z","shell.execute_reply":"2024-02-16T06:33:42.509398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ex = plt.imshow(X_train[789][:,:,0])","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:33:42.511941Z","iopub.execute_input":"2024-02-16T06:33:42.512706Z","iopub.status.idle":"2024-02-16T06:33:42.780653Z","shell.execute_reply.started":"2024-02-16T06:33:42.512664Z","shell.execute_reply":"2024-02-16T06:33:42.779725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## split the train data to train & valdation ","metadata":{}},{"cell_type":"code","source":"# Split the train and the validation set for the fitting\nX_train, X_val, Y_train, Y_val = train_test_split(X_train, y_train, test_size = 0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:33:42.781497Z","iopub.execute_input":"2024-02-16T06:33:42.781760Z","iopub.status.idle":"2024-02-16T06:33:43.175992Z","shell.execute_reply.started":"2024-02-16T06:33:42.781737Z","shell.execute_reply":"2024-02-16T06:33:43.175122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CNN Model ","metadata":{}},{"cell_type":"markdown","source":"## built the model","metadata":{}},{"cell_type":"code","source":"\nmodel = Sequential()\n\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu', input_shape = (28,28,1)))\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(Dropout(0.25))\n\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation = \"softmax\"))\n\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:33:43.177302Z","iopub.execute_input":"2024-02-16T06:33:43.177777Z","iopub.status.idle":"2024-02-16T06:33:44.016766Z","shell.execute_reply.started":"2024-02-16T06:33:43.177736Z","shell.execute_reply":"2024-02-16T06:33:44.015744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Crea el optimizador RMSprop utilizando la versión legacy\noptimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:33:44.018079Z","iopub.execute_input":"2024-02-16T06:33:44.018447Z","iopub.status.idle":"2024-02-16T06:33:44.026954Z","shell.execute_reply.started":"2024-02-16T06:33:44.018410Z","shell.execute_reply":"2024-02-16T06:33:44.026099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', optimizer = optimizer, metrics=[\"accuracy\"])\n\n\n\nannealer = LearningRateScheduler(lambda x: 1e-3 * 0.9 ** x)\n\n\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',patience=3,  verbose=1, factor=0.5, min_lr=0.00001)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:33:44.028113Z","iopub.execute_input":"2024-02-16T06:33:44.028928Z","iopub.status.idle":"2024-02-16T06:33:44.044860Z","shell.execute_reply.started":"2024-02-16T06:33:44.028894Z","shell.execute_reply":"2024-02-16T06:33:44.044030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# data augmantion ","metadata":{}},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n        featurewise_center=False,  # set input mean to 0 over the dataset\n        samplewise_center=False,  # set each sample mean to 0\n        featurewise_std_normalization=False,  # divide inputs by std of the dataset\n        samplewise_std_normalization=False,  # divide each input by its std\n        zca_whitening=False,  # apply ZCA whitening\n        rotation_range=10,  # randomly rotate images in the range (degrees, 0 to 180)\n        zoom_range = 0.1, # Randomly zoom image \n        width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)\n        height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)\n        horizontal_flip=False,  # randomly flip images\n        vertical_flip=False)  # randomly flip images\n\ndatagen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:33:44.045961Z","iopub.execute_input":"2024-02-16T06:33:44.046291Z","iopub.status.idle":"2024-02-16T06:33:44.133256Z","shell.execute_reply.started":"2024-02-16T06:33:44.046260Z","shell.execute_reply":"2024-02-16T06:33:44.132463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist = model.fit_generator(datagen.flow(X_train, Y_train, batch_size=16),\n                           steps_per_epoch=500,\n                           epochs=50, #Increase this when not on Kaggle kernel\n                           verbose=2,  #1 for ETA, 0 for silent\n                           validation_data=(X_val[:400,:], Y_val[:400,:]), #For speed\n                           callbacks=[learning_rate_reduction])","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:33:44.134535Z","iopub.execute_input":"2024-02-16T06:33:44.134822Z","iopub.status.idle":"2024-02-16T06:37:10.348799Z","shell.execute_reply.started":"2024-02-16T06:33:44.134796Z","shell.execute_reply":"2024-02-16T06:37:10.347890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# model accuracy","metadata":{}},{"cell_type":"code","source":"final_loss, final_acc = model.evaluate(X_val, Y_val, verbose=0)\nprint(\"Final loss: {0:.4f}, final accuracy: {1:.4f}\".format(final_loss, final_acc))","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:37:10.350328Z","iopub.execute_input":"2024-02-16T06:37:10.350760Z","iopub.status.idle":"2024-02-16T06:37:11.060370Z","shell.execute_reply.started":"2024-02-16T06:37:10.350724Z","shell.execute_reply":"2024-02-16T06:37:11.059367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluate the model","metadata":{}},{"cell_type":"code","source":"# Define needed variables\ntr_acc = hist.history['accuracy']\ntr_loss = hist.history['loss']\nval_acc = hist.history['val_accuracy']\nval_loss = hist.history['val_loss']\nindex_loss = np.argmin(val_loss)\nval_lowest = val_loss[index_loss]\nindex_acc = np.argmax(val_acc)\nacc_highest = val_acc[index_acc]\nEpochs = [i+1 for i in range(len(tr_acc))]\nloss_label = f'best epoch= {str(index_loss + 1)}'\nacc_label = f'best epoch= {str(index_acc + 1)}'\n\n# Plot training history\nplt.figure(figsize= (20, 8))\nplt.style.use('fivethirtyeight')\n\nplt.subplot(1, 2, 1)\nplt.plot(Epochs, tr_loss, 'r', label= 'Training loss')\nplt.plot(Epochs, val_loss, 'g', label= 'Validation loss')\nplt.scatter(index_loss + 1, val_lowest, s= 150, c= 'blue', label= loss_label)\nplt.title('Training and Validation Loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\n\nplt.subplot(1, 2, 2)\nplt.plot(Epochs, tr_acc, 'r', label= 'Training Accuracy')\nplt.plot(Epochs, val_acc, 'g', label= 'Validation Accuracy')\nplt.scatter(index_acc + 1 , acc_highest, s= 150, c= 'blue', label= acc_label)\nplt.title('Training and Validation Accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\n\nplt.tight_layout\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:37:11.061617Z","iopub.execute_input":"2024-02-16T06:37:11.061905Z","iopub.status.idle":"2024-02-16T06:37:11.735059Z","shell.execute_reply.started":"2024-02-16T06:37:11.061880Z","shell.execute_reply":"2024-02-16T06:37:11.734111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## confusion matrix ","metadata":{}},{"cell_type":"code","source":"def plot_confusion_matrix(cm, classes,normalize=False,title='Confusion matrix',cmap=plt.cm.Blues):\n\n    plt.figure(figsize=(16, 12))  \n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45)\n    plt.yticks(tick_marks, classes)\n\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, cm[i, j],\n                 horizontalalignment=\"center\",\n                 color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    plt.tight_layout()\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n\n# Predict the values from the validation dataset\nY_pred = model.predict(X_val)\n# Convert predictions classes to one hot vectors \nY_pred_classes = np.argmax(Y_pred,axis = 1) \n# Convert validation observations to one hot vectors\nY_true = np.argmax(Y_val,axis = 1) \n# compute the confusion matrix\nconfusion_mtx = confusion_matrix(Y_true, Y_pred_classes) \n# plot the confusion matrix\nplot_confusion_matrix(confusion_mtx, classes = range(10)) ","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:37:11.736147Z","iopub.execute_input":"2024-02-16T06:37:11.736436Z","iopub.status.idle":"2024-02-16T06:37:13.429102Z","shell.execute_reply.started":"2024-02-16T06:37:11.736411Z","shell.execute_reply":"2024-02-16T06:37:13.428272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## save the results","metadata":{}},{"cell_type":"code","source":"results = model.predict(test)\n\n# select the indix with the maximum probability\nresults = np.argmax(results,axis = 1)\n\nresults = pd.Series(results,name=\"Label\")\n\nsubmission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"digit_recognation.csv\",index=False)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:37:13.430307Z","iopub.execute_input":"2024-02-16T06:37:13.430604Z","iopub.status.idle":"2024-02-16T06:37:15.750162Z","shell.execute_reply.started":"2024-02-16T06:37:13.430580Z","shell.execute_reply":"2024-02-16T06:37:15.749296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}