{"cells":[{"metadata":{},"cell_type":"markdown","source":"based on : \n* https://www.kaggle.com/CVxTz/keras-cnn-starter\n* https://www.kaggle.com/jmourad100/keras-eda-and-cnn-starter\n\n# || Loading Packages"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport os, time, random, cv2, glob, pickle, librosa\nfrom pathlib import Path\nfrom PIL import Image\nimport imgaug as ia\nfrom imgaug import augmenters as iaa\nfrom tqdm import tqdm\n\nfrom keras.models import Model\nfrom keras.layers import (Convolution1D, Input, Dense, Flatten, Dropout, GlobalAveragePooling1D, concatenate,\n                          Activation, MaxPool1D, GlobalMaxPool1D, BatchNormalization, Concatenate, ReLU, LeakyReLU)\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping, LearningRateScheduler\nfrom keras.optimizers import Adam, SGD, RMSprop\nfrom keras.losses import sparse_categorical_crossentropy\nfrom keras.utils.np_utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nprint(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"markdown","source":"# || Configuration"},{"metadata":{"trusted":true},"cell_type":"code","source":"t_start = time.time()\n\n# Keras reproduce score (then init all model seed)\nseed_nb=14\nimport numpy as np \nnp.random.seed(seed_nb)\nimport tensorflow as tf\ntf.set_random_seed(seed_nb)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# || Data Preparation"},{"metadata":{"trusted":true},"cell_type":"code","source":"input_length = 5000\n\nbatch_size = 64\n\ndef audio_norm(data):\n\n    max_data = np.max(data)\n    min_data = np.min(data)\n    data = (data-min_data)/(max_data-min_data+0.0001)\n    return data-0.5\n\n\ndef load_audio_file(file_path, input_length=input_length):\n    data = librosa.core.load(file_path, sr=16000)[0] #, sr=16000\n    if len(data)>input_length:\n        max_offset = len(data)-input_length\n        offset = np.random.randint(max_offset)\n        data = data[offset:(input_length+offset)]\n        \n    else:\n        if input_length > len(data):\n            max_offset = input_length - len(data)\n            offset = np.random.randint(max_offset)\n        else:\n            offset = 0\n            \n        data = np.pad(data, (offset, input_length - len(data) - offset), \"constant\")\n        \n    data = audio_norm(data)\n    return data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_files = glob.glob(\"../input/train_curated/*.wav\")\ntrain_labels = pd.read_csv(\"../input/train_curated.csv\")\ntrain_labels['labels'] = train_labels['labels'].apply(lambda x: x.split(',')[0]) # only keep first label for now","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"file_to_label = {\"../input/train_curated/\"+k:v for k,v in zip(train_labels.fname.values, train_labels.labels.values)}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"list_labels = sorted(list(set(train_labels.labels.values)))\nlabel_to_int = {k:v for v,k in enumerate(list_labels)}\nint_to_label = {v:k for k,v in label_to_int.items()}\nfile_to_int = {k:label_to_int[v] for k,v in file_to_label.items()}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_model():\n    nclass = len(list_labels)\n    inp = Input(shape=(input_length, 1))\n    img_1 = Convolution1D(16, kernel_size=9, activation=\"relu\", padding=\"valid\")(inp)\n    img_1 = Convolution1D(16, kernel_size=9, activation=\"relu\", padding=\"valid\")(img_1)\n    img_1 = MaxPool1D(pool_size=16)(img_1)\n    img_1 = Dropout(rate=0.1)(img_1)\n    img_1 = Convolution1D(32, kernel_size=3, activation=\"relu\", padding=\"valid\")(img_1)\n    img_1 = Convolution1D(32, kernel_size=3, activation=\"relu\", padding=\"valid\")(img_1)\n    img_1 = MaxPool1D(pool_size=4)(img_1)\n    img_1 = Dropout(rate=0.1)(img_1)\n    img_1 = Convolution1D(32, kernel_size=3, activation=\"relu\", padding=\"valid\")(img_1)\n    img_1 = Convolution1D(32, kernel_size=3, activation=\"relu\", padding=\"valid\")(img_1)\n    img_1 = MaxPool1D(pool_size=4)(img_1)\n    img_1 = Dropout(rate=0.1)(img_1)\n    img_1 = Convolution1D(256, kernel_size=3, activation=\"relu\", padding=\"valid\")(img_1)\n    img_1 = Convolution1D(256, kernel_size=3, activation=\"relu\", padding=\"valid\")(img_1)\n    img_1 = GlobalMaxPool1D()(img_1)\n    img_1 = Dropout(rate=0.2)(img_1)\n\n    dense_1 = Dense(64, activation=\"relu\")(img_1)\n    dense_1 = Dense(1028, activation=\"relu\")(dense_1)\n    dense_1 = Dense(nclass, activation=\"softmax\")(dense_1)\n\n    model = Model(inputs=inp, outputs=dense_1)\n\n    model.compile(optimizer=Adam(0.001), loss=sparse_categorical_crossentropy, metrics=['acc'])\n    model.summary()\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def chunker(seq, size):\n    return (seq[pos:pos + size] for pos in range(0, len(seq), size))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def train_generator(list_files, batch_size=batch_size):\n    while True:\n        random.shuffle(list_files)\n        for batch_files in chunker(list_files, size=batch_size):\n            batch_data = [load_audio_file(fpath) for fpath in batch_files]\n            batch_data = np.array(batch_data)[:,:,np.newaxis]\n            batch_labels = [file_to_int[fpath] for fpath in batch_files]\n            batch_labels = np.array(batch_labels)\n            \n            yield batch_data, batch_labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tr_files, val_files = train_test_split(train_files, test_size=0.05)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = get_model()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit_generator(train_generator(tr_files), \n                    steps_per_epoch=len(tr_files)//batch_size, \n                    validation_data=train_generator(val_files),\n                    validation_steps=len(val_files)//batch_size,\n                    epochs=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"list_preds = []\nbatch_size = 128\ntest_files = glob.glob(\"../input/test/*.wav\")\ntest_files.sort()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for batch_files in tqdm(chunker(test_files, size=batch_size), total=len(test_files)//batch_size ):\n    batch_data = [load_audio_file(fpath) for fpath in batch_files]\n    batch_data = np.array(batch_data)[:,:,np.newaxis]\n    preds = model.predict(batch_data).tolist()\n    list_preds += preds","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"array_preds = np.array(list_preds)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv('../input/sample_submission.csv')\nfor i, v in enumerate(list_labels):\n    df[v] = array_preds[:, i]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['fname'] = df.fname.apply(lambda x: x.split(\"/\")[-1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.to_csv(\"submission.csv\", index=False)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"t_finish = time.time()\nprint(f\"Kernel run time = {(t_finish-t_start)/3600} hours\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}