{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-15T05:35:36.053055Z","iopub.execute_input":"2023-03-15T05:35:36.054705Z","iopub.status.idle":"2023-03-15T05:35:39.869198Z","shell.execute_reply.started":"2023-03-15T05:35:36.054633Z","shell.execute_reply":"2023-03-15T05:35:39.867923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# importing the Nessesecry Library\nimport tensorflow as tf\nimport tensorflow_hub as hub\nimport tensorflow_io as tfio\n\nimport pandas as pd\nimport numpy as np\nimport librosa\nimport glob\nimport matplotlib.pyplot as plt\n\nimport csv\nimport io\n\nfrom IPython.display import Audio","metadata":{"execution":{"iopub.status.busy":"2023-03-15T06:48:23.252491Z","iopub.execute_input":"2023-03-15T06:48:23.253002Z","iopub.status.idle":"2023-03-15T06:48:23.261782Z","shell.execute_reply.started":"2023-03-15T06:48:23.252959Z","shell.execute_reply":"2023-03-15T06:48:23.260222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i,j=librosa.load('/kaggle/input/birdclef-2023/train_audio/yetgre1/XC530241.ogg')\nAudio(data=i,rate=j)","metadata":{"execution":{"iopub.status.busy":"2023-03-15T05:39:53.918674Z","iopub.execute_input":"2023-03-15T05:39:53.920147Z","iopub.status.idle":"2023-03-15T05:39:53.947911Z","shell.execute_reply.started":"2023-03-15T05:39:53.920092Z","shell.execute_reply":"2023-03-15T05:39:53.946382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i,j=librosa.load('/kaggle/input/birdclef-2023/train_audio/edcsun3/XC434334.ogg')\nAudio(data=i,rate=j)","metadata":{"execution":{"iopub.status.busy":"2023-03-15T05:40:09.668674Z","iopub.execute_input":"2023-03-15T05:40:09.669182Z","iopub.status.idle":"2023-03-15T05:40:09.817679Z","shell.execute_reply.started":"2023-03-15T05:40:09.669143Z","shell.execute_reply":"2023-03-15T05:40:09.815369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now Loading the Model from Google\n","metadata":{}},{"cell_type":"code","source":"import tensorflow_hub as hub\n        \nkeras_layer = hub.load('https://kaggle.com/models/google/bird-vocalization-classifier/frameworks/TensorFlow2/variations/bird-vocalization-classifier/versions/1')\n","metadata":{"execution":{"iopub.status.busy":"2023-03-15T05:52:43.719041Z","iopub.execute_input":"2023-03-15T05:52:43.720270Z","iopub.status.idle":"2023-03-15T05:52:53.268595Z","shell.execute_reply.started":"2023-03-15T05:52:43.720222Z","shell.execute_reply":"2023-03-15T05:52:53.267183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_path = hub.resolve('https://kaggle.com/models/google/bird-vocalization-classifier/frameworks/tensorFlow2/variations/bird-vocalization-classifier/versions/1') + \"/assets/label.csv\"","metadata":{"execution":{"iopub.status.busy":"2023-03-15T05:53:07.039741Z","iopub.execute_input":"2023-03-15T05:53:07.040161Z","iopub.status.idle":"2023-03-15T05:53:07.045957Z","shell.execute_reply.started":"2023-03-15T05:53:07.040122Z","shell.execute_reply":"2023-03-15T05:53:07.044439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels_path)","metadata":{"execution":{"iopub.status.busy":"2023-03-15T05:53:53.303016Z","iopub.execute_input":"2023-03-15T05:53:53.303518Z","iopub.status.idle":"2023-03-15T05:53:53.310682Z","shell.execute_reply.started":"2023-03-15T05:53:53.303478Z","shell.execute_reply":"2023-03-15T05:53:53.309095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Find the name of the class with the top score when mean-aggregated across frames.\ndef class_names_from_csv(class_map_csv_text):\n    \"\"\"Returns list of class names corresponding to score vector.\"\"\"\n    with open(labels_path) as csv_file:\n        csv_reader = csv.reader(csv_file, delimiter=',')\n        class_names = [mid for mid, desc in csv_reader]\n        return class_names[1:]\n\n## note that the bird classifier classifies a much larger set of birds than the\n## competition, so we need to load the model's set of class names or else our \n## indices will be off.\nclasses = class_names_from_csv(labels_path)","metadata":{"execution":{"iopub.status.busy":"2023-03-15T05:55:13.999356Z","iopub.execute_input":"2023-03-15T05:55:14.000603Z","iopub.status.idle":"2023-03-15T05:55:14.034584Z","shell.execute_reply.started":"2023-03-15T05:55:14.000553Z","shell.execute_reply":"2023-03-15T05:55:14.033266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"this is the classes that are available in the Pretrained Model of Google Bird Classifier:\",len(set(classes)))","metadata":{"execution":{"iopub.status.busy":"2023-03-15T06:03:39.332274Z","iopub.execute_input":"2023-03-15T06:03:39.332743Z","iopub.status.idle":"2023-03-15T06:03:39.341434Z","shell.execute_reply.started":"2023-03-15T06:03:39.332707Z","shell.execute_reply":"2023-03-15T06:03:39.339859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_metadata = pd.read_csv(\"/kaggle/input/birdclef-2023/train_metadata.csv\")\ntrain_metadata.head(10)\ntraning_classes=sorted(train_metadata.primary_label.unique())\nprint(traning_classes[:5])\nprint(len(set(traning_classes)))","metadata":{"execution":{"iopub.status.busy":"2023-03-15T06:10:25.102851Z","iopub.execute_input":"2023-03-15T06:10:25.103368Z","iopub.status.idle":"2023-03-15T06:10:25.188915Z","shell.execute_reply.started":"2023-03-15T06:10:25.103319Z","shell.execute_reply":"2023-03-15T06:10:25.187067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count=0\nlist1=[]\nfor i in  traning_classes: \n    try:\n        c=classes.index(i)\n        list1.append(c)\n    except:\n        list1.append(0)\n        count+=1\n        print(i)\nprint(\"This are the number of classes which are not present in the Pre defined Traning model: \",count)","metadata":{"execution":{"iopub.status.busy":"2023-03-15T06:11:56.996606Z","iopub.execute_input":"2023-03-15T06:11:56.997075Z","iopub.status.idle":"2023-03-15T06:11:57.040259Z","shell.execute_reply.started":"2023-03-15T06:11:56.997039Z","shell.execute_reply":"2023-03-15T06:11:57.038849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def frame_audio(\n      audio_array: np.ndarray,\n      window_size_s: float = 5.0,\n      hop_size_s: float = 5.0,\n      sample_rate = 32000,\n      ) -> np.ndarray:\n    \n    \"\"\"Helper function for framing audio for inference.\"\"\"\n    \"\"\" using tf.signal \"\"\"\n    if window_size_s is None or window_size_s < 0:\n        return audio_array[np.newaxis, :]\n    frame_length = int(window_size_s * sample_rate)\n    hop_length = int(hop_size_s * sample_rate)\n    framed_audio = tf.signal.frame(audio_array, frame_length, hop_length, pad_end=True)\n    return framed_audio","metadata":{"execution":{"iopub.status.busy":"2023-03-15T06:34:21.743997Z","iopub.execute_input":"2023-03-15T06:34:21.745510Z","iopub.status.idle":"2023-03-15T06:34:21.756379Z","shell.execute_reply.started":"2023-03-15T06:34:21.745447Z","shell.execute_reply":"2023-03-15T06:34:21.753203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def ensure_sample_rate(waveform, original_sample_rate,\n                       desired_sample_rate=32000):\n    \"\"\"Resample waveform if required.\"\"\"\n    if original_sample_rate != desired_sample_rate:\n        waveform = tfio.audio.resample(waveform, original_sample_rate, desired_sample_rate)\n    return waveform,desired_sample_rate","metadata":{"execution":{"iopub.status.busy":"2023-03-15T06:24:04.239513Z","iopub.execute_input":"2023-03-15T06:24:04.240215Z","iopub.status.idle":"2023-03-15T06:24:04.248385Z","shell.execute_reply.started":"2023-03-15T06:24:04.240152Z","shell.execute_reply":"2023-03-15T06:24:04.246323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio,sample_rate=librosa.load('/kaggle/input/birdclef-2023/train_audio/blcapa2/XC120193.ogg')\nprint(sample_rate)\nbefore_sample=Audio(data=audio,rate=sample_rate)\naudio,sample_rate=ensure_sample_rate(audio,sample_rate)\nprint(sample_rate)\nafter_sample=Audio(data=audio,rate=sample_rate)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-15T06:26:05.596422Z","iopub.execute_input":"2023-03-15T06:26:05.596960Z","iopub.status.idle":"2023-03-15T06:26:05.850879Z","shell.execute_reply.started":"2023-03-15T06:26:05.596918Z","shell.execute_reply":"2023-03-15T06:26:05.849833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# this is the audio before sampeling\nbefore_sample","metadata":{"execution":{"iopub.status.busy":"2023-03-15T06:26:15.540894Z","iopub.execute_input":"2023-03-15T06:26:15.541322Z","iopub.status.idle":"2023-03-15T06:26:15.589347Z","shell.execute_reply.started":"2023-03-15T06:26:15.541288Z","shell.execute_reply":"2023-03-15T06:26:15.587925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#this is the Audio after sampleing\nafter_sample","metadata":{"execution":{"iopub.status.busy":"2023-03-15T06:26:30.080471Z","iopub.execute_input":"2023-03-15T06:26:30.080945Z","iopub.status.idle":"2023-03-15T06:26:30.149005Z","shell.execute_reply.started":"2023-03-15T06:26:30.080904Z","shell.execute_reply":"2023-03-15T06:26:30.146856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fixed_audio=frame_audio(audio)","metadata":{"execution":{"iopub.status.busy":"2023-03-15T06:35:19.128025Z","iopub.execute_input":"2023-03-15T06:35:19.129106Z","iopub.status.idle":"2023-03-15T06:35:30.623923Z","shell.execute_reply.started":"2023-03-15T06:35:19.129053Z","shell.execute_reply":"2023-03-15T06:35:30.622719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(fixed_audio)","metadata":{"execution":{"iopub.status.busy":"2023-03-15T06:35:41.082749Z","iopub.execute_input":"2023-03-15T06:35:41.083213Z","iopub.status.idle":"2023-03-15T06:35:41.092023Z","shell.execute_reply.started":"2023-03-15T06:35:41.083159Z","shell.execute_reply":"2023-03-15T06:35:41.089901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"librosa.display.waveshow(np.array(librosa.load('/kaggle/input/birdclef-2023/train_audio/yetgre1/XC530241.ogg')[0]))","metadata":{"execution":{"iopub.status.busy":"2023-03-15T06:46:48.850267Z","iopub.execute_input":"2023-03-15T06:46:48.850727Z","iopub.status.idle":"2023-03-15T06:46:49.470381Z","shell.execute_reply.started":"2023-03-15T06:46:48.850691Z","shell.execute_reply":"2023-03-15T06:46:49.468438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"librosa.display.waveshow(np.array(fixed_audio),sr=sample_rate)\nplt.title('waveform after Transforamtion')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-15T06:49:15.623796Z","iopub.execute_input":"2023-03-15T06:49:15.624346Z","iopub.status.idle":"2023-03-15T06:49:16.026222Z","shell.execute_reply.started":"2023-03-15T06:49:15.624304Z","shell.execute_reply":"2023-03-15T06:49:16.024603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logits,_ = keras_layer.infer_tf(fixed_audio[:1])","metadata":{"execution":{"iopub.status.busy":"2023-03-15T06:53:08.007942Z","iopub.execute_input":"2023-03-15T06:53:08.008560Z","iopub.status.idle":"2023-03-15T06:53:10.276462Z","shell.execute_reply.started":"2023-03-15T06:53:08.008507Z","shell.execute_reply":"2023-03-15T06:53:10.275459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(logits)\nprint(logits.shape)\nprint(np.argmax(logits))","metadata":{"execution":{"iopub.status.busy":"2023-03-15T06:53:15.022474Z","iopub.execute_input":"2023-03-15T06:53:15.022970Z","iopub.status.idle":"2023-03-15T06:53:15.031425Z","shell.execute_reply.started":"2023-03-15T06:53:15.022927Z","shell.execute_reply":"2023-03-15T06:53:15.029964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes[np.argmax(tf.nn.softmax(logits))],tf.nn.softmax(logits)[0][np.argmax(tf.nn.softmax(logits))]","metadata":{"execution":{"iopub.status.busy":"2023-03-15T07:01:00.200529Z","iopub.execute_input":"2023-03-15T07:01:00.201005Z","iopub.status.idle":"2023-03-15T07:01:00.214040Z","shell.execute_reply.started":"2023-03-15T07:01:00.200968Z","shell.execute_reply":"2023-03-15T07:01:00.212246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes[1150]","metadata":{"execution":{"iopub.status.busy":"2023-03-15T07:06:12.247512Z","iopub.execute_input":"2023-03-15T07:06:12.248057Z","iopub.status.idle":"2023-03-15T07:06:12.256484Z","shell.execute_reply.started":"2023-03-15T07:06:12.248010Z","shell.execute_reply":"2023-03-15T07:06:12.254730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_audio(filename, sample_submission, frame_limit_secs=None):\n    file_id = filename.split(\".ogg\")[0].split(\"/\")[-1]\n    audio, sample_rate = librosa.load(filename)\n    wav_data,sample_rate, = ensure_sample_rate(audio, sample_rate)\n    fixed_audio = frame_audio(wav_data)\n    frame = 5\n    all_logits, all_embeddings = keras_layer.infer_tf(fixed_audio[:1])\n    for window in fixed_audio[1:]:\n        if frame_limit_secs and frame > frame_limit_secs:\n            continue\n        \n        logits, embeddings = keras_layer.infer_tf(window[np.newaxis, :])\n        all_logits = np.concatenate([all_logits, logits], axis=0)\n        frame += 5\n        frame = 5\n    all_probabilities = []\n    for frame_logits in all_logits:\n        probabilities = tf.nn.softmax(frame_logits).numpy()\n        \n        ## set the appropriate row in the sample submission\n        sample_submission.loc[sample_submission.row_id == file_id + \"_\" + str(frame),traning_classes] = probabilities[list1]\n        frame += 5","metadata":{"execution":{"iopub.status.busy":"2023-03-15T07:29:21.046020Z","iopub.execute_input":"2023-03-15T07:29:21.046516Z","iopub.status.idle":"2023-03-15T07:29:21.057524Z","shell.execute_reply.started":"2023-03-15T07:29:21.046479Z","shell.execute_reply":"2023-03-15T07:29:21.055755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_samples = list(glob.glob(\"/kaggle/input/birdclef-2023/test_soundscapes/*.ogg\"))\ntest_samples\n","metadata":{"execution":{"iopub.status.busy":"2023-03-15T07:17:23.815422Z","iopub.execute_input":"2023-03-15T07:17:23.816384Z","iopub.status.idle":"2023-03-15T07:17:23.829271Z","shell.execute_reply.started":"2023-03-15T07:17:23.816327Z","shell.execute_reply":"2023-03-15T07:17:23.827488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub = pd.read_csv(\"/kaggle/input/birdclef-2023/sample_submission.csv\")\nsample_sub[traning_classes] = sample_sub[traning_classes].astype(np.float32)\nsample_sub.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-15T07:18:33.040947Z","iopub.execute_input":"2023-03-15T07:18:33.041402Z","iopub.status.idle":"2023-03-15T07:18:33.151732Z","shell.execute_reply.started":"2023-03-15T07:18:33.041363Z","shell.execute_reply":"2023-03-15T07:18:33.150296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-03-15T07:24:55.400571Z","iopub.execute_input":"2023-03-15T07:24:55.401019Z","iopub.status.idle":"2023-03-15T07:24:55.410359Z","shell.execute_reply.started":"2023-03-15T07:24:55.400983Z","shell.execute_reply":"2023-03-15T07:24:55.408681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frame_limit_secs = 15 if sample_sub.shape[0] == 3 else None\nfor sample_filename in test_samples:\n    predict_audio(sample_filename, sample_sub, frame_limit_secs=15)","metadata":{"execution":{"iopub.status.busy":"2023-03-15T07:29:25.300768Z","iopub.execute_input":"2023-03-15T07:29:25.301445Z","iopub.status.idle":"2023-03-15T07:33:55.030810Z","shell.execute_reply.started":"2023-03-15T07:29:25.301367Z","shell.execute_reply":"2023-03-15T07:33:55.029367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub","metadata":{"execution":{"iopub.status.busy":"2023-03-15T07:34:11.804681Z","iopub.execute_input":"2023-03-15T07:34:11.805722Z","iopub.status.idle":"2023-03-15T07:34:11.835537Z","shell.execute_reply.started":"2023-03-15T07:34:11.805676Z","shell.execute_reply":"2023-03-15T07:34:11.834504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-15T07:34:39.289823Z","iopub.execute_input":"2023-03-15T07:34:39.290843Z","iopub.status.idle":"2023-03-15T07:34:39.313288Z","shell.execute_reply.started":"2023-03-15T07:34:39.290798Z","shell.execute_reply":"2023-03-15T07:34:39.311896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}