{"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":"markdown","source":"# Import Libraries","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import os\nimport warnings\nimport shutil\nwarnings.filterwarnings(action='ignore')\nimport argparse\nimport math\nimport pandas as pd\nimport librosa\nimport librosa.display\nimport numpy as np\nimport seaborn as sns; sns.set(style='whitegrid')\nimport matplotlib.pyplot as plt\n\nfrom sklearn.utils import shuffle\nfrom PIL import Image\nfrom tqdm import tqdm,tnrange,tqdm_notebook\nimport tensorflow as tf\nfrom tqdm.keras import TqdmCallback\nfrom keras.callbacks import ReduceLROnPlateau,EarlyStopping,ModelCheckpoint\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator \nfrom tensorflow.keras import applications as app\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten,AveragePooling2D\nfrom tensorflow.keras.layers import Dense,BatchNormalization,Dropout\nfrom tensorflow.keras.models import Sequential \nimport pickle\nfrom tensorflow.keras.applications import EfficientNetB0\nfrom tensorflow.keras.layers import GlobalAveragePooling2D\nfrom tensorflow.keras.layers import Input\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.applications.efficientnet import preprocess_input\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom tensorflow.keras.callbacks import ModelCheckpoint\nfrom sklearn.metrics import classification_report","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:33:18.343493Z","iopub.execute_input":"2023-01-17T19:33:18.344057Z","iopub.status.idle":"2023-01-17T19:33:29.082637Z","shell.execute_reply.started":"2023-01-17T19:33:18.344018Z","shell.execute_reply":"2023-01-17T19:33:29.081112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Global variables","metadata":{"execution":{"iopub.status.busy":"2023-01-18T02:15:28.841077Z","iopub.execute_input":"2023-01-18T02:15:28.841739Z","iopub.status.idle":"2023-01-18T02:15:28.847685Z","shell.execute_reply.started":"2023-01-18T02:15:28.841691Z","shell.execute_reply":"2023-01-18T02:15:28.846507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nRANDOM_SEED = 1337\nSAMPLE_RATE = 32000\nSIGNAL_LENGTH = 5 # seconds\nSPEC_SHAPE = (224, 224) # height x width\nFMIN = 500\nFMAX = 12500\n# MAX_AUDIO_FILES = 10000\nEPOCHS=10","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:33:29.085156Z","iopub.execute_input":"2023-01-17T19:33:29.085983Z","iopub.status.idle":"2023-01-17T19:33:29.094500Z","shell.execute_reply.started":"2023-01-17T19:33:29.085943Z","shell.execute_reply":"2023-01-17T19:33:29.092808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load metadata file","metadata":{"execution":{"iopub.status.busy":"2023-01-18T02:15:28.849183Z","iopub.execute_input":"2023-01-18T02:15:28.849944Z","iopub.status.idle":"2023-01-18T02:15:28.862723Z","shell.execute_reply.started":"2023-01-18T02:15:28.849906Z","shell.execute_reply":"2023-01-18T02:15:28.861255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrain = pd.read_csv('/kaggle/input/birdclef-2022/train_metadata.csv',)","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:33:37.981424Z","iopub.execute_input":"2023-01-17T19:33:37.981909Z","iopub.status.idle":"2023-01-17T19:33:38.123482Z","shell.execute_reply.started":"2023-01-17T19:33:37.981873Z","shell.execute_reply":"2023-01-17T19:33:38.122019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(24, 8))\nsns.countplot(data= train, x='primary_label', ax=ax, order= train['primary_label'].value_counts().index)\nplt.xticks(rotation=90);","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:33:57.887744Z","iopub.execute_input":"2023-01-17T19:33:57.888248Z","iopub.status.idle":"2023-01-17T19:34:02.633972Z","shell.execute_reply.started":"2023-01-17T19:33:57.888211Z","shell.execute_reply":"2023-01-17T19:34:02.632923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.primary_label.unique()","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:34:04.610305Z","iopub.execute_input":"2023-01-17T19:34:04.611918Z","iopub.status.idle":"2023-01-17T19:34:04.622875Z","shell.execute_reply.started":"2023-01-17T19:34:04.611862Z","shell.execute_reply":"2023-01-17T19:34:04.621432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"avg_ratings = train.groupby(\"primary_label\").agg({\"rating\" : \"mean\"})\nplt.figure(figsize = (20, 6))\nsns.barplot(avg_ratings.index, avg_ratings.rating)\nplt.title(\"Average ratings on specific bird codes\")\nplt.xlabel(\"Bird Codes\")\nplt.ylabel(\"Rating\")\nplt.xticks(rotation = 90)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:34:11.569907Z","iopub.execute_input":"2023-01-17T19:34:11.570632Z","iopub.status.idle":"2023-01-17T19:34:15.905587Z","shell.execute_reply.started":"2023-01-17T19:34:11.570585Z","shell.execute_reply":"2023-01-17T19:34:15.904136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_feature_dist = train.primary_label.value_counts()\nplt.figure(figsize = (20, 20))\nplt.pie(target_feature_dist.values, labels= target_feature_dist.index)\nplt.title(\"Target Feature Distribution\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:34:18.958955Z","iopub.execute_input":"2023-01-17T19:34:18.960266Z","iopub.status.idle":"2023-01-17T19:34:20.593198Z","shell.execute_reply.started":"2023-01-17T19:34:18.960221Z","shell.execute_reply":"2023-01-17T19:34:20.591842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Limit the number of training samples and classes\n# First, only use high quality samples\ntrain = train.query('rating>=4')\n\n# Second, assume that birds with the most training samples are also the most common\n# A species needs at least 200 recordings with a rating above 4 to be considered common\nbirds_count = {}\nfor bird_species, count in zip(train.primary_label.unique(), \n                               train.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 >= 175] \n\nTRAIN = train.query('primary_label in @most_represented_birds')\nLABELS = sorted(TRAIN.primary_label.unique())\n\n# Let's see how many species and samples we have left\nprint('NUMBER OF SPECIES IN TRAIN DATA:', len(LABELS))\nprint('NUMBER OF SAMPLES IN TRAIN DATA:', len(TRAIN))\nprint('LABELS:', most_represented_birds)","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:34:29.346926Z","iopub.execute_input":"2023-01-17T19:34:29.347398Z","iopub.status.idle":"2023-01-17T19:34:29.379211Z","shell.execute_reply.started":"2023-01-17T19:34:29.347363Z","shell.execute_reply":"2023-01-17T19:34:29.377896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# saving labels \nwith open('LABELS.pkl','wb') as f:\n    pickle.dump(LABELS,f)","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:34:35.899455Z","iopub.execute_input":"2023-01-17T19:34:35.900630Z","iopub.status.idle":"2023-01-17T19:34:35.906387Z","shell.execute_reply.started":"2023-01-17T19:34:35.900584Z","shell.execute_reply":"2023-01-17T19:34:35.905172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Shuffle the training data and limit the number of audio files to MAX_AUDIO_FILES\nTRAIN = shuffle(TRAIN, random_state=RANDOM_SEED)\n\n# Define a function that splits an audio file, \n# extracts spectrograms and saves them in a working directory\ndef get_spectrograms(filepath, primary_label, output_dir):\n    \n    # Open the file with librosa (limited to the first 15 seconds)\n    sig, rate = librosa.load(filepath, sr=SAMPLE_RATE, offset=None, duration=15)\n    \n    # Split signal into five second chunks\n    sig_splits = []\n    for i in range(0, len(sig), int(SIGNAL_LENGTH * SAMPLE_RATE)):\n        split = sig[i:i + int(SIGNAL_LENGTH * SAMPLE_RATE)]\n\n        # End of signal?\n        if len(split) < int(SIGNAL_LENGTH * SAMPLE_RATE):\n            break\n        \n        sig_splits.append(split)\n        \n    # Extract mel spectrograms for each audio chunk\n    s_cnt = 0\n    saved_samples = []\n    for chunk in sig_splits:\n        \n        hop_length = int(SIGNAL_LENGTH * SAMPLE_RATE / (SPEC_SHAPE[1] - 1))\n        mel_spec = librosa.feature.melspectrogram(y=chunk, \n                                                  sr=SAMPLE_RATE, \n                                                  n_fft=1024, \n                                                  hop_length=hop_length, \n                                                  n_mels=SPEC_SHAPE[0], \n                                                  fmin=FMIN, \n                                                  fmax=FMAX)\n    \n        mel_spec = librosa.power_to_db(mel_spec, ref=np.max) \n        \n        # Normalize\n        mel_spec -= mel_spec.min()\n        mel_spec /= mel_spec.max()\n        \n        # Save as image file\n        save_dir = os.path.join(output_dir, primary_label)\n        if not os.path.exists(save_dir):\n            os.makedirs(save_dir)\n        save_path = os.path.join(save_dir, filepath.rsplit(os.sep, 1)[-1].rsplit('.', 1)[0] + \n                                 '_' + str(s_cnt) + '.png')\n        im = Image.fromarray(mel_spec * 255.0).convert(\"L\")\n        im.save(save_path)\n        \n        saved_samples.append(save_path)\n        s_cnt += 1\n        \n        \n    return saved_samples\n\nprint('FINAL NUMBER OF AUDIO FILES IN TRAINING DATA:', len(TRAIN))","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:34:45.635606Z","iopub.execute_input":"2023-01-17T19:34:45.636088Z","iopub.status.idle":"2023-01-17T19:34:45.654082Z","shell.execute_reply.started":"2023-01-17T19:34:45.636053Z","shell.execute_reply":"2023-01-17T19:34:45.652673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Parse audio files and extract training samples\ninput_dir = '/kaggle/input/birdclef-2022/train_audio'\noutput_dir = '../working/melspectrogram_dataset/'\nsamples = []\nwith tqdm(total=len(TRAIN)) as pbar:\n    for idx, row in TRAIN.iterrows():\n        pbar.update(1)\n        \n        if row.primary_label in most_represented_birds:\n            audio_file_path = os.path.join(input_dir, row.filename)\n            samples += get_spectrograms(audio_file_path, row.primary_label, output_dir)\n            \nTRAIN_SPECS = shuffle(samples, random_state=RANDOM_SEED)\nprint('SUCCESSFULLY EXTRACTED {} SPECTROGRAMS'.format(len(TRAIN_SPECS)))","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:35:06.189770Z","iopub.execute_input":"2023-01-17T19:35:06.190234Z","iopub.status.idle":"2023-01-17T19:45:09.554219Z","shell.execute_reply.started":"2023-01-17T19:35:06.190201Z","shell.execute_reply":"2023-01-17T19:45:09.552109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_folder = './melspectrogram_dataset'\n# valid_datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2)\ndatagen = ImageDataGenerator(\n    rotation_range=20,\n    zoom_range=0.15,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.15,\n    horizontal_flip=True,\n    fill_mode=\"nearest\",\n    preprocessing_function=preprocess_input,\n    validation_split=0.2)\n\ntrain_generator = datagen.flow_from_directory(train_folder, \n#                         target_size=(coefs.sshape[0],coefs.sshape[1]),  # target size\n                        target_size=(224,224),\n                        batch_size=64, \n                        seed=2022,\n                        shuffle=True,\n                        subset = \"training\",\n                        class_mode='categorical')    # batch size\nvalidation_generator = datagen.flow_from_directory(train_folder, \n#                         target_size=(coefs.sshape[0],coefs.sshape[1]),  # target size\n                        target_size=(224,224),\n                        batch_size=64,\n                        seed=2022,\n                        shuffle=True,\n                        subset = \"validation\",\n                        class_mode='categorical')    # batch size","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:45:20.695888Z","iopub.execute_input":"2023-01-17T19:45:20.697235Z","iopub.status.idle":"2023-01-17T19:45:21.336031Z","shell.execute_reply.started":"2023-01-17T19:45:20.697169Z","shell.execute_reply":"2023-01-17T19:45:21.334875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimg = cv2.imread('./melspectrogram_dataset/brnowl/XC635289_2.png')\n\nimg.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:45:46.431306Z","iopub.execute_input":"2023-01-17T19:45:46.431811Z","iopub.status.idle":"2023-01-17T19:45:46.483858Z","shell.execute_reply.started":"2023-01-17T19:45:46.431775Z","shell.execute_reply":"2023-01-17T19:45:46.482457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LEARNING_RATE = 0.001\nnum_epochs = 10\nBATCH_SIZE = 64\nIMG_SIZE = 224","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:46:03.436284Z","iopub.execute_input":"2023-01-17T19:46:03.436790Z","iopub.status.idle":"2023-01-17T19:46:03.442212Z","shell.execute_reply.started":"2023-01-17T19:46:03.436754Z","shell.execute_reply":"2023-01-17T19:46:03.441035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import 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 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\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import EfficientNetB0\nfrom tensorflow.keras.applications.efficientnet import preprocess_input\nfrom keras.utils.vis_utils import model_to_dot\nfrom tensorflow.keras.utils import plot_model\nfrom tensorflow.keras.applications import ResNet50","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:46:22.225996Z","iopub.execute_input":"2023-01-17T19:46:22.226523Z","iopub.status.idle":"2023-01-17T19:46:22.235832Z","shell.execute_reply.started":"2023-01-17T19:46:22.226477Z","shell.execute_reply":"2023-01-17T19:46:22.234815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CNN","metadata":{}},{"cell_type":"code","source":"def My_CNNmodel():\n\n  model = tf.keras.models.Sequential()\n  model.add(layers.Conv2D(8, (3, 3), padding='same',activation='relu', input_shape=(224,224, 3)))\n  model.add(layers.MaxPooling2D(pool_size=(2,2)))\n  model.add(layers.Conv2D(16, (3, 3), padding='same',activation='relu'))\n  model.add(layers.MaxPooling2D(pool_size=(2,2)))\n  model.add(layers.Conv2D(32, (3, 3), padding='same',activation='relu'))\n  model.add(layers.MaxPooling2D(pool_size=(2,2)))\n  model.add(layers.Conv2D(64, (3, 3), padding='same',activation='relu'))\n  model.add(layers.MaxPooling2D(pool_size=(2,2)))\n  model.add(layers.Flatten())\n  model.add(layers.Dense(512, activation='relu'))\n  model.add(layers.Dense(16, activation='sigmoid'))\n\n  opt=tf.keras.optimizers.Adam(0.001)\n  model.compile(optimizer=opt,\n              loss='binary_crossentropy', # loss='categorical_crossentropy' if softmax\n              metrics=['accuracy'])\n\n  return model","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:46:50.130881Z","iopub.execute_input":"2023-01-17T19:46:50.131339Z","iopub.status.idle":"2023-01-17T19:46:50.143399Z","shell.execute_reply.started":"2023-01-17T19:46:50.131303Z","shell.execute_reply":"2023-01-17T19:46:50.142133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=My_CNNmodel()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:47:00.983753Z","iopub.execute_input":"2023-01-17T19:47:00.984315Z","iopub.status.idle":"2023-01-17T19:47:01.277666Z","shell.execute_reply.started":"2023-01-17T19:47:00.984268Z","shell.execute_reply":"2023-01-17T19:47:01.276657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"categorical_crossentropy\", optimizer='adam')","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:47:14.812731Z","iopub.execute_input":"2023-01-17T19:47:14.813182Z","iopub.status.idle":"2023-01-17T19:47:14.825928Z","shell.execute_reply.started":"2023-01-17T19:47:14.813147Z","shell.execute_reply":"2023-01-17T19:47:14.824455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator,\n          epochs = 5, \n          validation_data=validation_generator)","metadata":{"execution":{"iopub.status.busy":"2023-01-17T19:47:24.570032Z","iopub.execute_input":"2023-01-17T19:47:24.570468Z","iopub.status.idle":"2023-01-17T20:02:13.357795Z","shell.execute_reply.started":"2023-01-17T19:47:24.570436Z","shell.execute_reply":"2023-01-17T20:02:13.356469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"H = history\nplt.style.use(\"ggplot\")\nplt.figure()\nplt.plot(np.arange(0, len(H.history[\"loss\"])), H.history[\"loss\"], label=\"train_loss\")\nplt.plot(np.arange(0, len(H.history[\"val_loss\"])), H.history[\"val_loss\"], label=\"val_loss\")\nplt.title(\"Training Loss\")\nplt.xlabel(\"Epoch #\")\nplt.ylabel(\"Loss\")\nplt.legend(loc=\"lower left\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-17T20:02:19.188300Z","iopub.execute_input":"2023-01-17T20:02:19.188843Z","iopub.status.idle":"2023-01-17T20:02:19.451004Z","shell.execute_reply.started":"2023-01-17T20:02:19.188804Z","shell.execute_reply":"2023-01-17T20:02:19.449814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('bird_model_cnn.h5')","metadata":{"execution":{"iopub.status.busy":"2023-01-17T20:02:34.243194Z","iopub.execute_input":"2023-01-17T20:02:34.243648Z","iopub.status.idle":"2023-01-17T20:02:34.407230Z","shell.execute_reply.started":"2023-01-17T20:02:34.243612Z","shell.execute_reply":"2023-01-17T20:02:34.406159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Resnet 50","metadata":{"execution":{"iopub.status.busy":"2023-01-17T20:02:48.207772Z","iopub.execute_input":"2023-01-17T20:02:48.208252Z","iopub.status.idle":"2023-01-17T20:02:48.214065Z","shell.execute_reply.started":"2023-01-17T20:02:48.208218Z","shell.execute_reply":"2023-01-17T20:02:48.212370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = ResNet50(weights='imagenet',include_top=False, input_shape=(224,224,3))\n\n# Construct the head of the model that will be placed on top of the base model\nhead_model = base_model.output\nhead_model = GlobalAveragePooling2D()(head_model)\nhead_model = Flatten(name=\"flatten\")(head_model)\nhead_model = Dense(16, activation=\"softmax\")(head_model)\nmodel = Model(inputs=base_model.input, outputs=head_model)\n\nfor layer in base_model.layers:\n    layer.trainable = True\nmodel.summary()\nmodel.compile(loss=\"categorical_crossentropy\", optimizer='adam', metrics=[\"accuracy\"])\n\n\nH = model.fit(\n    train_generator,\n    steps_per_epoch=train_generator.samples // train_generator.batch_size,\n    validation_data=validation_generator,\n    validation_steps=validation_generator.samples // validation_generator.batch_size,\n    epochs=num_epochs)","metadata":{"execution":{"iopub.status.busy":"2023-01-17T20:02:59.216287Z","iopub.execute_input":"2023-01-17T20:02:59.216788Z","iopub.status.idle":"2023-01-18T02:15:28.838075Z","shell.execute_reply.started":"2023-01-17T20:02:59.216749Z","shell.execute_reply":"2023-01-18T02:15:28.836770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.style.use(\"ggplot\")\nplt.figure()\nplt.plot(np.arange(0, len(H.history[\"loss\"])), H.history[\"loss\"], label=\"train_loss\")\nplt.plot(np.arange(0, len(H.history[\"val_loss\"])), H.history[\"val_loss\"], label=\"val_loss\")\nplt.plot(np.arange(0, len(H.history[\"accuracy\"])), H.history[\"accuracy\"], label=\"train_acc\")\nplt.plot(np.arange(0, len(H.history[\"val_accuracy\"])), H.history[\"val_accuracy\"], label=\"val_acc\")\nplt.title(\"Training Loss and Accuracy\")\nplt.xlabel(\"Epoch #\")\nplt.ylabel(\"Loss/Accuracy\")\nplt.legend(loc=\"lower left\")\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('bird_model_resnet50.h5')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EfficientNetB0","metadata":{}},{"cell_type":"code","source":"base_model = EfficientNetB0(weights='imagenet',include_top=False, input_shape=(224,224,3))\n\n# Construct the head of the model that will be placed on top of the base model\nhead_model = base_model.output\nhead_model = GlobalAveragePooling2D()(head_model)\nhead_model = Flatten(name=\"flatten\")(head_model)\nhead_model = Dense(16, activation=\"softmax\")(head_model)\nmodel = Model(inputs=base_model.input, outputs=head_model)\n\nfor layer in base_model.layers:\n    layer.trainable = True\nmodel.summary()\nmodel.compile(loss=\"categorical_crossentropy\", optimizer='adam', metrics=[\"accuracy\"])\n\nH = model.fit(\n    train_generator,\n    steps_per_epoch=train_generator.samples // train_generator.batch_size,\n    validation_data=validation_generator,\n    validation_steps=validation_generator.samples // validation_generator.batch_size,\n    epochs=num_epochs)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.style.use(\"ggplot\")\nplt.figure()\nplt.plot(np.arange(0, len(H.history[\"loss\"])), H.history[\"loss\"], label=\"train_loss\")\nplt.plot(np.arange(0, len(H.history[\"val_loss\"])), H.history[\"val_loss\"], label=\"val_loss\")\nplt.plot(np.arange(0, len(H.history[\"accuracy\"])), H.history[\"accuracy\"], label=\"train_acc\")\nplt.plot(np.arange(0, len(H.history[\"val_accuracy\"])), H.history[\"val_accuracy\"], label=\"val_acc\")\nplt.title(\"Training Loss and Accuracy\")\nplt.xlabel(\"Epoch #\")\nplt.ylabel(\"Loss/Accuracy\")\nplt.legend(loc=\"lower left\")\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('bird_model_efficientnetB0.h5')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_audio_dir = '../input/birdclef-2022/test_soundscapes/'\nfile_list = [f.split('.')[0] for f in sorted(os.listdir(test_audio_dir))]\n\nprint('Number of test soundscapes:', len(file_list))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\nwith open('../input/birdclef-2022/scored_birds.json') as sbfile:\n    scored_birds = json.load(sbfile)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(threshold):\n#     row_id = []\n    pred = {'row_id': [], 'target': []}\n    scnt = 0\n    for afile in file_list:\n        # Open it with librosa\n        path = test_audio_dir + afile + '.ogg'\n        sig, rate = librosa.load(path, sr=SAMPLE_RATE)\n        sig_splits = []\n        for i in range(0, len(sig), int(SIGNAL_LENGTH * SAMPLE_RATE)):\n            split = sig[i:i + int(SIGNAL_LENGTH * SAMPLE_RATE)]\n\n            # End of signal?\n            if len(split) < int(SIGNAL_LENGTH * SAMPLE_RATE):\n                break\n\n            sig_splits.append(split)\n\n        seconds= 0\n        for chunk in sig_splits:\n\n            # Keep track of the end time of each chunk\n            seconds += 5\n\n            # Get the spectrogram\n            hop_length = int(SIGNAL_LENGTH * SAMPLE_RATE / (SPEC_SHAPE[1] - 1))\n            mel_spec = librosa.feature.melspectrogram(y=chunk, \n                                                      sr=SAMPLE_RATE, \n                                                      n_fft=1024, \n                                                      hop_length=hop_length, \n                                                      n_mels=SPEC_SHAPE[0], \n                                                      fmin=FMIN, \n                                                      fmax=FMAX)\n\n            mel_spec = librosa.power_to_db(mel_spec, ref=np.max) \n\n            # Normalize to match the value range we used during training.\n            # That's something you should always double check!\n            mel_spec -= mel_spec.min()\n            mel_spec /= mel_spec.max()\n\n            # Add channel axis to 2D array\n            mel_spec = np.expand_dims(mel_spec, -1)\n\n            # Add new dimension for batch size\n            mel_spec = np.expand_dims(mel_spec, 0)\n\n            # Predict\n            p = 0.5*model.predict(mel_spec)[0] \n\n            # Get highest scoring species\n            idx = p.argmax()\n            species = LABELS[idx]\n            score = p[idx]\n            chunks = [[] for i in range(12)]\n            for idx,i in enumerate(range(len(chunks))):        \n                for bird in scored_birds:\n                    chunk_end_time = (i + 1) * 5\n                # Prepare submission entry\n                    row_id = afile + '_' + bird + '_' + str(chunk_end_time)\n#                     row_id.append(afile.split(os.sep)[-1].rsplit('_', 1)[0] + \n#                                   '_' + str(seconds))  \n                    # Decide if it's a \"nocall\" or a species by applying a threshold\n#                     pred['row_id'].append(row_id)\n#                     pred['target'].append(True if score > threshold else False)\n            \n                    pred['row_id'].append(row_id)\n                    pred['target'].append(True if score > threshold else False)\n                       \n\n    result = pd.DataFrame(pred, columns = ['row_id', 'target'])\n    return result","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result=predict(0.3)\nresult","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result\nresults = result.to_csv('submission_bird.csv',index=False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = pd.read_csv('./submission_bird.csv')\nresults","metadata":{},"execution_count":null,"outputs":[]}]}