{"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-12T06:37:15.679786Z","iopub.execute_input":"2023-04-12T06:37:15.680606Z","iopub.status.idle":"2023-04-12T06:37:15.693089Z","shell.execute_reply.started":"2023-04-12T06:37:15.680562Z","shell.execute_reply":"2023-04-12T06:37:15.691824Z"},"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-12T06:37:17.696485Z","iopub.execute_input":"2023-04-12T06:37:17.696901Z","iopub.status.idle":"2023-04-12T06:37:22.366283Z","shell.execute_reply.started":"2023-04-12T06:37:17.696864Z","shell.execute_reply":"2023-04-12T06:37:22.364885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip show accelerate","metadata":{"execution":{"iopub.status.busy":"2023-04-12T06:37:28.506505Z","iopub.execute_input":"2023-04-12T06:37:28.507527Z","iopub.status.idle":"2023-04-12T06:37:38.017198Z","shell.execute_reply.started":"2023-04-12T06:37:28.507488Z","shell.execute_reply":"2023-04-12T06:37:38.015930Z"},"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-12T06:37:48.498001Z","iopub.execute_input":"2023-04-12T06:37:48.498443Z","iopub.status.idle":"2023-04-12T06:38:11.414899Z","shell.execute_reply.started":"2023-04-12T06:37:48.498404Z","shell.execute_reply":"2023-04-12T06:38:11.413499Z"},"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-12T06:38:19.268067Z","iopub.execute_input":"2023-04-12T06:38:19.268638Z","iopub.status.idle":"2023-04-12T06:38:22.364651Z","shell.execute_reply.started":"2023-04-12T06:38:19.268595Z","shell.execute_reply":"2023-04-12T06:38:22.363381Z"},"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-12T06:39:24.928579Z","iopub.execute_input":"2023-04-12T06:39:24.929044Z","iopub.status.idle":"2023-04-12T06:39:25.183613Z","shell.execute_reply.started":"2023-04-12T06:39:24.929005Z","shell.execute_reply":"2023-04-12T06:39:25.182490Z"},"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-12T06:39:59.642090Z","iopub.execute_input":"2023-04-12T06:39:59.643173Z","iopub.status.idle":"2023-04-12T06:39:59.676798Z","shell.execute_reply.started":"2023-04-12T06:39:59.643134Z","shell.execute_reply":"2023-04-12T06:39:59.675375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"most_represented_birds","metadata":{"execution":{"iopub.status.busy":"2023-04-12T06:40:03.081274Z","iopub.execute_input":"2023-04-12T06:40:03.082695Z","iopub.status.idle":"2023-04-12T06:40:03.092154Z","shell.execute_reply.started":"2023-04-12T06:40:03.082650Z","shell.execute_reply":"2023-04-12T06:40:03.090746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_df.primary_label.unique())","metadata":{"execution":{"iopub.status.busy":"2023-04-12T06:40:09.007551Z","iopub.execute_input":"2023-04-12T06:40:09.007951Z","iopub.status.idle":"2023-04-12T06:40:09.016809Z","shell.execute_reply.started":"2023-04-12T06:40:09.007915Z","shell.execute_reply":"2023-04-12T06:40:09.015221Z"},"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-12T06:40:12.136320Z","iopub.execute_input":"2023-04-12T06:40:12.137031Z","iopub.status.idle":"2023-04-12T06:40:12.148986Z","shell.execute_reply.started":"2023-04-12T06:40:12.136991Z","shell.execute_reply":"2023-04-12T06:40:12.147631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.primary_label.unique()","metadata":{"execution":{"iopub.status.busy":"2023-04-12T06:40:22.117641Z","iopub.execute_input":"2023-04-12T06:40:22.118230Z","iopub.status.idle":"2023-04-12T06:40:22.126958Z","shell.execute_reply.started":"2023-04-12T06:40:22.118193Z","shell.execute_reply":"2023-04-12T06:40:22.125849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.primary_label.unique()","metadata":{"execution":{"iopub.status.busy":"2023-04-12T06:40:26.216709Z","iopub.execute_input":"2023-04-12T06:40:26.217175Z","iopub.status.idle":"2023-04-12T06:40:26.226398Z","shell.execute_reply.started":"2023-04-12T06:40:26.217134Z","shell.execute_reply":"2023-04-12T06:40:26.225311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_df)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T06:40:30.241985Z","iopub.execute_input":"2023-04-12T06:40:30.242481Z","iopub.status.idle":"2023-04-12T06:40:30.252780Z","shell.execute_reply.started":"2023-04-12T06:40:30.242436Z","shell.execute_reply":"2023-04-12T06:40:30.251678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = shuffle(train_df)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T06:40:32.921853Z","iopub.execute_input":"2023-04-12T06:40:32.922389Z","iopub.status.idle":"2023-04-12T06:40:32.932100Z","shell.execute_reply.started":"2023-04-12T06:40:32.922299Z","shell.execute_reply":"2023-04-12T06:40:32.930732Z"},"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-12T06:40:41.143008Z","iopub.execute_input":"2023-04-12T06:40:41.143444Z","iopub.status.idle":"2023-04-12T06:40:41.150574Z","shell.execute_reply.started":"2023-04-12T06:40:41.143403Z","shell.execute_reply":"2023-04-12T06:40:41.148810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(training_df)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T06:40:42.781303Z","iopub.execute_input":"2023-04-12T06:40:42.782405Z","iopub.status.idle":"2023-04-12T06:40:42.790020Z","shell.execute_reply.started":"2023-04-12T06:40:42.782350Z","shell.execute_reply":"2023-04-12T06:40:42.788702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(validation_df)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T06:40:48.161955Z","iopub.execute_input":"2023-04-12T06:40:48.162533Z","iopub.status.idle":"2023-04-12T06:40:48.170565Z","shell.execute_reply.started":"2023-04-12T06:40:48.162496Z","shell.execute_reply":"2023-04-12T06:40:48.169363Z"},"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-12T06:40:53.081528Z","iopub.execute_input":"2023-04-12T06:40:53.081910Z","iopub.status.idle":"2023-04-12T06:40:53.334269Z","shell.execute_reply.started":"2023-04-12T06:40:53.081875Z","shell.execute_reply":"2023-04-12T06:40:53.332852Z"},"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-12T06:40:56.001864Z","iopub.execute_input":"2023-04-12T06:40:56.002574Z","iopub.status.idle":"2023-04-12T06:40:56.009670Z","shell.execute_reply.started":"2023-04-12T06:40:56.002529Z","shell.execute_reply":"2023-04-12T06:40:56.008324Z"},"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-12T06:40:58.441796Z","iopub.execute_input":"2023-04-12T06:40:58.442486Z","iopub.status.idle":"2023-04-12T06:40:58.453653Z","shell.execute_reply.started":"2023-04-12T06:40:58.442442Z","shell.execute_reply":"2023-04-12T06:40:58.452296Z"},"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-12T06:41:01.961498Z","iopub.execute_input":"2023-04-12T06:41:01.962230Z","iopub.status.idle":"2023-04-12T06:41:01.968801Z","shell.execute_reply.started":"2023-04-12T06:41:01.962189Z","shell.execute_reply":"2023-04-12T06:41:01.967419Z"},"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":{"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":{"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":{"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":{"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":{"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-12T06:41:19.802291Z","iopub.execute_input":"2023-04-12T06:41:19.803316Z","iopub.status.idle":"2023-04-12T06:41:19.815277Z","shell.execute_reply.started":"2023-04-12T06:41:19.803270Z","shell.execute_reply":"2023-04-12T06:41:19.814025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf /kaggle/working/melspectrogram","metadata":{"execution":{"iopub.status.busy":"2023-04-12T06:49:08.699982Z","iopub.execute_input":"2023-04-12T06:49:08.700749Z","iopub.status.idle":"2023-04-12T06:49:09.804140Z","shell.execute_reply.started":"2023-04-12T06:49:08.700700Z","shell.execute_reply":"2023-04-12T06:49:09.802585Z"},"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-12T06:49:12.160793Z","iopub.execute_input":"2023-04-12T06:49:12.162105Z"},"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":{"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":{"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-12T06:43:20.142695Z","iopub.execute_input":"2023-04-12T06:43:20.143766Z","iopub.status.idle":"2023-04-12T06:43:20.297161Z","shell.execute_reply.started":"2023-04-12T06:43:20.143721Z","shell.execute_reply":"2023-04-12T06:43:20.295298Z"},"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":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = val_ds.class_names\nprint(class_names)","metadata":{"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":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"sparse_categorical_crossentropy\", optimizer='adam', metrics=['accuracy'])","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evaluate = model.evaluate(test_ds)\nprint(evaluate)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf /kaggle/working/test/","metadata":{"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":{"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":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_training(history)","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]}]}