{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Reference\n\n1. https://www.kaggle.com/daisukelab/cnn-2d-basic-solution-powered-by-fast-ai.\n2. https://www.kaggle.com/daisukelab/fat2019_prep_mels1\n3. https://www.kaggle.com/c/freesound-audio-tagging-2019/overview/timeline\n4. https://nbviewer.jupyter.org/github/fastai/course-v3/blob/master/nbs/dl1/lesson3-planet.ipynb\n\n# New\nI tried training with more models and 2 models: vgg_16 , vgg_19 with the best results.\nAlso, I changed the bz from 64 to 128 and also gave better results."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-input":false},"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom pathlib import Path\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm_notebook\nimport IPython\nimport IPython.display\nimport PIL\nimport pickle\nimport gc\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## File/folder definitions\n\n- `df` will handle training data.\n- `test_df` will handle test data."},{"metadata":{"trusted":true},"cell_type":"code","source":"DATA = Path('../input/freesound-audio-tagging-2019')\nPREPROCESSED = Path('../input/fat2019_prep_mels1')\nWORK = Path('work')\nPath(WORK).mkdir(exist_ok=True, parents=True)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"CSV_TRN_CURATED = DATA/'train_curated.csv'\nCSV_TRN_NOISY = PREPROCESSED/'trn_noisy_best50s.csv'\nCSV_SUBMISSION = DATA/'sample_submission.csv'\n\nMELS_TRN_CURATED = PREPROCESSED/'mels_train_curated.pkl'\nMELS_TRN_NOISY = '../input/fat2019_prep_mels1/mels_trn_noisy_best50s.pkl'\nMELS_TEST = PREPROCESSED/'mels_test.pkl'\n\ntrn_curated_df = pd.read_csv(CSV_TRN_CURATED)\ntrn_noisy_df = pd.read_csv(CSV_TRN_NOISY)\ntest_df = pd.read_csv(CSV_SUBMISSION)\n\ndf = pd.concat([trn_curated_df, trn_noisy_df], ignore_index=True) # not enough memory\n# df = pd.concat([trn_curated_df], ignore_index=True)\ntest_df = pd.read_csv(CSV_SUBMISSION)\n\nX_train = pickle.load(open(MELS_TRN_CURATED, 'rb'))  # Create an empty dictionary\nwith open(\"../input/fat2019_prep_mels1/mels_trn_noisy_best50s.pkl\", 'rb') as f:\n    X_train.append(pickle.load(f))   # Update contents of file2 to the dictionary\n\nX_test = pickle.load(open(MELS_TEST, 'rb'))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Custom `open_image` for fast.ai library to load data from memory\n\n- Important note: Random cropping 1 sec, this is working like augmentation."},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai import *\nfrom fastai.vision import *\nfrom fastai.vision.data import *\nimport random\n\nCUR_X_FILES, CUR_X = list(df.fname.values), X_train\n\ndef open_fat2019_image(fn, convert_mode, after_open)->Image:\n    # open\n    idx = CUR_X_FILES.index(fn.split('/')[-1])\n    x = PIL.Image.fromarray(CUR_X[idx])\n    # crop\n    time_dim, base_dim = x.size\n    crop_x = random.randint(0, time_dim - base_dim)\n    x = x.crop([crop_x, 0, crop_x+base_dim, base_dim])    \n    # standardize\n    return Image(pil2tensor(x, np.float32).div_(255))\n\nvision.data.open_image = open_fat2019_image","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Follow multi-label classification\n\n- Almost following fast.ai course: https://nbviewer.jupyter.org/github/fastai/course-v3/blob/master/nbs/dl1/lesson3-planet.ipynb\n- But `pretrained=False`"},{"metadata":{"trusted":true},"cell_type":"code","source":"tfms = get_transforms(do_flip=True, max_rotate=0, max_lighting=0.1, max_zoom=0, max_warp=0.)\nsrc = (ImageList.from_csv(WORK, Path('..')/CSV_TRN_CURATED, folder='trn_curated')\n       .split_by_rand_pct(0.2)\n       .label_from_df(label_delim=',')\n)\ndata = (src.transform(tfms, size=128)\n        .databunch(bs=128).normalize(imagenet_stats)\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"list_model = [ models.vgg16_bn, models.vgg19_bn]\npredicts = None\ni = 0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(list_model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(list_model[i])\nCUR_X_FILES, CUR_X = list(df.fname.values), X_train\nf_score = partial(fbeta, thresh=0.2)\nlearn = cnn_learner(data, list_model[i], pretrained=False, metrics=[f_score])\nlearn.unfreeze()\nlearn.lr_find()\n\nlearn.fit_one_cycle(5, slice(1e-6, 1e-1))\n\nlearn.lr_find()\n\nlearn.fit_one_cycle(100, slice(1e-6, 1e-2))\n\nlearn.export()\n\nCUR_X_FILES, CUR_X = list(test_df.fname.values), X_test\n\ntest = ImageList.from_csv(WORK, Path('..')/CSV_SUBMISSION, folder='test')\nlearn = load_learner(WORK, test=test)\npreds, _ = learn.get_preds(ds_type=DatasetType.Test)\nif predicts is None:\n    predicts = preds\nelse:\n    predicts += preds\ni+=1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(list_model[i])\nCUR_X_FILES, CUR_X = list(df.fname.values), X_train\nf_score = partial(fbeta, thresh=0.2)\nlearn = cnn_learner(data, list_model[i], pretrained=False, metrics=[f_score])\nlearn.unfreeze()\nlearn.lr_find()\n\nlearn.fit_one_cycle(5, slice(1e-6, 1e-1))\n\nlearn.lr_find()\n\nlearn.fit_one_cycle(100, slice(1e-6, 1e-2))\n\nlearn.export()\n\nCUR_X_FILES, CUR_X = list(test_df.fname.values), X_test\n\ntest = ImageList.from_csv(WORK, Path('..')/CSV_SUBMISSION, folder='test')\nlearn = load_learner(WORK, test=test)\npreds, _ = learn.get_preds(ds_type=DatasetType.Test)\nif predicts is None:\n    predicts = preds\nelse:\n    predicts += preds\ni+=1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(i)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predicts = predicts/i","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df[learn.data.classes] = predicts\ntest_df.to_csv('submission.csv', index=False)\ntest_df.head()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}