{"cells":[{"metadata":{},"cell_type":"markdown","source":"## Pre-Trained Model:\n\nXception Model trained in Part 2_Notebook 1 can be added from [here](https://www.kaggle.com/timothyalexjohn/xception-birdcall-model)","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"# Notebook_1 || Part 3\n\nThis Notebook is the **3rd Part** of 'Birdsong_Classifier_Keras_CNN' Notebook for \"Cornell Birdcall Identification\" challenge.\n\nIn the [Previous Part](https://www.kaggle.com/timothyalexjohn/birdsong-classifier-keras-cnn-part-2-notebook-1) of Notebook_1 we trained CNNs with extracted features.  \n\nIn This Part we upload last Notebook's Output and make a prediction. ","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"## Notebooks in this Challenge:\n\n* Notebook_1:\n       \n > [Part 1](https://www.kaggle.com/timothyalexjohn/birdsong-classifier-keras-cnn-part-1-notebook-1):  Converting Audio Files to Spectrogram Image Files using Librosa\n  \n > [Part 2](https://www.kaggle.com/timothyalexjohn/birdsong-classifier-keras-cnn-part-2-notebook-1):  Running CNN on Extracted Features / Training\n \n > [Part 3](https://www.kaggle.com/timothyalexjohn/birdsong-classifier-keras-cnn-part-3-notebook-1): Prediction of test data\n \n\n* Notebook_2:\n        \n >Running LSTM Cells directly on Audio Files\n \n \n...Links will be updated soon!","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"## Debugger\nHere is a [Debugger_Notebook](https://www.kaggle.com/timothyalexjohn/birdsong-classifier-keras-cnn-debugger-notebook-1) that helped me find the structure of my submission using \"example_test_audio\" file\n\n...for anyone who finds it difficult to submit and get a score other than '0.544'","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"## Importing Library","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import librosa            \nimport librosa.display\nimport numpy as np\nimport pandas as pd\nimport os\nimport cv2\n\nimport tensorflow.keras as tk\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":false,"trusted":true},"cell_type":"code","source":"tk.__version__    # Important: set the Environment Preferences to \"Latest Environment\"\n                  # tk._version should be >= 2.4.0 ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Test Directory","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"test_dir = '../input/birdsong-recognition/test_audio/'\ntest_csv_dir = '../input/birdsong-recognition/test.csv'\ntest_filesave_dir = '/kaggle/working/'\n\ntest_df = pd.read_csv(test_csv_dir)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Test Spectrogram Function","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"row_id = []\n\ndef create_spectrogram(df, start_time, duration, mode):\n\n    if mode== 'test':\n        filepath = (test_dir +df[3] +'.mp3') \n    \n    try:\n        fig = plt.figure(figsize=[2.7,2.7])\n        clip, sample_rate = librosa.load(filepath, sr=None, offset=start_time, duration=duration)\n        S = librosa.feature.melspectrogram(y=clip, sr=sample_rate)\n        librosa.display.specshow(librosa.power_to_db(S, ref=np.max))\n\n        if mode== 'test':\n            if not os.path.exists(test_filesave_dir):\n                os.makedirs(test_filesave_dir)  \n            plt.savefig((test_filesave_dir +df[1] +'.jpg'), bbox_inches='tight',pad_inches=0, facecolor='black')\n            \n            row_id.append(df[1])\n            \n        fig.clear()          \n        plt.close(fig)\n        plt.close()\n        plt.close('all')   \n        plt.cla()\n        fig.clf()\n        plt.clf()\n        plt.close()\n    \n    except:\n        print(\"found a broken Audio File\")     ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Getting Rid of Un-Wanted Warnings when Loading Audio Files\nimport warnings\n\nwarnings.filterwarnings(\"ignore\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for row in test_df.values:\n    \n    if row[0]=='site_3' :\n        start_time= 0\n        create_spectrogram(row, start_time, duration= None, mode= 'test')\n    else :\n        start_time = row[2] - 5\n        create_spectrogram(row, start_time, duration= 5, mode= 'test')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Pre-Trained Model","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"model = tk.models.load_model('../input/xception-birdcall-model')    # Important: Read the Note above","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Prediction","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# DICTIONARY EXTRACTED FROM  \"train_generator.class_indices\"  from Part_2\n\nbird_dict = {'aldfly': 0, 'ameavo': 1, 'amebit': 2, 'amecro': 3, 'amegfi': 4, 'amekes': 5, 'amepip': 6, 'amered': 7, 'amerob': 8, 'amewig': 9, 'amewoo': 10, 'amtspa': 11, 'annhum': 12, 'astfly': 13, 'baisan': 14, 'baleag': 15, 'balori': 16, 'banswa': 17, 'barswa': 18, 'bawwar': 19, 'belkin1': 20, 'belspa2': 21, 'bewwre': 22, 'bkbcuc': 23, 'bkbmag1': 24, 'bkbwar': 25, 'bkcchi': 26, 'bkchum': 27, 'bkhgro': 28, 'bkpwar': 29, 'bktspa': 30, 'blkpho': 31, 'blugrb1': 32, 'blujay': 33, 'bnhcow': 34, 'boboli': 35, 'bongul': 36, 'brdowl': 37, 'brebla': 38, 'brespa': 39, 'brncre': 40, 'brnthr': 41, 'brthum': 42, 'brwhaw': 43, 'btbwar': 44, 'btnwar': 45, 'btywar': 46, 'buffle': 47, 'buggna': 48, 'buhvir': 49, 'bulori': 50, 'bushti': 51, 'buwtea': 52, 'buwwar': 53, 'cacwre': 54, 'calgul': 55, 'calqua': 56, 'camwar': 57, 'cangoo': 58, 'canwar': 59, 'canwre': 60, 'carwre': 61, 'casfin': 62, 'caster1': 63, 'casvir': 64, 'cedwax': 65, 'chispa': 66, 'chiswi': 67, 'chswar': 68, 'chukar': 69, 'clanut': 70, 'cliswa': 71, 'comgol': 72, 'comgra': 73, 'comloo': 74, 'commer': 75, 'comnig': 76, 'comrav': 77, 'comred': 78, 'comter': 79, 'comyel': 80, 'coohaw': 81, 'coshum': 82, 'cowscj1': 83, 'daejun': 84, 'doccor': 85, 'dowwoo': 86, 'dusfly': 87, 'eargre': 88, 'easblu': 89, 'easkin': 90, 'easmea': 91, 'easpho': 92, 'eastow': 93, 'eawpew': 94, 'eucdov': 95, 'eursta': 96, 'evegro': 97, 'fiespa': 98, 'fiscro': 99, 'foxspa': 100, 'gadwal': 101, 'gcrfin': 102, 'gnttow': 103, 'gnwtea': 104, 'gockin': 105, 'gocspa': 106, 'goleag': 107, 'grbher3': 108, 'grcfly': 109, 'greegr': 110, 'greroa': 111, 'greyel': 112, 'grhowl': 113, 'grnher': 114, 'grtgra': 115, 'grycat': 116, 'gryfly': 117, 'haiwoo': 118, 'hamfly': 119, 'hergul': 120, 'herthr': 121, 'hoomer': 122, 'hoowar': 123, 'horgre': 124, 'horlar': 125, 'houfin': 126, 'houspa': 127, 'houwre': 128, 'indbun': 129, 'juntit1': 130, 'killde': 131, 'labwoo': 132, 'larspa': 133, 'lazbun': 134, 'leabit': 135, 'leafly': 136, 'leasan': 137, 'lecthr': 138, 'lesgol': 139, 'lesnig': 140, 'lesyel': 141, 'lewwoo': 142, 'linspa': 143, 'lobcur': 144, 'lobdow': 145, 'logshr': 146, 'lotduc': 147, 'louwat': 148, 'macwar': 149, 'magwar': 150, 'mallar3': 151, 'marwre': 152, 'merlin': 153, 'moublu': 154, 'mouchi': 155, 'moudov': 156, 'norcar': 157, 'norfli': 158, 'norhar2': 159, 'normoc': 160, 'norpar': 161, 'norpin': 162, 'norsho': 163, 'norwat': 164, 'nrwswa': 165, 'nutwoo': 166, 'olsfly': 167, 'orcwar': 168, 'osprey': 169, 'ovenbi1': 170, 'palwar': 171, 'pasfly': 172, 'pecsan': 173, 'perfal': 174, 'phaino': 175, 'pibgre': 176, 'pilwoo': 177, 'pingro': 178, 'pinjay': 179, 'pinsis': 180, 'pinwar': 181, 'plsvir': 182, 'prawar': 183, 'purfin': 184, 'pygnut': 185, 'rebmer': 186, 'rebnut': 187, 'rebsap': 188, 'rebwoo': 189, 'redcro': 190, 'redhea': 191, 'reevir1': 192, 'renpha': 193, 'reshaw': 194, 'rethaw': 195, 'rewbla': 196, 'ribgul': 197, 'rinduc': 198, 'robgro': 199, 'rocpig': 200, 'rocwre': 201, 'rthhum': 202, 'ruckin': 203, 'rudduc': 204, 'rufgro': 205, 'rufhum': 206, 'rusbla': 207, 'sagspa1': 208, 'sagthr': 209, 'savspa': 210, 'saypho': 211, 'scatan': 212, 'scoori': 213, 'semplo': 214, 'semsan': 215, 'sheowl': 216, 'shshaw': 217, 'snobun': 218, 'snogoo': 219, 'solsan': 220, 'sonspa': 221, 'sora': 222, 'sposan': 223, 'spotow': 224, 'stejay': 225, 'swahaw': 226, 'swaspa': 227, 'swathr': 228, 'treswa': 229, 'truswa': 230, 'tuftit': 231, 'tunswa': 232, 'veery': 233, 'vesspa': 234, 'vigswa': 235, 'warvir': 236, 'wesblu': 237, 'wesgre': 238, 'weskin': 239, 'wesmea': 240, 'wessan': 241, 'westan': 242, 'wewpew': 243, 'whbnut': 244, 'whcspa': 245, 'whfibi': 246, 'whtspa': 247, 'whtswi': 248, 'wilfly': 249, 'wilsni1': 250, 'wiltur': 251, 'winwre3': 252, 'wlswar': 253, 'wooduc': 254, 'wooscj2': 255, 'woothr': 256, 'y00475': 257, 'yebfly': 258, 'yebsap': 259, 'yehbla': 260, 'yelwar': 261, 'yerwar': 262, 'yetvir': 263}\n\nrev_bird_dict = {value : key for (key, value) in bird_dict.items()}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def predict(path):\n    img = cv2.imread(str(path))\n    img = cv2.resize(img, (150,150))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img = img.astype(np.float32)/255.\n    img = np.reshape(img,(1,150,150,3))\n    return model.predict(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"try:\n    target = []\n\n    for img in row_id:\n        target.append(predict(test_filesave_dir +img +'.jpg'))\n    \nexcept:\n    pass\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Submission","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"try:   \n    pred_class = []\n    for row in target:\n        if row.max()< 0.45:\n            pred_class.append('nocall')\n        else:\n            pred_class.append(rev_bird_dict[np.argmax(row, axis=1)[0]])\n    \n    predict_df = pd.DataFrame(row_id)\n    predict_df.columns = ['row_id']\n    predict_df['birds'] = pred_class\n    \nexcept:\n    predict_df = pd.read_csv('../input/birdsong-recognition/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Very Important to remove all other output files other than \"submission.csv\"\n\ntry:\n    \n    for img in row_id:\n        os.remove(test_filesave_dir +img +'.jpg')\n    \nexcept:\n    pass","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predict_df.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Note \n\nNotebook_1 series ends here.\n\nNotebook_2 will be updated shortly...\n\n### Thank You!!","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"### Please Upvote if you liked the Notebook Series!! ","execution_count":null}],"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":4,"nbformat_minor":4}