{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":952401,"sourceType":"datasetVersion","datasetId":517172}],"dockerImageVersionId":30761,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#import the necessary libraries\nimport numpy as np\nimport pandas as pd\nfrom pathlib import Path\nimport os.path\nimport matplotlib.pyplot as plt\nfrom IPython.display import Image\nimport matplotlib.cm as cm\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, classification_report, roc_auc_score\nfrom sklearn.preprocessing import LabelBinarizer\nimport tensorflow as tf\nimport seaborn as sns\nfrom time import perf_counter\nimport shutil\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout\nfrom sklearn.metrics import precision_score, recall_score, f1_score, roc_curve, auc, precision_recall_curve","metadata":{"_uuid":"f3c7322f-5ca8-4a0f-b3d6-887578ce3fed","_cell_guid":"1b55f1fe-a4ee-49f7-b2e5-1c54f955c7da","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T03:35:14.926749Z","iopub.execute_input":"2025-02-27T03:35:14.927523Z","iopub.status.idle":"2025-02-27T03:35:33.003027Z","shell.execute_reply.started":"2025-02-27T03:35:14.927487Z","shell.execute_reply":"2025-02-27T03:35:33.002169Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#distribution of dataset\nimport os\n\ndef count_images_in_folder(folder_path):\n    # List of common image file extensions\n    image_extensions = {'.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.gif'}\n    \n    # Initialize image count\n    image_count = 0\n    \n    # Traverse through the directory\n    for root, dirs, files in os.walk(folder_path):\n        for file in files:\n            # Check if the file has an image extension\n            if os.path.splitext(file)[1].lower() in image_extensions:\n                image_count += 1\n    \n    return image_count\n\n# Call the function to get the number of images\nno_dr_images = count_images_in_folder('/kaggle/input/diabetic-retinopathy-224x224-gaussian-filtered/gaussian_filtered_images/gaussian_filtered_images/No_DR')\nprint(f\"Number of images in the No_DR folder: {no_dr_images}\")\n\nmild_images = count_images_in_folder('/kaggle/input/diabetic-retinopathy-224x224-gaussian-filtered/gaussian_filtered_images/gaussian_filtered_images/Mild')\nprint(f\"Number of images in the Mild folder: {mild_images}\")\n\nmoderate_images = count_images_in_folder('/kaggle/input/diabetic-retinopathy-224x224-gaussian-filtered/gaussian_filtered_images/gaussian_filtered_images/Moderate')\nprint(f\"Number of images in the Moderate folder: {moderate_images}\")\n\nsevere_images = count_images_in_folder('/kaggle/input/diabetic-retinopathy-224x224-gaussian-filtered/gaussian_filtered_images/gaussian_filtered_images/Severe')\nprint(f\"Number of images in the Severe folder: {severe_images}\")\n\nproliferate_images = count_images_in_folder('/kaggle/input/diabetic-retinopathy-224x224-gaussian-filtered/gaussian_filtered_images/gaussian_filtered_images/Proliferate_DR')\nprint(f\"Number of images in the Proliferate folder: {proliferate_images}\")","metadata":{"_uuid":"ce918672-65d6-4f3c-9742-cfb24230ef87","_cell_guid":"e8c4c4c1-e404-4d67-8645-8d13584dc3ea","trusted":true,"collapsed":false,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2025-02-27T03:35:33.004619Z","iopub.execute_input":"2025-02-27T03:35:33.005162Z","iopub.status.idle":"2025-02-27T03:35:38.349556Z","shell.execute_reply.started":"2025-02-27T03:35:33.005133Z","shell.execute_reply":"2025-02-27T03:35:38.348727Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#defining the paths and corresponding labels\nimage_dir = Path('/kaggle/input/diabetic-retinopathy-224x224-gaussian-filtered')\n# Get filepaths and labels\nfilepaths = list(image_dir.glob(r'**/*.png'))\nlabels = list(map(lambda x: os.path.split(os.path.split(x)[0])[1], filepaths))","metadata":{"_uuid":"d8d9e2ce-3250-457b-8693-32b0d9238d5e","_cell_guid":"273af1b1-9b27-4f88-b411-c0fc4e22c43f","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T03:35:38.350630Z","iopub.execute_input":"2025-02-27T03:35:38.350934Z","iopub.status.idle":"2025-02-27T03:35:38.861499Z","shell.execute_reply.started":"2025-02-27T03:35:38.350908Z","shell.execute_reply":"2025-02-27T03:35:38.860879Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#image dataset\nfilepaths = pd.Series(filepaths, name='Filepath').astype(str)\nlabels = pd.Series(labels, name='Label')\n\n# Concatenate filepaths and labels\nimage_df = pd.concat([filepaths, labels], axis=1)\n\n# Shuffle the DataFrame and reset index\nimage_df = image_df.sample(frac=1).reset_index(drop = True)\n\n# Show the result\nimage_df.head(3)","metadata":{"_uuid":"76c0bcf4-780d-4dc8-95d0-d709c310e115","_cell_guid":"b43e6760-9ec9-408c-9726-85938eaab128","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T03:35:38.862397Z","iopub.execute_input":"2025-02-27T03:35:38.862645Z","iopub.status.idle":"2025-02-27T03:35:38.899577Z","shell.execute_reply.started":"2025-02-27T03:35:38.862620Z","shell.execute_reply":"2025-02-27T03:35:38.898750Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Display some pictures of the dataset with their labels\nfig, axes = plt.subplots(nrows=3, ncols=4, figsize=(10, 7),\n                        subplot_kw={'xticks': [], 'yticks': []})\n\nfor i, ax in enumerate(axes.flat):\n    ax.imshow(plt.imread(image_df.Filepath[i]))\n    ax.set_title(image_df.Label[i])\nplt.tight_layout()\nplt.show()","metadata":{"_uuid":"6342c7d7-164b-4b7b-b206-66d452c806f4","_cell_guid":"5ec3e6b8-54cc-4a66-a056-c6e5a6abbd59","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T03:35:38.901421Z","iopub.execute_input":"2025-02-27T03:35:38.901679Z","iopub.status.idle":"2025-02-27T03:35:40.019512Z","shell.execute_reply.started":"2025-02-27T03:35:38.901654Z","shell.execute_reply":"2025-02-27T03:35:40.018404Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Display the number of pictures of each category\nvc = image_df['Label'].value_counts()\nplt.figure(figsize=(9,5))\nsns.barplot(x = vc.index, y = vc, palette = \"rocket\")\nplt.title(\"Number of pictures of each category\", fontsize = 15)\nplt.show()","metadata":{"_uuid":"bad9666b-33b4-4067-822e-ad6fb7ebe30c","_cell_guid":"fd282045-6dc8-49d2-a1a0-4bd48ec7350f","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T03:35:40.020787Z","iopub.execute_input":"2025-02-27T03:35:40.021120Z","iopub.status.idle":"2025-02-27T03:35:40.278430Z","shell.execute_reply.started":"2025-02-27T03:35:40.021086Z","shell.execute_reply":"2025-02-27T03:35:40.277656Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load the Images with a generator and Data Augmentation\ndef create_gen():\n    train_generator = tf.keras.preprocessing.image.ImageDataGenerator(\n        preprocessing_function=tf.keras.applications.mobilenet_v2.preprocess_input,\n        validation_split=0.1\n    )\n\n    test_generator = tf.keras.preprocessing.image.ImageDataGenerator(\n        preprocessing_function=tf.keras.applications.mobilenet_v2.preprocess_input\n    )\n\n    train_images = train_generator.flow_from_dataframe(\n        dataframe=train_df,\n        x_col='Filepath',\n        y_col='Label',\n        target_size=(224, 224),\n        color_mode='rgb',\n        class_mode='categorical',\n        batch_size=32,\n        shuffle=True,\n        seed=0,\n        subset='training',\n        rotation_range=30, # Uncomment to use data augmentation\n        zoom_range=0.15,\n        width_shift_range=0.2,\n        height_shift_range=0.2,\n        shear_range=0.15,\n        horizontal_flip=True,\n        fill_mode=\"nearest\"\n    )\n\n    val_images = train_generator.flow_from_dataframe(\n        dataframe=train_df,\n        x_col='Filepath',\n        y_col='Label',\n        target_size=(224, 224),\n        color_mode='rgb',\n        class_mode='categorical',\n        batch_size=32,\n        shuffle=True,\n        seed=0,\n        subset='validation',\n        rotation_range=30, # Uncomment to use data augmentation\n        zoom_range=0.15,\n        width_shift_range=0.2,\n        height_shift_range=0.2,\n        shear_range=0.15,\n        horizontal_flip=True,\n        fill_mode=\"nearest\"\n    )\n\n    test_images = test_generator.flow_from_dataframe(\n        dataframe=test_df,\n        x_col='Filepath',\n        y_col='Label',\n        target_size=(224, 224),\n        color_mode='rgb',\n        class_mode='categorical',\n        batch_size=32,\n        shuffle=False\n    )\n    \n    return train_generator,test_generator,train_images,val_images,test_images","metadata":{"_uuid":"54e5f7f9-1d07-4e49-a42b-aa9b1051bfd0","_cell_guid":"c1a83228-9878-4c79-a39e-2c970970a3e5","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T03:35:40.279627Z","iopub.execute_input":"2025-02-27T03:35:40.279989Z","iopub.status.idle":"2025-02-27T03:35:40.287640Z","shell.execute_reply.started":"2025-02-27T03:35:40.279950Z","shell.execute_reply":"2025-02-27T03:35:40.286827Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Load all the pretrained models\ndef get_model(model):\n    kwargs =    {'input_shape':(224, 224, 3),\n                'include_top':False,\n                'weights':'imagenet',\n                'pooling':'avg'}\n    \n    pretrained_model = model(**kwargs)\n    pretrained_model.trainable = False\n    \n    inputs = pretrained_model.input\n\n    x = tf.keras.layers.Dense(1024, activation='relu')(pretrained_model.output)\n    x = tf.keras.layers.Dropout(0.5)(x)  # Dropout layer to reduce overfitting\n    x = tf.keras.layers.Dense(512, activation='relu')(x)\n    x = tf.keras.layers.Dropout(0.3)(x)  # Another dropout for regularization\n    \n    outputs = tf.keras.layers.Dense(5, activation='softmax')(x)\n\n    model = tf.keras.Model(inputs=inputs, outputs=outputs)\n\n    model.compile(\n        optimizer='adam',\n        loss='categorical_crossentropy',\n        metrics=['accuracy']\n    )\n    \n    return model","metadata":{"_uuid":"87549ded-9ca1-4b0a-8fe3-2a331ea19e97","_cell_guid":"04c60046-2e9e-4c35-beee-789bd375b907","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T03:35:40.288586Z","iopub.execute_input":"2025-02-27T03:35:40.288913Z","iopub.status.idle":"2025-02-27T03:35:40.304224Z","shell.execute_reply.started":"2025-02-27T03:35:40.288888Z","shell.execute_reply":"2025-02-27T03:35:40.303642Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Separate in train and test data\ntrain_df, test_df = train_test_split(image_df, train_size=0.8, shuffle=True, random_state=1,  stratify=labels)","metadata":{"_uuid":"734bf5dd-ae6e-4b1b-8fe5-04dd04e83772","_cell_guid":"34951fdc-d21b-48a9-a0ed-84a375ba0489","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T03:35:40.305369Z","iopub.execute_input":"2025-02-27T03:35:40.305935Z","iopub.status.idle":"2025-02-27T03:35:40.326170Z","shell.execute_reply.started":"2025-02-27T03:35:40.305909Z","shell.execute_reply":"2025-02-27T03:35:40.325349Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create the generators\ntrain_generator,test_generator,train_images,val_images,test_images=create_gen()\nprint('\\n')","metadata":{"_uuid":"445dea86-cfce-493a-9336-411dbbb49d3b","_cell_guid":"c9e4cddc-056e-4998-a440-ac63e806be40","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T03:35:40.327142Z","iopub.execute_input":"2025-02-27T03:35:40.327397Z","iopub.status.idle":"2025-02-27T03:35:41.510100Z","shell.execute_reply.started":"2025-02-27T03:35:40.327373Z","shell.execute_reply":"2025-02-27T03:35:41.509165Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Dictionary with the models\nmodels = {\n    \"DenseNet169\": {\"model\":tf.keras.applications.DenseNet169, \"perf\":0},\n    \"DenseNet201\": {\"model\":tf.keras.applications.DenseNet201, \"perf\":0},\n    \"EfficientNetB5\": {\"model\":tf.keras.applications.EfficientNetB5, \"perf\":0},\n    \"InceptionResNetV2\": {\"model\":tf.keras.applications.InceptionResNetV2, \"perf\":0},\n    \"InceptionV3\": {\"model\":tf.keras.applications.InceptionV3, \"perf\":0},\n    \"MobileNet\": {\"model\":tf.keras.applications.MobileNet, \"perf\":0},\n    \"MobileNetV2\": {\"model\":tf.keras.applications.MobileNetV2, \"perf\":0},\n    \"MobileNetV3Large\": {\"model\":tf.keras.applications.MobileNetV3Large, \"perf\":0},\n    \"NASNetMobile\": {\"model\":tf.keras.applications.NASNetMobile, \"perf\":0},\n    \"ResNet101V2\": {\"model\":tf.keras.applications.ResNet101V2, \"perf\":0},\n    \"ResNet152V2\": {\"model\":tf.keras.applications.ResNet152V2, \"perf\":0},\n    \"ResNet50V2\": {\"model\":tf.keras.applications.ResNet50V2, \"perf\":0},\n    \"VGG16\": {\"model\":tf.keras.applications.VGG16, \"perf\":0},\n    \"VGG19\": {\"model\":tf.keras.applications.VGG19, \"perf\":0},\n    \"Xception\": {\"model\":tf.keras.applications.Xception, \"perf\":0}\n}\n\n# Fit the models\nfor name, model in models.items():\n    \n    # Get the model\n    m = get_model(model['model'])\n    models[name]['model'] = m\n    \n    start = perf_counter()\n    \n    # Fit the model\n    history = m.fit(train_images,validation_data=val_images,epochs=5,verbose=1)\n    \n    # Sav the duration, the train_accuracy and the val_accuracy\n    duration = perf_counter() - start\n    duration = round(duration,2)\n    models[name]['perf'] = duration\n    print(f\"{name:20} trained in {duration} sec\")\n    \n    val_acc = history.history['val_accuracy']\n    models[name]['val_acc'] = [round(v,4) for v in val_acc]\n    \n    train_acc = history.history['accuracy']\n    models[name]['train_accuracy'] = [round(v,4) for v in train_acc]","metadata":{"_uuid":"ff0afdd4-6e86-4461-951f-4431113eeb17","_cell_guid":"3c022a88-8517-4a1f-b633-e4dd234cf0df","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T03:35:41.511787Z","iopub.execute_input":"2025-02-27T03:35:41.512310Z","iopub.status.idle":"2025-02-27T04:32:47.451092Z","shell.execute_reply.started":"2025-02-27T03:35:41.512269Z","shell.execute_reply":"2025-02-27T04:32:47.443942Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create a DataFrame with the results\nmodels_result = []\n\nfor name, v in models.items():\n    models_result.append([ name, \n                          models[name]['train_accuracy'][-1],\n                          models[name]['val_acc'][-1], \n                          models[name]['perf']])\n    \ndf_results = pd.DataFrame(models_result, \n                          columns = ['model','train_accuracy','val_accuracy','Training time (sec)'])","metadata":{"_uuid":"4fbd5679-8a8b-460e-b496-c9448ddadeb9","_cell_guid":"76c02ff3-47d8-43ca-8d0f-341c9d5e0819","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T04:32:47.460029Z","iopub.execute_input":"2025-02-27T04:32:47.460300Z","iopub.status.idle":"2025-02-27T04:32:47.537318Z","shell.execute_reply.started":"2025-02-27T04:32:47.460276Z","shell.execute_reply":"2025-02-27T04:32:47.536702Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#training accuracy of different pretrained models\nplt.figure(figsize = (15,5))\nsns.barplot(x = 'model', y = 'train_accuracy', data = df_results)\nplt.title('Accuracy on the Training Set (after 5 epochs)', fontsize = 15)\nplt.ylim(0,1)\nplt.xticks(rotation=90)\nplt.show()","metadata":{"_uuid":"fd80cedc-4c3c-414a-a635-ca1cdedb1d5b","_cell_guid":"fcd84a0f-50c2-4fcf-a604-1056799de0ff","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T04:32:47.538302Z","iopub.execute_input":"2025-02-27T04:32:47.538622Z","iopub.status.idle":"2025-02-27T04:32:48.273884Z","shell.execute_reply.started":"2025-02-27T04:32:47.538583Z","shell.execute_reply":"2025-02-27T04:32:48.272905Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#validation accuracy of different pretrained models\nplt.figure(figsize = (15,5))\nsns.barplot(x = 'model', y = 'val_accuracy', data = df_results)\nplt.title('Accuracy on the Validation Set (after 5 epochs)', fontsize = 15)\nplt.ylim(0,1)\nplt.xticks(rotation=90)\nplt.show()","metadata":{"_uuid":"8fac251c-6a06-4058-a7f3-7e84e9e2c105","_cell_guid":"f038392a-fdbf-422d-9a7f-228cb40fb124","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T04:32:48.277120Z","iopub.execute_input":"2025-02-27T04:32:48.277397Z","iopub.status.idle":"2025-02-27T04:32:48.682283Z","shell.execute_reply.started":"2025-02-27T04:32:48.277369Z","shell.execute_reply":"2025-02-27T04:32:48.681468Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#training time of different pretrained models\nplt.figure(figsize = (15,5))\nsns.barplot(x = 'model', y = 'Training time (sec)', data = df_results)\nplt.title('Training time for each model in sec', fontsize = 15)\nplt.xticks(rotation=90)\nplt.show()","metadata":{"_uuid":"091c94b5-7aef-4c14-a3f8-c04d9cc145d1","_cell_guid":"29fce12f-a18c-40dd-846c-95d8940b1686","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T04:32:48.683508Z","iopub.execute_input":"2025-02-27T04:32:48.684292Z","iopub.status.idle":"2025-02-27T04:32:49.097614Z","shell.execute_reply.started":"2025-02-27T04:32:48.684252Z","shell.execute_reply":"2025-02-27T04:32:49.096757Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Normalize the metrics to rank models fairly\ndf_results['Train Rank'] = df_results['train_accuracy'].rank(ascending=False)\ndf_results['Val Rank'] = df_results['val_accuracy'].rank(ascending=False)\ndf_results['Time Rank'] = df_results['Training time (sec)'].rank(ascending=True)  # Lower time is better\n\n# Compute the final ranking\ndf_results['Final Rank'] = df_results[['Train Rank', 'Val Rank', 'Time Rank']].mean(axis=1)\ndf_results.sort_values(by='Final Rank', ascending=True, inplace=True)\ndf_results.reset_index(drop=True, inplace=True)\ndf_results","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-27T04:32:49.098687Z","iopub.execute_input":"2025-02-27T04:32:49.098970Z","iopub.status.idle":"2025-02-27T04:32:49.145755Z","shell.execute_reply.started":"2025-02-27T04:32:49.098944Z","shell.execute_reply":"2025-02-27T04:32:49.144968Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Select the best model\nbest_model_name = df_results.iloc[0]['model']\nbest_model_fn = models[best_model_name]['model']\n\nprint(f\"Best model found: {best_model_name}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-27T04:32:49.146719Z","iopub.execute_input":"2025-02-27T04:32:49.146974Z","iopub.status.idle":"2025-02-27T04:32:49.151290Z","shell.execute_reply.started":"2025-02-27T04:32:49.146950Z","shell.execute_reply":"2025-02-27T04:32:49.150509Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load the best pretained model based on both training accuracy,validation accuracy and training time\npretrained_model = best_model_fn\n\npretrained_model.trainable = False","metadata":{"_uuid":"b13f59b4-0acb-40da-ab7d-aaf8af3bc3ff","_cell_guid":"37d199d5-0563-4740-ba92-5b47cd331441","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T04:32:49.152239Z","iopub.execute_input":"2025-02-27T04:32:49.152467Z","iopub.status.idle":"2025-02-27T04:32:49.163498Z","shell.execute_reply.started":"2025-02-27T04:32:49.152443Z","shell.execute_reply":"2025-02-27T04:32:49.162742Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(pretrained_model.output.shape)","metadata":{"_uuid":"5d326048-a631-4dbf-8144-db18b5b5d0c1","_cell_guid":"c20ff4d6-f6be-4892-9c22-2a3d85f5146e","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T04:32:49.164420Z","iopub.execute_input":"2025-02-27T04:32:49.164678Z","iopub.status.idle":"2025-02-27T04:32:49.180309Z","shell.execute_reply.started":"2025-02-27T04:32:49.164652Z","shell.execute_reply":"2025-02-27T04:32:49.179643Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Loading the model\ninputs = pretrained_model.input\n\n\nx = pretrained_model.output\nx = Dense(1024, activation='relu')(x)\nx = Dropout(0.5)(x)  # Dropout layer to reduce overfitting\nx = Dense(512, activation='relu')(x)\nx = Dropout(0.3)(x)  # Another dropout for regularization\noutputs = Dense(5, activation='softmax')(x)\n\n\nmodel = tf.keras.Model(inputs=inputs, outputs=outputs)\n\nmodel.compile(\n    optimizer='adam',\n    loss='categorical_crossentropy',\n    metrics=['accuracy']\n)\n\nhistory = model.fit(\n    train_images,\n    validation_data=val_images,\n    batch_size = 32,\n    epochs=50,\n    callbacks=[\n        tf.keras.callbacks.EarlyStopping(\n            monitor='val_loss',\n            patience=2,\n            restore_best_weights=True\n        )\n    ]\n)","metadata":{"_uuid":"ed512340-64e1-4971-aca9-c90f54eb29ef","_cell_guid":"243fe1ea-c019-4642-a968-75288d510c7d","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T04:32:49.181269Z","iopub.execute_input":"2025-02-27T04:32:49.181927Z","iopub.status.idle":"2025-02-27T04:34:36.168018Z","shell.execute_reply.started":"2025-02-27T04:32:49.181861Z","shell.execute_reply":"2025-02-27T04:34:36.165504Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Accuracy\npd.DataFrame(history.history)[['accuracy','val_accuracy']].plot()\nplt.title(\"Accuracy\")\nplt.show()","metadata":{"_uuid":"c308cf55-82d5-47d6-9269-743c3c8b17a2","_cell_guid":"d9e7d42f-114a-4c07-91c1-2f4176d9931d","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T04:34:36.173438Z","iopub.execute_input":"2025-02-27T04:34:36.174973Z","iopub.status.idle":"2025-02-27T04:34:36.615026Z","shell.execute_reply.started":"2025-02-27T04:34:36.174941Z","shell.execute_reply":"2025-02-27T04:34:36.614067Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Loss\npd.DataFrame(history.history)[['loss','val_loss']].plot()\nplt.title(\"Loss\")\nplt.show()","metadata":{"_uuid":"b44b5187-061c-4635-a217-391e1c372f93","_cell_guid":"c20c403c-9fb3-4cad-8848-efc93c2083e0","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T04:34:36.616053Z","iopub.execute_input":"2025-02-27T04:34:36.616407Z","iopub.status.idle":"2025-02-27T04:34:36.849460Z","shell.execute_reply.started":"2025-02-27T04:34:36.616369Z","shell.execute_reply":"2025-02-27T04:34:36.848530Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Evaluation\nresults = model.evaluate(test_images, verbose=0)\nprint(\"Test Loss: {:.5f}\".format(results[0]))\nprint(\"Accuracy on the test set: {:.2f}%\".format(results[1] * 100))","metadata":{"_uuid":"0c5ae473-d060-4aac-bde8-8e1c9e1eda76","_cell_guid":"0ea981f0-f7b0-4fab-9e61-7e5ee48071a1","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T04:34:36.850760Z","iopub.execute_input":"2025-02-27T04:34:36.851097Z","iopub.status.idle":"2025-02-27T04:34:49.458420Z","shell.execute_reply.started":"2025-02-27T04:34:36.851060Z","shell.execute_reply":"2025-02-27T04:34:49.456140Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict the label of the test_images\ny_pred_probs = model.predict(test_images)\npred = np.argmax(y_pred_probs,axis=1)\n\n# Map the label\nlabels = (train_images.class_indices)\nlabels = dict((v,k) for k,v in labels.items())\npred = [labels[k] for k in pred]\n\n# Display the result\nprint(f'The first 5 predictions: {pred[:5]}')","metadata":{"_uuid":"e652a881-f2fd-4d11-9e34-4bf280dc6913","_cell_guid":"b590dca0-f659-4bc5-ae3a-25538ec48f30","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T04:34:49.460867Z","iopub.execute_input":"2025-02-27T04:34:49.461144Z","iopub.status.idle":"2025-02-27T04:34:58.761917Z","shell.execute_reply.started":"2025-02-27T04:34:49.461117Z","shell.execute_reply":"2025-02-27T04:34:58.759690Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Classification report\ny_test = list(test_df.Label)\nprint(classification_report(y_test, pred))","metadata":{"_uuid":"0ef8990c-39c8-4491-b767-9aa8c0ffeb03","_cell_guid":"8a9a262f-affd-48c8-83ca-55fc73f9e9d9","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T04:34:58.763312Z","iopub.execute_input":"2025-02-27T04:34:58.764143Z","iopub.status.idle":"2025-02-27T04:34:58.824040Z","shell.execute_reply.started":"2025-02-27T04:34:58.764102Z","shell.execute_reply":"2025-02-27T04:34:58.823350Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Normalized Confusion Matrix\ncf_matrix = confusion_matrix(y_test, pred, normalize='true')\nplt.figure(figsize = (10,6))\nsns.heatmap(cf_matrix, annot=True, xticklabels = sorted(set(y_test)), yticklabels = sorted(set(y_test)))\nplt.title('Normalized Confusion Matrix')\nplt.show()","metadata":{"_uuid":"16143bea-a464-4dc6-bc8e-0fd04b8cc90a","_cell_guid":"baeb5173-913f-42db-8507-a9cb64630b24","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T04:34:58.825136Z","iopub.execute_input":"2025-02-27T04:34:58.825479Z","iopub.status.idle":"2025-02-27T04:34:59.152679Z","shell.execute_reply.started":"2025-02-27T04:34:58.825443Z","shell.execute_reply":"2025-02-27T04:34:59.151834Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Compute metrics\nprecision = precision_score(y_test, pred, average='weighted')\nrecall = recall_score(y_test, pred, average='weighted')\nf1 = f1_score(y_test, pred, average='weighted')\n\nprint(f\"Precision: {precision}\")\nprint(f\"Recall: {recall}\")\nprint(f\"F1 Score: {f1}\")","metadata":{"_uuid":"25bfc77b-6601-4631-8895-7a125ced6d50","_cell_guid":"dd78f848-5f19-4fa4-a010-d35c2da2f0a4","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T04:34:59.153667Z","iopub.execute_input":"2025-02-27T04:34:59.153921Z","iopub.status.idle":"2025-02-27T04:34:59.173423Z","shell.execute_reply.started":"2025-02-27T04:34:59.153897Z","shell.execute_reply":"2025-02-27T04:34:59.172771Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Evaluation scores\ny_true = y_test\ny_pred = pred\n\n# Binarize the labels for multiclass classification\nlb = LabelBinarizer()\ny_true_binarized = lb.fit_transform(y_true)\ny_pred_binarized = lb.transform(y_pred)\nclasses = lb.classes_\n\n# ROC AUC score for each class\nroc_auc_scores = {}\nfor idx, class_name in enumerate(lb.classes_):\n    roc_auc_scores[class_name] = roc_auc_score(y_true_binarized[:, idx], y_pred_binarized[:, idx])\n\nprint(\"ROC AUC Scores by Class:\")\nfor class_name, score in roc_auc_scores.items():\n    print(f\"Class {class_name}: {score:.4f}\")\n\n# Overall micro and macro ROC AUC scores\noverall_micro_auc = roc_auc_score(y_true_binarized, y_pred_binarized, average='micro')\noverall_macro_auc = roc_auc_score(y_true_binarized, y_pred_binarized, average='macro')\nprint(\"\\n\")\nprint(f\"Overall Micro ROC AUC: {overall_micro_auc:.4f}\")\nprint(f\"Overall Macro ROC AUC: {overall_macro_auc:.4f}\")","metadata":{"_uuid":"42057ed3-5d94-4273-a0ea-4abedf01e04a","_cell_guid":"5d724361-85b0-4dc8-a49e-09546d196d9c","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T04:34:59.174541Z","iopub.execute_input":"2025-02-27T04:34:59.174810Z","iopub.status.idle":"2025-02-27T04:34:59.200392Z","shell.execute_reply.started":"2025-02-27T04:34:59.174785Z","shell.execute_reply":"2025-02-27T04:34:59.199604Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ROC-AUC Curve for each class\nplt.figure(figsize=(10, 8))\nfor idx, class_name in enumerate(classes):\n    fpr, tpr, _ = roc_curve(y_true_binarized[:, idx], y_pred_probs[:, idx])\n    roc_auc = auc(fpr, tpr)\n    plt.plot(fpr, tpr, label=f\"Class {class_name} (AUC = {roc_auc:.2f})\")\n\nplt.plot([0, 1], [0, 1], 'k--', label=\"Random Guess\")\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"ROC-AUC Curve\")\nplt.legend()\nplt.grid()\nplt.show()","metadata":{"_uuid":"bda132fb-6107-4163-967f-dc0fd1c9d0f8","_cell_guid":"82cb981c-db0d-4e91-804e-b29f26d845d6","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T04:34:59.201225Z","iopub.execute_input":"2025-02-27T04:34:59.201465Z","iopub.status.idle":"2025-02-27T04:34:59.460158Z","shell.execute_reply.started":"2025-02-27T04:34:59.201441Z","shell.execute_reply":"2025-02-27T04:34:59.459341Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Precision-Recall Curve for each class\nplt.figure(figsize=(10, 8))\nfor idx, class_name in enumerate(classes):\n    precision, recall, _ = precision_recall_curve(y_true_binarized[:, idx], y_pred_probs[:, idx])\n    plt.plot(recall, precision, label=f\"Class {class_name}\")\n\nplt.xlabel(\"Recall\")\nplt.ylabel(\"Precision\")\nplt.title(\"Precision-Recall Curve\")\nplt.legend()\nplt.grid()\nplt.show()","metadata":{"_uuid":"3d8ffe3c-d358-4db6-827d-2982f6bc4cff","_cell_guid":"fde62e3a-b773-4e88-809a-cd28bf2f6b14","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T04:34:59.461299Z","iopub.execute_input":"2025-02-27T04:34:59.461644Z","iopub.status.idle":"2025-02-27T04:34:59.705617Z","shell.execute_reply.started":"2025-02-27T04:34:59.461605Z","shell.execute_reply":"2025-02-27T04:34:59.704820Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Display some pictures of the dataset with their labels and the predictions\nfig, axes = plt.subplots(nrows=3, ncols=3, figsize=(15, 15),\n                        subplot_kw={'xticks': [], 'yticks': []})\n\nfor i, ax in enumerate(axes.flat):\n    ax.imshow(plt.imread(test_df.Filepath.iloc[i]))\n    ax.set_title(f\"True: {test_df.Label.iloc[i]}\\nPredicted: {pred[i]}\")\nplt.tight_layout()\nplt.show()","metadata":{"_uuid":"a9ac1618-4832-44a3-81e9-e62a9c16aaa5","_cell_guid":"deed4edc-95f1-470e-8ae2-874020938768","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T04:34:59.706660Z","iopub.execute_input":"2025-02-27T04:34:59.706949Z","iopub.status.idle":"2025-02-27T04:35:01.491959Z","shell.execute_reply.started":"2025-02-27T04:34:59.706923Z","shell.execute_reply":"2025-02-27T04:35:01.491034Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#model summary\nmodel.summary()","metadata":{"_uuid":"02638036-fe95-4277-83ed-c9bce54993ca","_cell_guid":"9e41a394-e7be-48b4-befd-a334b734d98e","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T04:35:01.493088Z","iopub.execute_input":"2025-02-27T04:35:01.493360Z","iopub.status.idle":"2025-02-27T04:35:01.576277Z","shell.execute_reply.started":"2025-02-27T04:35:01.493334Z","shell.execute_reply":"2025-02-27T04:35:01.575440Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#save the model\nmodel.save('my_model.h5')","metadata":{"_uuid":"cc2ad106-790d-4e2e-bea9-6be40a7d2674","_cell_guid":"62e671b7-b56f-498b-be00-41603b6e725f","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T04:35:01.577697Z","iopub.execute_input":"2025-02-27T04:35:01.578476Z","iopub.status.idle":"2025-02-27T04:35:01.862618Z","shell.execute_reply.started":"2025-02-27T04:35:01.578424Z","shell.execute_reply":"2025-02-27T04:35:01.861956Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"shutil.move('my_model.h5', '/kaggle/working/my_model.h5')","metadata":{"_uuid":"6f719555-0500-4a50-b05b-3989f4a49713","_cell_guid":"46b6b9da-a0ce-43dd-b946-f97cf0d2a214","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-27T04:35:01.863798Z","iopub.execute_input":"2025-02-27T04:35:01.864563Z","iopub.status.idle":"2025-02-27T04:35:01.870482Z","shell.execute_reply.started":"2025-02-27T04:35:01.864523Z","shell.execute_reply":"2025-02-27T04:35:01.869699Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null}]}