{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":70203,"databundleVersionId":8068726,"sourceType":"competition"},{"sourceId":8033468,"sourceType":"datasetVersion","datasetId":4735360}],"dockerImageVersionId":30685,"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\n# import os\n# for 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","execution":{"iopub.status.busy":"2024-04-13T03:32:54.669234Z","iopub.execute_input":"2024-04-13T03:32:54.669653Z","iopub.status.idle":"2024-04-13T03:32:55.626324Z","shell.execute_reply.started":"2024-04-13T03:32:54.669618Z","shell.execute_reply":"2024-04-13T03:32:55.625160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Read Library**","metadata":{}},{"cell_type":"code","source":"import os\n\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nfrom tensorflow.keras import layers, models\n\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.applications import MobileNetV2, VGG16\nfrom tensorflow.keras.layers import Input, Dense, GlobalAveragePooling2D, concatenate\nfrom tensorflow.keras.models import Model\n\nfrom sklearn.preprocessing import LabelBinarizer\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import roc_auc_score, roc_curve, auc\n\nAUTOTUNE = tf.data.experimental.AUTOTUNE","metadata":{"execution":{"iopub.status.busy":"2024-04-13T03:32:55.628378Z","iopub.execute_input":"2024-04-13T03:32:55.628924Z","iopub.status.idle":"2024-04-13T03:33:09.662876Z","shell.execute_reply.started":"2024-04-13T03:32:55.628886Z","shell.execute_reply":"2024-04-13T03:33:09.661958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Base directory where the folders are stored\nimage_folder = \"/kaggle/input/birdclef-2024-mel-spectrograms/train_images/\"\n\n# Mapping of classes to numerical labels\n# Classes are alphabetically sorted - so we can easily restore class order when we load\nclass_labels = {class_name: i for i, class_name in enumerate(sorted(os.listdir(image_folder)))}\nnum_classes = len(class_labels)\n\n# Collect all file paths and their corresponding class labels\nfile_paths = []\nlabels = []\n\n# New structure to keep track of groups\nsamples = {}\n\nfor class_name in os.listdir(image_folder):\n    class_dir = os.path.join(image_folder, class_name)\n    for filename in os.listdir(class_dir):\n        # Extract base sample name from filename (files split at \"_\")\n        sample_base = filename.split('_')[0] \n        full_path = os.path.join(class_dir, filename)\n        \n        if sample_base not in samples:\n            samples[sample_base] = {'files': [], 'label': class_labels[class_name]}\n        samples[sample_base]['files'].append(full_path)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-13T03:33:09.669484Z","iopub.execute_input":"2024-04-13T03:33:09.669964Z","iopub.status.idle":"2024-04-13T03:33:16.474778Z","shell.execute_reply.started":"2024-04-13T03:33:09.669907Z","shell.execute_reply":"2024-04-13T03:33:16.473870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_size = 0.2\nbatch_size = 32\n\n# Convert the samples dictionary into a list for splitting\nsamples_list = list(samples.items())\n\n# Split the list of tuples into training and validation sets\ntrain_samples, val_samples = train_test_split(samples_list, test_size=test_size, random_state=42)\n\ndef preprocess_image(file_path, label):\n    img = tf.io.read_file(file_path)\n    img = tf.image.decode_jpeg(img, channels=3)\n    img = tf.image.random_contrast(img, lower=0.2, upper=1.8)\n    img = tf.image.random_brightness(img, max_delta=0.2)\n\n    #scale 0-1\n    img = tf.cast(img, tf.float32) / 255.0\n\n    #scale -1 to +1\n    #img = tf.cast(img, tf.float32) / 255.0  # Normalize to [0, 1]\n#     img = img * 2.0 - 1.0  # Scale to [-1, +1]\n    \n#     label = tf.one_hot(label, depth=num_classes)\n    return img, label\n\n# Function to extract file paths and labels from the split samples\ndef extract_files_and_labels(sample_list):\n    file_paths = []\n    labels = []\n    for _, sample_info in sample_list:\n        file_paths.extend(sample_info['files'])\n        labels.extend([sample_info['label']] * len(sample_info['files']))\n    return file_paths, labels\n\ntrain_files, train_labels = extract_files_and_labels(train_samples)\nval_files, val_labels = extract_files_and_labels(val_samples)\n\nnum_classes = np.max(train_labels) + 1\n\n# Create a tf.data.Dataset from file paths and labels\ntrain_dataset = tf.data.Dataset.from_tensor_slices((train_files, train_labels))\ntrain_dataset = train_dataset.map(preprocess_image, num_parallel_calls=AUTOTUNE)\ntrain_dataset = train_dataset.shuffle(buffer_size=1000).batch(batch_size).prefetch(AUTOTUNE)\n\nval_dataset = tf.data.Dataset.from_tensor_slices((val_files, val_labels))\nval_dataset = val_dataset.map(preprocess_image, num_parallel_calls=AUTOTUNE)\nval_dataset = val_dataset.batch(batch_size).prefetch(AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T05:00:04.052717Z","iopub.execute_input":"2024-04-13T05:00:04.053412Z","iopub.status.idle":"2024-04-13T05:00:04.316489Z","shell.execute_reply.started":"2024-04-13T05:00:04.053380Z","shell.execute_reply":"2024-04-13T05:00:04.315691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define input shape for VGG16\ninput_shape_vgg = (224, 224, 3)\n\n# Create input for VGG16\ninput_vgg = Input(shape=input_shape_vgg, name='input_vgg')\n\n# Load VGG16 model\nbase_model_vgg = VGG16(weights='imagenet', include_top=False, input_tensor=input_vgg)\nbase_model_vgg.trainable = False\n\n# Global average pooling for VGG16\nx_vgg = GlobalAveragePooling2D()(base_model_vgg.output)\n\n# Add a fully-connected layer\nx = Dense(1024, activation='relu')(x_vgg)\n# Add a logistic layer matching class count\npredictions = Dense(1, activation='softmax')(x)\n\n# Define learning rate scheduler\ninitial_learning_rate = 0.00005\nlr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(\n    initial_learning_rate=initial_learning_rate,\n    decay_steps=1000,\n    decay_rate=0.96,\n    staircase=True)\n\n# Define optimizer\noptimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)\n\n# Define the model with one input (VGG16)\nmodel = Model(inputs=input_vgg, outputs=predictions)\n\n# Compile the model\nmodel.compile(optimizer=optimizer,\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2024-04-13T05:00:05.145787Z","iopub.execute_input":"2024-04-13T05:00:05.146639Z","iopub.status.idle":"2024-04-13T05:00:05.427886Z","shell.execute_reply.started":"2024-04-13T05:00:05.146603Z","shell.execute_reply":"2024-04-13T05:00:05.427106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Turn off all warnings except for errors\ntf.get_logger().setLevel('ERROR')\n\n# # Fit the model with callbacks\n# history_extract = model.fit(train_dataset, \n#                                                      epochs=3,\n#                                                      steps_per_epoch=len(train_dataset),\n#                                                      validation_data=val_dataset,\n#                                                      validation_steps=int(len(val_dataset)),\n#                                                     )","metadata":{"execution":{"iopub.status.busy":"2024-04-13T05:02:24.506929Z","iopub.execute_input":"2024-04-13T05:02:24.507789Z","iopub.status.idle":"2024-04-13T05:02:24.513416Z","shell.execute_reply.started":"2024-04-13T05:02:24.507749Z","shell.execute_reply":"2024-04-13T05:02:24.512230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_size = 0.2\nbatch_size = 32\n\n# Convert the samples dictionary into a list for splitting\nsamples_list = list(samples.items())\n\n# Split the list of tuples into training and validation sets\ntrain_samples, val_samples = train_test_split(samples_list, test_size=test_size, random_state=42)\n\n# Function to extract file paths and labels from the split samples\ndef extract_files_and_labels(sample_list):\n    file_paths_1 = []\n    file_paths_2 = []\n    labels = []\n    for _, sample_info in sample_list:\n        file_paths_1.extend(sample_info['files'])# Assuming the same files for both inputs\n        labels.extend([sample_info['label']] * len(sample_info['files']))  # Assuming labels are the same for both inputs\n    return file_paths_1, labels\n\n# Extract file paths and labels for training and validation sets\ntrain_files_1, train_labels = extract_files_and_labels(train_samples)\nval_files_1, val_labels = extract_files_and_labels(val_samples)\n\ndef preprocess_image(file_path_1):\n    img_1 = tf.io.read_file(file_path_1)\n    img_1 = tf.image.decode_jpeg(img_1, channels=3)\n    img_1 = tf.image.random_contrast(img_1, lower=0.2, upper=1.8)\n    img_1 = tf.image.random_brightness(img_1, max_delta=0.2)\n    img_1 = tf.cast(img_1, tf.float32) / 255.0  # Scale to [0, 1]\n\n    return img_1, img_1\n\ndef preprocess_label(label):\n#     label = tf.one_hot(label, depth=num_classes)\n    return label\n\n# Create tf.data.Dataset for training set\ntrain_dataset = tf.data.Dataset.from_tensor_slices(train_files_1)\ntrain_dataset = train_dataset.map(preprocess_image, num_parallel_calls=AUTOTUNE)\n\nlabel_dataset = tf.data.Dataset.from_tensor_slices(train_labels)\nlabel_dataset = label_dataset.map(preprocess_label, num_parallel_calls=AUTOTUNE)\n\ntrain_dataset = tf.data.Dataset.zip((train_dataset, label_dataset))\ntrain_dataset = train_dataset.shuffle(buffer_size=1000).batch(32).prefetch(AUTOTUNE)\n\nnum_classes = np.max(train_labels) + 1\n\n# Create tf.data.Dataset for validation set\nval_dataset = tf.data.Dataset.from_tensor_slices(val_files_1)\nval_dataset = val_dataset.map(preprocess_image, num_parallel_calls=AUTOTUNE)\n\nlabel_val_dataset = tf.data.Dataset.from_tensor_slices(val_labels)\nlabel_val_dataset = label_val_dataset.map(preprocess_label, num_parallel_calls=AUTOTUNE)\n\nval_dataset = tf.data.Dataset.zip((val_dataset, label_val_dataset))\nval_dataset = val_dataset.shuffle(buffer_size=1000).batch(32).prefetch(AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T05:02:27.057736Z","iopub.execute_input":"2024-04-13T05:02:27.058621Z","iopub.status.idle":"2024-04-13T05:02:27.271924Z","shell.execute_reply.started":"2024-04-13T05:02:27.058589Z","shell.execute_reply":"2024-04-13T05:02:27.270957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.preprocessing import OneHotEncoder\n# from tensorflow.keras.preprocessing.image import img_to_array, load_img\n\n# test_size = 0.2\n# batch_size = 32\n\n# # Convert the samples dictionary into a list for splitting\n# samples_list = list(samples.items())\n\n# # Split the list of tuples into training and validation sets\n# train_samples, val_samples = train_test_split(samples_list, test_size=test_size, random_state=42)\n\n# # Function to extract file paths and labels from the split samples\n# def extract_files_and_labels(sample_list):\n#     file_paths_1 = []\n#     labels = []\n#     for _, sample_info in sample_list:\n#         file_paths_1.extend(sample_info['files'])# Assuming the same files for both inputs\n#         labels.extend([sample_info['label']] * len(sample_info['files']))  # Assuming labels are the same for both inputs\n#     return file_paths_1, labels\n\n# # Extract file paths and labels for training and validation sets\n# train_files_1, train_labels = extract_files_and_labels(train_samples)\n# val_files_1, val_labels = extract_files_and_labels(val_samples)\n\n# # One-hot encode the labels\n# encoder = OneHotEncoder()\n# train_labels_encoded = encoder.fit_transform(np.array(train_labels[:1000]).reshape(-1, 1)).toarray()\n# val_labels_encoded = encoder.fit_transform(np.array(val_labels[:1000]).reshape(-1, 1)).toarray()\n\n# # Load and preprocess training images\n# train_images = []\n# for path in train_files_1[:1000]:\n#     img = load_img(path, target_size=(224, 224))  # Resizing image to desired size\n#     img_array = img_to_array(img)\n#     train_images.append(img_array)\n# train_images = np.array(train_images)\n\n# # Load and preprocess validation images\n# val_images = []\n# for path in val_files_1[:1000]:\n#     img = load_img(path, target_size=(224, 224))  # Resizing image to desired size\n#     img_array = img_to_array(img)\n#     val_images.append(img_array)\n# val_images = np.array(val_images)\n\n# # Create datasets using tf.data.Dataset.from_tensor_slices for training and validation\n# train_dataset = tf.data.Dataset.from_tensor_slices((train_images, {'input_1': train_images, 'input_2': train_images, 'output': train_labels[:1000]}))\n# val_dataset = tf.data.Dataset.from_tensor_slices((val_images, {'input_1': val_images, 'input_2': val_images, 'output': val_labels[:1000]}))\n\n# # Shuffle and batch the datasets for training\n# BATCH_SIZE = 32\n# train_dataset = train_dataset.shuffle(buffer_size=len(train_images)).batch(BATCH_SIZE).prefetch(AUTOTUNE)\n# val_dataset = val_dataset.batch(BATCH_SIZE).prefetch(AUTOTUNE)\n\n# # Example of iterating over the training dataset\n# for batch in train_dataset.take(1):\n#     images_batch, inputs_batch = batch[0], batch[1]\n#     input_1_data, input_2_data, output_data = inputs_batch['input_1'], inputs_batch['input_2'], inputs_batch['output']\n#     print(\"Shape of input 1:\", input_1_data.shape)\n#     print(\"Shape of input 2:\", input_2_data.shape)\n#     print(\"Shape of output:\", output_data.shape)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-13T05:02:58.836134Z","iopub.execute_input":"2024-04-13T05:02:58.836831Z","iopub.status.idle":"2024-04-13T05:02:58.843613Z","shell.execute_reply.started":"2024-04-13T05:02:58.836793Z","shell.execute_reply":"2024-04-13T05:02:58.842531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define input shapes for both models\ninput_shape_mobilenet = (224, 224, 3)\ninput_shape_vgg = (224, 224, 3)\n\n# Create inputs for both models\ninput_mobilenet = Input(shape=input_shape_mobilenet, name='input_mobilenet')\ninput_vgg = Input(shape=input_shape_vgg, name='input_vgg')\n\n# Load MobileNetV2 model\nbase_model_mobilenet = MobileNetV2(weights='imagenet', include_top=False, input_tensor=input_mobilenet)\n\n# Load VGG16 model\nbase_model_vgg = VGG16(weights='imagenet', include_top=False, input_tensor=input_vgg)\nbase_model_mobilenet.trainable = False\n\nnumberOfLayer = -10\n# Allow both base models to be trainable\n# Set only the top layer of MobileNetV2 as trainable\nfor layer in base_model_mobilenet.layers[:numberOfLayer]:\n    layer.trainable = True\n    \nfor layer in base_model_mobilenet.layers[:numberOfLayer]:\n    pass\n#     print(layer.trainable)\n\nbase_model_vgg.trainable = True\n\n# Global average pooling for both base models\nx_mobilenet = GlobalAveragePooling2D()(base_model_mobilenet.output)\nx_vgg = GlobalAveragePooling2D()(base_model_vgg.output)\n\n# Concatenate features from both models\ncombined_features = concatenate([x_mobilenet, x_vgg])\n\n# Add a fully-connected layer\nx = Dense(1024, activation='relu')(combined_features)\nx = Dense(512, activation='relu')(x)\nx = Dense(256, activation='relu')(x)\n\n# Add a logistic layer matching class count\npredictions = Dense(num_classes, activation='softmax')(x)\n\n# Define learning rate scheduler\ninitial_learning_rate = 0.00005\nlr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(\n    initial_learning_rate=initial_learning_rate,\n    decay_steps=1000,\n    decay_rate=0.96,\n    staircase=True)\n\n# Define optimizer\noptimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)\n\n# Define the model with two inputs\nmodel = Model(inputs=[input_mobilenet,input_vgg], outputs=predictions)\n\n# Compile the model\nmodel.compile(optimizer=optimizer,\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-13T05:03:00.299422Z","iopub.execute_input":"2024-04-13T05:03:00.299784Z","iopub.status.idle":"2024-04-13T05:03:01.402797Z","shell.execute_reply.started":"2024-04-13T05:03:00.299755Z","shell.execute_reply":"2024-04-13T05:03:01.401967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from tensorflow.keras.utils import plot_model\n\n# Plot the model\n# plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T05:03:10.912697Z","iopub.execute_input":"2024-04-13T05:03:10.913639Z","iopub.status.idle":"2024-04-13T05:03:10.917520Z","shell.execute_reply.started":"2024-04-13T05:03:10.913605Z","shell.execute_reply":"2024-04-13T05:03:10.916444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_data = list(train_dataset.as_numpy_iterator())  # Convert PrefetchDataset to a list\n\n# # Assuming the features and labels are stored in the first and second elements of the tuple\n# train_dataset_features = [data[0] for data in train_data]\n# train_dataset_labels = [data[1] for data in train_data]","metadata":{"execution":{"iopub.status.busy":"2024-04-13T05:03:11.161220Z","iopub.execute_input":"2024-04-13T05:03:11.161574Z","iopub.status.idle":"2024-04-13T05:03:11.166517Z","shell.execute_reply.started":"2024-04-13T05:03:11.161547Z","shell.execute_reply":"2024-04-13T05:03:11.165242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# len([data[1] for data in train_data][0][0]), num_classes","metadata":{"execution":{"iopub.status.busy":"2024-04-13T05:03:11.646529Z","iopub.execute_input":"2024-04-13T05:03:11.647349Z","iopub.status.idle":"2024-04-13T05:03:11.651412Z","shell.execute_reply.started":"2024-04-13T05:03:11.647318Z","shell.execute_reply":"2024-04-13T05:03:11.650240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# len(train_dataset_features) , len(train_dataset_labels)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T05:03:11.864623Z","iopub.execute_input":"2024-04-13T05:03:11.865186Z","iopub.status.idle":"2024-04-13T05:03:11.871082Z","shell.execute_reply.started":"2024-04-13T05:03:11.865157Z","shell.execute_reply":"2024-04-13T05:03:11.869978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_data = list(train_dataset.as_numpy_iterator())  # Convert PrefetchDataset to a list\n\n# # Assuming the features and labels are stored in the first and second elements of the tuple\n# train_dataset_features = [data[0] for data in train_data]\n# train_dataset_features2 = [data[1] for data in train_data]","metadata":{"execution":{"iopub.status.busy":"2024-04-13T05:03:12.048030Z","iopub.execute_input":"2024-04-13T05:03:12.048344Z","iopub.status.idle":"2024-04-13T05:03:12.052531Z","shell.execute_reply.started":"2024-04-13T05:03:12.048319Z","shell.execute_reply":"2024-04-13T05:03:12.051581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# len(train_dataset_features2[0][0])","metadata":{"execution":{"iopub.status.busy":"2024-04-13T05:03:12.813699Z","iopub.execute_input":"2024-04-13T05:03:12.814579Z","iopub.status.idle":"2024-04-13T05:03:12.818454Z","shell.execute_reply.started":"2024-04-13T05:03:12.814545Z","shell.execute_reply":"2024-04-13T05:03:12.817463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.config.experimental_run_functions_eagerly(True)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T05:03:13.348350Z","iopub.execute_input":"2024-04-13T05:03:13.348933Z","iopub.status.idle":"2024-04-13T05:03:13.357231Z","shell.execute_reply.started":"2024-04-13T05:03:13.348890Z","shell.execute_reply":"2024-04-13T05:03:13.356057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Turn off all warnings except for errors\ntf.get_logger().setLevel('ERROR')\n\n# Fit the model with callbacks\nhistory_feature_extract = model.fit(train_dataset, \n                                                     epochs=3,\n                                                     steps_per_epoch=len(train_dataset),\n                                                     validation_data=val_dataset,\n                                                     validation_steps=int(len(val_dataset)),\n                                                    )","metadata":{"execution":{"iopub.status.busy":"2024-04-13T05:03:13.558296Z","iopub.execute_input":"2024-04-13T05:03:13.558652Z","iopub.status.idle":"2024-04-13T05:25:32.671731Z","shell.execute_reply.started":"2024-04-13T05:03:13.558624Z","shell.execute_reply":"2024-04-13T05:25:32.670767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming you have defined and trained your model already\n\n# Get predictions on validation data\nval_predictions = model.predict(val_dataset)\n\n# Convert tensors to numpy arrays\nval_labels_np = np.concatenate([y.numpy() for x, y in val_dataset], axis=0)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-13T05:25:32.674416Z","iopub.execute_input":"2024-04-13T05:25:32.674892Z","iopub.status.idle":"2024-04-13T05:26:04.993008Z","shell.execute_reply.started":"2024-04-13T05:25:32.674856Z","shell.execute_reply":"2024-04-13T05:26:04.991925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_predictions , val_labels_np","metadata":{"execution":{"iopub.status.busy":"2024-04-13T05:26:04.994334Z","iopub.execute_input":"2024-04-13T05:26:04.994708Z","iopub.status.idle":"2024-04-13T05:26:05.002845Z","shell.execute_reply.started":"2024-04-13T05:26:04.994673Z","shell.execute_reply":"2024-04-13T05:26:05.001665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert integer labels to one-hot encoded format\nlabel_binarizer = LabelBinarizer()\nval_labels_one_hot = label_binarizer.fit_transform(val_labels_np)\n\n# Calculate ROC AUC for each class\nroc_auc_scores = []\nfpr = dict()\ntpr = dict()\nroc_auc = dict()\nfor i in range(val_labels_one_hot.shape[1]):  # Iterate over each class\n    fpr[i], tpr[i], _ = roc_curve(val_labels_one_hot[:, i], val_predictions[:, i])\n    roc_auc[i] = auc(fpr[i], tpr[i])\n    roc_auc_scores.append(roc_auc[i])\n\n# Calculate average ROC AUC\navg_roc_auc = sum(roc_auc_scores) / len(roc_auc_scores)\n\n# Print average ROC AUC\nprint(f\"Average ROC AUC: {avg_roc_auc * 100:.2f}%\")\n\n# Get indices of top 10 classes with highest ROC AUC scores\ntop_10_indices = sorted(range(len(roc_auc_scores)), key=lambda i: roc_auc_scores[i], reverse=True)[:10]\n\n# Plot ROC curve for top 10 classes\nplt.figure(figsize=(8, 6))\nfor i in top_10_indices:\n    plt.plot(fpr[i], tpr[i], label=f\"Class {i} (AUC = {roc_auc[i]:.2f})\")\nplt.plot([0, 1], [0, 1], color='navy', linestyle='--')\nplt.xlim([0.0, 1.0])\nplt.ylim([0.0, 1.05])\nplt.xlabel('False Positive Rate')\nplt.ylabel('True Positive Rate')\nplt.title('ROC Curve for Top 10 Classes')\nplt.legend(loc=\"lower right\")\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-04-13T05:26:05.005374Z","iopub.execute_input":"2024-04-13T05:26:05.005790Z","iopub.status.idle":"2024-04-13T05:26:05.634437Z","shell.execute_reply.started":"2024-04-13T05:26:05.005752Z","shell.execute_reply":"2024-04-13T05:26:05.633414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert integer labels to one-hot encoded format\nlabel_binarizer = LabelBinarizer()\nval_labels_one_hot = label_binarizer.fit_transform(val_labels_np)\n\n# Calculate ROC AUC for each class\nroc_auc_scores = []\nfor i in range(val_labels_one_hot.shape[1]):  # Iterate over each class\n    roc_auc = roc_auc_score(val_labels_one_hot[:, i], val_predictions[:, i])\n    roc_auc_scores.append(roc_auc)\n\n# Print ROC AUC scores for each class\n\nprint(f\"ROC AUC : {sum(roc_auc_scores)/len(roc_auc_scores)*100}\")\nfor i, score in enumerate(roc_auc_scores):\n    print(f\"ROC AUC for class {i}: {score*100}\")","metadata":{"execution":{"iopub.status.busy":"2024-04-13T05:26:05.635681Z","iopub.execute_input":"2024-04-13T05:26:05.636028Z","iopub.status.idle":"2024-04-13T05:26:06.080890Z","shell.execute_reply.started":"2024-04-13T05:26:05.636000Z","shell.execute_reply":"2024-04-13T05:26:06.079767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}