{"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":8045887,"sourceType":"datasetVersion","datasetId":4744304},{"sourceId":26431,"sourceType":"modelInstanceVersion","modelInstanceId":22241},{"sourceId":3836,"sourceType":"modelInstanceVersion","modelInstanceId":2739}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Bird Species Classification-(BirdCLEF 2024)**","metadata":{}},{"cell_type":"markdown","source":"![](https://www.wassupmate.com/wp-content/uploads/2015/07/Colorful-Parrots-Wallpaper.jpg)","metadata":{}},{"cell_type":"markdown","source":"# **Import Libraries**","metadata":{}},{"cell_type":"code","source":"import os\nimport glob\nimport shutil\nimport zipfile\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport numpy as np\nimport plotly.express as px\nimport librosa\nfrom IPython.display import Audio\nimport pandas as pd\nimport pickle\nfrom joblib import dump, load\nfrom pathlib import Path\nfrom imblearn.over_sampling import RandomOverSampler\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix\nimport librosa.display\nfrom tqdm import tqdm\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.preprocessing import OneHotEncoder, StandardScaler\nimport tensorflow as tf\nimport tensorflow_hub as hub\nfrom tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.layers import (Conv2D, MaxPooling2D, Flatten, Dense, Dropout, GlobalAveragePooling2D, Input, LSTM, Bidirectional, Attention, GRU, TimeDistributed, Conv1D, MaxPooling1D, Concatenate, concatenate, Reshape)\nfrom tensorflow.keras.applications import ResNet50 ,VGG16\nfrom tensorflow.keras.applications.resnet import preprocess_input\nfrom tensorflow.keras.regularizers import l2\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import LearningRateScheduler","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:44:07.727592Z","iopub.execute_input":"2024-04-11T05:44:07.728352Z","iopub.status.idle":"2024-04-11T05:44:12.941902Z","shell.execute_reply.started":"2024-04-11T05:44:07.728313Z","shell.execute_reply":"2024-04-11T05:44:12.941025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Collection and Processing**","metadata":{}},{"cell_type":"code","source":"meta_data = pd.read_csv('/kaggle/input/birdclef-2024/train_metadata.csv')\nmeta_data.head(4)","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:44:12.943929Z","iopub.execute_input":"2024-04-11T05:44:12.945010Z","iopub.status.idle":"2024-04-11T05:44:13.084851Z","shell.execute_reply.started":"2024-04-11T05:44:12.944973Z","shell.execute_reply":"2024-04-11T05:44:13.083850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta_data.info()","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:44:13.086034Z","iopub.execute_input":"2024-04-11T05:44:13.086363Z","iopub.status.idle":"2024-04-11T05:44:13.119599Z","shell.execute_reply.started":"2024-04-11T05:44:13.086337Z","shell.execute_reply":"2024-04-11T05:44:13.118608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Exploratory Data Analysis (EDA)**","metadata":{}},{"cell_type":"code","source":"fig = px.scatter_mapbox(meta_data, lat='latitude', lon='longitude', color='common_name', \n                        hover_name='common_name', hover_data=['latitude', 'longitude'], \n                        title='Origin of Bird Species',\n                        zoom=1, height=600, template='plotly_dark')\nfig.update_layout(\n    mapbox_style=\"white-bg\",\n    mapbox_layers=[\n        {\n            \"below\": 'traces',\n            \"sourcetype\": \"raster\",\n            \"sourceattribution\": \"United States Geological Survey\",\n            \"source\": [\n                \"https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}\"\n            ]\n        }\n      ])\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:44:13.122051Z","iopub.execute_input":"2024-04-11T05:44:13.122362Z","iopub.status.idle":"2024-04-11T05:44:14.556871Z","shell.execute_reply.started":"2024-04-11T05:44:13.122337Z","shell.execute_reply":"2024-04-11T05:44:14.555933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def audio_waveframe(file_path):\n    # Load the audio file\n    audio_data, sampling_rate = librosa.load(file_path)\n    # Calculate the duration of the audio file\n    duration = len(audio_data) / sampling_rate\n    # Create a time array for plotting\n    time = np.arange(0, duration, 1/sampling_rate)\n    # Plot the waveform\n    plt.figure(figsize=(30, 4))\n    plt.plot(time, audio_data, color='blue')\n    plt.title('Audio Waveform')\n    plt.xlabel('Time (s)')\n    plt.ylabel('Amplitude')\n    plot = plt.show()\n    return plot\n\ndef spectrogram(file_path):\n    # Compute the short-time Fourier transform (STFT)\n    n_fft = 500  # Number of FFT points 2048\n    hop_length = 50  # Hop length for STFT 512\n    audio_data, sampling_rate = librosa.load(file_path)\n    stft = librosa.stft(audio_data, n_fft=n_fft, hop_length=hop_length)\n    # Convert the magnitude spectrogram to decibels (log scale)\n    spectrogram = librosa.amplitude_to_db(np.abs(stft))\n    # Plot the spectrogram\n    plt.figure(figsize=(30, 6))\n    librosa.display.specshow(spectrogram, sr=sampling_rate, hop_length=hop_length, x_axis='time', y_axis='linear')\n    plt.colorbar(format='%+2.0f dB')\n    plt.title('Spectrogram')\n    plt.xlabel('Time (s)')\n    plt.ylabel('Frequency (Hz)')\n    plt.tight_layout()\n    plot = plt.show()\n    return plot\n\ndef audio_analysis(file_path):\n    aw = audio_waveframe(file_path)\n    spg = spectrogram(file_path)\n    return aw, spg","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:44:14.558171Z","iopub.execute_input":"2024-04-11T05:44:14.558502Z","iopub.status.idle":"2024-04-11T05:44:14.571032Z","shell.execute_reply.started":"2024-04-11T05:44:14.558472Z","shell.execute_reply":"2024-04-11T05:44:14.570029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio_analysis('/kaggle/input/birdclef-2024/train_audio/asbfly/XC134896.ogg')\nAudio('/kaggle/input/birdclef-2024/train_audio/asbfly/XC134896.ogg')","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:44:14.572327Z","iopub.execute_input":"2024-04-11T05:44:14.572636Z","iopub.status.idle":"2024-04-11T05:44:19.476181Z","shell.execute_reply.started":"2024-04-11T05:44:14.572591Z","shell.execute_reply":"2024-04-11T05:44:19.475282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Feature Engineering**","metadata":{}},{"cell_type":"markdown","source":"# 1. Feature Extraction","metadata":{}},{"cell_type":"code","source":"def convert_to_spectrogram(file_path):\n    # Load the audio file\n    y, sr = librosa.load(file_path, sr=None)\n    \n    # Calculate the spectrogram\n    spectrogram = librosa.feature.melspectrogram(y=y, sr=sr)\n    \n    return spectrogram\n\ndef preprocess_sounds(main_folder):\n    # Create empty lists to store spectrograms and labels\n    spectrograms = []\n    labels = []\n    counter = 0\n    # Iterate over each subfolder in the main folder\n    for folder_name in os.listdir(main_folder):\n        subfolder_path = os.path.join(main_folder, folder_name)\n        if os.path.isdir(subfolder_path) and (len(os.listdir(subfolder_path)) >= 100):\n            label = folder_name\n            # Iterate over each file in the subfolder\n            conter_image = 0\n            for file_name in tqdm(os.listdir(subfolder_path), desc='Processing {}'.format(folder_name)):\n                if file_name.endswith('.ogg'):  # Adjust extension if needed\n                    file_path = os.path.join(subfolder_path, file_name)\n                    spectrogram = convert_to_spectrogram(file_path)\n                    \n                    # Append the spectrogram to the list\n                    spectrograms.append(spectrogram)\n                    # Append the label to the labels list\n                    labels.append(label)\n                    conter_image +=1\n                if conter_image > 100:\n                    break\n        counter+=1\n        if counter == 10 :\n            break\n\n    return spectrograms, labels","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:44:19.477779Z","iopub.execute_input":"2024-04-11T05:44:19.478772Z","iopub.status.idle":"2024-04-11T05:44:19.489811Z","shell.execute_reply.started":"2024-04-11T05:44:19.478724Z","shell.execute_reply":"2024-04-11T05:44:19.488901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Path to the folder containing sound files\nfolder_path = '/kaggle/input/birdclef-2024/train_audio'\n\n# Preprocess sounds and store spectrograms and labels\nspectrograms, labels = preprocess_sounds(folder_path)","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:44:19.490771Z","iopub.execute_input":"2024-04-11T05:44:19.491089Z","iopub.status.idle":"2024-04-11T05:45:50.346134Z","shell.execute_reply.started":"2024-04-11T05:44:19.491062Z","shell.execute_reply":"2024-04-11T05:45:50.344754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.array(labels).shape","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:45:50.348337Z","iopub.execute_input":"2024-04-11T05:45:50.350443Z","iopub.status.idle":"2024-04-11T05:45:50.359210Z","shell.execute_reply.started":"2024-04-11T05:45:50.350389Z","shell.execute_reply":"2024-04-11T05:45:50.357997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(spectrograms),len(spectrograms))\n\narray_shapes = [arr.shape[1] for arr in spectrograms]\n\nprint(sum(array_shapes)/len(array_shapes))\n\n# Determine the target shape for resizing\ntarget_shape = (224, 224)  # Replace with your desired shape\n\n# Resize each array in the list to the target shape\nresized_spectrograms = []\nfor arr in spectrograms:\n    resized_arr = np.zeros(target_shape)  # Initialize a new array of the target shape\n    resized_arr[:min(target_shape[0], arr.shape[0]), :min(target_shape[1], arr.shape[1])] = arr[:min(target_shape[0], arr.shape[0]), :min(target_shape[1], arr.shape[1])]\n    resized_spectrograms.append(resized_arr)\n\n# Convert spectrograms and labels to NumPy arrays\nspectrograms_array = np.array(resized_spectrograms)\nlabels_array = np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:45:50.365790Z","iopub.execute_input":"2024-04-11T05:45:50.366685Z","iopub.status.idle":"2024-04-11T05:45:50.581685Z","shell.execute_reply.started":"2024-04-11T05:45:50.366636Z","shell.execute_reply":"2024-04-11T05:45:50.580901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the spectrograms and labels\nnp.savez('spectrograms.npz', spectrograms=spectrograms_array)\npd.DataFrame(labels_array, columns=['label']).to_csv('labels.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:45:50.582743Z","iopub.execute_input":"2024-04-11T05:45:50.583060Z","iopub.status.idle":"2024-04-11T05:45:51.094091Z","shell.execute_reply.started":"2024-04-11T05:45:50.583023Z","shell.execute_reply":"2024-04-11T05:45:51.093095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the spectrograms and labels\nloaded_data = np.load('spectrograms.npz')\nloaded_spectrograms = loaded_data['spectrograms']\nloaded_labels = pd.read_csv('labels.csv')['label']","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:45:51.095527Z","iopub.execute_input":"2024-04-11T05:45:51.095880Z","iopub.status.idle":"2024-04-11T05:45:51.339643Z","shell.execute_reply.started":"2024-04-11T05:45:51.095846Z","shell.execute_reply":"2024-04-11T05:45:51.338605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming spectrograms_array and labels_array are your NumPy arrays\nnum_samples = len(loaded_spectrograms)\n\nfor i in range(3):\n    idx = np.random.randint(num_samples)  # Choose a random index\n    spectrogram = loaded_spectrograms[idx]\n    label = loaded_labels[idx]\n\n    plt.figure(figsize=(10, 4))\n    librosa.display.specshow(librosa.power_to_db(spectrogram, ref=np.max), y_axis='mel', fmax=8000, x_axis='time')\n    plt.colorbar(format='%+2.0f dB')\n    plt.title(f'{label}')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:45:51.340882Z","iopub.execute_input":"2024-04-11T05:45:51.341212Z","iopub.status.idle":"2024-04-11T05:45:52.582291Z","shell.execute_reply.started":"2024-04-11T05:45:51.341167Z","shell.execute_reply":"2024-04-11T05:45:52.581433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Augmentation**","metadata":{}},{"cell_type":"code","source":"\n# Create ImageDataGenerator for augmentation\ndatagen = ImageDataGenerator(\n    rotation_range=20,      # Rotate images randomly up to 20 degrees\n    width_shift_range=0.1,  # Shift images horizontally by up to 10%\n    height_shift_range=0.1, # Shift images vertically by up to 10%\n    zoom_range=0.1,         # Zoom images by up to 10%\n    horizontal_flip=True,   # Flip images horizontally\n    vertical_flip=False,    # Don't flip images vertically\n    fill_mode='nearest'     # Fill points outside the input boundaries using nearest value\n)\n\n# Generate augmented images and labels\nnum_augmented = 5  # Number of augmented images to generate for each original image\nmax_plots = 25     # Maximum number of plots\nnum_plots = min(len(loaded_spectrograms) * num_augmented, max_plots)\naugmented_images = []\naugmented_labels = []\n\nfor idx, spectrogram in enumerate(loaded_spectrograms):\n    # Reshape spectrogram to match expected input shape for ImageDataGenerator\n    spectrogram = np.expand_dims(spectrogram, axis=-1)\n    spectrogram = np.expand_dims(spectrogram, axis=0)\n    \n    # Generate augmented images\n    for batch in datagen.flow(spectrogram, batch_size=1):\n        augmented_images.append(batch[0][:, :, 0])  # Remove the channel dimension\n        augmented_labels.append(loaded_labels[idx])\n        if len(augmented_images) >= 5:\n            break\n    if len(augmented_images) >= len(loaded_spectrograms):\n        break\n\n# Convert lists to numpy arrays\naugmented_images = np.array(augmented_images)\naugmented_labels = np.array(augmented_labels)\n\n# Plot augmented images\nplt.figure(figsize=(10, 10))\nfor i in range(num_plots):\n    plt.subplot(5, min(num_plots // 5, 5), i + 1)  # Adjusting the number of columns\n    librosa.display.specshow(librosa.power_to_db(augmented_images[i], ref=np.max), y_axis='mel', fmax=8000, x_axis='time')\n#     plt.colorbar(format='%+2.0f dB')\n    plt.title(f\"Label: {augmented_labels[i]}\")\n    plt.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:45:52.583373Z","iopub.execute_input":"2024-04-11T05:45:52.583677Z","iopub.status.idle":"2024-04-11T05:45:57.404731Z","shell.execute_reply.started":"2024-04-11T05:45:52.583652Z","shell.execute_reply":"2024-04-11T05:45:57.403747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Concatenate original spectrograms and augmented images\ncombined_spectrograms = np.concatenate((loaded_spectrograms, augmented_images), axis=0)\ncombined_labels = np.concatenate((labels_array, np.repeat(augmented_labels, 1)), axis=0)","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:45:57.405945Z","iopub.execute_input":"2024-04-11T05:45:57.406296Z","iopub.status.idle":"2024-04-11T05:45:57.540142Z","shell.execute_reply.started":"2024-04-11T05:45:57.406266Z","shell.execute_reply":"2024-04-11T05:45:57.539016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" combined_spectrograms.shape , labels_array.shape ,augmented_labels.shape ,combined_labels.shape, num_augmented , loaded_labels.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:45:57.541594Z","iopub.execute_input":"2024-04-11T05:45:57.541972Z","iopub.status.idle":"2024-04-11T05:45:57.549181Z","shell.execute_reply.started":"2024-04-11T05:45:57.541938Z","shell.execute_reply":"2024-04-11T05:45:57.548276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = len(loaded_labels.value_counts())","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:45:57.550498Z","iopub.execute_input":"2024-04-11T05:45:57.550797Z","iopub.status.idle":"2024-04-11T05:45:57.559210Z","shell.execute_reply.started":"2024-04-11T05:45:57.550773Z","shell.execute_reply":"2024-04-11T05:45:57.558314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combined_labels","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:45:57.560448Z","iopub.execute_input":"2024-04-11T05:45:57.560776Z","iopub.status.idle":"2024-04-11T05:45:57.570088Z","shell.execute_reply.started":"2024-04-11T05:45:57.560747Z","shell.execute_reply":"2024-04-11T05:45:57.569004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Reshape data**","metadata":{}},{"cell_type":"code","source":"encoder = OneHotEncoder(categories='auto', sparse=False)\nlabels = combined_labels.reshape(-1, 1)\n# Fit and transform the training labels\nonehot_labels = encoder.fit_transform(labels)\n\n# Print the shape of the one-hot encoded labels\nprint(\"One-hot encoded labels :\", onehot_labels[:10])","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:45:57.571172Z","iopub.execute_input":"2024-04-11T05:45:57.571466Z","iopub.status.idle":"2024-04-11T05:45:57.582782Z","shell.execute_reply.started":"2024-04-11T05:45:57.571444Z","shell.execute_reply":"2024-04-11T05:45:57.581700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# mean = loaded_spectrograms.mean(axis=(0, 1))  # Calculate mean along batch and height dimensions\n# std = loaded_spectrograms.std(axis=(0, 1))    # Calculate standard deviation along batch and height dimensions\n\n# # Normalize the data\n# X_normalized = (loaded_spectrograms - mean) / std","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:45:57.584448Z","iopub.execute_input":"2024-04-11T05:45:57.584797Z","iopub.status.idle":"2024-04-11T05:45:57.589271Z","shell.execute_reply.started":"2024-04-11T05:45:57.584769Z","shell.execute_reply":"2024-04-11T05:45:57.588096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **StandardScaler**","metadata":{}},{"cell_type":"code","source":"scaler = StandardScaler()\nflattened_spectrograms = combined_spectrograms.reshape((combined_spectrograms.shape[0], -1))\nflattened_spectrograms_standardized = scaler.fit_transform(flattened_spectrograms)\nstandardized_spectrograms = flattened_spectrograms_standardized.reshape(combined_spectrograms.shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:45:57.590372Z","iopub.execute_input":"2024-04-11T05:45:57.590693Z","iopub.status.idle":"2024-04-11T05:45:58.282536Z","shell.execute_reply.started":"2024-04-11T05:45:57.590668Z","shell.execute_reply":"2024-04-11T05:45:58.281499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"standardized_spectrograms.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:45:58.283997Z","iopub.execute_input":"2024-04-11T05:45:58.284435Z","iopub.status.idle":"2024-04-11T05:45:58.291122Z","shell.execute_reply.started":"2024-04-11T05:45:58.284378Z","shell.execute_reply":"2024-04-11T05:45:58.290094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Expand dimensions to add the third channel\nstandardized_spectrograms = np.expand_dims(standardized_spectrograms, axis=-1)\n\n# Repeat the grayscale channel three times to match RGB format\nstandardized_spectrograms = np.repeat(standardized_spectrograms, 3, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:45:58.292339Z","iopub.execute_input":"2024-04-11T05:45:58.292630Z","iopub.status.idle":"2024-04-11T05:45:58.742893Z","shell.execute_reply.started":"2024-04-11T05:45:58.292608Z","shell.execute_reply":"2024-04-11T05:45:58.741866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"standardized_spectrograms.shape , onehot_labels.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:45:58.744232Z","iopub.execute_input":"2024-04-11T05:45:58.744569Z","iopub.status.idle":"2024-04-11T05:45:58.754776Z","shell.execute_reply.started":"2024-04-11T05:45:58.744540Z","shell.execute_reply":"2024-04-11T05:45:58.753908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Split data**","metadata":{}},{"cell_type":"code","source":"# Split the data into training and testing sets\nspectrograms_train, spectrograms_test, labels_train, labels_test = train_test_split(standardized_spectrograms, onehot_labels, test_size=0.2, random_state=42)\n\n# Print the shapes of the training and testing sets\nprint(\"Training spectrograms shape:\", len(spectrograms_train))\nprint(\"Testing spectrograms shape:\", len(spectrograms_test))\nprint(\"Training labels shape:\", len(labels_train))\nprint(\"Testing labels shape:\", len(labels_test))","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:45:58.756343Z","iopub.execute_input":"2024-04-11T05:45:58.756738Z","iopub.status.idle":"2024-04-11T05:45:59.120747Z","shell.execute_reply.started":"2024-04-11T05:45:58.756706Z","shell.execute_reply":"2024-04-11T05:45:59.119766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# spectrograms_train = np.expand_dims(spectrograms_train, axis=-1)\n# spectrograms_test = np.expand_dims(spectrograms_test, axis=-1)\n# X_normalized[0]","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:45:59.122239Z","iopub.execute_input":"2024-04-11T05:45:59.123019Z","iopub.status.idle":"2024-04-11T05:45:59.127157Z","shell.execute_reply.started":"2024-04-11T05:45:59.122982Z","shell.execute_reply":"2024-04-11T05:45:59.126167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spectrograms_train.shape ,spectrograms_train[1].shape","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:45:59.128475Z","iopub.execute_input":"2024-04-11T05:45:59.128821Z","iopub.status.idle":"2024-04-11T05:45:59.137314Z","shell.execute_reply.started":"2024-04-11T05:45:59.128790Z","shell.execute_reply":"2024-04-11T05:45:59.136391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Model 1 NN**","metadata":{}},{"cell_type":"code","source":"\n# Define the model\nmodel = Sequential()\n\n# Add convolutional layers\nmodel.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(224, 224, 3)))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\n# Flatten the output of the convolutional layers\nmodel.add(Flatten())\n\n# Add dense layers for classification\nmodel.add(Dense(64, activation='relu'))\n# model.add(Dropout(0.2))\nmodel.add(Dense(num_classes, activation='softmax')) # Adjust the output units according to your task\n\n# Compile the model\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n\n# Print the summary of the model\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:45:59.144697Z","iopub.execute_input":"2024-04-11T05:45:59.145005Z","iopub.status.idle":"2024-04-11T05:45:59.857960Z","shell.execute_reply.started":"2024-04-11T05:45:59.144973Z","shell.execute_reply":"2024-04-11T05:45:59.857030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming you have prepared your training data and labels\nmodel.fit(spectrograms_train, labels_train, batch_size=32, epochs=10, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:45:59.859568Z","iopub.execute_input":"2024-04-11T05:45:59.860181Z","iopub.status.idle":"2024-04-11T05:46:21.670450Z","shell.execute_reply.started":"2024-04-11T05:45:59.860143Z","shell.execute_reply":"2024-04-11T05:46:21.669505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Model2 with ResNet50**","metadata":{}},{"cell_type":"code","source":"\n# Load the ResNet50 model pre-trained on ImageNet\nresnet_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n\n# Freeze the layers of the ResNet50 model\nfor layer in resnet_model.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:46:21.671901Z","iopub.execute_input":"2024-04-11T05:46:21.672213Z","iopub.status.idle":"2024-04-11T05:46:23.053934Z","shell.execute_reply.started":"2024-04-11T05:46:21.672188Z","shell.execute_reply":"2024-04-11T05:46:23.053092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Add your custom classification layers\nx = resnet_model.output\nx = GlobalAveragePooling2D()(x)\nx = Dense(128, activation='relu')(x)\npredictions = Dense(num_classes, activation='softmax')(x)  # Adjust num_classes\n\n# Create the final model\nmodel = Model(inputs=resnet_model.input, outputs=predictions)\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Print model summary\n# model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:46:23.055148Z","iopub.execute_input":"2024-04-11T05:46:23.055462Z","iopub.status.idle":"2024-04-11T05:46:23.107886Z","shell.execute_reply.started":"2024-04-11T05:46:23.055436Z","shell.execute_reply":"2024-04-11T05:46:23.106949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming you have prepared your training data and labels\nmodel.fit(spectrograms_train, labels_train, batch_size=16, epochs=10, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:46:23.109235Z","iopub.execute_input":"2024-04-11T05:46:23.110027Z","iopub.status.idle":"2024-04-11T05:47:14.233991Z","shell.execute_reply.started":"2024-04-11T05:46:23.109993Z","shell.execute_reply":"2024-04-11T05:47:14.232863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Model3 Two input - ResNet50 , CNN**","metadata":{}},{"cell_type":"code","source":"# Load the ResNet50 model pre-trained on ImageNet\nresnet_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n# Freeze the layers of the ResNet50 model\nfor layer in resnet_model.layers[:10]:\n    layer.trainable = True\n# for layer in resnet_model.layers[:]:\n#     layer.trainable = False\n# for layer in resnet_model.layers:\n#     print(\"layer.trainable layer :\",layer.trainable)","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:47:14.238122Z","iopub.execute_input":"2024-04-11T05:47:14.238421Z","iopub.status.idle":"2024-04-11T05:47:15.394082Z","shell.execute_reply.started":"2024-04-11T05:47:14.238396Z","shell.execute_reply":"2024-04-11T05:47:15.393216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Assuming you already have resnet_model loaded\n\n# Get the output tensor from ResNet50\nx_resnet = resnet_model.output\n\n# Add global average pooling layer\nx_resnet = GlobalAveragePooling2D()(x_resnet)\n\n# Add dense layer for feature extraction\nx_resnet = Dense(128, activation='relu')(x_resnet)\n\n# Define the input layer for the additional convolutional layers\ninput_layer_conv = Input(shape=(224, 224, 3))\n\n# Add the convolutional layers to the input layer\nx_conv = Conv2D(32, kernel_size=(3, 3), activation='relu')(input_layer_conv)\nx_conv = MaxPooling2D(pool_size=(2, 2))(x_conv)\nx_conv = Conv2D(64, kernel_size=(3, 3), activation='relu')(x_conv)\nx_conv = MaxPooling2D(pool_size=(2, 2))(x_conv)\nx_conv = Flatten()(x_conv)\n\n# Concatenate the outputs of the ResNet50 and convolutional layers\nconcatenated_output = Concatenate()([x_resnet, x_conv])\n\n# Add dense layers for classification\nx = Dropout(0.5)(concatenated_output)\nx = Dense(512, activation='relu')(x)\nx = Dropout(0.5)(x)\nx = Dense(128, activation='relu')(x)\npredictions = Dense(num_classes, activation='sigmoid')(x)  # Adjust num_classes\n\n# Create the final model\nmodel = Model(inputs=[resnet_model.input, input_layer_conv], outputs=predictions)\n# Print model summary\n# model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:47:15.395214Z","iopub.execute_input":"2024-04-11T05:47:15.395511Z","iopub.status.idle":"2024-04-11T05:47:15.489395Z","shell.execute_reply.started":"2024-04-11T05:47:15.395480Z","shell.execute_reply":"2024-04-11T05:47:15.488336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n# Assuming you have prepared your training data and labels\nmodel.fit([spectrograms_train,spectrograms_train], labels_train, batch_size=16, epochs=10, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:47:15.490702Z","iopub.execute_input":"2024-04-11T05:47:15.490981Z","iopub.status.idle":"2024-04-11T05:50:37.176203Z","shell.execute_reply.started":"2024-04-11T05:47:15.490957Z","shell.execute_reply":"2024-04-11T05:50:37.175268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **model4 CNN , lstm , gru , Bidirectional , Attention , with change learning rate**  ","metadata":{}},{"cell_type":"code","source":"\n# Input layer\ninput_layer = Input(shape=(224, 224, 3))  # Assuming 224x224 RGB images\n\n# CNN layer\ncnn_layer = Conv2D(32, (3, 3), activation='relu')(input_layer)\ncnn_layer = MaxPooling2D(pool_size=(2, 2))(cnn_layer)\n\n# Flatten layer\nflatten_layer = TimeDistributed(Flatten())(cnn_layer)\n\nlstm_layer = LSTM(64, return_sequences=True)(flatten_layer)\n\n# Bidirectional LSTM layer with kernel regularization\nbilstm_layer = Bidirectional(LSTM(64, return_sequences=True))(lstm_layer)\n\n# Apply Attention mechanism\nattention_layer = Attention()([bilstm_layer, bilstm_layer])\n\n# GRU layer with kernel regularization\ngru_layer = GRU(64, return_sequences=True)(attention_layer)\n\n# Dropout layer\ndropout_layer = Dropout(0.5)(gru_layer)\n\n# Convolutional layer with kernel regularization\nconv1d_layer = Conv1D(64, 3, activation='relu')(dropout_layer)\n\n# MaxPooling1D layer\npooling_layer = MaxPooling1D(pool_size=2)(conv1d_layer)\n\n# Flatten layer\nflatten_layer = Flatten()(pooling_layer)\n\n# Dense layer with kernel regularization\ndense_layers = Dense(1216, activation='relu')(flatten_layer)\n\n# Dropout layer\ndropout_layer = Dropout(0.2)(dense_layers)\n\n# Output layer\noutput_layer = Dense(num_classes, activation='softmax')(dropout_layer)\n\n# Create model\nmodel = Model(inputs=input_layer, outputs=output_layer)\n\n# Define custom learning rate scheduler function\ndef lr_scheduler(epoch, lr):\n    if epoch < 5:\n        return lr\n    else:\n        return float(lr * tf.math.exp(-0.1))\n\n# Define learning rate scheduler callback\nlr_scheduler_callback = LearningRateScheduler(lr_scheduler)\n\n# Compile model with custom learning rate and Adam optimizer\nmodel.compile(optimizer=Adam(learning_rate=0.001), loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Print model summary\n# model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:50:37.180912Z","iopub.execute_input":"2024-04-11T05:50:37.181240Z","iopub.status.idle":"2024-04-11T05:50:37.442751Z","shell.execute_reply.started":"2024-04-11T05:50:37.181212Z","shell.execute_reply":"2024-04-11T05:50:37.441735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    x=spectrograms_train,\n    y=labels_train,\n    batch_size=32,\n    epochs=10,\n    validation_split=0.2,\n    callbacks=[lr_scheduler_callback]\n)","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:50:37.444002Z","iopub.execute_input":"2024-04-11T05:50:37.444288Z","iopub.status.idle":"2024-04-11T05:51:13.150366Z","shell.execute_reply.started":"2024-04-11T05:50:37.444263Z","shell.execute_reply":"2024-04-11T05:51:13.149388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming you already have resnet_model loaded\n\n# Get the output tensor from ResNet50\nx_resnet = resnet_model.output\n\n# Add global average pooling layer\nx_resnet = GlobalAveragePooling2D()(x_resnet)\n\n# Add dense layer for feature extraction\nx_resnet = Dense(128, activation='relu')(x_resnet)\n\n# Define the input layer for the additional convolutional layers\ninput_layer_conv = Input(shape=(224, 224, 3))\n\n# Add the convolutional layers to the input layer\nx_conv = Conv2D(32, kernel_size=(3, 3), activation='relu')(input_layer_conv)\nx_conv = MaxPooling2D(pool_size=(2, 2))(x_conv)\nx_conv = Conv2D(64, kernel_size=(3, 3), activation='relu')(x_conv)\nx_conv = MaxPooling2D(pool_size=(2, 2))(x_conv)\nx_conv = Flatten()(x_conv)\n\n# Concatenate the outputs of the ResNet50 and convolutional layers\nconcatenated_output = Concatenate()([x_resnet, x_conv])\n\n# Add dense layers for classification\nx = Dropout(0.5)(concatenated_output)\nx = Dense(128, activation='relu')(x)\npredictions = Dense(num_classes, activation='softmax')(x)  # Adjust num_classes\n\n# Create the final model\nmodel = Model(inputs=[resnet_model.input, input_layer_conv], outputs=predictions)\n\n# Define custom learning rate scheduler function\ndef lr_scheduler(epoch, lr):\n    if epoch < 5:\n        return lr\n    else:\n        return float(lr * tf.math.exp(-0.1))\n\n# Define learning rate scheduler callback\nlr_scheduler_callback = LearningRateScheduler(lr_scheduler)\n\n# Compile model with custom learning rate and Adam optimizer\nmodel.compile(optimizer=Adam(learning_rate=0.001), loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Print model summary\n# model.summary()\n","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:51:13.159845Z","iopub.execute_input":"2024-04-11T05:51:13.160241Z","iopub.status.idle":"2024-04-11T05:51:13.262948Z","shell.execute_reply.started":"2024-04-11T05:51:13.160209Z","shell.execute_reply":"2024-04-11T05:51:13.261821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n# Assuming you have prepared your training data and labels\nmodel.fit([spectrograms_train,spectrograms_train], labels_train, batch_size=16, epochs=10, validation_split=0.2, callbacks=[lr_scheduler_callback])","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:51:13.264415Z","iopub.execute_input":"2024-04-11T05:51:13.264705Z","iopub.status.idle":"2024-04-11T05:54:06.622305Z","shell.execute_reply.started":"2024-04-11T05:51:13.264680Z","shell.execute_reply":"2024-04-11T05:54:06.621269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Model5 with 4 input to Bidirectional , VGG , GRU , ResNet**","metadata":{}},{"cell_type":"code","source":"# Define input shapes and other parameters\nsequence_length = 800  # Example sequence length\nlstm_units = 32  # Example LSTM units\ngru_units = 64  # Example GRU units\nimage_height = 224\nimage_width = 224\nimage_channels = 3\n\n# Reshape the input for LSTM and GRU layers\ninput_bilstm = Input(shape= (150528,1))\ninput_gru = Input(shape=(150528,1))\n\n# Define input layers for ResNet50 and VGG16\ninput_resnet = Input(shape=(image_height, image_width, image_channels))\ninput_vgg = Input(shape=(image_height, image_width, image_channels))\n\n# Define the architecture for LSTM and GRU layers\nbilstm_layer = Bidirectional(LSTM(units=lstm_units))(input_bilstm)\ngru_layer = GRU(units=gru_units)(input_gru)\n\n# Define the architecture for ResNet50\nresnet_model = ResNet50(weights='imagenet', include_top=False, input_shape=(image_height, image_width, image_channels))\nfor layer in resnet_model.layers:\n    layer.trainable = False\nresnet_features = resnet_model(input_resnet)\nresnet_features = Flatten()(resnet_features)  # Flatten the output of ResNet50\n\n# Define the architecture for VGG16\nvgg_model = VGG16(weights='imagenet', include_top=False, input_shape=(image_height, image_width, image_channels))\nfor layer in vgg_model.layers:\n    layer.trainable = False\nvgg_features = vgg_model(input_vgg)\nvgg_features = Flatten()(vgg_features)  # Flatten the output of VGG16\n\n# Concatenate the outputs from different models\nmerged = concatenate([bilstm_layer, resnet_features, gru_layer, vgg_features])\n\n# Add additional layers if needed\noutput = Dense(units=num_classes, activation='softmax')(merged)\n\n# Create and compile the model\nmodel = Model(inputs=[input_bilstm, input_resnet, input_gru, input_vgg], outputs=output)\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Display the model summary\nprint(model.summary())\n","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:54:06.623732Z","iopub.execute_input":"2024-04-11T05:54:06.624414Z","iopub.status.idle":"2024-04-11T05:54:08.249263Z","shell.execute_reply.started":"2024-04-11T05:54:06.624377Z","shell.execute_reply":"2024-04-11T05:54:08.248227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import plot_model\n\n# Define your model here\n\n# Plot the model\nplot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:54:08.250658Z","iopub.execute_input":"2024-04-11T05:54:08.250974Z","iopub.status.idle":"2024-04-11T05:54:08.595911Z","shell.execute_reply.started":"2024-04-11T05:54:08.250947Z","shell.execute_reply":"2024-04-11T05:54:08.595013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the desired image dimensions\ndesired_height = 224\ndesired_width = 224\n\n# Initialize a list to store resized images\nresized_images = []\n\n# Loop through each element in spectrograms_train and resize\nfor i in range(len(spectrograms_train)): \n    # Append the resized image to the list\n    resized_images.append(spectrograms_train[0].reshape(1,-1)[0])\n\n# Convert the list of resized images to a numpy array\nresized_images = np.array(resized_images)\n# Assuming your data is stored in a variable called spectrograms_data\n# resized_images = np.expand_dims(resized_images, axis=-1)\n# resized_images..reshape(800,-1)[0]\nresized_images.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:54:08.597141Z","iopub.execute_input":"2024-04-11T05:54:08.597970Z","iopub.status.idle":"2024-04-11T05:54:08.844060Z","shell.execute_reply.started":"2024-04-11T05:54:08.597942Z","shell.execute_reply":"2024-04-11T05:54:08.843104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Ensure the shape of spectrograms_train_resnet and spectrograms_train_vgg match the input shape\nspectrograms_train_resnet = spectrograms_train.reshape(-1, image_height, image_width, image_channels)\nspectrograms_train_vgg = spectrograms_train.reshape(-1, image_height, image_width, image_channels)\nspectrograms_train_vgg.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:54:08.845188Z","iopub.execute_input":"2024-04-11T05:54:08.845478Z","iopub.status.idle":"2024-04-11T05:54:08.852213Z","shell.execute_reply.started":"2024-04-11T05:54:08.845454Z","shell.execute_reply":"2024-04-11T05:54:08.851356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming you have your spectrogram features in spectrograms_train and labels in labels_train\n# spectrograms_train shape: (num_samples, sequence_length, image_height * image_width * image_channels)\n# labels_train shape: (num_samples, num_classes)\n\n# Fit the model\nhistory = model.fit([resized_images, spectrograms_train_resnet, resized_images, spectrograms_train_vgg],labels_train,batch_size=1,epochs=1,validation_split=0.2 )\n\n# Optionally, you can also specify validation data if you have it separately\n# history = model.fit(\n#     x=[spectrograms_train, spectrograms_train, spectrograms_train, spectrograms_train],\n#     y=labels_train,\n#     batch_size=batch_size,\n#     epochs=num_epochs,\n#     validation_data=([spectrograms_val, spectrograms_val, spectrograms_val, spectrograms_val], labels_val)\n# )\n","metadata":{"execution":{"iopub.status.busy":"2024-04-11T05:54:08.853524Z","iopub.execute_input":"2024-04-11T05:54:08.853814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the model.\nmodel = hub.load('https://kaggle.com/models/google/bird-vocalization-classifier/frameworks/tensorFlow2/variations/bird-vocalization-classifier/versions/1')\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Model Evaluation**","metadata":{}},{"cell_type":"code","source":"def evaluate_model(y_true, y_pred):\n    # Calculate accuracy\n    accuracy = accuracy_score(y_true, y_pred)\n    # Calculate precision\n    precision = precision_score(y_true, y_pred, average='weighted')\n    # Calculate recall\n    recall = recall_score(y_true, y_pred, average='weighted')\n    # Calculate F1 score\n    f1 = f1_score(y_true, y_pred, average='weighted')\n    \n    return accuracy, precision, recall, f1\n\n# Evaluate the model\ny_predict = random_forest_model.predict(x_test)\naccuracy, precision, recall, f1 = evaluate_model(y_test, y_predict)\n# Print evaluation metrics\nprint(\"Accuracy:\", accuracy)\nprint(\"Precision:\", precision)\nprint(\"Recall:\", recall)\nprint(\"F1 Score:\", f1)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This code defines a function `evaluate_model` to assess the performance of a classification model using various metrics such as accuracy, precision, recall, and F1 score. It takes the true labels (`y_true`) and predicted labels (`y_pred`) as inputs. The function calculates each metric using scikit-learn's built-in functions (`accuracy_score`, `precision_score`, `recall_score`, `f1_score`) and returns these metrics. Finally, it evaluates the model on a test set and prints out the computed evaluation metrics.","metadata":{}},{"cell_type":"markdown","source":"# **Model Testing and Deployment**","metadata":{}},{"cell_type":"code","source":"# dump(random_forest_model, 'audio_classifier_model.joblib')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = load('/kaggle/working/audio_classifier_model.joblib')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This code utilizes the `joblib` library to save (`dump`) a trained random forest model (`random_forest_model`) as a file named `'audio_classifier_model.joblib'`. Later, it loads (`load`) the saved model from the specified path into a variable named `model`. This allows the trained model to be stored persistently and reused for future predictions without needing to retrain it each time.","metadata":{}},{"cell_type":"code","source":"def audio_classification(file_path):\n    audio = file_path\n    print(audio)\n    extracted_features = extract_features(audio).reshape(1, -1)\n    # extracted_features = x_test[112].reshape(1, -1)\n    y_predict = random_forest_model.predict(extracted_features)\n    labels_list = annotated_data['label'].unique()\n    encoded_label = annotated_data['encoded_label'].unique()\n\n    labels = {}\n    for label, prediction in zip(encoded_label, labels_list):\n        labels[label] = prediction\n    if y_predict[0] in labels.keys():\n        predicted = ('Predicted Class:', labels[y_predict[0]])\n    return predicted","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_path = '/kaggle/input/birdclef-2024/unlabeled_soundscapes/1001358022.ogg'\naudio_analysis(file_path)\nAudio(file_path)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio_classification(file_path)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This Python function, `audio_classification`, takes a file path to an audio file as input. It:\n1. Reads the audio file from the given path.\n2. Extracts features from the audio using a function called `extract_features`.\n3. Predicts the class of the audio using a pre-trained machine learning model.\n4. Returns the predicted class label for the audio file.","metadata":{}},{"cell_type":"markdown","source":"# **Project Submission**","metadata":{}},{"cell_type":"code","source":"test_soundscapes = '/kaggle/input/birdclef-2024/test_soundscapes'\n\nfor path in Path(test_soundscapes).glob(\"*.ogg\"):\n    print(path)\n    print(path.stem)\n    print(path.stem.split(\"_\"))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.DataFrame(\n     [(path.stem, *path.stem.split(\"_\"), path) for path in Path(test_soundscapes).glob(\"*.ogg\")],\n    columns = [\"filename\", \"name\" ,\"id\", \"path\"]\n)\nprint(test.shape)\ntest.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = test.filename.values.tolist()\n\nbird_cols = list(pd.get_dummies(meta_data['primary_label']).columns)\nsubmission_df = pd.DataFrame(columns=['row_id']+bird_cols)\nsubmission_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, file in enumerate(filenames):\n    predicted = random_forest_model.predict[i]\n    num_rows = len(predicted)\n    row_ids = [f'{file}_{(i+1)*5}' for i in range(num_rows)]\n    df = pd.DataFrame(columns=['row_id']+bird_cols)\n    \n    df['row_id'] = row_ids\n    df[bird_cols] = predicted\n    \n    submission_df = pd.concat([submission_df,df]).reset_index(drop=True)\n    submission_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}