{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":65471,"databundleVersionId":7211464,"sourceType":"competition"}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# 1) Importing Libraries","metadata":{}},{"cell_type":"code","source":"# Ignore  the warnings\nimport warnings\nwarnings.filterwarnings('always')\nwarnings.filterwarnings('ignore')\n\n# data visualisation and manipulation\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom matplotlib import style\nimport seaborn as sns\n \n#configure\n#sets matplotlib to inline and displays graphs below the corressponding cell.\n%matplotlib inline  \nstyle.use('fivethirtyeight')\nsns.set(style='whitegrid',color_codes=True)\n\n#model\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.model_selection import KFold\nfrom sklearn.metrics import accuracy_score,precision_score,recall_score,confusion_matrix,roc_curve,roc_auc_score\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.preprocessing import LabelEncoder\n\n#preprocess for Agumentaion.\nfrom keras.preprocessing.image import ImageDataGenerator\n\n#DL libraraies\nfrom keras import backend as K\nfrom keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.optimizers import Adam,SGD,Adagrad,Adadelta,RMSprop, Nadam\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Activation, Dropout, BatchNormalization\nfrom keras.utils import to_categorical\n\n# specifically for cnn\nfrom keras.layers import Dropout, Flatten, Activation\nfrom keras.layers import Conv2D, MaxPooling2D, BatchNormalization\n \nimport tensorflow as tf\nimport random as rn\n\nimport cv2                  \nimport numpy as np  \nfrom tqdm import tqdm\nimport os                   \nfrom random import shuffle  \n\n","metadata":{"execution":{"iopub.status.busy":"2023-12-18T02:20:09.352750Z","iopub.execute_input":"2023-12-18T02:20:09.353297Z","iopub.status.idle":"2023-12-18T02:20:27.928439Z","shell.execute_reply.started":"2023-12-18T02:20:09.353254Z","shell.execute_reply":"2023-12-18T02:20:27.927135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n# os.listdir('Final_Arabic_Alpha_dataset/train/')","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-12-18T02:20:27.931355Z","iopub.execute_input":"2023-12-18T02:20:27.932276Z","iopub.status.idle":"2023-12-18T02:20:27.940984Z","shell.execute_reply.started":"2023-12-18T02:20:27.932227Z","shell.execute_reply":"2023-12-18T02:20:27.939737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2) Training dataset preparation","metadata":{}},{"cell_type":"markdown","source":"### 2.1) Get the training dataset","metadata":{}},{"cell_type":"code","source":"DIR = '/kaggle/input/arabic-letters-classification/Final_Arabic_Alpha_dataset/Final_Arabic_Alpha_dataset/train'\nX = []\nZ = []\nLabel_names = []\nIMG_SIZE = 64\ndictionary_of_labels = {}","metadata":{"execution":{"iopub.status.busy":"2023-12-18T02:20:27.944508Z","iopub.execute_input":"2023-12-18T02:20:27.946069Z","iopub.status.idle":"2023-12-18T02:20:27.981684Z","shell.execute_reply.started":"2023-12-18T02:20:27.946021Z","shell.execute_reply":"2023-12-18T02:20:27.980409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_train_data():\n    for label in tqdm(os.listdir(DIR)):\n        if label == '.DS_Store' :\n            continue\n        label_path = os.path.join(DIR, label)\n        for image_name in os.listdir(label_path): \n            image_path = os.path.join(label_path, image_name)\n            result_label = image_name[8:-8]\n            img = cv2.imread(image_path, cv2.IMREAD_COLOR)\n            if img is None:\n                print('Wrong path:', img)\n            else:\n                img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))\n                X.append(np.array(img))\n                Z.append(int(label))\n        dictionary_of_labels[int(label)] = str(result_label)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T02:20:27.983031Z","iopub.execute_input":"2023-12-18T02:20:27.983642Z","iopub.status.idle":"2023-12-18T02:20:28.001264Z","shell.execute_reply.started":"2023-12-18T02:20:27.983607Z","shell.execute_reply":"2023-12-18T02:20:27.999679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"make_train_data()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T02:20:28.004664Z","iopub.execute_input":"2023-12-18T02:20:28.005434Z","iopub.status.idle":"2023-12-18T02:25:01.584138Z","shell.execute_reply.started":"2023-12-18T02:20:28.005392Z","shell.execute_reply":"2023-12-18T02:25:01.582557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(Z))\n#42559","metadata":{"execution":{"iopub.status.busy":"2023-12-18T02:25:01.586206Z","iopub.execute_input":"2023-12-18T02:25:01.587067Z","iopub.status.idle":"2023-12-18T02:25:01.594220Z","shell.execute_reply.started":"2023-12-18T02:25:01.587016Z","shell.execute_reply":"2023-12-18T02:25:01.592913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2.3) Visualizing some Random Images","metadata":{}},{"cell_type":"code","source":"fig,ax=plt.subplots(5,2)\nfig.set_size_inches(15,15)\nfor i in range(5):\n    for j in range (2):\n        l=rn.randint(0,len(Z))\n        ax[i,j].imshow(X[l])\n        ax[i,j].set_title('Letter: '+ dictionary_of_labels[Z[l]])\n        \nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T02:25:01.596048Z","iopub.execute_input":"2023-12-18T02:25:01.596569Z","iopub.status.idle":"2023-12-18T02:25:05.718578Z","shell.execute_reply.started":"2023-12-18T02:25:01.596526Z","shell.execute_reply":"2023-12-18T02:25:05.717571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2.4 ) Label Encoding the Z array (0 - 64)","metadata":{}},{"cell_type":"code","source":"le=LabelEncoder()\nY=le.fit_transform(Z)\nX=np.array(X)\n# X=X/255","metadata":{"execution":{"iopub.status.busy":"2023-12-18T02:25:05.719974Z","iopub.execute_input":"2023-12-18T02:25:05.720327Z","iopub.status.idle":"2023-12-18T02:25:06.077309Z","shell.execute_reply.started":"2023-12-18T02:25:05.720285Z","shell.execute_reply":"2023-12-18T02:25:06.076074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Z = np.array(Z) ","metadata":{"execution":{"iopub.status.busy":"2023-12-18T02:25:06.079248Z","iopub.execute_input":"2023-12-18T02:25:06.079752Z","iopub.status.idle":"2023-12-18T02:25:06.091047Z","shell.execute_reply.started":"2023-12-18T02:25:06.079687Z","shell.execute_reply":"2023-12-18T02:25:06.089279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2.5) Splitting into Training and Validation Sets","metadata":{}},{"cell_type":"code","source":"x_train,x_test,y_train,y_test=train_test_split(X,Z,test_size=0.25,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T02:25:06.093212Z","iopub.execute_input":"2023-12-18T02:25:06.094255Z","iopub.status.idle":"2023-12-18T02:25:06.385691Z","shell.execute_reply.started":"2023-12-18T02:25:06.094210Z","shell.execute_reply":"2023-12-18T02:25:06.384376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2.6) Setting the Random Seeds","metadata":{}},{"cell_type":"code","source":"np.random.seed(42)\nrn.seed(42)\ntf.random.set_seed(42)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T02:25:06.387239Z","iopub.execute_input":"2023-12-18T02:25:06.387736Z","iopub.status.idle":"2023-12-18T02:25:06.394366Z","shell.execute_reply.started":"2023-12-18T02:25:06.387667Z","shell.execute_reply":"2023-12-18T02:25:06.393102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3) Modeling","metadata":{}},{"cell_type":"markdown","source":"### 3.1) Building the ConvNet Model","metadata":{}},{"cell_type":"code","source":"# modelling starts using a CNN.\n\nmodel = Sequential()\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same',activation ='relu', input_shape = (64,64,3)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same',activation ='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n \n\nmodel.add(Conv2D(filters =96, kernel_size = (3,3),padding = 'Same',activation ='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n\nmodel.add(Conv2D(filters = 96, kernel_size = (3,3),padding = 'Same',activation ='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(512))\nmodel.add(Activation('relu'))\nmodel.add(Dense(65, activation = \"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2023-12-18T02:25:06.396410Z","iopub.execute_input":"2023-12-18T02:25:06.396902Z","iopub.status.idle":"2023-12-18T02:25:06.723386Z","shell.execute_reply.started":"2023-12-18T02:25:06.396864Z","shell.execute_reply":"2023-12-18T02:25:06.722060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3.2 ) Using a LR Annealer","metadata":{}},{"cell_type":"code","source":"batch_size=128\nepochs=50\n\n# from keras.callbacks import ReduceLROnPlateau\n# red_lr= ReduceLROnPlateau(monitor='val_accuracy',patience=5,verbose=1,factor=0.1)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T02:25:06.725161Z","iopub.execute_input":"2023-12-18T02:25:06.725926Z","iopub.status.idle":"2023-12-18T02:25:06.732977Z","shell.execute_reply.started":"2023-12-18T02:25:06.725872Z","shell.execute_reply":"2023-12-18T02:25:06.731503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3.3 ) Data Augmentation to prevent Overfitting","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.2,  # randomly shift images horizontally (fraction of total width)\n        height_shift_range=0.2,  # randomly shift images vertically (fraction of total height)\n        horizontal_flip=True,  # randomly flip images\n        vertical_flip=False)  # randomly flip images\ndatagen.fit(x_train)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T02:25:06.740862Z","iopub.execute_input":"2023-12-18T02:25:06.741388Z","iopub.status.idle":"2023-12-18T02:25:08.271648Z","shell.execute_reply.started":"2023-12-18T02:25:06.741336Z","shell.execute_reply":"2023-12-18T02:25:08.270389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3.4 ) Compiling the Keras Model & Summary","metadata":{}},{"cell_type":"code","source":"model.compile(optimizer=Adam(lr=0.01),loss='sparse_categorical_crossentropy',metrics=['accuracy'])\n\n# model.compile(optimizer = SGD(lr=0.1) ,loss='sparse_categorical_crossentropy', metrics=['accuracy']) \n\n\n# model.compile(optimizer=RMSprop(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])\n# model.compile(optimizer=Adagrad(lr=0.01), loss='categorical_crossentropy', metrics=['accuracy'])\n# model.compile(optimizer=Nadam(lr=0.002), loss='categorical_crossentropy', metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2023-12-18T02:25:08.273657Z","iopub.execute_input":"2023-12-18T02:25:08.274157Z","iopub.status.idle":"2023-12-18T02:25:08.303876Z","shell.execute_reply.started":"2023-12-18T02:25:08.274102Z","shell.execute_reply":"2023-12-18T02:25:08.302494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T02:25:08.305797Z","iopub.execute_input":"2023-12-18T02:25:08.306301Z","iopub.status.idle":"2023-12-18T02:25:08.359171Z","shell.execute_reply.started":"2023-12-18T02:25:08.306251Z","shell.execute_reply":"2023-12-18T02:25:08.357503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"# y_train = np.expand_dims(y_train, axis=-1)\n# y_test = np.expand_dims(y_test, axis=-1)\n# x_test = np.expand_dims(x_test, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T02:25:08.361119Z","iopub.execute_input":"2023-12-18T02:25:08.361968Z","iopub.status.idle":"2023-12-18T02:25:08.369001Z","shell.execute_reply.started":"2023-12-18T02:25:08.361912Z","shell.execute_reply":"2023-12-18T02:25:08.367906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(x_train.shape)\n# print(y_train.shape)\n# print(x_test.shape)\n# print(y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T02:25:08.370222Z","iopub.execute_input":"2023-12-18T02:25:08.370715Z","iopub.status.idle":"2023-12-18T02:25:08.385435Z","shell.execute_reply.started":"2023-12-18T02:25:08.370665Z","shell.execute_reply":"2023-12-18T02:25:08.383270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# History = model.fit_generator(datagen.flow(x_train,y_train, batch_size=batch_size),\n                              #epochs = epochs, validation_data = (x_test,y_test),\n                            #  verbose = 1, steps_per_epoch=250)\nHistory = model.fit(x_train,y_train,epochs=epochs,batch_size=batch_size,validation_data = (x_test,y_test))","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-12-18T02:25:08.387522Z","iopub.execute_input":"2023-12-18T02:25:08.388004Z","iopub.status.idle":"2023-12-18T04:54:32.569600Z","shell.execute_reply.started":"2023-12-18T02:25:08.387963Z","shell.execute_reply":"2023-12-18T04:54:32.568661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4 ) Evaluating the Model Performance","metadata":{}},{"cell_type":"code","source":"plt.plot(History.history['loss'])\nplt.plot(History.history['val_loss'])\nplt.title('Model Loss')\nplt.ylabel('Loss')\nplt.xlabel('Epochs')\nplt.legend(['train', 'test'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T04:54:32.571000Z","iopub.execute_input":"2023-12-18T04:54:32.571518Z","iopub.status.idle":"2023-12-18T04:54:32.979580Z","shell.execute_reply.started":"2023-12-18T04:54:32.571474Z","shell.execute_reply":"2023-12-18T04:54:32.978209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(History.history['accuracy'])\nplt.plot(History.history['val_accuracy'])\nplt.title('Model Accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epochs')\nplt.legend(['train', 'test'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T04:54:32.981051Z","iopub.execute_input":"2023-12-18T04:54:32.981432Z","iopub.status.idle":"2023-12-18T04:54:33.424290Z","shell.execute_reply.started":"2023-12-18T04:54:32.981399Z","shell.execute_reply":"2023-12-18T04:54:33.423150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5 ) Visualizing Predictons on the Validation Set","metadata":{}},{"cell_type":"code","source":"pred=model.predict(x_test)\npred_digits=np.argmax(pred,axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T04:54:33.425546Z","iopub.execute_input":"2023-12-18T04:54:33.425927Z","iopub.status.idle":"2023-12-18T04:54:54.540885Z","shell.execute_reply.started":"2023-12-18T04:54:33.425891Z","shell.execute_reply":"2023-12-18T04:54:54.539302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 5.1) Get a list of correct and incorrect predictions","metadata":{}},{"cell_type":"code","source":"i=0\nprop_class=[]\nmis_class=[]\n\nfor i in range(len(y_test)):\n    if(y_test[i]==pred_digits[i]):\n        prop_class.append(i)\n#     if(len(prop_class)==8):\n#         break\n\ni=0\nfor i in range(len(y_test)):\n    if(y_test[i] != pred_digits[i]):\n        mis_class.append(i)\n#     if(len(mis_class) == 8):\n#         break","metadata":{"execution":{"iopub.status.busy":"2023-12-18T04:54:54.542485Z","iopub.execute_input":"2023-12-18T04:54:54.542956Z","iopub.status.idle":"2023-12-18T04:54:54.566808Z","shell.execute_reply.started":"2023-12-18T04:54:54.542912Z","shell.execute_reply":"2023-12-18T04:54:54.565336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Number of correct predictions: {len(prop_class)}\")\nprint(f\"Number of incorrect predictions: {len(mis_class)}\")\nprint(f\"Accuracy: {len(prop_class)/(len(prop_class) + len(mis_class) ) } \")","metadata":{"execution":{"iopub.status.busy":"2023-12-18T04:54:54.568356Z","iopub.execute_input":"2023-12-18T04:54:54.568795Z","iopub.status.idle":"2023-12-18T04:54:54.579311Z","shell.execute_reply.started":"2023-12-18T04:54:54.568730Z","shell.execute_reply":"2023-12-18T04:54:54.577912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 5.2) Visualizing two smaples of correct and incorrect predictions","metadata":{}},{"cell_type":"markdown","source":"#### Correct","metadata":{}},{"cell_type":"code","source":"warnings.filterwarnings('always')\nwarnings.filterwarnings('ignore')\n\ncount=0\nfig,ax=plt.subplots(4,2)\nfig.set_size_inches(15,15)\nfor i in range (4):\n    for j in range (2):\n        ax[i,j].imshow(x_test[prop_class[count]])\n        ax[i,j].set_title(\"Predicted Letter : \" + str(dictionary_of_labels[pred_digits[prop_class[count]]])+\n                          \"\\n\" + \"Actual Letter : \" + str( dictionary_of_labels[y_test[prop_class[count]]]))\n\n        plt.tight_layout()\n        count+=1","metadata":{"execution":{"iopub.status.busy":"2023-12-18T04:54:54.581251Z","iopub.execute_input":"2023-12-18T04:54:54.581607Z","iopub.status.idle":"2023-12-18T04:55:00.792986Z","shell.execute_reply.started":"2023-12-18T04:54:54.581577Z","shell.execute_reply":"2023-12-18T04:55:00.791992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Incorrect","metadata":{}},{"cell_type":"code","source":"warnings.filterwarnings('always')\nwarnings.filterwarnings('ignore')\n\ncount=0\nfig,ax=plt.subplots(4,2)\nfig.set_size_inches(15,15)\nfor i in range (4):\n    for j in range (2):\n        ax[i,j].imshow(x_test[mis_class[count]])\n        ax[i,j].set_title(\"Predicted Letter : \" + str( dictionary_of_labels[pred_digits[mis_class[count]]])+\n                          \"\\n\" + \"Actual Letter : \" + str( dictionary_of_labels[y_test[mis_class[count]]]))\n\n        plt.tight_layout()\n        count+=1","metadata":{"execution":{"iopub.status.busy":"2023-12-18T04:55:00.794267Z","iopub.execute_input":"2023-12-18T04:55:00.795245Z","iopub.status.idle":"2023-12-18T04:55:06.692844Z","shell.execute_reply.started":"2023-12-18T04:55:00.795205Z","shell.execute_reply":"2023-12-18T04:55:06.691652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 6) Perediction of Test Images","metadata":{}},{"cell_type":"markdown","source":"### 6.1) Get the test images","metadata":{}},{"cell_type":"code","source":"DIR_Test = '/kaggle/input/arabic-letters-classification/Final_Arabic_Alpha_dataset/Final_Arabic_Alpha_dataset/test'\nX_test = []\nimage_names = []","metadata":{"execution":{"iopub.status.busy":"2023-12-18T04:55:06.694298Z","iopub.execute_input":"2023-12-18T04:55:06.695053Z","iopub.status.idle":"2023-12-18T04:55:06.701062Z","shell.execute_reply.started":"2023-12-18T04:55:06.695001Z","shell.execute_reply":"2023-12-18T04:55:06.699805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_test_data():\n        for image_name in tqdm(os.listdir(DIR_Test)): \n            image_path = os.path.join(DIR_Test, image_name)\n            image_names.append(image_name)\n            img = cv2.imread(image_path, cv2.IMREAD_COLOR)\n            if img is None:\n                print('Wrong path:', img)\n            else:\n                img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))\n                X_test.append(np.array(img))","metadata":{"execution":{"iopub.status.busy":"2023-12-18T04:55:06.702651Z","iopub.execute_input":"2023-12-18T04:55:06.703967Z","iopub.status.idle":"2023-12-18T04:55:06.721737Z","shell.execute_reply.started":"2023-12-18T04:55:06.703929Z","shell.execute_reply":"2023-12-18T04:55:06.720643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"make_test_data()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T04:55:06.723321Z","iopub.execute_input":"2023-12-18T04:55:06.723750Z","iopub.status.idle":"2023-12-18T04:56:45.855436Z","shell.execute_reply.started":"2023-12-18T04:55:06.723687Z","shell.execute_reply":"2023-12-18T04:56:45.854309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test=np.array(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T04:56:45.856974Z","iopub.execute_input":"2023-12-18T04:56:45.857341Z","iopub.status.idle":"2023-12-18T04:56:45.930770Z","shell.execute_reply.started":"2023-12-18T04:56:45.857309Z","shell.execute_reply":"2023-12-18T04:56:45.929259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 6.2) Make predictions of test images","metadata":{}},{"cell_type":"code","source":"pred_test=model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T04:56:45.932679Z","iopub.execute_input":"2023-12-18T04:56:45.933176Z","iopub.status.idle":"2023-12-18T04:57:01.519884Z","shell.execute_reply.started":"2023-12-18T04:56:45.933132Z","shell.execute_reply":"2023-12-18T04:57:01.518739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_digits_test=np.argmax(pred_test,axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T04:57:01.521981Z","iopub.execute_input":"2023-12-18T04:57:01.522818Z","iopub.status.idle":"2023-12-18T04:57:01.529016Z","shell.execute_reply.started":"2023-12-18T04:57:01.522770Z","shell.execute_reply":"2023-12-18T04:57:01.527592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 6.3) Visualisations of some random predicted images","metadata":{}},{"cell_type":"code","source":"fig,ax=plt.subplots(8,3)\nfig.set_size_inches(15,15)\nfor i in tqdm(range (8)):\n    for j in range (3):\n        image_number=rn.randint(0,len(X_test))\n        ax[i,j].imshow(X_test[image_number])\n        ax[i,j].set_title(f\" {image_names[image_number]} Predicted Letter : \" +\n                          str(dictionary_of_labels[pred_digits_test[image_number]]))\n        plt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T04:57:01.530487Z","iopub.execute_input":"2023-12-18T04:57:01.530949Z","iopub.status.idle":"2023-12-18T04:57:31.543910Z","shell.execute_reply.started":"2023-12-18T04:57:01.530909Z","shell.execute_reply":"2023-12-18T04:57:31.542778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 6.3) exporting the predicted","metadata":{}},{"cell_type":"code","source":"predictions = pred_digits_test\npredictions_label = [dictionary_of_labels[i] for i in pred_digits_test]\nimage_numbers = [int(s[:-4]) for s in image_names]","metadata":{"execution":{"iopub.status.busy":"2023-12-18T04:57:31.545420Z","iopub.execute_input":"2023-12-18T04:57:31.546127Z","iopub.status.idle":"2023-12-18T04:57:31.560888Z","shell.execute_reply.started":"2023-12-18T04:57:31.546084Z","shell.execute_reply":"2023-12-18T04:57:31.559982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({'ID': image_numbers, 'Label': predictions})","metadata":{"execution":{"iopub.status.busy":"2023-12-18T04:57:31.562089Z","iopub.execute_input":"2023-12-18T04:57:31.562539Z","iopub.status.idle":"2023-12-18T04:57:31.581660Z","shell.execute_reply.started":"2023-12-18T04:57:31.562498Z","shell.execute_reply":"2023-12-18T04:57:31.580600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.sort_values(by='ID')","metadata":{"execution":{"iopub.status.busy":"2023-12-18T04:57:31.583694Z","iopub.execute_input":"2023-12-18T04:57:31.584587Z","iopub.status.idle":"2023-12-18T04:57:31.605987Z","shell.execute_reply.started":"2023-12-18T04:57:31.584533Z","shell.execute_reply":"2023-12-18T04:57:31.604789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('/kaggle/working/predictions5.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T04:57:31.608257Z","iopub.execute_input":"2023-12-18T04:57:31.608996Z","iopub.status.idle":"2023-12-18T04:57:31.644907Z","shell.execute_reply.started":"2023-12-18T04:57:31.608951Z","shell.execute_reply":"2023-12-18T04:57:31.643689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}