{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Setup the environment\nNUM_CLASSES = 65\nIMAGE_SIZE = (160, 160)\nBATCH_SIZE = 512\nSEED = 43\nEPOCHS = 20\n\n# Define dataset directories\ntrain_data = '/kaggle/input/arabic-letters-classification/Final_Arabic_Alpha_dataset/Final_Arabic_Alpha_dataset/train'\ntest_data = '/kaggle/input/arabic-letters-classification/Final_Arabic_Alpha_dataset/Final_Arabic_Alpha_dataset/test/'\n\n# Load training dataset\ntrain_images = tf.keras.utils.image_dataset_from_directory(\n    train_data,\n    validation_split=0.2,\n    color_mode='grayscale',       # Images are in grayscale\n    label_mode=\"categorical\",     # Use categorical labels\n    subset=\"training\",            # Subset for training\n    shuffle=True,                 # Shuffle the dataset\n    seed=SEED,                    # Set seed for reproducibility\n    image_size=IMAGE_SIZE,        # Resize images to specified size\n    batch_size=BATCH_SIZE          # Batch size for training\n)\n\n# Load validation dataset\ntrain_validation = tf.keras.utils.image_dataset_from_directory(\n    train_data,\n    validation_split=0.2,\n    label_mode=\"categorical\",\n    color_mode='grayscale',\n    subset=\"validation\",\n    shuffle=True,\n    seed=SEED,\n    image_size=IMAGE_SIZE,\n    batch_size=BATCH_SIZE\n)\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-12-23T20:18:56.983776Z","iopub.execute_input":"2023-12-23T20:18:56.984707Z","iopub.status.idle":"2023-12-23T20:19:04.270420Z","shell.execute_reply.started":"2023-12-23T20:18:56.984653Z","shell.execute_reply":"2023-12-23T20:19:04.269475Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n\n# Visualize samples from the training dataset\nclass_names = train_images.class_names  # Get class names from the dataset\n\nplt.figure(figsize=(10, 10))\n\n# Iterate over batches of images and labels\nfor images, labels in train_images.take(2):  # Display images from the first two batches\n    for i in range(9):\n        ax = plt.subplot(3, 3, i + 1)  # Create subplots for a 3x3 grid\n        plt.imshow(images[i].numpy().astype(\"uint8\").squeeze(), cmap=\"gray\")  # Display the image\n        plt.title(class_names[np.argmax(labels[i])])  # Display the class label\n        plt.axis(\"off\")  # Turn off axis labels for better visualization\n\nplt.show()  # Show the plot","metadata":{"execution":{"iopub.status.busy":"2023-12-23T20:19:21.168847Z","iopub.execute_input":"2023-12-23T20:19:21.169226Z","iopub.status.idle":"2023-12-23T20:19:23.998978Z","shell.execute_reply.started":"2023-12-23T20:19:21.169196Z","shell.execute_reply":"2023-12-23T20:19:23.998114Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras import models, layers, optimizers, callbacks\n\n\n# Replace these values with the actual dimensions of your images\nimage_height = 64\nimage_width = 64\nchannels = 1  # Assuming grayscale, set to 3 for RGB\nnum_classes = 65  # Number of classes (Arabic alphabets)\n\n# Define the CNN model\nmodel = models.Sequential()\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(image_height, image_width, channels)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(128, activation='relu'))\nmodel.add(layers.Dense(num_classes, activation='softmax'))\n\n# Compile the model\nmodel.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\n# Split the data into training and testing sets\nX_train, X_test, y_train, y_test = train_test_split(images, labels, test_size=0.2, random_state=42)\n\n# Train the model\nmodel.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test))\n\n# Evaluate the model on the test set\ntest_loss, test_acc = model.evaluate(X_test, y_test)\nprint(f'Test accuracy: {test_acc}')\n","metadata":{"execution":{"iopub.status.busy":"2023-12-23T22:16:41.965899Z","iopub.execute_input":"2023-12-23T22:16:41.966767Z","iopub.status.idle":"2023-12-23T22:16:42.075454Z","shell.execute_reply.started":"2023-12-23T22:16:41.966721Z","shell.execute_reply":"2023-12-23T22:16:42.074341Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\n# Save the trained model\nmodel.save('nn-arabic-handwriting-320210295.keras')\n\n# Load the test dataset\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\n)\n\n# Make predictions on the test dataset\npredictions = model.predict(test_images)\n\n# Get the list of image filenames in the test dataset\nimg_list = os.listdir(test_data)\n\n# Get the list of class labels\nlabels_list = sorted(os.listdir(train_data))\nprint(labels_list)\n\n# Create a DataFrame to store predictions\ndf_predictions = pd.DataFrame(columns=['ID', 'Label'], dtype=(np.int32, np.int32))\n\n# Get the predicted class indices\npredictions_mod = np.argmax(predictions, 1)\n\n# Populate the DataFrame with image IDs and predicted labels\nfor idx, image in enumerate(sorted(img_list)):\n    # Create a DataFrame row with image ID and predicted label\n    df2 = pd.DataFrame([[int(image.split(\".\")[0]), int(labels_list[predictions_mod[idx]])]], columns=['ID', 'Label'])\n    \n    # Concatenate the new row to the main DataFrame\n    df_predictions = pd.concat([df_predictions, df2])\n\n# Sort the DataFrame by image ID\ndf_predictions.sort_values(by='ID', inplace=True)\ndf_predictions.reset_index(drop=True)\n\n# Save the predictions to a CSV file\ndf_predictions.to_csv('predictions.csv', index=False, header=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:31:29.033662Z","iopub.execute_input":"2023-12-23T21:31:29.034062Z","iopub.status.idle":"2023-12-23T21:31:42.727441Z","shell.execute_reply.started":"2023-12-23T21:31:29.034033Z","shell.execute_reply":"2023-12-23T21:31:42.726650Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plotting the training and validation loss curves\nplt.figure(figsize=(12, 6))\n\n# Plotting the training loss\nplt.subplot(1, 2, 1)\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('Epochs')\nplt.ylabel('Loss')\nplt.legend()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:36:15.458474Z","iopub.execute_input":"2023-12-23T21:36:15.458856Z","iopub.status.idle":"2023-12-23T21:36:15.725290Z","shell.execute_reply.started":"2023-12-23T21:36:15.458826Z","shell.execute_reply":"2023-12-23T21:36:15.724385Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plotting the training and validation accuracy curves\nplt.subplot(1, 2, 2)\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('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-23T21:37:05.202191Z","iopub.execute_input":"2023-12-23T21:37:05.203073Z","iopub.status.idle":"2023-12-23T21:37:05.552029Z","shell.execute_reply.started":"2023-12-23T21:37:05.203038Z","shell.execute_reply":"2023-12-23T21:37:05.551045Z"},"trusted":true},"outputs":[],"execution_count":null}]}