{"cells":[{"metadata":{},"cell_type":"markdown","source":"Trying fastaudio for myself : https://www.kaggle.com/scart97/fastaudio-starter-kit"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"!pip install --upgrade light-the-torch\n!ltt install torch torchvision torchaudio\n!pip install --upgrade git+http://github.com/fastaudio/fastaudio.git","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# this is needed for the library\nimport pkg_resources\ndef placeholder(x):\n    raise pkg_resources.DistributionNotFound\npkg_resources.get_distribution = placeholder","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import pandas as pd\nfrom fastaudio.all import *\nfrom fastai.vision.all import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path(\"../input/rfcx-species-audio-detection\")\nfor i in path.ls():\n    print(i)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_path = path / 'train'\ntest_path = path / 'test'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_files = get_audio_files(train_path)\nlen(train_files)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"index =math.floor(random.random()*4727)\nprint(index)\n\n# random bird chirping from amazon!\naudio = AudioTensor.create(train_files[index])\naudio.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train_tp = pd.read_csv(path/ 'train_tp.csv')\ndf_train_tp[\"recording_id\"] = df_train_tp[\"recording_id\"].map(lambda x: \"train/\"+x)\ndf_train_tp.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train_tp.iloc[:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train_tp = df_train_tp.drop(['t_min', 't_max', 'f_min', 'f_max', 'songtype_id'],axis=1)\ndf_train_tp['species_id'] = df_train_tp['species_id'].astype(str)\ndf_train_tp.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train_tp['species_id'] = df_train_tp.groupby('recording_id')['species_id'].transform(\",\".join)\ndf_train_tp = df_train_tp.reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train_tp","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# this part is wizadry\naudio_to_spec = AudioToSpec.from_cfg(AudioConfig.BasicMelSpectrogram(n_fft=512))\ndata_augmentation = [AddNoise(color=NoiseColor.White, noise_level=0.1), SignalShifter(max_pct=0.3)]\n\nblocks = DataBlock(blocks=(AudioBlock, MultiCategoryBlock), get_x = ColReader('recording_id', pref=str(path.resolve()) + \"/\" , suff='.flac'),\n                   get_y = ColReader('species_id', label_delim=','),\n                   item_tfms = data_augmentation,\n                   batch_tfms = audio_to_spec,\n                   splitter = RandomSplitter(valid_pct=0.2, seed=42))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls = blocks.dataloaders(df_train_tp,bs=24)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls.show_batch(ncols=3, nrows=2, figsize=(20, 10))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learner = cnn_learner(dls, resnet18, config={\"n_in\":1})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learner.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learner.fine_tune(10, base_lr=5e-2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learner.recorder.plot_loss()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df = pd.read_csv(path / 'sample_submission.csv')\nsubmission_df[\"recording_id\"] = submission_df[\"recording_id\"].map(lambda x: \"test/\"+x)\nsubmission_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"It works !"}],"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}