{"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":30616,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Imported Libraries","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt\nimport tensorflow as tf\nfrom tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:13:50.477299Z","iopub.execute_input":"2023-12-23T21:13:50.478206Z","iopub.status.idle":"2023-12-23T21:14:04.231721Z","shell.execute_reply.started":"2023-12-23T21:13:50.478175Z","shell.execute_reply":"2023-12-23T21:14:04.230559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data loading","metadata":{}},{"cell_type":"code","source":"train_path = '/kaggle/input/arabic-letters-classification/Final_Arabic_Alpha_dataset/Final_Arabic_Alpha_dataset/train'\ntest_path = '/kaggle/input/arabic-letters-classification/Final_Arabic_Alpha_dataset/Final_Arabic_Alpha_dataset/test'","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:14:17.873842Z","iopub.execute_input":"2023-12-23T21:14:17.874246Z","iopub.status.idle":"2023-12-23T21:14:17.878731Z","shell.execute_reply.started":"2023-12-23T21:14:17.874216Z","shell.execute_reply":"2023-12-23T21:14:17.877794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = tf.keras.utils.image_dataset_from_directory(\n    train_path,\n    validation_split=0.2,\n    color_mode='grayscale',\n    label_mode=\"categorical\",\n    subset=\"training\",\n    shuffle=True,\n    seed=43,\n    image_size=(160,160),\n    batch_size=512)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:14:18.001182Z","iopub.execute_input":"2023-12-23T21:14:18.001545Z","iopub.status.idle":"2023-12-23T21:14:25.62035Z","shell.execute_reply.started":"2023-12-23T21:14:18.001516Z","shell.execute_reply":"2023-12-23T21:14:25.619241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_validation = tf.keras.utils.image_dataset_from_directory(\n    train_path,\n    validation_split=0.2,\n    label_mode=\"categorical\",\n    color_mode='grayscale',\n    subset=\"validation\",\n    shuffle=True,\n    seed=43,\n    image_size=(160,160),\n    batch_size=512)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:14:25.622113Z","iopub.execute_input":"2023-12-23T21:14:25.622428Z","iopub.status.idle":"2023-12-23T21:14:32.308659Z","shell.execute_reply.started":"2023-12-23T21:14:25.622404Z","shell.execute_reply":"2023-12-23T21:14:32.307607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for images, labels in train_images:\n    print(images.shape)\n    print(labels.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:14:32.310263Z","iopub.execute_input":"2023-12-23T21:14:32.310589Z","iopub.status.idle":"2023-12-23T21:14:36.967282Z","shell.execute_reply.started":"2023-12-23T21:14:32.310562Z","shell.execute_reply":"2023-12-23T21:14:36.966186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Architecture\n\n","metadata":{}},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    tf.keras.layers.Rescaling(1./255),\n    tf.keras.layers.experimental.preprocessing.RandomRotation(0.1),\n    tf.keras.layers.experimental.preprocessing.RandomZoom(0.1),\n    tf.keras.layers.Conv2D(32, (3, 3), activation = 'relu', padding = 'same'),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Conv2D(64, (3, 3), activation = 'relu', padding = 'same'),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Conv2D(64, (3, 3), activation = 'relu', padding = 'same'),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Conv2D(128, (3, 3), activation = 'relu', padding = 'same'),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Conv2D(128, (3, 3), activation = 'relu', padding = 'same'),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Conv2D(256, (3, 3), activation = 'relu', padding = 'same'),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(1024, activation='relu',\n#                            kernel_regularizer=tf.keras.regularizers.l2(0.001)\n                         ),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Dense(65, activation=tf.nn.softmax)\n])","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:14:36.970173Z","iopub.execute_input":"2023-12-23T21:14:36.971086Z","iopub.status.idle":"2023-12-23T21:14:37.037264Z","shell.execute_reply.started":"2023-12-23T21:14:36.971044Z","shell.execute_reply":"2023-12-23T21:14:37.036373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Model Summary:","metadata":{}},{"cell_type":"code","source":"model.build(input_shape=(512, 160, 160, 1))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:14:37.038523Z","iopub.execute_input":"2023-12-23T21:14:37.038824Z","iopub.status.idle":"2023-12-23T21:14:37.402182Z","shell.execute_reply.started":"2023-12-23T21:14:37.038799Z","shell.execute_reply":"2023-12-23T21:14:37.401164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Model Optimization:","metadata":{}},{"cell_type":"code","source":"learning_rate = 0.001\nlr_mod = tf.keras.optimizers.schedules.ExponentialDecay(learning_rate, decay_steps=1000000000000,\\\n                                                             decay_rate=0.99999, staircase=True)\n\nmodel.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=lr_mod), loss = 'categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:14:37.403579Z","iopub.execute_input":"2023-12-23T21:14:37.403861Z","iopub.status.idle":"2023-12-23T21:14:37.439567Z","shell.execute_reply.started":"2023-12-23T21:14:37.403836Z","shell.execute_reply":"2023-12-23T21:14:37.438508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Training ","metadata":{}},{"cell_type":"code","source":"h1 = model.fit(train_images,\n              validation_data=train_validation,\n              epochs=5)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:14:37.441283Z","iopub.execute_input":"2023-12-23T21:14:37.441573Z","iopub.status.idle":"2023-12-23T21:17:33.065312Z","shell.execute_reply.started":"2023-12-23T21:14:37.441549Z","shell.execute_reply":"2023-12-23T21:17:33.064196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(h1.history['loss'])\nplt.plot(h1.history['val_loss'])\nplt.title('Model Loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:19:54.48766Z","iopub.execute_input":"2023-12-23T21:19:54.488456Z","iopub.status.idle":"2023-12-23T21:19:54.870514Z","shell.execute_reply.started":"2023-12-23T21:19:54.488423Z","shell.execute_reply":"2023-12-23T21:19:54.869552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(h1.history['accuracy'])\nplt.plot(h1.history['val_accuracy'])\nplt.title('Model Accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:19:54.996093Z","iopub.execute_input":"2023-12-23T21:19:54.996507Z","iopub.status.idle":"2023-12-23T21:19:55.286471Z","shell.execute_reply.started":"2023-12-23T21:19:54.996477Z","shell.execute_reply":"2023-12-23T21:19:55.285428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prediction:","metadata":{}},{"cell_type":"code","source":"test_images = tf.keras.utils.image_dataset_from_directory(\n        test_path,\n        labels=None,\n        label_mode=\"categorical\",\n        color_mode='grayscale',\n        shuffle=False,\n        image_size=(160,160),\n        batch_size=512)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:20:42.192905Z","iopub.execute_input":"2023-12-23T21:20:42.193325Z","iopub.status.idle":"2023-12-23T21:20:47.388465Z","shell.execute_reply.started":"2023-12-23T21:20:42.193294Z","shell.execute_reply":"2023-12-23T21:20:47.387524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for images in test_images:\n    print(images.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:20:19.232936Z","iopub.status.idle":"2023-12-23T21:20:19.23337Z","shell.execute_reply.started":"2023-12-23T21:20:19.23316Z","shell.execute_reply":"2023-12-23T21:20:19.233186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test_images)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:20:19.235418Z","iopub.status.idle":"2023-12-23T21:20:19.235823Z","shell.execute_reply.started":"2023-12-23T21:20:19.23563Z","shell.execute_reply":"2023-12-23T21:20:19.235648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_list = os.listdir(datasets[1])","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:20:19.239555Z","iopub.status.idle":"2023-12-23T21:20:19.23995Z","shell.execute_reply.started":"2023-12-23T21:20:19.23976Z","shell.execute_reply":"2023-12-23T21:20:19.239779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_list = sorted(os.listdir(datasets[0]))\nprint(labels_list)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:20:19.242962Z","iopub.status.idle":"2023-12-23T21:20:19.243379Z","shell.execute_reply.started":"2023-12-23T21:20:19.243179Z","shell.execute_reply":"2023-12-23T21:20:19.243198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[1][22]","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:20:19.244846Z","iopub.status.idle":"2023-12-23T21:20:19.245812Z","shell.execute_reply.started":"2023-12-23T21:20:19.245606Z","shell.execute_reply":"2023-12-23T21:20:19.245627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# load prediction to 'predictions.csv':","metadata":{}},{"cell_type":"code","source":"predictions.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:20:19.247672Z","iopub.status.idle":"2023-12-23T21:20:19.248086Z","shell.execute_reply.started":"2023-12-23T21:20:19.247897Z","shell.execute_reply":"2023-12-23T21:20:19.247915Z"},"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#     print(image, predictions_mod[idx])\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:20:19.250015Z","iopub.status.idle":"2023-12-23T21:20:19.250971Z","shell.execute_reply.started":"2023-12-23T21:20:19.250774Z","shell.execute_reply":"2023-12-23T21:20:19.250794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(predictions_mod)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:20:19.252492Z","iopub.status.idle":"2023-12-23T21:20:19.252903Z","shell.execute_reply.started":"2023-12-23T21:20:19.252709Z","shell.execute_reply":"2023-12-23T21:20:19.252726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_predictions.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:20:19.254267Z","iopub.status.idle":"2023-12-23T21:20:19.254838Z","shell.execute_reply.started":"2023-12-23T21:20:19.254623Z","shell.execute_reply":"2023-12-23T21:20:19.254648Z"},"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:20:19.256693Z","iopub.status.idle":"2023-12-23T21:20:19.257116Z","shell.execute_reply.started":"2023-12-23T21:20:19.256917Z","shell.execute_reply":"2023-12-23T21:20:19.256941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_predictions.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:20:19.259297Z","iopub.status.idle":"2023-12-23T21:20:19.259719Z","shell.execute_reply.started":"2023-12-23T21:20:19.259531Z","shell.execute_reply":"2023-12-23T21:20:19.259549Z"},"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-23T21:20:19.261791Z","iopub.status.idle":"2023-12-23T21:20:19.262203Z","shell.execute_reply.started":"2023-12-23T21:20:19.261991Z","shell.execute_reply":"2023-12-23T21:20:19.262009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}