{"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":"gpu","dataSources":[{"sourceId":65471,"databundleVersionId":7211464,"sourceType":"competition"}],"dockerImageVersionId":30627,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.utils import image_dataset_from_directory\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization, Rescaling\nfrom tensorflow.keras.callbacks import LearningRateScheduler\nfrom tensorflow.keras.optimizers import Adam\nfrom sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\nimport tensorflow as tf ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-23T21:25:16.300498Z","iopub.execute_input":"2023-12-23T21:25:16.300892Z","iopub.status.idle":"2023-12-23T21:25:16.310588Z","shell.execute_reply.started":"2023-12-23T21:25:16.300855Z","shell.execute_reply":"2023-12-23T21:25:16.309600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '/kaggle/input/arabic-letters-classification/Final_Arabic_Alpha_dataset/Final_Arabic_Alpha_dataset/train'\ntest_dir = '/kaggle/input/arabic-letters-classification/Final_Arabic_Alpha_dataset/Final_Arabic_Alpha_dataset/test'","metadata":{"execution":{"iopub.status.busy":"2023-12-23T19:29:57.148705Z","iopub.execute_input":"2023-12-23T19:29:57.149378Z","iopub.status.idle":"2023-12-23T19:29:57.154453Z","shell.execute_reply.started":"2023-12-23T19:29:57.149344Z","shell.execute_reply":"2023-12-23T19:29:57.153461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_size = (224, 224)\nbatch_size = 32","metadata":{"execution":{"iopub.status.busy":"2023-12-23T19:29:59.171411Z","iopub.execute_input":"2023-12-23T19:29:59.171786Z","iopub.status.idle":"2023-12-23T19:29:59.176611Z","shell.execute_reply.started":"2023-12-23T19:29:59.171758Z","shell.execute_reply":"2023-12-23T19:29:59.175580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = image_dataset_from_directory(\n    train_dir,\n    image_size=img_size,\n    batch_size=batch_size,\n    label_mode='categorical',\n    validation_split=0.2,\n    seed=42,\n    subset='training'\n)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T19:30:12.906004Z","iopub.execute_input":"2023-12-23T19:30:12.906875Z","iopub.status.idle":"2023-12-23T19:30:18.633011Z","shell.execute_reply.started":"2023-12-23T19:30:12.906846Z","shell.execute_reply":"2023-12-23T19:30:18.632148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_dataset = image_dataset_from_directory(\n    train_dir,\n    image_size=img_size,\n    batch_size=batch_size,\n    label_mode='categorical',\n    validation_split=0.2,\n    seed=42,\n    subset='validation'\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-23T19:30:24.029251Z","iopub.execute_input":"2023-12-23T19:30:24.029690Z","iopub.status.idle":"2023-12-23T19:30:30.778123Z","shell.execute_reply.started":"2023-12-23T19:30:24.029662Z","shell.execute_reply":"2023-12-23T19:30:30.777033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    Rescaling(1./255, input_shape=(img_size[0], img_size[1], 3)),\n    Conv2D(64, (3, 3), activation='relu', padding='same'),\n    BatchNormalization(),\n    MaxPooling2D((2, 2)),\n    \n    Conv2D(128, (3, 3), activation='relu', padding='same'),\n    BatchNormalization(),\n    MaxPooling2D((2, 2)),\n    \n    Conv2D(256, (3, 3), activation='relu', padding='same'),\n    BatchNormalization(),\n    MaxPooling2D((2, 2)),\n    \n    Conv2D(512, (3, 3), activation='relu', padding='same'),\n    BatchNormalization(),\n    MaxPooling2D((2, 2)),\n    \n    Flatten(),\n    \n    Dense(1024, activation='relu'),\n    BatchNormalization(),\n    Dropout(0.5),\n    \n    Dense(512, activation='relu'),\n    BatchNormalization(),\n    Dropout(0.5),\n    \n    Dense(256, activation='relu'),\n    BatchNormalization(),\n    Dropout(0.5),\n    \n    Dense(65, activation='softmax')\n])\n\n# Learning Rate Scheduler\ndef lr_schedule(epoch):\n    lr = 1e-4\n    if epoch > 15:\n        lr *= 0.1\n    elif epoch > 10:\n        lr *= 0.5\n    return lr\n\nlr_scheduler = LearningRateScheduler(lr_schedule)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T19:30:55.846991Z","iopub.execute_input":"2023-12-23T19:30:55.847923Z","iopub.status.idle":"2023-12-23T19:30:56.400146Z","shell.execute_reply.started":"2023-12-23T19:30:55.847888Z","shell.execute_reply":"2023-12-23T19:30:56.399090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=Adam(learning_rate=1e-4), loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Training with history for visualization\nhistory = model.fit(train_dataset, validation_data=validation_dataset, epochs=50, callbacks=[lr_scheduler])","metadata":{"execution":{"iopub.status.busy":"2023-12-23T19:36:05.820114Z","iopub.execute_input":"2023-12-23T19:36:05.820850Z","iopub.status.idle":"2023-12-23T21:01:03.605315Z","shell.execute_reply.started":"2023-12-23T19:36:05.820817Z","shell.execute_reply":"2023-12-23T21:01:03.604207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 4))","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:01:18.625887Z","iopub.execute_input":"2023-12-23T21:01:18.626785Z","iopub.status.idle":"2023-12-23T21:01:18.642237Z","shell.execute_reply.started":"2023-12-23T21:01:18.626747Z","shell.execute_reply":"2023-12-23T21:01:18.641329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.subplot(1, 2, 1)\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:01:21.829818Z","iopub.execute_input":"2023-12-23T21:01:21.830206Z","iopub.status.idle":"2023-12-23T21:01:22.121246Z","shell.execute_reply.started":"2023-12-23T21:01:21.830174Z","shell.execute_reply":"2023-12-23T21:01:22.120312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.subplot(1, 2, 2)\nplt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Training and Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:01:29.760384Z","iopub.execute_input":"2023-12-23T21:01:29.760756Z","iopub.status.idle":"2023-12-23T21:01:30.022556Z","shell.execute_reply.started":"2023-12-23T21:01:29.760726Z","shell.execute_reply":"2023-12-23T21:01:30.021632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:01:33.261309Z","iopub.execute_input":"2023-12-23T21:01:33.261672Z","iopub.status.idle":"2023-12-23T21:01:33.269793Z","shell.execute_reply.started":"2023-12-23T21:01:33.261645Z","shell.execute_reply":"2023-12-23T21:01:33.268736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = image_dataset_from_directory(\n    test_dir,\n    labels=None,\n    color_mode=\"rgb\",\n    shuffle=False,\n    image_size=img_size,\n    batch_size=batch_size,\n    label_mode='categorical'\n)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:01:39.102772Z","iopub.execute_input":"2023-12-23T21:01:39.103691Z","iopub.status.idle":"2023-12-23T21:01:46.264848Z","shell.execute_reply.started":"2023-12-23T21:01:39.103660Z","shell.execute_reply":"2023-12-23T21:01:46.264062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for images in test_dataset:\n    print(images.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:28:41.055071Z","iopub.execute_input":"2023-12-23T21:28:41.055910Z","iopub.status.idle":"2023-12-23T21:28:41.132756Z","shell.execute_reply.started":"2023-12-23T21:28:41.055868Z","shell.execute_reply":"2023-12-23T21:28:41.131807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test_dataset)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:29:09.088855Z","iopub.execute_input":"2023-12-23T21:29:09.089661Z","iopub.status.idle":"2023-12-23T21:29:18.908503Z","shell.execute_reply.started":"2023-12-23T21:29:09.089626Z","shell.execute_reply":"2023-12-23T21:29:18.907493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_list = os.listdir(test_dir)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:29:22.528228Z","iopub.execute_input":"2023-12-23T21:29:22.528611Z","iopub.status.idle":"2023-12-23T21:29:22.538246Z","shell.execute_reply.started":"2023-12-23T21:29:22.528583Z","shell.execute_reply":"2023-12-23T21:29:22.537107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_list = sorted(os.listdir(train_dir))\nprint(labels_list)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:29:24.780095Z","iopub.execute_input":"2023-12-23T21:29:24.781113Z","iopub.status.idle":"2023-12-23T21:29:24.786985Z","shell.execute_reply.started":"2023-12-23T21:29:24.781075Z","shell.execute_reply":"2023-12-23T21:29:24.785793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[1][22]","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:29:27.363647Z","iopub.execute_input":"2023-12-23T21:29:27.363999Z","iopub.status.idle":"2023-12-23T21:29:27.371314Z","shell.execute_reply.started":"2023-12-23T21:29:27.363973Z","shell.execute_reply":"2023-12-23T21:29:27.370073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:29:30.677800Z","iopub.execute_input":"2023-12-23T21:29:30.678220Z","iopub.status.idle":"2023-12-23T21:29:30.686206Z","shell.execute_reply.started":"2023-12-23T21:29:30.678186Z","shell.execute_reply":"2023-12-23T21:29:30.685068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_predictions = pd.DataFrame(columns=['ID','Label'], dtype=(np.int32,np.int32))\n\npredictions_mod = np.argmax(predictions, 1)\n\nfor idx,image in enumerate(sorted(img_list)):\n\n    df2 = pd.DataFrame([[int(image.split(\".\")[0]), int(labels_list[predictions_mod[idx]])]], columns=['ID','Label'])\n    df_predictions = pd.concat([df_predictions, df2])","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:29:32.985993Z","iopub.execute_input":"2023-12-23T21:29:32.986949Z","iopub.status.idle":"2023-12-23T21:29:38.231442Z","shell.execute_reply.started":"2023-12-23T21:29:32.986909Z","shell.execute_reply":"2023-12-23T21:29:38.230362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(predictions_mod)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:29:41.983425Z","iopub.execute_input":"2023-12-23T21:29:41.983770Z","iopub.status.idle":"2023-12-23T21:29:41.989736Z","shell.execute_reply.started":"2023-12-23T21:29:41.983742Z","shell.execute_reply":"2023-12-23T21:29:41.988693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_predictions.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:29:49.456173Z","iopub.execute_input":"2023-12-23T21:29:49.456969Z","iopub.status.idle":"2023-12-23T21:29:49.467093Z","shell.execute_reply.started":"2023-12-23T21:29:49.456934Z","shell.execute_reply":"2023-12-23T21:29:49.466001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_predictions.sort_values(by='ID', inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:29:52.298094Z","iopub.execute_input":"2023-12-23T21:29:52.298526Z","iopub.status.idle":"2023-12-23T21:29:52.305319Z","shell.execute_reply.started":"2023-12-23T21:29:52.298487Z","shell.execute_reply":"2023-12-23T21:29:52.304112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_predictions.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:29:54.091848Z","iopub.execute_input":"2023-12-23T21:29:54.092670Z","iopub.status.idle":"2023-12-23T21:29:54.105939Z","shell.execute_reply.started":"2023-12-23T21:29:54.092635Z","shell.execute_reply":"2023-12-23T21:29:54.104733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_predictions.to_csv('predictions_11.csv', index=False, header=True) ","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:29:59.447077Z","iopub.execute_input":"2023-12-23T21:29:59.447471Z","iopub.status.idle":"2023-12-23T21:29:59.477252Z","shell.execute_reply.started":"2023-12-23T21:29:59.447440Z","shell.execute_reply":"2023-12-23T21:29:59.476350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}