{"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":"gpu","dataSources":[{"sourceId":70203,"databundleVersionId":8068726,"sourceType":"competition"}],"dockerImageVersionId":30674,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport librosa  # For audio feature extraction\nimport os\nimport seaborn as sns\nimport sklearn\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-09T16:03:12.26207Z","iopub.execute_input":"2024-04-09T16:03:12.263127Z","iopub.status.idle":"2024-04-09T16:03:12.267698Z","shell.execute_reply.started":"2024-04-09T16:03:12.263091Z","shell.execute_reply":"2024-04-09T16:03:12.266734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata= pd.read_csv(\"/kaggle/input/birdclef-2024/train_metadata.csv\")\nmetadata.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-09T16:03:15.288121Z","iopub.execute_input":"2024-04-09T16:03:15.288465Z","iopub.status.idle":"2024-04-09T16:03:15.488434Z","shell.execute_reply.started":"2024-04-09T16:03:15.288436Z","shell.execute_reply":"2024-04-09T16:03:15.487521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata.count()","metadata":{"execution":{"iopub.status.busy":"2024-04-09T16:03:18.584355Z","iopub.execute_input":"2024-04-09T16:03:18.584732Z","iopub.status.idle":"2024-04-09T16:03:18.61585Z","shell.execute_reply.started":"2024-04-09T16:03:18.584694Z","shell.execute_reply":"2024-04-09T16:03:18.614828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata.primary_label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-04-09T16:03:21.758565Z","iopub.execute_input":"2024-04-09T16:03:21.758931Z","iopub.status.idle":"2024-04-09T16:03:21.774842Z","shell.execute_reply.started":"2024-04-09T16:03:21.758902Z","shell.execute_reply":"2024-04-09T16:03:21.773697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata.secondary_labels.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-04-09T16:03:24.651547Z","iopub.execute_input":"2024-04-09T16:03:24.651914Z","iopub.status.idle":"2024-04-09T16:03:24.663128Z","shell.execute_reply.started":"2024-04-09T16:03:24.651878Z","shell.execute_reply":"2024-04-09T16:03:24.662071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\n# Load the metadata CSV file\nmetadata_path = \"/kaggle/input/birdclef-2024/train_metadata.csv\"\nmetadata = pd.read_csv(metadata_path)\n\n# Display the first few rows of the metadata\nprint(metadata.head())\n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-09T16:03:28.118902Z","iopub.execute_input":"2024-04-09T16:03:28.11926Z","iopub.status.idle":"2024-04-09T16:03:28.235344Z","shell.execute_reply.started":"2024-04-09T16:03:28.119232Z","shell.execute_reply":"2024-04-09T16:03:28.234433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport librosa\nimport numpy as np\n\n# Define a function to extract spectrograms from audio files\ndef extract_spectrogram(audio_file, duration=5, sr=22050, n_fft=2048, hop_length=512):\n    # Load audio file\n    audio, _ = librosa.load(audio_file, sr=sr, duration=duration, mono=True)\n    # Compute spectrogram\n    spectrogram = librosa.feature.melspectrogram(y=audio, sr=sr, n_fft=n_fft, hop_length=hop_length)\n    spectrogram = librosa.power_to_db(spectrogram, ref=np.max)  # Convert to dB scale\n    return spectrogram\n\n# Example usage:\naudio_file = \"/kaggle/input/birdclef-2024/train_audio/asbfly/XC134896.ogg\"\nspectrogram = extract_spectrogram(audio_file)\nprint(\"Spectrogram shape:\", spectrogram.shape)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-09T16:03:32.661478Z","iopub.execute_input":"2024-04-09T16:03:32.66216Z","iopub.status.idle":"2024-04-09T16:03:42.068003Z","shell.execute_reply.started":"2024-04-09T16:03:32.662128Z","shell.execute_reply":"2024-04-09T16:03:42.066533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nfrom skimage.transform import resize  # Import resize function from scikit-image\n\n# Define the target shape for spectrograms\ntarget_shape = (128, 128)  # Example target shape, adjust as needed\n\n# Split the data into features (spectrograms) and labels\nX = []  # Spectrograms\ny = []  # Labels\n\nfor index, row in metadata.iterrows():\n    audio_file = \"/kaggle/input/birdclef-2024/train_audio/\" + row['filename']\n    spectrogram = extract_spectrogram(audio_file)\n    # Resize spectrogram to target shape\n    spectrogram_resized = resize(spectrogram, target_shape)\n    X.append(spectrogram_resized)\n    y.append(row['primary_label'])  # Update to use 'primary_label' column\n\n# Convert lists to numpy arrays\nX = np.array(X)\ny = np.array(y)\n\n# Split the data into training and validation sets (80% training, 20% validation)\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Print the shapes of the split datasets\nprint(\"Training data - Features shape:\", X_train.shape, \"Labels shape:\", y_train.shape)\nprint(\"Validation data - Features shape:\", X_val.shape, \"Labels shape:\", y_val.shape)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-09T16:22:02.743418Z","iopub.execute_input":"2024-04-09T16:22:02.743854Z","iopub.status.idle":"2024-04-09T16:41:57.102555Z","shell.execute_reply.started":"2024-04-09T16:22:02.743818Z","shell.execute_reply":"2024-04-09T16:41:57.101585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming you have loaded your metadata CSV file and extracted the unique bird species labels\nunique_labels = metadata['primary_label'].unique()\nnum_classes = len(unique_labels)\n\nprint(\"Number of unique classes (bird species):\", num_classes)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-09T16:49:17.354764Z","iopub.execute_input":"2024-04-09T16:49:17.355119Z","iopub.status.idle":"2024-04-09T16:49:17.364107Z","shell.execute_reply.started":"2024-04-09T16:49:17.355091Z","shell.execute_reply":"2024-04-09T16:49:17.363147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"new method****","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\n# Initialize LabelEncoder\nlabel_encoder = LabelEncoder()\n\n# Fit LabelEncoder on the bird species labels\nlabel_encoder.fit(y_train)\n\n# Convert string labels to integer indices\ny_train_encoded = label_encoder.transform(y_train)\ny_val_encoded = label_encoder.transform(y_val)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-09T16:49:23.520288Z","iopub.execute_input":"2024-04-09T16:49:23.520985Z","iopub.status.idle":"2024-04-09T16:49:23.546371Z","shell.execute_reply.started":"2024-04-09T16:49:23.520953Z","shell.execute_reply":"2024-04-09T16:49:23.545619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\n# Encode labels\nlabel_encoder = LabelEncoder()\ny_encoded = label_encoder.fit_transform(y)\n\n# Train-validation split\nX_train, X_val, y_train, y_val = train_test_split(X, y_encoded, test_size=0.2, random_state=42)\n\n# Define the number of classes\nnum_classes = len(label_encoder.classes_)\n\n# Define the CNN model architecture\nmodel = Sequential([\n    Conv2D(32, (3, 3), activation='relu', input_shape=(X_train.shape[1], X_train.shape[2], 1)),\n    MaxPooling2D((2, 2)),\n    Conv2D(64, (3, 3), activation='relu'),\n    MaxPooling2D((2, 2)),\n    Conv2D(128, (3, 3), activation='relu'),\n    MaxPooling2D((2, 2)),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dense(num_classes, activation='softmax')\n])\n\n# Compile the model\nmodel.compile(optimizer=Adam(), loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\n# Train the model\nhistory = model.fit(X_train, y_train, epochs=15, validation_data=(X_val, y_val))\n","metadata":{"execution":{"iopub.status.busy":"2024-04-09T17:23:04.119421Z","iopub.execute_input":"2024-04-09T17:23:04.120326Z","iopub.status.idle":"2024-04-09T17:24:41.094261Z","shell.execute_reply.started":"2024-04-09T17:23:04.120291Z","shell.execute_reply":"2024-04-09T17:24:41.093179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\n# Initialize LabelEncoder\nlabel_encoder = LabelEncoder()\n\n# Fit LabelEncoder on the bird species labels\nlabel_encoder.fit(y_train)\n\n# Convert string labels to integer indices for validation set\ny_val_encoded = label_encoder.transform(y_val)\n\n# Evaluate the model on the validation set\nval_loss, val_accuracy = model.evaluate(X_val, y_val_encoded)\nprint(\"Validation loss:\", val_loss)\nprint(\"Validation accuracy:\", val_accuracy)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-09T17:25:27.138331Z","iopub.execute_input":"2024-04-09T17:25:27.139156Z","iopub.status.idle":"2024-04-09T17:25:28.465136Z","shell.execute_reply.started":"2024-04-09T17:25:27.139121Z","shell.execute_reply":"2024-04-09T17:25:28.464185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport librosa\n\n# Assuming test soundscapes are in the provided directory\ntest_soundscapes_dir = \"/kaggle/input/birdclef-2024/test_soundscapes\"\n\n# List all audio files in the directory\ntest_soundscapes_files = [os.path.join(test_soundscapes_dir, f) for f in os.listdir(test_soundscapes_dir) if f.endswith('.ogg')]\n\n# Load each audio file\ntest_soundscapes = []\nfor file_path in test_soundscapes_files:\n    audio, sr = librosa.load(file_path, sr=None)  # Load audio file\n    test_soundscapes.append(audio)\n\n# Now test_soundscapes should contain the loaded audio data\n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-09T17:25:33.012099Z","iopub.execute_input":"2024-04-09T17:25:33.012473Z","iopub.status.idle":"2024-04-09T17:25:33.03183Z","shell.execute_reply.started":"2024-04-09T17:25:33.01244Z","shell.execute_reply":"2024-04-09T17:25:33.031108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv('/kaggle/input/birdclef-2024/sample_submission.csv')\ndisplay(sample_submission.head())","metadata":{"execution":{"iopub.status.busy":"2024-04-09T17:42:31.942422Z","iopub.execute_input":"2024-04-09T17:42:31.942899Z","iopub.status.idle":"2024-04-09T17:42:31.982112Z","shell.execute_reply.started":"2024-04-09T17:42:31.942864Z","shell.execute_reply":"2024-04-09T17:42:31.981198Z"},"trusted":true},"execution_count":null,"outputs":[]}]}