{"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":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow import keras\nimport tensorflow_addons as tfa\nfrom sklearn.metrics import f1_score\nfrom sklearn.model_selection import StratifiedKFold\nfrom datetime import datetime\nimport os\nfrom tensorflow.train import BytesList, FloatList, Int64List \nfrom tensorflow.train import Feature, Features, Example\nfrom sklearn import metrics","metadata":{"tags":[],"execution":{"iopub.status.busy":"2022-08-21T16:07:57.119370Z","iopub.execute_input":"2022-08-21T16:07:57.120632Z","iopub.status.idle":"2022-08-21T16:08:03.438713Z","shell.execute_reply.started":"2022-08-21T16:07:57.119918Z","shell.execute_reply":"2022-08-21T16:08:03.437754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_dir = '../input/datasetwithfilter/dataset with filter'\nmetadata_path = '../input/birdclef-2022/train_metadata.csv'","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:08:03.440517Z","iopub.execute_input":"2022-08-21T16:08:03.441482Z","iopub.status.idle":"2022-08-21T16:08:03.450859Z","shell.execute_reply.started":"2022-08-21T16:08:03.441443Z","shell.execute_reply":"2022-08-21T16:08:03.448841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata = pd.read_csv(metadata_path, na_values='[]')\nmetadata.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:08:03.452044Z","iopub.execute_input":"2022-08-21T16:08:03.453504Z","iopub.status.idle":"2022-08-21T16:08:03.575299Z","shell.execute_reply.started":"2022-08-21T16:08:03.453453Z","shell.execute_reply":"2022-08-21T16:08:03.574321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SAMPLE_RATE = 32000\nSAMPLE_RATE","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:08:03.578171Z","iopub.execute_input":"2022-08-21T16:08:03.578859Z","iopub.status.idle":"2022-08-21T16:08:03.586028Z","shell.execute_reply.started":"2022-08-21T16:08:03.578820Z","shell.execute_reply":"2022-08-21T16:08:03.584878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = np.array(['brnowl', 'comsan', 'mallar3', 'norcar'])\nclass_names","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:08:03.587619Z","iopub.execute_input":"2022-08-21T16:08:03.588121Z","iopub.status.idle":"2022-08-21T16:08:03.597416Z","shell.execute_reply.started":"2022-08-21T16:08:03.588083Z","shell.execute_reply":"2022-08-21T16:08:03.596382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = len(class_names)\nnum_classes","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:08:03.599074Z","iopub.execute_input":"2022-08-21T16:08:03.599471Z","iopub.status.idle":"2022-08-21T16:08:03.610061Z","shell.execute_reply.started":"2022-08-21T16:08:03.599421Z","shell.execute_reply":"2022-08-21T16:08:03.608815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rows = metadata.primary_label.isin(class_names)\nmetadata = metadata[rows]\nmetadata.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:08:03.612031Z","iopub.execute_input":"2022-08-21T16:08:03.612332Z","iopub.status.idle":"2022-08-21T16:08:03.641037Z","shell.execute_reply.started":"2022-08-21T16:08:03.612308Z","shell.execute_reply":"2022-08-21T16:08:03.640143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_folds(data):\n    data['fold'] = -1\n    skf = StratifiedKFold(n_splits=10, shuffle=False, random_state=None)\n    y = data.primary_label\n    \n    for fold, (train_indices, test_indices) in enumerate(skf.split(data, y), 1):\n        data.iloc[test_indices, -1] = fold\n        \n    return data","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:08:03.642807Z","iopub.execute_input":"2022-08-21T16:08:03.643536Z","iopub.status.idle":"2022-08-21T16:08:03.649656Z","shell.execute_reply.started":"2022-08-21T16:08:03.643498Z","shell.execute_reply":"2022-08-21T16:08:03.648752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata = create_folds(metadata)\nmetadata.tail()","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:08:03.651244Z","iopub.execute_input":"2022-08-21T16:08:03.651898Z","iopub.status.idle":"2022-08-21T16:08:03.683216Z","shell.execute_reply.started":"2022-08-21T16:08:03.651856Z","shell.execute_reply":"2022-08-21T16:08:03.682391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rows = metadata.secondary_labels.isna()\nmetadata = metadata[rows]\nmetadata.tail()","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:08:03.687675Z","iopub.execute_input":"2022-08-21T16:08:03.688495Z","iopub.status.idle":"2022-08-21T16:08:03.707625Z","shell.execute_reply.started":"2022-08-21T16:08:03.688459Z","shell.execute_reply":"2022-08-21T16:08:03.706719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_metadata = metadata[metadata.fold <= 8]\ntrain_metadata.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:08:03.708975Z","iopub.execute_input":"2022-08-21T16:08:03.709719Z","iopub.status.idle":"2022-08-21T16:08:03.729691Z","shell.execute_reply.started":"2022-08-21T16:08:03.709686Z","shell.execute_reply":"2022-08-21T16:08:03.728774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_metadata = metadata[metadata.fold == 9]\nvalid_metadata.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:08:03.731096Z","iopub.execute_input":"2022-08-21T16:08:03.732122Z","iopub.status.idle":"2022-08-21T16:08:03.752499Z","shell.execute_reply.started":"2022-08-21T16:08:03.732086Z","shell.execute_reply":"2022-08-21T16:08:03.751594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_metadata = metadata[metadata.fold == 10]\ntest_metadata.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:08:03.754271Z","iopub.execute_input":"2022-08-21T16:08:03.754947Z","iopub.status.idle":"2022-08-21T16:08:03.773810Z","shell.execute_reply.started":"2022-08-21T16:08:03.754911Z","shell.execute_reply":"2022-08-21T16:08:03.772651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_description = {\n    \"serialized_audio_segment\": tf.io.FixedLenFeature([], tf.string, b''),\n}\n\ndef preprocess(serialized_example):\n    parsed_example = tf.io.parse_single_example(serialized_example, feature_description)\n    audio_segment = tf.io.parse_tensor(parsed_example['serialized_audio_segment'], out_type=tf.float32)\n    audio_segment.set_shape((160000,))\n    return audio_segment","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:08:03.775577Z","iopub.execute_input":"2022-08-21T16:08:03.775943Z","iopub.status.idle":"2022-08-21T16:08:03.782783Z","shell.execute_reply.started":"2022-08-21T16:08:03.775909Z","shell.execute_reply":"2022-08-21T16:08:03.781770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_log_mel_spectrogram(waveform):\n    spectrogram = tf.abs(tf.signal.stft(\n        signals=waveform,\n        frame_length=1024,\n        frame_step=512,\n        fft_length=None,\n        window_fn=tf.signal.hann_window,\n        pad_end=False))\n    \n    linear_to_mel_weight_matrix  = tf.signal.linear_to_mel_weight_matrix(\n        num_mel_bins=120,\n        num_spectrogram_bins=513,\n        sample_rate=SAMPLE_RATE,\n        lower_edge_hertz=100.0,\n        upper_edge_hertz=SAMPLE_RATE/2)\n    \n    mel_spectrogram = tf.matmul(spectrogram, linear_to_mel_weight_matrix)\n    \n    mel_spectrogram = tf.maximum(mel_spectrogram, 1e-16)\n    \n    log_mel_spectrogram = tf.math.log(mel_spectrogram)\n    \n    log_mel_spectrogram = tf.transpose(log_mel_spectrogram)\n    \n    return tf.expand_dims(log_mel_spectrogram, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:08:03.784254Z","iopub.execute_input":"2022-08-21T16:08:03.785271Z","iopub.status.idle":"2022-08-21T16:08:03.793471Z","shell.execute_reply.started":"2022-08-21T16:08:03.785237Z","shell.execute_reply":"2022-08-21T16:08:03.792467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_dataset(metadata):\n    filepaths = []\n    labels = []\n\n    for index, series in metadata.iterrows():\n        filename = series['filename']\n        filename = os.path.splitext(filename)[0]\n        global_path = os.path.join(dataset_dir, filename, '*')\n        fpaths = tf.io.gfile.glob(global_path)\n        filepaths += fpaths\n        primary_label = series['primary_label']\n        \n        for i in range(len(fpaths)):\n            label = (primary_label == class_names).astype('float32')\n            labels.append(label)\n            \n    df = pd.DataFrame([])\n    df['filepath'] = filepaths\n    df['label'] = labels    \n    dataset_X = tf.data.TFRecordDataset(df.filepath)\n    dataset_X = dataset_X.map(preprocess)\n    dataset_X = dataset_X.map(get_log_mel_spectrogram)\n    dataset_y = tf.data.Dataset.from_tensor_slices(df.label.to_list())\n    dataset = tf.data.Dataset.zip((dataset_X, dataset_y)) \n    dataset = dataset.cache()\n    dataset = dataset.batch(16)\n    return dataset.prefetch(1)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:08:03.794920Z","iopub.execute_input":"2022-08-21T16:08:03.796012Z","iopub.status.idle":"2022-08-21T16:08:03.807206Z","shell.execute_reply.started":"2022-08-21T16:08:03.795977Z","shell.execute_reply":"2022-08-21T16:08:03.806120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_set = create_dataset(valid_metadata)\ntest_set = create_dataset(test_metadata)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:08:03.808765Z","iopub.execute_input":"2022-08-21T16:08:03.809477Z","iopub.status.idle":"2022-08-21T16:08:12.072547Z","shell.execute_reply.started":"2022-08-21T16:08:03.809442Z","shell.execute_reply":"2022-08-21T16:08:12.071500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_set.element_spec","metadata":{"tags":[],"execution":{"iopub.status.busy":"2022-08-21T16:08:12.074327Z","iopub.execute_input":"2022-08-21T16:08:12.074887Z","iopub.status.idle":"2022-08-21T16:08:12.083153Z","shell.execute_reply.started":"2022-08-21T16:08:12.074852Z","shell.execute_reply":"2022-08-21T16:08:12.082224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start = datetime.now()\n\nfor X, y in valid_set:\n    pass\n\nend = datetime.now()\nduration = end - start\nprint(duration)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:08:12.084763Z","iopub.execute_input":"2022-08-21T16:08:12.085369Z","iopub.status.idle":"2022-08-21T16:09:05.538253Z","shell.execute_reply.started":"2022-08-21T16:08:12.085333Z","shell.execute_reply":"2022-08-21T16:09:05.537228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start = datetime.now()\n\nfor X, y in valid_set:\n    pass\n\nend = datetime.now()\nduration = end - start\nprint(duration)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:09:05.539960Z","iopub.execute_input":"2022-08-21T16:09:05.540633Z","iopub.status.idle":"2022-08-21T16:09:05.736019Z","shell.execute_reply.started":"2022-08-21T16:09:05.540596Z","shell.execute_reply":"2022-08-21T16:09:05.734529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start = datetime.now()\n\nfor X, y in test_set:\n    pass\n\nend = datetime.now()\nduration = end - start\nprint(duration)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:09:05.741519Z","iopub.execute_input":"2022-08-21T16:09:05.741984Z","iopub.status.idle":"2022-08-21T16:09:59.839066Z","shell.execute_reply.started":"2022-08-21T16:09:05.741946Z","shell.execute_reply":"2022-08-21T16:09:59.838088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_50_model = keras.models.load_model(\"../input/pretrained-models-with-filters/pretrained ResNet-50 with filter\")\nvgg_19_model = keras.models.load_model(\"../input/pretrained-models-with-filters/pretrained VGG-19 with filter\")\nxception_model = keras.models.load_model(\"../input/pretrained-models-with-filters/pretrained Xception with filter\")","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:09:59.840393Z","iopub.execute_input":"2022-08-21T16:09:59.841002Z","iopub.status.idle":"2022-08-21T16:10:26.134499Z","shell.execute_reply.started":"2022-08-21T16:09:59.840965Z","shell.execute_reply":"2022-08-21T16:10:26.133429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_50_model.evaluate(valid_set)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:10:26.136479Z","iopub.execute_input":"2022-08-21T16:10:26.136873Z","iopub.status.idle":"2022-08-21T16:10:47.776289Z","shell.execute_reply.started":"2022-08-21T16:10:26.136835Z","shell.execute_reply":"2022-08-21T16:10:47.775291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_valid = (list(valid_set.map(lambda X, y: y)))\ny_valid = np.concatenate(y_valid, axis=0)\ny_valid.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:10:47.777658Z","iopub.execute_input":"2022-08-21T16:10:47.778609Z","iopub.status.idle":"2022-08-21T16:10:47.992325Z","shell.execute_reply.started":"2022-08-21T16:10:47.778571Z","shell.execute_reply":"2022-08-21T16:10:47.991374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_proba1 = resnet_50_model.predict(valid_set)\ny_pred1 = y_proba1 > 0.5\nf1_score(y_valid, y_pred1, average='macro')","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:10:47.993590Z","iopub.execute_input":"2022-08-21T16:10:47.993938Z","iopub.status.idle":"2022-08-21T16:10:51.764396Z","shell.execute_reply.started":"2022-08-21T16:10:47.993903Z","shell.execute_reply":"2022-08-21T16:10:51.763393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_proba1 = resnet_50_model.predict(valid_set)\ny_proba2 = vgg_19_model.predict(valid_set)\ny_proba3 = xception_model.predict(valid_set)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:10:51.765715Z","iopub.execute_input":"2022-08-21T16:10:51.766388Z","iopub.status.idle":"2022-08-21T16:11:05.274205Z","shell.execute_reply.started":"2022-08-21T16:10:51.766350Z","shell.execute_reply":"2022-08-21T16:11:05.273220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weights = []\n\nfor w1 in np.arange(0, 1.01, 0.01):\n    for w2 in np.arange(0, 1.01, 0.01):\n        if (w1 + w2) > 1.:\n            break\n        w3 = 1 - (w1 + w2)\n        weights.append([w1, w2, w3])\n        \nweights = np.array(weights)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:11:05.276133Z","iopub.execute_input":"2022-08-21T16:11:05.276496Z","iopub.status.idle":"2022-08-21T16:11:05.295394Z","shell.execute_reply.started":"2022-08-21T16:11:05.276459Z","shell.execute_reply":"2022-08-21T16:11:05.294460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = []\n\nfor w1, w2, w3 in weights:\n    y_proba = w1 * y_proba1 + w2 * y_proba2 + w3 * y_proba3\n    y_pred = y_proba > 0.5\n    score = f1_score(y_valid, y_pred, average='macro')\n    scores.append(score)\n    \nw1, w2, w3 = weights[np.argmax(scores)]\nw1, w2, w3","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:11:05.301595Z","iopub.execute_input":"2022-08-21T16:11:05.302364Z","iopub.status.idle":"2022-08-21T16:11:21.189234Z","shell.execute_reply.started":"2022-08-21T16:11:05.302327Z","shell.execute_reply":"2022-08-21T16:11:21.188283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.max(scores)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:11:21.190748Z","iopub.execute_input":"2022-08-21T16:11:21.191446Z","iopub.status.idle":"2022-08-21T16:11:21.198458Z","shell.execute_reply.started":"2022-08-21T16:11:21.191377Z","shell.execute_reply":"2022-08-21T16:11:21.197522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = (list(test_set.map(lambda X, y: y)))\ny_test = np.concatenate(y_test, axis=0)\ny_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:25:05.667739Z","iopub.execute_input":"2022-08-21T16:25:05.668346Z","iopub.status.idle":"2022-08-21T16:25:05.872579Z","shell.execute_reply.started":"2022-08-21T16:25:05.668314Z","shell.execute_reply":"2022-08-21T16:25:05.871684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_proba1 = resnet_50_model.predict(test_set)\ny_proba2 = vgg_19_model.predict(test_set)\ny_proba3 = xception_model.predict(test_set)\n\ny_proba = w1 * y_proba1 + w2 * y_proba2 + w3 * y_proba3\ny_pred = y_proba > 0.5\nf1_score(y_test, y_pred, average='macro')","metadata":{"execution":{"iopub.status.busy":"2022-08-21T16:25:46.970813Z","iopub.execute_input":"2022-08-21T16:25:46.971172Z","iopub.status.idle":"2022-08-21T16:25:58.484347Z","shell.execute_reply.started":"2022-08-21T16:25:46.971143Z","shell.execute_reply":"2022-08-21T16:25:58.483339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}