{"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":"# Sample submission\n\nAssuming you have looked at our other notebooks, you should be familiar with extracting training samples from focal recordings, train a model and then make predictions on soundscape data. The only step that is missing is a valid submission to enter the leaderboard. In this tutorial, we will introduce the very basics of how to run inference on a hidden test set.\n\nFirst thing we need to do is to look for test soundscapes. **The hidden test set will only appear if you submit the notebook.** Yet, you can use the training soundscapes to test your workflow.","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\n\n# First, get a list of soundscape files to process.\n# We'll use the test_soundscape directory if it contains \"ogg\" files\n# (which it only does when submitting the notebook), \n# otherwise we'll use the train_soundscape folder to make predictions.\ndef list_files(path):\n    return [os.path.join(path, f) for f in os.listdir(path) if f.rsplit('.', 1)[-1] in ['ogg']]\ntest_audio = list_files('../input/birdclef-2021/test_soundscapes')\nif len(test_audio) == 0:\n    test_audio = list_files('../input/birdclef-2021/train_soundscapes')\n    \nprint('{} FILES IN TEST SET.'.format(len(test_audio)))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Filenames already include all the data that we need to make a valid submission (test and train soundscapes have the same naming scheme). \n\nTake a look at this example:","metadata":{}},{"cell_type":"code","source":"path = test_audio[0]\ndata = path.split(os.sep)[-1].rsplit('.', 1)[0].split('_')\n\nprint('FILEPATH:', path)\nprint('ID: {}, SITE: {}, DATE: {}'.format(data[0], data[1], data[2]))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We could load the *test.csv* as well, but for now, there's no benefit in doing so. However, the *test.csv* might come in handy when confirming that your generated row_id actually exists in the test data.\n\nLet's make some predictions. In this tutorial, we'll simply use mock predictions, so that we can focus on the submission process.","metadata":{}},{"cell_type":"code","source":"# This is where we will store our results\npred = {'row_id': [], 'birds': []}\n\n# Analyze each soundscape recording\nfor path in test_audio:\n    \n    # Open file with Librosa\n    \n    # Split file into 5-second chunks\n    \n    # Extract spectrogram for each chunk\n    \n    # Predict on spectrogram\n    \n    # Get row_id and birds and store result\n    # (maybe using a post-filter based on location)\n    \n    # The above steps are just placeholders, we will use mock predictions.\n    # Our \"model\" will predict \"nocall\" for each spectrogram.\n    fileinfo = path.split(os.sep)[-1].rsplit('.', 1)[0].split('_')\n    for second in range(5, 605, 5):\n        row_id = fileinfo[0] + '_'  + fileinfo[1] + '_'  + str(second)  \n        pred['row_id'].append(row_id)\n        pred['birds'].append('nocall')\n        \n# Make a new data frame and look at a few \"results\"\nresults = pd.DataFrame(pred, columns = ['row_id', 'birds'])\nresults.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Finally, we have to convert our result data frame to a csv file named \"submission.csv\". This way, it will get rated when submitting the notebook.","metadata":{}},{"cell_type":"code","source":"# Convert our results to csv\nresults.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Ok, but how do we actually submit? These are the steps you need to take:\n\n1. Save the notebook by clicking \"Save Version\" in the upper right corner (only visible in edit mode).\n2. The notebook will then show up under \"Code\" on the main competition page (it will also show up under \"Your work\").\n3. Click and view the notebook.\n4. Now click on the three dots in the upper right corner and select \"Submit to Competition\" (see screenshot below).\n5. Follow the on-screen instructions.\n6. Wait for the notebook to finish, results will show up under \"My Submissions\".\n\n![How to submit to the competition](https://tuc.cloud/index.php/s/z9eWEA8ZtbHki3i/preview)\n\nThat's it. Please make sure to check out our other notebooks, leave a comment if you have any remarks and please don't hesitate to start a new forum thread if you have any questions.","metadata":{}}]}