{"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\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":"2023-04-26T14:59:07.665906Z","iopub.execute_input":"2023-04-26T14:59:07.666287Z","iopub.status.idle":"2023-04-26T14:59:07.74123Z","shell.execute_reply.started":"2023-04-26T14:59:07.66624Z","shell.execute_reply":"2023-04-26T14:59:07.740063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport wave\nfrom scipy.io import wavfile\nimport os\nimport librosa\nfrom librosa.feature import melspectrogram\nimport warnings\nfrom sklearn.utils import shuffle\nfrom sklearn.utils import class_weight\nfrom PIL import Image\nfrom uuid import uuid4\nimport sklearn\nfrom tqdm import tqdm\nimport IPython.display as ipd\n\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import Input\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout, Activation, Rescaling\nfrom tensorflow.keras.layers import BatchNormalization, GlobalAveragePooling2D\nfrom tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout, Activation, LSTM, SimpleRNN, Conv1D, Input, BatchNormalization, GlobalAveragePooling2D\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import EfficientNetB0\n\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set()\n\nseed = 30\ntf.random.set_seed(seed)\nnp.random.seed(seed)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T14:59:07.743288Z","iopub.execute_input":"2023-04-26T14:59:07.743606Z","iopub.status.idle":"2023-04-26T14:59:36.041304Z","shell.execute_reply.started":"2023-04-26T14:59:07.743578Z","shell.execute_reply":"2023-04-26T14:59:36.040201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip show accelerate","metadata":{"execution":{"iopub.status.busy":"2023-04-26T14:59:36.043054Z","iopub.execute_input":"2023-04-26T14:59:36.043976Z","iopub.status.idle":"2023-04-26T14:59:46.084832Z","shell.execute_reply.started":"2023-04-26T14:59:36.043924Z","shell.execute_reply":"2023-04-26T14:59:46.083456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install git+https://github.com/huggingface/accelerate","metadata":{"execution":{"iopub.status.busy":"2023-04-26T14:59:46.088986Z","iopub.execute_input":"2023-04-26T14:59:46.089342Z","iopub.status.idle":"2023-04-26T15:00:10.382889Z","shell.execute_reply.started":"2023-04-26T14:59:46.089302Z","shell.execute_reply":"2023-04-26T15:00:10.381635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from accelerate import Accelerator, notebook_launcher # main interface, distributed launcher\nfrom accelerate.utils import set_seed # reproducability across devices","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:10.38574Z","iopub.execute_input":"2023-04-26T15:00:10.386151Z","iopub.status.idle":"2023-04-26T15:00:15.531855Z","shell.execute_reply.started":"2023-04-26T15:00:10.386111Z","shell.execute_reply":"2023-04-26T15:00:15.530577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/birdclef-2021/train_metadata.csv')","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:15.533758Z","iopub.execute_input":"2023-04-26T15:00:15.534174Z","iopub.status.idle":"2023-04-26T15:00:16.023641Z","shell.execute_reply.started":"2023-04-26T15:00:15.534128Z","shell.execute_reply":"2023-04-26T15:00:16.022501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df.query(\"rating>=5\")\nbirds_count = {}\nfor bird_species, count in zip(train_df.primary_label.unique(), train_df.groupby(\"primary_label\")[\"primary_label\"].count().values):\n    birds_count[bird_species] = count\nmost_represented_birds = [key for key,value in birds_count.items() if value in range(50,70)]\n\ntrain_df = train_df.query(\"primary_label in @most_represented_birds\")","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:16.024982Z","iopub.execute_input":"2023-04-26T15:00:16.025385Z","iopub.status.idle":"2023-04-26T15:00:16.070567Z","shell.execute_reply.started":"2023-04-26T15:00:16.025343Z","shell.execute_reply":"2023-04-26T15:00:16.069503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"most_represented_birds","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:16.071999Z","iopub.execute_input":"2023-04-26T15:00:16.072926Z","iopub.status.idle":"2023-04-26T15:00:16.082598Z","shell.execute_reply.started":"2023-04-26T15:00:16.072874Z","shell.execute_reply":"2023-04-26T15:00:16.081365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_df.primary_label.unique())","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:16.084128Z","iopub.execute_input":"2023-04-26T15:00:16.084684Z","iopub.status.idle":"2023-04-26T15:00:16.098124Z","shell.execute_reply.started":"2023-04-26T15:00:16.084644Z","shell.execute_reply":"2023-04-26T15:00:16.096988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"birds_to_recognise = sorted(most_represented_birds[:15])\nprint(birds_to_recognise)\ntrain_df = train_df.query(\"primary_label in @birds_to_recognise\")","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:16.103935Z","iopub.execute_input":"2023-04-26T15:00:16.10503Z","iopub.status.idle":"2023-04-26T15:00:16.119779Z","shell.execute_reply.started":"2023-04-26T15:00:16.104952Z","shell.execute_reply":"2023-04-26T15:00:16.118644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.primary_label.unique()","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:16.121608Z","iopub.execute_input":"2023-04-26T15:00:16.122027Z","iopub.status.idle":"2023-04-26T15:00:16.132053Z","shell.execute_reply.started":"2023-04-26T15:00:16.121989Z","shell.execute_reply":"2023-04-26T15:00:16.130856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.primary_label.unique()","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:16.134208Z","iopub.execute_input":"2023-04-26T15:00:16.134775Z","iopub.status.idle":"2023-04-26T15:00:16.143506Z","shell.execute_reply.started":"2023-04-26T15:00:16.134734Z","shell.execute_reply":"2023-04-26T15:00:16.142338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_df)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:16.145486Z","iopub.execute_input":"2023-04-26T15:00:16.145951Z","iopub.status.idle":"2023-04-26T15:00:16.155025Z","shell.execute_reply.started":"2023-04-26T15:00:16.145905Z","shell.execute_reply":"2023-04-26T15:00:16.153941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = shuffle(train_df)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:16.156705Z","iopub.execute_input":"2023-04-26T15:00:16.157256Z","iopub.status.idle":"2023-04-26T15:00:16.165576Z","shell.execute_reply.started":"2023-04-26T15:00:16.157214Z","shell.execute_reply":"2023-04-26T15:00:16.164704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_percentage = 0.8\ntraining_item_count = int(len(train_df)*0.8)\nvalidation_item_count = int(len(train_df)*0.1)\ntest_item_count = int(len(train_df)*0.1)\ntraining_df = train_df[:training_item_count]\nvalidation_df = train_df[training_item_count:training_item_count+validation_item_count]\ntest_df = train_df[training_item_count+validation_item_count:]","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:16.168338Z","iopub.execute_input":"2023-04-26T15:00:16.168708Z","iopub.status.idle":"2023-04-26T15:00:16.177307Z","shell.execute_reply.started":"2023-04-26T15:00:16.168671Z","shell.execute_reply":"2023-04-26T15:00:16.176215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(training_df)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:16.180724Z","iopub.execute_input":"2023-04-26T15:00:16.18101Z","iopub.status.idle":"2023-04-26T15:00:16.192647Z","shell.execute_reply.started":"2023-04-26T15:00:16.180984Z","shell.execute_reply":"2023-04-26T15:00:16.191226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(validation_df)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:16.194482Z","iopub.execute_input":"2023-04-26T15:00:16.196135Z","iopub.status.idle":"2023-04-26T15:00:16.204355Z","shell.execute_reply.started":"2023-04-26T15:00:16.196106Z","shell.execute_reply":"2023-04-26T15:00:16.202905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wav, sr = librosa.load(\"/kaggle/input/birdclef-2021/train_short_audio/banana/XC112602.ogg\")","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:16.206838Z","iopub.execute_input":"2023-04-26T15:00:16.207361Z","iopub.status.idle":"2023-04-26T15:00:17.465335Z","shell.execute_reply.started":"2023-04-26T15:00:16.207316Z","shell.execute_reply":"2023-04-26T15:00:17.463914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_time_series(data):\n    fig = plt.figure(figsize=(14, 8))\n    plt.title('Raw wave ')\n    plt.ylabel('Amplitude')\n    plt.plot(np.linspace(0, 1, len(data)), data)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:17.467214Z","iopub.execute_input":"2023-04-26T15:00:17.467747Z","iopub.status.idle":"2023-04-26T15:00:17.475427Z","shell.execute_reply.started":"2023-04-26T15:00:17.467687Z","shell.execute_reply":"2023-04-26T15:00:17.473969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_training(hist):\n    tr_acc = hist.history['accuracy']\n    tr_loss = hist.history['loss']\n    val_acc = hist.history['val_accuracy']\n    val_loss = hist.history['val_loss']\n    index_loss = np.argmin(val_loss)\n    val_lowest = val_loss[index_loss]\n    index_acc = np.argmax(val_acc)\n    acc_highest = val_acc[index_acc]\n\n    plt.figure(figsize= (20, 8))\n    plt.style.use('fivethirtyeight')\n    Epochs = [i+1 for i in range(len(tr_acc))]\n    loss_label = f'best epoch= {str(index_loss + 1)}'\n    acc_label = f'best epoch= {str(index_acc + 1)}'\n    plt.subplot(1, 2, 1)\n    plt.plot(Epochs, tr_loss, 'r', label= 'Training loss')\n    plt.plot(Epochs, val_loss, 'g', label= 'Validation loss')\n    plt.scatter(index_loss + 1, val_lowest, s= 150, c= 'blue', label= loss_label)\n    plt.title('Training and Validation Loss')\n    plt.xlabel('Epochs')\n    plt.ylabel('Loss')\n    plt.legend()\n    plt.subplot(1, 2, 2)\n    plt.plot(Epochs, tr_acc, 'r', label= 'Training Accuracy')\n    plt.plot(Epochs, val_acc, 'g', label= 'Validation Accuracy')\n    plt.scatter(index_acc + 1 , acc_highest, s= 150, c= 'blue', label= acc_label)\n    plt.title('Training and Validation Accuracy')\n    plt.xlabel('Epochs')\n    plt.ylabel('Accuracy')\n    plt.legend()\n    plt.tight_layout\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:17.477129Z","iopub.execute_input":"2023-04-26T15:00:17.47853Z","iopub.status.idle":"2023-04-26T15:00:17.492803Z","shell.execute_reply.started":"2023-04-26T15:00:17.478485Z","shell.execute_reply":"2023-04-26T15:00:17.491669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# stretching the sound\ndef stretch(data, rate=1):\n    input_length = sr\n    data = librosa.effects.time_stretch(data, rate=rate)\n    data = np.pad(data, (0, max(0, input_length - len(data))), \"constant\")\n\n    return data","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:17.49461Z","iopub.execute_input":"2023-04-26T15:00:17.495178Z","iopub.status.idle":"2023-04-26T15:00:17.510851Z","shell.execute_reply.started":"2023-04-26T15:00:17.495137Z","shell.execute_reply":"2023-04-26T15:00:17.50969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_stretch =stretch(wav, 0.8)\nipd.Audio(data_stretch, rate=sr)\nplot_time_series(data_stretch)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:17.512627Z","iopub.execute_input":"2023-04-26T15:00:17.512999Z","iopub.status.idle":"2023-04-26T15:00:20.274457Z","shell.execute_reply.started":"2023-04-26T15:00:17.51296Z","shell.execute_reply":"2023-04-26T15:00:20.273322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_stretch =stretch(wav, 1.2)\nipd.Audio(data_stretch, rate=sr)\nplot_time_series(data_stretch)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:20.276316Z","iopub.execute_input":"2023-04-26T15:00:20.277047Z","iopub.status.idle":"2023-04-26T15:00:21.46848Z","shell.execute_reply.started":"2023-04-26T15:00:20.277006Z","shell.execute_reply":"2023-04-26T15:00:21.467376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Whitenoise\nwn = np.random.randn(len(wav))\nwav_wn = wav + 0.01*wn\n# We limited the amplitude of the noise so we can still hear the word even with the noise, \n#which is the objective\nipd.Audio(wav_wn, rate=sr)\nplot_time_series(wav_wn)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:21.476058Z","iopub.execute_input":"2023-04-26T15:00:21.478553Z","iopub.status.idle":"2023-04-26T15:00:22.518863Z","shell.execute_reply.started":"2023-04-26T15:00:21.478517Z","shell.execute_reply":"2023-04-26T15:00:22.517672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Whitenoise\nwn = np.random.randn(len(wav))\nwav_wn = wav + 0.01*wn\n# We limited the amplitude of the noise so we can still hear the word even with the noise, \n#which is the objective\nipd.Audio(wav_wn, rate=sr)\nplot_time_series(wav_wn)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:22.520008Z","iopub.execute_input":"2023-04-26T15:00:22.520517Z","iopub.status.idle":"2023-04-26T15:00:23.385015Z","shell.execute_reply.started":"2023-04-26T15:00:22.52047Z","shell.execute_reply":"2023-04-26T15:00:23.383879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Pitch\nwav_p = librosa.effects.pitch_shift(wav,sr=sr, n_steps=4)\nipd.Audio(wav_p, rate=sr)\nplot_time_series(wav)\nplot_time_series(wav_p)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:23.38671Z","iopub.execute_input":"2023-04-26T15:00:23.387155Z","iopub.status.idle":"2023-04-26T15:00:25.276232Z","shell.execute_reply.started":"2023-04-26T15:00:23.387115Z","shell.execute_reply":"2023-04-26T15:00:25.275247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_sample(filename, bird, output_folder):\n    wave_data, wave_rate = librosa.load(filename)\n    wave_data, _ = librosa.effects.trim(wave_data)\n    wave_data = stretch(wave_data, 1.2)\n    wn = np.random.randn(len(wave_data))\n    wave_data = wave_data + 0.01*wn\n    wave_data = librosa.effects.pitch_shift(wave_data, sr=wave_rate, n_steps=4)\n    \n    #only take 5s samples and add them to the dataframe\n    song_sample = []\n    sample_length = 5*wave_rate\n    samples_from_file = []\n    #The variable below is chosen mainly to create a 216x216 image\n    N_mels=216\n    for idx in range(0,len(wave_data),sample_length): \n        song_sample = wave_data[idx:idx+sample_length]\n        if len(song_sample)>=sample_length:\n            mel = melspectrogram(y=song_sample, n_mels=N_mels)\n            db = librosa.power_to_db(mel)\n            normalised_db = sklearn.preprocessing.minmax_scale(db)\n            filename = str(uuid4())+\".jpg\"\n            db_array = (np.asarray(normalised_db)*255).astype(np.uint8)\n            db_image =  Image.fromarray(np.array([db_array, db_array, db_array]).T)\n            db_image.save(\"{}{}\".format(output_folder,filename))\n            \n            samples_from_file.append({\"song_sample\":\"{}{}\".format(output_folder,filename),\n                                            \"db\":db_array,\"bird\":bird})\n    return samples_from_file","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:25.277793Z","iopub.execute_input":"2023-04-26T15:00:25.280881Z","iopub.status.idle":"2023-04-26T15:00:25.290511Z","shell.execute_reply.started":"2023-04-26T15:00:25.280848Z","shell.execute_reply":"2023-04-26T15:00:25.289504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf /kaggle/working/melspectrogram","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:25.298903Z","iopub.execute_input":"2023-04-26T15:00:25.299954Z","iopub.status.idle":"2023-04-26T15:00:26.313988Z","shell.execute_reply.started":"2023-04-26T15:00:25.299907Z","shell.execute_reply":"2023-04-26T15:00:26.312513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"warnings.filterwarnings(\"ignore\")\ntrain_samples = pd.DataFrame(columns=[\"song_sample\",\"bird\"])\ntrain_list = []\n\noutput_folder = \"/kaggle/working/melspectrogram/\"\nos.mkdir(output_folder)\noutput_folder += \"train/\"\nos.mkdir(output_folder)\n\nwith tqdm(total=len(training_df)) as pbar:\n    for idx, row in training_df.iterrows():\n        pbar.update(1)\n        try:\n            audio_file_path = \"../input/birdclef-2021/train_short_audio/\"\n            audio_file_path += row.primary_label\n            if row.primary_label in birds_to_recognise:\n                outf = output_folder + row.primary_label + \"/\"\n                if os.path.isdir(outf) == False:\n                    os.mkdir(outf)\n                train_list += get_sample('{}/{}'.format(audio_file_path, row.filename), row.primary_label, outf) \n        except:\n            raise\n            print(\"{} is corrupted\".format(audio_file_path))\n            \ntrain_samples = pd.DataFrame(train_list)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:00:26.316498Z","iopub.execute_input":"2023-04-26T15:00:26.316958Z","iopub.status.idle":"2023-04-26T15:21:38.158711Z","shell.execute_reply.started":"2023-04-26T15:00:26.316904Z","shell.execute_reply":"2023-04-26T15:21:38.155504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"warnings.filterwarnings(\"ignore\")\nvalidation_samples = pd.DataFrame(columns=[\"song_sample\",\"bird\"])\nvalidation_list = []\n\noutput_folder = \"/kaggle/working/melspectrogram/validation/\"\nos.mkdir(output_folder)\nwith tqdm(total=len(validation_df)) as pbar:\n    for idx, row in validation_df.iterrows():\n        pbar.update(1)\n        try:\n            audio_file_path = \"../input/birdclef-2021/train_short_audio/\"\n            audio_file_path += row.primary_label\n            if row.primary_label in birds_to_recognise:\n                outf = output_folder + row.primary_label + \"/\"\n                if os.path.isdir(outf) == False:\n                    os.mkdir(outf)\n                validation_list += get_sample('{}/{}'.format(audio_file_path, row.filename), row.primary_label, outf) \n        except:\n            raise\n            print(\"{} is corrupted\".format(audio_file_path))\n            \nvalidation_samples = pd.DataFrame(validation_list)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:21:38.160732Z","iopub.execute_input":"2023-04-26T15:21:38.16124Z","iopub.status.idle":"2023-04-26T15:24:29.213126Z","shell.execute_reply.started":"2023-04-26T15:21:38.161199Z","shell.execute_reply":"2023-04-26T15:24:29.209983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"warnings.filterwarnings(\"ignore\")\ntest_samples = pd.DataFrame(columns=[\"song_sample\",\"bird\"])\ntest_list = []\n\noutput_folder = \"/kaggle/working/melspectrogram/test/\"\nos.mkdir(output_folder)\nwith tqdm(total=len(test_df)) as pbar:\n    for idx, row in test_df.iterrows():\n        pbar.update(1)\n        try:\n            audio_file_path = \"../input/birdclef-2021/train_short_audio/\"\n            audio_file_path += row.primary_label\n            if row.primary_label in birds_to_recognise:\n                outf = output_folder + row.primary_label + \"/\"\n                if os.path.isdir(outf) == False:\n                    os.mkdir(outf)\n                test_list += get_sample('{}/{}'.format(audio_file_path, row.filename), row.primary_label, outf) \n        except:\n            raise\n            print(\"{} is corrupted\".format(audio_file_path))\n            \ntest_samples = pd.DataFrame(test_list)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:24:29.214874Z","iopub.execute_input":"2023-04-26T15:24:29.215287Z","iopub.status.idle":"2023-04-26T15:27:06.453344Z","shell.execute_reply.started":"2023-04-26T15:24:29.215233Z","shell.execute_reply":"2023-04-26T15:27:06.450066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = \"/kaggle/working/melspectrogram/train/\"\nbatch_size = 32\ntrain_ds = tf.keras.utils.image_dataset_from_directory(\n  data_dir,\n  seed=123,\n  image_size=(216, 216),\n  batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:27:06.455096Z","iopub.execute_input":"2023-04-26T15:27:06.455501Z","iopub.status.idle":"2023-04-26T15:27:16.880546Z","shell.execute_reply.started":"2023-04-26T15:27:06.455461Z","shell.execute_reply":"2023-04-26T15:27:16.879454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir_val = \"/kaggle/working/melspectrogram/validation/\"\nbatch_size = 32\nval_ds = tf.keras.utils.image_dataset_from_directory(\n  data_dir_val,\n  seed=123,\n  image_size=(216, 216),\n  batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:27:16.883191Z","iopub.execute_input":"2023-04-26T15:27:16.884006Z","iopub.status.idle":"2023-04-26T15:27:17.011614Z","shell.execute_reply.started":"2023-04-26T15:27:16.883961Z","shell.execute_reply":"2023-04-26T15:27:17.01045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir_test = \"/kaggle/working/melspectrogram/test/\"\nbatch_size = 32\ntest_ds = tf.keras.utils.image_dataset_from_directory(\n  data_dir_test,\n  seed=123,\n  image_size=(216, 216),\n  batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:27:17.014126Z","iopub.execute_input":"2023-04-26T15:27:17.015229Z","iopub.status.idle":"2023-04-26T15:27:17.143017Z","shell.execute_reply.started":"2023-04-26T15:27:17.015187Z","shell.execute_reply":"2023-04-26T15:27:17.14187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = val_ds.class_names\nprint(class_names)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:27:17.144614Z","iopub.execute_input":"2023-04-26T15:27:17.144977Z","iopub.status.idle":"2023-04-26T15:27:17.150058Z","shell.execute_reply.started":"2023-04-26T15:27:17.144924Z","shell.execute_reply":"2023-04-26T15:27:17.149007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape = (216,216,3)\neffnet_layers = EfficientNetB0(weights=None, include_top=False, input_shape=input_shape)\n\nfor layer in effnet_layers.layers:\n    layer.trainable = True\n\ndropout_dense_layer = 0.3\n\nmodel = Sequential()\nmodel.add(Rescaling(1./255, input_shape=(216,216, 3)))\nmodel.add(effnet_layers)\nmodel.add(GlobalAveragePooling2D())\nmodel.add(Dense(256, use_bias=False))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Dropout(dropout_dense_layer))\nmodel.add(Dense(len(train_df.primary_label.unique()), activation=\"softmax\"))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:27:17.151622Z","iopub.execute_input":"2023-04-26T15:27:17.152384Z","iopub.status.idle":"2023-04-26T15:27:20.036562Z","shell.execute_reply.started":"2023-04-26T15:27:17.152339Z","shell.execute_reply":"2023-04-26T15:27:20.035329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [ReduceLROnPlateau(monitor='val_loss', patience=2, verbose=1, factor=0.7),\n             EarlyStopping(monitor='val_loss', patience=5),\n             ModelCheckpoint(filepath='model.h5', monitor='val_loss', save_best_only=True)]","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:27:20.038007Z","iopub.execute_input":"2023-04-26T15:27:20.038408Z","iopub.status.idle":"2023-04-26T15:27:20.045411Z","shell.execute_reply.started":"2023-04-26T15:27:20.038351Z","shell.execute_reply":"2023-04-26T15:27:20.044144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"sparse_categorical_crossentropy\", optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:27:20.047124Z","iopub.execute_input":"2023-04-26T15:27:20.047828Z","iopub.status.idle":"2023-04-26T15:27:20.077598Z","shell.execute_reply.started":"2023-04-26T15:27:20.047785Z","shell.execute_reply":"2023-04-26T15:27:20.076627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epoch = 1\nhistory = model.fit(train_ds,\n          epochs = epoch, \n          validation_data=val_ds,\n          callbacks = callbacks\n        )","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:27:20.079067Z","iopub.execute_input":"2023-04-26T15:27:20.079544Z","iopub.status.idle":"2023-04-26T15:29:01.697404Z","shell.execute_reply.started":"2023-04-26T15:27:20.079504Z","shell.execute_reply":"2023-04-26T15:29:01.69619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evaluate = model.evaluate(test_ds)\nprint(evaluate)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:29:01.699397Z","iopub.execute_input":"2023-04-26T15:29:01.699792Z","iopub.status.idle":"2023-04-26T15:29:03.280127Z","shell.execute_reply.started":"2023-04-26T15:29:01.699752Z","shell.execute_reply":"2023-04-26T15:29:03.278958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf /kaggle/working/test/","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:29:03.283364Z","iopub.execute_input":"2023-04-26T15:29:03.283814Z","iopub.status.idle":"2023-04-26T15:29:04.402395Z","shell.execute_reply.started":"2023-04-26T15:29:03.283781Z","shell.execute_reply":"2023-04-26T15:29:04.400762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"warnings.filterwarnings(\"ignore\")\noutput_folder = \"/kaggle/working/test/\"\nos.mkdir(output_folder)\nget_sample('/kaggle/input/birdclef-2021/train_short_audio/bcnher/XC182583.ogg', 'bcnher', output_folder)\ntest_img = os.listdir(output_folder)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:29:04.406584Z","iopub.execute_input":"2023-04-26T15:29:04.406912Z","iopub.status.idle":"2023-04-26T15:29:04.970055Z","shell.execute_reply.started":"2023-04-26T15:29:04.406882Z","shell.execute_reply":"2023-04-26T15:29:04.968338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = []\nnum_img = 0\nfor img in test_img:\n    img_path = output_folder + img\n    img = tf.keras.utils.load_img(\n    img_path, target_size=(216,216)\n    )\n    img_array = tf.keras.utils.img_to_array(img)\n    img_array = tf.expand_dims(img_array, 0) # Create a batch\n\n    predictions = model.predict(img_array)\n    score = tf.nn.softmax(predictions[0])\n    result.append(score)\n    num_img += 1\n\nresult = pd.DataFrame(result)\nresult = pd.DataFrame(result.T)\nresult = result.sum(axis=1)/num_img\nprint(result)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:29:04.971913Z","iopub.execute_input":"2023-04-26T15:29:04.972329Z","iopub.status.idle":"2023-04-26T15:29:07.200945Z","shell.execute_reply.started":"2023-04-26T15:29:04.972268Z","shell.execute_reply":"2023-04-26T15:29:07.199588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\n    \"This audio most likely belongs to {} with a {:.2f} percent confidence.\"\n    .format(class_names[np.argmax(result)], 100 * np.max(result))\n)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:29:07.203268Z","iopub.execute_input":"2023-04-26T15:29:07.204476Z","iopub.status.idle":"2023-04-26T15:29:07.211776Z","shell.execute_reply.started":"2023-04-26T15:29:07.204433Z","shell.execute_reply":"2023-04-26T15:29:07.210362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_training(history)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:29:07.213918Z","iopub.execute_input":"2023-04-26T15:29:07.214847Z","iopub.status.idle":"2023-04-26T15:29:07.79724Z","shell.execute_reply.started":"2023-04-26T15:29:07.214805Z","shell.execute_reply":"2023-04-26T15:29:07.796206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_name = 'EffecientNetB0'\nsubject = 'Birds-Species'\n# acc = test_score[1] * 100\nsave_path = '/kaggle/working'\n\nsave_id = str(f'{model_name}-{subject}-{\"%.2f\" %round(91, 2)}.h5')\nmodel_save_loc = os.path.join(save_path, save_id)\nmodel.save(model_save_loc)\nprint(f'model was saved as {model_save_loc}')","metadata":{"execution":{"iopub.status.busy":"2023-04-26T15:29:07.799415Z","iopub.execute_input":"2023-04-26T15:29:07.800194Z","iopub.status.idle":"2023-04-26T15:29:08.959759Z","shell.execute_reply.started":"2023-04-26T15:29:07.800154Z","shell.execute_reply":"2023-04-26T15:29:08.958075Z"},"trusted":true},"execution_count":null,"outputs":[]}]}