{"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":"nvidiaTeslaT4","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 numpy as np\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow import keras","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-24T00:55:14.675047Z","iopub.execute_input":"2023-12-24T00:55:14.67576Z","iopub.status.idle":"2023-12-24T00:55:14.679937Z","shell.execute_reply.started":"2023-12-24T00:55:14.675729Z","shell.execute_reply":"2023-12-24T00:55:14.679029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = '/kaggle/input/arabic-letters-classification/Final_Arabic_Alpha_dataset/Final_Arabic_Alpha_dataset/train'","metadata":{"execution":{"iopub.status.busy":"2023-12-24T00:55:20.445393Z","iopub.execute_input":"2023-12-24T00:55:20.445749Z","iopub.status.idle":"2023-12-24T00:55:20.450314Z","shell.execute_reply.started":"2023-12-24T00:55:20.445719Z","shell.execute_reply":"2023-12-24T00:55:20.449277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CLASSES_COUNT = 65\nIMAGE_SIZE = (160, 160)\nBATCH_SIZE = 256\nSEED = 65\nEPOCHS = 20","metadata":{"execution":{"iopub.status.busy":"2023-12-23T23:51:03.428666Z","iopub.execute_input":"2023-12-23T23:51:03.429574Z","iopub.status.idle":"2023-12-23T23:51:03.434338Z","shell.execute_reply.started":"2023-12-23T23:51:03.429532Z","shell.execute_reply":"2023-12-23T23:51:03.433176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_split,test_split = tf.keras.utils.image_dataset_from_directory(\n    dataset,\n    validation_split=0.2,\n    color_mode='grayscale',\n    label_mode=\"categorical\",\n    subset=\"both\",\n    shuffle=True,\n    seed=SEED,\n    image_size=IMAGE_SIZE,\n    batch_size=BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T23:51:07.981349Z","iopub.execute_input":"2023-12-23T23:51:07.982238Z","iopub.status.idle":"2023-12-23T23:51:15.15018Z","shell.execute_reply.started":"2023-12-23T23:51:07.982204Z","shell.execute_reply":"2023-12-23T23:51:15.149197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential([\n    keras.layers.Input(shape=(IMAGE_SIZE[0], IMAGE_SIZE[1], 1)),\n    keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same'),\n    keras.layers.MaxPooling2D(2, 2),\n    keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same'),\n    keras.layers.MaxPooling2D(2, 2),\n    keras.layers.Conv2D(128, (3, 3), activation='relu', padding='same'),\n    keras.layers.MaxPooling2D(2, 2),\n    keras.layers.Conv2D(256, (3, 3), activation='relu', padding='same'),\n    keras.layers.MaxPooling2D(2, 2),\n    keras.layers.Flatten(),\n    keras.layers.Dense(1024, activation='relu'),\n    tf.keras.layers.Dropout(0.5), \n    tf.keras.layers.Dense(CLASSES_COUNT, activation=tf.nn.softmax)\n])\nmodel.build(input_shape=(BATCH_SIZE, IMAGE_SIZE[0], IMAGE_SIZE[1], 1))","metadata":{"execution":{"iopub.status.busy":"2023-12-24T00:00:37.634536Z","iopub.execute_input":"2023-12-24T00:00:37.634918Z","iopub.status.idle":"2023-12-24T00:00:37.774702Z","shell.execute_reply.started":"2023-12-24T00:00:37.634885Z","shell.execute_reply":"2023-12-24T00:00:37.773771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(\n    initial_learning_rate=1e-3,\n    decay_steps=10000,\n    decay_rate=0.9)\noptimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)\nmodel.compile(optimizer=optimizer,\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-12-24T00:00:40.091408Z","iopub.execute_input":"2023-12-24T00:00:40.091782Z","iopub.status.idle":"2023-12-24T00:00:40.109728Z","shell.execute_reply.started":"2023-12-24T00:00:40.091751Z","shell.execute_reply":"2023-12-24T00:00:40.108979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-24T00:00:42.657518Z","iopub.execute_input":"2023-12-24T00:00:42.6579Z","iopub.status.idle":"2023-12-24T00:00:42.692281Z","shell.execute_reply.started":"2023-12-24T00:00:42.657851Z","shell.execute_reply":"2023-12-24T00:00:42.691466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_split,\n    epochs=EPOCHS,\n    validation_data=test_split\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-24T00:02:15.610588Z","iopub.execute_input":"2023-12-24T00:02:15.610987Z","iopub.status.idle":"2023-12-24T00:12:20.870863Z","shell.execute_reply.started":"2023-12-24T00:02:15.610955Z","shell.execute_reply":"2023-12-24T00:12:20.869882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-24T00:12:47.611796Z","iopub.execute_input":"2023-12-24T00:12:47.612135Z","iopub.status.idle":"2023-12-24T00:12:47.93246Z","shell.execute_reply.started":"2023-12-24T00:12:47.612107Z","shell.execute_reply":"2023-12-24T00:12:47.931558Z"},"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-24T00:12:52.261943Z","iopub.execute_input":"2023-12-24T00:12:52.262678Z","iopub.status.idle":"2023-12-24T00:12:52.550757Z","shell.execute_reply.started":"2023-12-24T00:12:52.262646Z","shell.execute_reply":"2023-12-24T00:12:52.549864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = '/kaggle/input/arabic-letters-classification/Final_Arabic_Alpha_dataset/Final_Arabic_Alpha_dataset/test'\ntest_images = tf.keras.utils.image_dataset_from_directory(\n        test_data,\n        labels=None,\n        label_mode=\"categorical\",\n        color_mode='grayscale',\n        shuffle=False,\n        image_size=IMAGE_SIZE,\n        batch_size=BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2023-12-24T00:51:17.538422Z","iopub.execute_input":"2023-12-24T00:51:17.539257Z","iopub.status.idle":"2023-12-24T00:51:22.7678Z","shell.execute_reply.started":"2023-12-24T00:51:17.539225Z","shell.execute_reply":"2023-12-24T00:51:22.766796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test_images)","metadata":{"execution":{"iopub.status.busy":"2023-12-24T00:55:29.540805Z","iopub.execute_input":"2023-12-24T00:55:29.541163Z","iopub.status.idle":"2023-12-24T00:55:33.575704Z","shell.execute_reply.started":"2023-12-24T00:55:29.541133Z","shell.execute_reply":"2023-12-24T00:55:33.574797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_list = os.listdir(test_data)","metadata":{"execution":{"iopub.status.busy":"2023-12-24T00:53:00.696634Z","iopub.execute_input":"2023-12-24T00:53:00.697247Z","iopub.status.idle":"2023-12-24T00:53:00.705788Z","shell.execute_reply.started":"2023-12-24T00:53:00.697214Z","shell.execute_reply":"2023-12-24T00:53:00.704862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_list = sorted(os.listdir(dataset))","metadata":{"execution":{"iopub.status.busy":"2023-12-24T00:55:26.28333Z","iopub.execute_input":"2023-12-24T00:55:26.284043Z","iopub.status.idle":"2023-12-24T00:55:26.290511Z","shell.execute_reply.started":"2023-12-24T00:55:26.284009Z","shell.execute_reply":"2023-12-24T00:55:26.289757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-24T01:05:09.976026Z","iopub.execute_input":"2023-12-24T01:05:09.976382Z","iopub.status.idle":"2023-12-24T01:05:09.98299Z","shell.execute_reply.started":"2023-12-24T01:05:09.976352Z","shell.execute_reply":"2023-12-24T01:05:09.982046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\ndf_predictions = pd.DataFrame(columns=['ID','Label'], dtype=(np.int32,np.int32))\npredictions_mod = np.argmax(predictions, 1)\nfor idx,image in enumerate(sorted(img_list)):\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])\n","metadata":{"execution":{"iopub.status.busy":"2023-12-24T01:07:58.63476Z","iopub.execute_input":"2023-12-24T01:07:58.635521Z","iopub.status.idle":"2023-12-24T01:08:03.320632Z","shell.execute_reply.started":"2023-12-24T01:07:58.635476Z","shell.execute_reply":"2023-12-24T01:08:03.31965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(predictions_mod)","metadata":{"execution":{"iopub.status.busy":"2023-12-24T01:08:03.322159Z","iopub.execute_input":"2023-12-24T01:08:03.322445Z","iopub.status.idle":"2023-12-24T01:08:03.327658Z","shell.execute_reply.started":"2023-12-24T01:08:03.322423Z","shell.execute_reply":"2023-12-24T01:08:03.326711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_predictions.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-24T01:08:14.369615Z","iopub.execute_input":"2023-12-24T01:08:14.370329Z","iopub.status.idle":"2023-12-24T01:08:14.38624Z","shell.execute_reply.started":"2023-12-24T01:08:14.370299Z","shell.execute_reply":"2023-12-24T01:08:14.385363Z"},"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-24T01:08:38.812611Z","iopub.execute_input":"2023-12-24T01:08:38.813218Z","iopub.status.idle":"2023-12-24T01:08:38.818616Z","shell.execute_reply.started":"2023-12-24T01:08:38.813188Z","shell.execute_reply":"2023-12-24T01:08:38.817573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_predictions.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-24T01:08:42.85313Z","iopub.execute_input":"2023-12-24T01:08:42.85371Z","iopub.status.idle":"2023-12-24T01:08:42.864965Z","shell.execute_reply.started":"2023-12-24T01:08:42.853678Z","shell.execute_reply":"2023-12-24T01:08:42.863955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_predictions.to_csv('predictions.csv', index=False, header=True) ","metadata":{"execution":{"iopub.status.busy":"2023-12-24T01:09:43.595146Z","iopub.execute_input":"2023-12-24T01:09:43.595951Z","iopub.status.idle":"2023-12-24T01:09:43.622141Z","shell.execute_reply.started":"2023-12-24T01:09:43.595918Z","shell.execute_reply":"2023-12-24T01:09:43.621383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, classification_report\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n# Load true labels for the test set\ntrue_labels = [int(label.split(\".\")[0]) for label in sorted(img_list)]\n\n# Extract confusion matrix\nconf_matrix = confusion_matrix(true_labels, predictions_mod)\n\n# Plot confusion matrix\nplt.figure(figsize=(12, 10))\nsns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', xticklabels=labels_list, yticklabels=labels_list)\nplt.title('Confusion Matrix')\nplt.xlabel('Predicted Labels')\nplt.ylabel('True Labels')\nplt.show()\n\n# Display classification report\nprint(\"Classification Report:\\n\", classification_report(true_labels, predictions_mod, target_names=labels_list))\n","metadata":{"execution":{"iopub.status.busy":"2023-12-24T01:41:45.473639Z","iopub.execute_input":"2023-12-24T01:41:45.474308Z"},"trusted":true},"execution_count":null,"outputs":[]}]}