{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n#!pip install scipy\n!pip install --upgrade librosa\n","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:33:59.745705Z","iopub.execute_input":"2023-09-18T16:33:59.746304Z","iopub.status.idle":"2023-09-18T16:34:13.726612Z","shell.execute_reply.started":"2023-09-18T16:33:59.746255Z","shell.execute_reply":"2023-09-18T16:34:13.725423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import librosa\naudio='/kaggle/input/birdclef-2023/train_audio/abethr1/XC128013.ogg'\ndata,samp_rate = librosa.load(audio)","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:21:03.371142Z","iopub.execute_input":"2023-09-18T16:21:03.371593Z","iopub.status.idle":"2023-09-18T16:21:03.467082Z","shell.execute_reply.started":"2023-09-18T16:21:03.371561Z","shell.execute_reply":"2023-09-18T16:21:03.465770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.figure(figsize=(12,4))\nplt.plot(data)","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:21:05.045375Z","iopub.execute_input":"2023-09-18T16:21:05.045802Z","iopub.status.idle":"2023-09-18T16:21:05.703955Z","shell.execute_reply.started":"2023-09-18T16:21:05.045767Z","shell.execute_reply":"2023-09-18T16:21:05.702716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Feature Extraction with MFCC \nmfccs= librosa.feature.mfcc(y=data,sr=samp_rate, n_mfcc=40)\nprint(mfccs.shape)","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:21:10.508486Z","iopub.execute_input":"2023-09-18T16:21:10.508883Z","iopub.status.idle":"2023-09-18T16:21:10.617869Z","shell.execute_reply.started":"2023-09-18T16:21:10.508853Z","shell.execute_reply":"2023-09-18T16:21:10.612244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"voices_per_bird = 10\nnum_classes_to_keep = 15","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:28:16.256113Z","iopub.execute_input":"2023-09-18T16:28:16.257216Z","iopub.status.idle":"2023-09-18T16:28:16.262916Z","shell.execute_reply.started":"2023-09-18T16:28:16.257176Z","shell.execute_reply":"2023-09-18T16:28:16.261367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load metadata\nmetadata = pd.read_csv('/kaggle/input/birdclef-2023/train_metadata.csv')\n\nselected_metadata = metadata.groupby('primary_label').head(voices_per_bird)\n\n\n# Set a specific random state for reproducibility\nrandom_state = 42\n\n# Get a random sample of 10 unique classes\nunique_classes = selected_metadata['primary_label'].unique()\nnp.random.seed(random_state)\nselected_classes = np.random.choice(unique_classes, num_classes_to_keep, replace=False)\n\n# Filter the DataFrame to keep only the selected classes\nfiltered_df = selected_metadata[selected_metadata['primary_label'].isin(selected_classes)]\n\n# Save the filtered DataFrame to a new CSV file\nselected_metadata = filtered_df \n\nprint(len(selected_metadata))\nselected_metadata.head()\nprint(\"Selected classes to keep:\")\nfor selected_class in selected_classes:\n    print(selected_class)","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:28:18.405391Z","iopub.execute_input":"2023-09-18T16:28:18.406670Z","iopub.status.idle":"2023-09-18T16:28:18.516038Z","shell.execute_reply.started":"2023-09-18T16:28:18.406631Z","shell.execute_reply":"2023-09-18T16:28:18.514686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"desired_duration=10\ndef feature_extractor(filename):\n    data,sr = librosa.load(filename, duration = desired_duration, res_type='kaise_best')\n    if len(data) < sr * desired_duration:\n        data = np.pad(data, (0, sr * desired_duration - len(data)))\n         \n    \n    mfcc_features = librosa.feature.mfcc(y=data,sr=samp_rate, n_mfcc=40)\n    mfcc_scaled_features = np.mean(mfcc_features.T, axis=0)\n    return mfcc_scaled_features","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:34:18.682954Z","iopub.execute_input":"2023-09-18T16:34:18.683392Z","iopub.status.idle":"2023-09-18T16:34:18.693176Z","shell.execute_reply.started":"2023-09-18T16:34:18.683358Z","shell.execute_reply":"2023-09-18T16:34:18.691628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Runtime - 2min 14 sec 775 iterations for 3 clips per bird\nfrom tqdm import tqdm #Allows to see progress\nextracted_features=[]\nfor index_num,row in tqdm(selected_metadata.iterrows()):\n    filename= \"/kaggle/input/birdclef-2023/train_audio/\" + row[\"filename\"] \n    final_class_labels=row[\"primary_label\"]\n    data=feature_extractor(filename)\n    extracted_features.append([data,final_class_labels])","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:34:20.389646Z","iopub.execute_input":"2023-09-18T16:34:20.390104Z","iopub.status.idle":"2023-09-18T16:34:20.587059Z","shell.execute_reply.started":"2023-09-18T16:34:20.390069Z","shell.execute_reply":"2023-09-18T16:34:20.585373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features_df = pd.DataFrame(extracted_features, columns=['features','class'])\nprint(features_df.shape)\nprint(features_df.head())\n","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:26:45.944012Z","iopub.execute_input":"2023-09-18T16:26:45.946518Z","iopub.status.idle":"2023-09-18T16:26:45.968890Z","shell.execute_reply.started":"2023-09-18T16:26:45.946445Z","shell.execute_reply":"2023-09-18T16:26:45.967168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_distribution = features_df['class'].value_counts()\n\n# Print the distribution\nprint(class_distribution)","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:26:45.972849Z","iopub.execute_input":"2023-09-18T16:26:45.974455Z","iopub.status.idle":"2023-09-18T16:26:45.997531Z","shell.execute_reply.started":"2023-09-18T16:26:45.974388Z","shell.execute_reply":"2023-09-18T16:26:45.995868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Filter out classes with fewer than 3 instances to avoid class imbalance\nmin_instances = voices_per_bird\nfiltered_classes = class_distribution[class_distribution >= min_instances].index.tolist()\n\n# Create a new DataFrame with only the rows that belong to the filtered classes\nfiltered_dataframe = features_df[features_df['class'].isin(filtered_classes)].copy()  # Removed .tolist()\n\n# Calculate how many rows and unique classes were removed\nrows_removed = len(features_df) - len(filtered_dataframe)\nunique_classes_removed = len(class_distribution) - len(filtered_classes)\n\n# Print the filtered DataFrame\nprint(filtered_dataframe.head())\n\n# Print the number of rows and unique classes removed\nprint(f\"Rows removed: {rows_removed}\")\nprint(f\"Unique classes removed: {unique_classes_removed}\")\nprint(\"Final Shape: \",filtered_dataframe.shape)\nprint(\"Unique Birds:\",filtered_dataframe.shape[0]/voices_per_bird)","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:26:46.001118Z","iopub.execute_input":"2023-09-18T16:26:46.001881Z","iopub.status.idle":"2023-09-18T16:26:46.048530Z","shell.execute_reply.started":"2023-09-18T16:26:46.001820Z","shell.execute_reply":"2023-09-18T16:26:46.046393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = np.array(filtered_dataframe['features'].tolist())\ny = np.array(filtered_dataframe['class'].tolist())\nprint(X.shape)\nprint(y.shape)","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:27:04.047783Z","iopub.execute_input":"2023-09-18T16:27:04.048204Z","iopub.status.idle":"2023-09-18T16:27:04.056657Z","shell.execute_reply.started":"2023-09-18T16:27:04.048174Z","shell.execute_reply":"2023-09-18T16:27:04.055329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nlabel_encoder = LabelEncoder()\ny = label_encoder.fit_transform(y)\nprint(y.shape)","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:27:07.891180Z","iopub.execute_input":"2023-09-18T16:27:07.891578Z","iopub.status.idle":"2023-09-18T16:27:07.899092Z","shell.execute_reply.started":"2023-09-18T16:27:07.891545Z","shell.execute_reply":"2023-09-18T16:27:07.897767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:27:09.356243Z","iopub.execute_input":"2023-09-18T16:27:09.356654Z","iopub.status.idle":"2023-09-18T16:27:09.364414Z","shell.execute_reply.started":"2023-09-18T16:27:09.356622Z","shell.execute_reply":"2023-09-18T16:27:09.363092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:27:10.697225Z","iopub.execute_input":"2023-09-18T16:27:10.697623Z","iopub.status.idle":"2023-09-18T16:27:10.703622Z","shell.execute_reply.started":"2023-09-18T16:27:10.697594Z","shell.execute_reply":"2023-09-18T16:27:10.702387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense,Dropout,Activation,Flatten\nfrom tensorflow.keras.optimizers import Adam\nfrom sklearn import metrics","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:27:12.400589Z","iopub.execute_input":"2023-09-18T16:27:12.401138Z","iopub.status.idle":"2023-09-18T16:27:12.409270Z","shell.execute_reply.started":"2023-09-18T16:27:12.401096Z","shell.execute_reply":"2023-09-18T16:27:12.408007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_labels= int(y.shape[0]/voices_per_bird)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:23:54.890245Z","iopub.execute_input":"2023-09-18T16:23:54.890640Z","iopub.status.idle":"2023-09-18T16:23:54.896372Z","shell.execute_reply.started":"2023-09-18T16:23:54.890610Z","shell.execute_reply":"2023-09-18T16:23:54.895093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Dense(100,input_shape=(40,)))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\n\nmodel.add(Dense(200))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\n\nmodel.add(Dense(100))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\n\nmodel.add(Dense(1))\nmodel.add(Activation('softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:27:15.790524Z","iopub.execute_input":"2023-09-18T16:27:15.790936Z","iopub.status.idle":"2023-09-18T16:27:15.887184Z","shell.execute_reply.started":"2023-09-18T16:27:15.790884Z","shell.execute_reply":"2023-09-18T16:27:15.885781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:27:18.697094Z","iopub.execute_input":"2023-09-18T16:27:18.698256Z","iopub.status.idle":"2023-09-18T16:27:18.739541Z","shell.execute_reply.started":"2023-09-18T16:27:18.698217Z","shell.execute_reply":"2023-09-18T16:27:18.738318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')\n","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:27:21.261575Z","iopub.execute_input":"2023-09-18T16:27:21.262061Z","iopub.status.idle":"2023-09-18T16:27:21.280812Z","shell.execute_reply.started":"2023-09-18T16:27:21.262025Z","shell.execute_reply":"2023-09-18T16:27:21.279029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ModelCheckpoint\nfrom datetime import datetime\nnum_epochs=50\nnum_batch_size=32\n\ncheckpointer = ModelCheckpoint(filepath='/kaggle/working/audio_classification.hdf5', verbose=1, save_best_only=True)\nstart=datetime.now()\nmodel.fit(X_train,y_train, batch_size=num_batch_size, epochs=num_epochs, validation_data=(X_test,y_test), callbacks=[checkpointer])\nduration = datetime.now() - start\nprint(\"Training completed in time :\",duration)","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:27:23.324648Z","iopub.execute_input":"2023-09-18T16:27:23.325064Z","iopub.status.idle":"2023-09-18T16:27:27.867433Z","shell.execute_reply.started":"2023-09-18T16:27:23.325031Z","shell.execute_reply":"2023-09-18T16:27:27.866136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_accuracy=model.evaluate(X_test,y_test,verbose=0)\nprint(test_accuracy[1]*100)\nif test_accuracy[1]*100 > 60:\n    print(\"YAY\")\nelse:\n    print(\"NOOOOOOOOO\")","metadata":{"execution":{"iopub.status.busy":"2023-09-18T16:27:32.018301Z","iopub.execute_input":"2023-09-18T16:27:32.018723Z","iopub.status.idle":"2023-09-18T16:27:32.119837Z","shell.execute_reply.started":"2023-09-18T16:27:32.018689Z","shell.execute_reply":"2023-09-18T16:27:32.117691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}